2

I have builded and deployed a Rust Solana Smart Contract using Anchor to devnet successfully. I have this initMaster function on the Smart Conract:

pub fn init_master(_ctx: Context<InitMaster>) -> Result<()> {
 Ok(())
}

with this:

#[derive(Accounts)]
pub struct InitMaster<'info> {
    #[account(
        init,
        payer = payer,
        space = 4 + 8,
        seeds = [MASTER_SEED.as_bytes()],
        bump
    )]
    pub master: Account<'info, Master>,

    #[account(mut)]
    pub payer: Signer<'info>,

    pub system_program: Program<'info, System>,
}

#[account]
pub struct Master {
    pub last_id: u32,
}

and in Angular application I created this function:

async initMaster() {
    const account = Keypair.fromSecretKey(this.privateKey);
    try {
      const txInstruction = new TransactionInstruction({
        keys: [
          {
            pubkey: await this.getMasterAddress(),
            isSigner: false,
            isWritable: false,
          },
          {
            pubkey: this.provider.publicKey,
            isSigner: true,
            isWritable: false,
          },
        ],
        programId: new PublicKey(PROGRAM_ID),
      });
      const tx = new Transaction().add(txInstruction);
      tx.recentBlockhash = (
        await this.connection.getRecentBlockhash()
      ).blockhash;
      tx.feePayer = this.provider.publicKey;
      const { signature } = await this.provider.signAndSendTransaction(tx);
      await this.connection.confirmTransaction(signature);
    } catch (err) {
      console.log('Error initiating the master:', err);
    }
  }

and this:

async getMasterAddress() {
    return (
      await PublicKey.findProgramAddressSync(
        [Buffer.from(MASTER_SEED)],
        PROGRAM_ID
      )
    )[0];
}

I CHECKED ALL THE VALUES AND ALL IS OK but when I trigger the initMaster function, the Phantom Wallet Popup with Confirm transaction appear, I click confirm, nothing happen and I get this error in console:

VM4120:3 GOT ERROR Error: Unexpected error
    at oi.<anonymous> (<anonymous>:3:214849)
    at Generator.next (<anonymous>)
    at s (<anonymous>:1:1456)
    at _ZoneDelegate.invoke (zone.js:368:26)
    at Object.onInvoke (core.mjs:11083:33)
    at _ZoneDelegate.invoke (zone.js:367:52)
    at Zone.run (zone.js:129:43)
    at zone.js:1257:36
    at _ZoneDelegate.invokeTask (zone.js:402:31)
    at core.mjs:10757:55
(anonymous) @ VM4120:3
s @ VM4120:1
invoke @ zone.js:368
onInvoke @ core.mjs:11083
invoke @ zone.js:367
run @ zone.js:129
(anonymous) @ zone.js:1257
invokeTask @ zone.js:402
(anonymous) @ core.mjs:10757
onInvokeTask @ core.mjs:10757
invokeTask @ zone.js:401
onInvokeTask @ core.mjs:11070
invokeTask @ zone.js:401
runTask @ zone.js:173
drainMicroTaskQueue @ zone.js:581
Zone - Promise.then (async)
onScheduleTask @ core.mjs:10751
scheduleTask @ zone.js:382
onScheduleTask @ zone.js:279
scheduleTask @ zone.js:382
scheduleTask @ zone.js:216
scheduleMicroTask @ zone.js:236
scheduleResolveOrReject @ zone.js:1247
resolvePromise @ zone.js:1184
(anonymous) @ zone.js:1100
(anonymous) @ zone.js:1116
invoke @ zone.js:368
onInvoke @ core.mjs:11083
invoke @ zone.js:367
run @ zone.js:129
(anonymous) @ zone.js:1257
invokeTask @ zone.js:402
(anonymous) @ core.mjs:10757
onInvokeTask @ core.mjs:10757
invokeTask @ zone.js:401
onInvokeTask @ core.mjs:11070
invokeTask @ zone.js:401
runTask @ zone.js:173
drainMicroTaskQueue @ zone.js:581
Zone - Promise.then (async)
onScheduleTask @ core.mjs:10751
scheduleTask @ zone.js:382
onScheduleTask @ zone.js:279
scheduleTask @ zone.js:382
scheduleTask @ zone.js:216
scheduleMicroTask @ zone.js:236
scheduleResolveOrReject @ zone.js:1247
resolvePromise @ zone.js:1184
(anonymous) @ zone.js:1100
(anonymous) @ zone.js:1116
u @ VM4120:1
s @ VM4120:1
invoke @ zone.js:368
onInvoke @ core.mjs:11083
invoke @ zone.js:367
run @ zone.js:129
(anonymous) @ zone.js:1257
invokeTask @ zone.js:402
(anonymous) @ core.mjs:10757
onInvokeTask @ core.mjs:10757
invokeTask @ zone.js:401
onInvokeTask @ core.mjs:11070
invokeTask @ zone.js:401
runTask @ zone.js:173
drainMicroTaskQueue @ zone.js:581
Zone - Promise.then (async)
onScheduleTask @ core.mjs:10751
scheduleTask @ zone.js:382
onScheduleTask @ zone.js:279
scheduleTask @ zone.js:382
scheduleTask @ zone.js:216
scheduleMicroTask @ zone.js:236
scheduleResolveOrReject @ zone.js:1247
then @ zone.js:1442
u @ VM4120:1
s @ VM4120:1
invoke @ zone.js:368
onInvoke @ core.mjs:11083
invoke @ zone.js:367
run @ zone.js:129
(anonymous) @ zone.js:1257
invokeTask @ zone.js:402
(anonymous) @ core.mjs:10757
onInvokeTask @ core.mjs:10757
invokeTask @ zone.js:401
onInvokeTask @ core.mjs:11070
invokeTask @ zone.js:401
runTask @ zone.js:173
drainMicroTaskQueue @ zone.js:581
Zone - Promise.then (async)
onScheduleTask @ core.mjs:10751
scheduleTask @ zone.js:382
onScheduleTask @ zone.js:279
scheduleTask @ zone.js:382
scheduleTask @ zone.js:216
scheduleMicroTask @ zone.js:236
scheduleResolveOrReject @ zone.js:1247
resolvePromise @ zone.js:1184
(anonymous) @ zone.js:1100
(anonymous) @ zone.js:1116
invoke @ zone.js:368
onInvoke @ core.mjs:11083
invoke @ zone.js:367
run @ zone.js:129
(anonymous) @ zone.js:1257
invokeTask @ zone.js:402
(anonymous) @ core.mjs:10757
onInvokeTask @ core.mjs:10757
invokeTask @ zone.js:401
onInvokeTask @ core.mjs:11070
invokeTask @ zone.js:401
runTask @ zone.js:173
drainMicroTaskQueue @ zone.js:581
Zone - Promise.then (async)
onScheduleTask @ core.mjs:10751
scheduleTask @ zone.js:382
onScheduleTask @ zone.js:279
scheduleTask @ zone.js:382
scheduleTask @ zone.js:216
scheduleMicroTask @ zone.js:236
scheduleResolveOrReject @ zone.js:1247
resolvePromise @ zone.js:1184
(anonymous) @ zone.js:1100
(anonymous) @ zone.js:1116
u @ VM4120:1
s @ VM4120:1
invoke @ zone.js:368
onInvoke @ core.mjs:11083
invoke @ zone.js:367
run @ zone.js:129
(anonymous) @ zone.js:1257
invokeTask @ zone.js:402
(anonymous) @ core.mjs:10757
onInvokeTask @ core.mjs:10757
invokeTask @ zone.js:401
onInvokeTask @ core.mjs:11070
invokeTask @ zone.js:401
runTask @ zone.js:173
drainMicroTaskQueue @ zone.js:581
Zone - Promise.then (async)
onScheduleTask @ core.mjs:10751
scheduleTask @ zone.js:382
onScheduleTask @ zone.js:279
scheduleTask @ zone.js:382
scheduleTask @ zone.js:216
scheduleMicroTask @ zone.js:236
scheduleResolveOrReject @ zone.js:1247
resolvePromise @ zone.js:1184
(anonymous) @ zone.js:1100
(anonymous) @ zone.js:1116
invoke @ zone.js:368
onInvoke @ core.mjs:11083
invoke @ zone.js:367
run @ zone.js:129
(anonymous) @ zone.js:1257
invokeTask @ zone.js:402
(anonymous) @ core.mjs:10757
onInvokeTask @ core.mjs:10757
invokeTask @ zone.js:401
onInvokeTask @ core.mjs:11070
invokeTask @ zone.js:401
runTask @ zone.js:173
drainMicroTaskQueue @ zone.js:581
Zone - Promise.then (async)
onScheduleTask @ core.mjs:10751
scheduleTask @ zone.js:382
onScheduleTask @ zone.js:279
scheduleTask @ zone.js:382
scheduleTask @ zone.js:216
scheduleMicroTask @ zone.js:236
scheduleResolveOrReject @ zone.js:1247
resolvePromise @ zone.js:1184
(anonymous) @ zone.js:1100
(anonymous) @ zone.js:1116
(anonymous) @ VM4120:3
n @ VM4120:3
(anonymous) @ contentScript.js:167
Show 159 more frames
Show less
data.service.ts:238 Error initiating the master: Error: Unexpected error
    at oi.<anonymous> (<anonymous>:3:214849)
    at Generator.next (<anonymous>)
    at s (<anonymous>:1:1456)
    at _ZoneDelegate.invoke (zone.js:368:26)
    at Object.onInvoke (core.mjs:11083:33)
    at _ZoneDelegate.invoke (zone.js:367:52)
    at Zone.run (zone.js:129:43)
    at zone.js:1257:36
    at _ZoneDelegate.invokeTask (zone.js:402:31)
    at core.mjs:10757:55

this is the Transaction:

transaction

How can I fix this? What is wrong?

1 Answer 1

1

Hey there so whats happening is that your function doesnt do nothing. All you do is throwing an Ok. To fix this you can do :

pub fn init_master(ctx: Context<InitMaster>, seed: u64, last_id: u32) -> Result<()> {
 ctx.accounts.init(last_id, seed, &ctx.bumps)
}

#[derive(Accounts)]
#[instruction(seed: u64)]
pub struct InitMaster<'info> {
    #[account(
        init,
        payer = payer,
        space = 4 + 8 + 1 + 8 ,
        seeds = [seed.to_le_bytes().as_ref()],//Fixed seed also
        bump
    )]
    pub master: Account<'info, Master>,

    #[account(mut)]
    pub payer: Signer<'info>,

    pub system_program: Program<'info, System>,
}
impl<'info> InitMaster<'info> {
 pub fn init(
        &mut self,
        last_id: u32,
        bumps: &InitMasterBumps,
    ) -> Result<()> {

        self.master.set_inner(Master{ 
            last_id,
            bump: bumps.master});

            Ok(())
}
}

#[account]
pub struct Master {
    pub last_id: u32,
    pub seed: u64,
    pub bump: u8
}

This code should properly initialize the Master acc with the provided last_id and seed values.

The way you had it before, with seeds = [MASTER_SEED.as_bytes()], is incorrect.

6
  • I will try with this but, why on Solana Playground everything works perfectly? The initMaster, createLottery, buyTicket, pickWinner and claimPrize? Feb 16 at 3:20
  • Well, because in Rust these functions return a result using the Result type. It can either be an Ok or an Error. And since you're not performing any logic and just returning Ok(()), it seems like everything is fine. The reason it compiled and deployed successfully is because this account is not used in any function. Feb 16 at 4:16
  • I got this error from your code:i.ibb.co/SQkgsxq/Screenshot-2024-02-16-at-10-13-30.png Feb 16 at 8:13
  • can we chat a little bit beliveN DAOist.dev ? Feb 16 at 19:00
  • The error above is because you didnt added the seed to your Master struct. If you dont want to pass the seed as an Arg you can change your seed to seeds = [b"MASTER_SEED"] But yes we can chat. Feb 16 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.