Henry-E / dog-money

basis for a tutorial on intermediate level functionality in anchor
13 stars 5 forks source link

Error processing Instruction 0: custom program error: 0xa1 Decimal 161 #3

Open AuroraLantean opened 3 years ago

AuroraLantean commented 3 years ago

Hi Henry, Okay Anchor package has just been updated to 0.12.0, so I have updated the Rust program dependencies to Anchor 0.12.0 in the Cargo.toml file: [dependencies] anchor-lang = "0.12.0" anchor-spl = "0.12.0"

And updated Anchor.toml... And also updated project-serum/anchor to 0.12.0

Then updated the Rust program as below according to the latest IpoPool example in the Anchor examples: const provider = anchor.Provider.local(); anchor.setProvider(provider);

Replace "program.provider" by "provider"

const usdcMint = await createMint(provider); const dataAccount = anchor.web3.Keypair.generate();

await program.rpc.initializeUser(.. {}, //signers: [dataAccount], //instructions: [await program.account.dataAccount.//createInstruction(dataAccount)], )

Please see my forked repo: https://github.com/AuroraLantean/SolanaAnchorDemo1 $ yarn install $ yarn run t1 ... this should give you a successful test result for your MeMe-Num tutorial.

$ yarn run t2 ... this will give you an error below for your Dog Money tutorial. Transaction simulation failed: Error processing Instruction 0: custom program error: 0xa1 Program 3iz9W2uXTYCSvpxaX5jMPqcyb9Vx5rTxATTXJR1Jt9VX invoke [1] Program log: Custom program error: 0xa1 Program 3iz9W2uXTYCSvpxaX5jMPqcyb9Vx5rTxATTXJR1Jt9VX consumed 5320 of 200000 compute units Program 3iz9W2uXTYCSvpxaX5jMPqcyb9Vx5rTxATTXJR1Jt9VX failed: custom program error: 0xa1 Translating error SendTransactionError: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: custom program error: 0xa1 at Connection.sendEncodedTransaction (.../node_modules/@solana/web3.js/lib/index.cjs.js:4882:13) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async Connection.sendRawTransaction (.../node_modules/@solana/web3.js/lib/index.cjs.js:4841:20) at async Object.sendAndConfirmRawTransaction (.../node_modules/@solana/web3.js/lib/index.cjs.js:6668:21) at async Provider.send (.../node_modules/@project-serum/anchor/dist/cjs/provider.js:84:22) at async Object.rpc [as initializeUser] (.../node_modules/@project-serum/anchor/dist/cjs/program/namespace/rpc.js:11:31) at async Context. (.../dog-money/tests/dog-money.js:57:5) { logs: [ 'Program 3iz9W2uXTYCSvpxaX5jMPqcyb9Vx5rTxATTXJR1Jt9VX invoke [1]', 'Program log: Custom program error: 0xa1', 'Program 3iz9W2uXTYCSvpxaX5jMPqcyb9Vx5rTxATTXJR1Jt9VX consumed 5320 of 200000 compute units', 'Program 3iz9W2uXTYCSvpxaX5jMPqcyb9Vx5rTxATTXJR1Jt9VX failed: custom program error: 0xa1' ] }

0xa1 in hex is 161 in decimal From your instruction of finding the error code: https://www.notion.so/Debugging-Custom-Anchor-Errors-b8540dd418c44a4e939ab17c56a3fd3b https://github.com/project-serum/anchor/blob/master/lang/src/error.rs but I could not find the error code for 161... please help! Thank you

Henry-E commented 3 years ago

Here is error code 161 https://github.com/project-serum/anchor/blob/f6329ceef1f06bfdb2168005234cdf408124727d/lang/src/error.rs#L53

It's possible you're passing in an uninitialized program account. This would result in getting an error saying your 8 bytes discriminator is missing

AuroraLantean commented 3 years ago

Henry, I was using your code with slight modification... and I did not change the pub struct InitializeUser<'info> {...} How come the code does not work? According to https://project-serum.github.io/anchor/tutorials/tutorial-1.html#initialize-instruction, it seems I need to add this #[account(init)] on one of the InitializeUser struct's fields, and I tried it with no success... Now I have even reduced the program code to:

#[program]
pub mod dog_money {
    use super::*;
    pub fn initialize_user(ctx: Context<InitializeUser>) -> ProgramResult {
        Ok(())
    }
}
#[derive(Accounts)]
pub struct InitializeUser<'info> {
    //#[account(init)]
    program_signer: AccountInfo<'info>,
    #[account(associated = authority, with = usdc_mint)]
    user_data: ProgramAccount<'info, UserData>,
    #[account(signer)]
    authority: AccountInfo<'info>,
    usdc_mint: CpiAccount<'info, Mint>,
    rent: Sysvar<'info, Rent>,
    system_program: AccountInfo<'info>,
}
#[associated]//#[account]
pub struct UserData {
    pub first_deposit: i64,
}

And with your Anchor testing code:

    await program.rpc.initializeUser({
      accounts: {
        programSigner,
        userData,
        authority: provider.wallet.publicKey,
        usdcMint,
        rent: anchor.web3.SYSVAR_RENT_PUBKEY,
        systemProgram: anchor.web3.SystemProgram.programId,
        clock: anchor.web3.SYSVAR_CLOCK_PUBKEY,
      },
      // signers: [dataAccount],
      // instructions: [await program.account.userData.createInstruction(dataAccount)],
    });

Any idea where the bug is? Thank you

Henry-E commented 3 years ago
    #[account(associated = authority, with = usdc_mint)]
    user_data: ProgramAccount<'info, UserData>,

you're missing init here

Henry-E commented 3 years ago

the associated syntax is going away though and being replaced with seeds so you might want to play around with that syntax instead.

AuroraLantean commented 3 years ago

Okay actually I have tried it:

[account(associated = authority, with = usdc_mint, init)]

...

[associated]

pub struct UserData { pub first_deposit: i64, }

Then I got this: error[E0599]: no function or associated item named default found for struct UserData in the current scope --> programs/dog-money/src/lib.rs:14:10 14 #[derive(Accounts)] ^^^^^^^^ function or associated item not found in UserData ... 41 pub struct UserData { ------------------- function or associated item default not found for this

= help: items from traits can only be used if the trait is implemented and in scope = note: the following trait defines an item default, perhaps you need to implement it: candidate #1: Default = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info) Any idea?

Henry-E commented 3 years ago

Add the default attribute

On Fri, Aug 6, 2021, 3:42 PM AuroraLantean @.***> wrote:

Okay actually I have tried it:

[account(associated = authority, with = usdc_mint, init)]

...

[associated]

pub struct UserData { pub first_deposit: i64, }

Then I got this: error[E0599]: no function or associated item named default found for struct UserData in the current scope --> programs/dog-money/src/lib.rs:14:10 14 #[derive(Accounts)] ^^^^^^^^ function or associated item not found in UserData ... 41 pub struct UserData { ------------------- function or associated item default not found for this

= help: items from traits can only be used if the trait is implemented and in scope = note: the following trait defines an item default, perhaps you need to implement it: candidate #1 https://github.com/Henry-E/dog-money/issues/1: Default = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info) Any idea?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/Henry-E/dog-money/issues/3#issuecomment-894309018, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADAHMGBKXFNSN3ZBTFFATDTT3PYFDANCNFSM5BUD5X7A . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email .

AuroraLantean commented 3 years ago

Hi Henry, I got it!

#[associated]
pub struct UserData {
    pub first_deposit: i64,
}
impl Default for UserData {
  fn default() -> UserData { 
    UserData{
      __nonce: 0,
      first_deposit: 0,
    }
  }
}

Regarding the seeds syntax, I tried those but none of them worked:

[account(seeds = [authority])]

[account(seeds)]

[account(seeds = [authority, usdc_mint])]

may I know how to do it? As a token of thanks for the videos you made, you can use my code with all Anchor's latest modification! https://github.com/AuroraLantean/SolanaAnchorDemo1 Thank you!

Henry-E commented 3 years ago

Check the anchor repo for example usage of the seeds syntax

On Fri, Aug 6, 2021, 4:31 PM AuroraLantean @.***> wrote:

Hi Henry, I got it!

[associated]

pub struct UserData { pub first_deposit: i64, } impl Default for UserData { fn default() -> UserData { UserData{ __nonce: 0, first_deposit: 0, } } }

Regarding the seeds syntax, I tried those but none of them worked:

[account(seeds = [authority])]

[account(seeds)]

[account(seeds = [authority, usdc_mint])]

may I know how to do it? As a token of thanks for the videos you made, you can use my code with all Anchor's latest modification! https://github.com/AuroraLantean/SolanaAnchorDemo1 Thank you!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/Henry-E/dog-money/issues/3#issuecomment-894339728, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADAHMGAMQXAZVSDJ36PSJ2TT3P55DANCNFSM5BUD5X7A . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email .