henry-e / dog-money Goto Github PK
View Code? Open in Web Editor NEWbasis for a tutorial on intermediate level functionality in anchor
basis for a tutorial on intermediate level functionality in anchor
Hi Henry. Somehow I got this error:
TypeError: Cannot read property 'toString' of undefined
at /mnt/sda5/0Programming/Solana/anchor1/node_modules/@solana/web3.js/lib/index.cjs.js:808:47
at Array.forEach ()
at Transaction.compileMessage (/mnt/sda5/0Programming/Solana/anchor1/node_modules/@solana/web3.js/lib/index.cjs.js:807:18)
at Transaction._compile (/mnt/sda5/0Programming/Solana/anchor1/node_modules/@solana/web3.js/lib/index.cjs.js:912:26)
at Transaction.partialSign (/mnt/sda5/0Programming/Solana/anchor1/node_modules/@solana/web3.js/lib/index.cjs.js:1044:26)
at NodeWallet.signTransaction (/mnt/sda5/0Programming/Solana/anchor1/node_modules/@project-serum/anchor/dist/cjs/provider.js:161:12)
at Provider.send (/mnt/sda5/0Programming/Solana/anchor1/node_modules/@project-serum/anchor/dist/cjs/provider.js:77:27)
at processTicksAndRejections (node:internal/process/task_queues:94:5)
at async createMint (tests/utils/index.js:39:3)
at async Context. (tests/dog-money.js:27:22)
tests/dog-money.js:27:22
const usdcMint = await createMint(program.provider, program.provider.wallet.publicKey);
tests/utils/index.js:39:3)
const tx = new anchor.web3.Transaction();
tx.add(...instructions);
console.log("mint:", mint, ", tx:", tx);
await provider.send(tx, [mint]);
console outputs:
mint: Keypair {
_keypair: {
publicKey: Uint8Array(32) [171, 95, 92, 181,... ],
secretKey: Uint8Array(64) [
19, 183, 50, 155, ... ]
}
}
tx: Transaction {
signatures: [],
feePayer: undefined,
instructions: [
TransactionInstruction {
keys: [Array],
programId: [PublicKey],
data: <Buffer 00 00 00 00 60 4d 16 00 00 00 00 00 52 00 00 00 00 00 00 00 06 dd f6 e1 d7 65 a1 93 d9 cb e1 46 ce eb 79 ac 1c b4 85 ed 5f 5b 37 91 3a 8c f5 85 7e ff ... 2 more bytes>
},
TransactionInstruction {
keys: [Array],
programId: [PublicKey],
data: <Buffer 00 06 79 5f c7 29 5d 79 b5 bd 54 b3 48 36 b7 43 2c ec 9d 1f b4 20 d3 20 bf 0e 13 e1 14 fa 29 b8 b9 0e 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... 17 more bytes>
}
],
recentBlockhash: undefined,
nonceInfo: undefined
}
So somehow this tx object has properties of undefined... how did this happen?
May I know what went wrong? Thanks
Hi Henry, I followed you on how to stake USDC tokens from an user account to a program account.
May I know how to withdraw USDC tokens from a program account to an user account?
I tried it but it failed right after "check4":
pub fn withdraw_usdc(ctx: Context<UsdcDoge>, amount: u64, nonce: u8) -> ProgramResult {
let seeds = &[ctx.accounts.usdc_mint.to_account_info().key.as_ref(), &[nonce], ];
let signer = &[&seeds[..]];
msg!("check3");
let cpi_accounts = Transfer {
from: ctx.accounts.usdc_pgid.to_account_info(),
to: ctx.accounts.usdc_user.to_account_info(),
authority: ctx.accounts.program_signer.clone(),
};
let cpi_program = ctx.accounts.token_program.clone();
let cpi_ctx = CpiContext::new_with_signer(cpi_program, cpi_accounts, signer);
msg!("check4");
token::transfer(cpi_ctx, amount).expect("transfer2 failed");//?;
Ok(())
}
...
#[derive(Accounts)]
pub struct UsdcDoge<'info> {
pub program_signer: AccountInfo<'info>,
#[account(signer)]//authority should sign this txn
pub authority: AccountInfo<'info>,
pub usdc_mint: CpiAccount<'info, Mint>,
#[account(mut, "usdc_user.owner == *authority.key")]
pub usdc_user: CpiAccount<'info, TokenAccount>,
#[account(mut)]
pub usdc_pgid: CpiAccount<'info, TokenAccount>,
#[account(mut,
"doge_mint.mint_authority == COption::Some(*program_signer.key)")]
pub doge_mint: CpiAccount<'info, Mint>,
#[account(mut, "doge_user.owner == *authority.key")]
pub doge_user: CpiAccount<'info, TokenAccount>,
#[account(mut)]
pub doge_pgid: CpiAccount<'info, TokenAccount>,
//pub program_id: Pubkey<'info>,
// We already know its address and that it's executable
#[account(executable,"token_program.key == &token::ID")]
pub token_program: AccountInfo<'info>,
pub system_program: AccountInfo<'info>,
}
'Program log: Error: owner does not match'
Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: custom program error: 0x4
'Program log: --------------== withdraw_usdc',
'Program log: check1',
'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]',
'Program log: Instruction: Transfer',
'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 3402 of 188791 compute units',
'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success',
'Program log: transfer1 successful. amount: 5000000',
'Program log: check3',
'Program log: check4',
'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]',
'Program log: Instruction: Transfer',
'Program log: Error: owner does not match',
'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 2902 of 181411 compute units',
'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA failed: custom program error: 0x4',
'Program J1JQ8f2s77Xhx7xQCRZmMTK2j3HuahgeSMUuccL5ywnb consumed 200000 of 200000 compute units',
'Program J1JQ8f2s77Xhx7xQCRZmMTK2j3HuahgeSMUuccL5ywnb failed: custom program error: 0x4'
In Anchor script:
await program.rpc.withdrawUsdc(
amt2, nonce, {
accounts: {
programSigner,
authority: userKeys.publicKey,
usdcMint,
usdcUser,
usdcPgid,
dogeMint,
dogeUser,
dogePgid,
tokenProgram: TOKEN_PROGRAM_ID,
systemProgram: anchor.web3.SystemProgram.programId,
},
signers: [userKeys],
});
the above accounts object, amt2, and nonce values are exactly the same as that in the stakeUsdc function call... so I don't know what owner is wrong...
before it fails, USDC are on usdc_pgid account, which is made from:
usdcPgid = await createTokenAccount(provider, usdcMint, program.programId);//arguments: privider, mint, owner
The owner of usdc_pgid is program.programId, so I should make cpi_accounts to use programId as authority... like:
authority: ctx.program_id.to_account_info(),
error[E0599]: no method named to_account_info found for reference &anchor_lang::prelude::Pubkey in the current scope --> programs/doge/src/lib.rs:265:33 | 265 | authority: ctx.program_id.to_account_info(), | ^^^^^^^^^^^^^^^ method not found in &anchor_lang::prelude::Pubkey
anchor_lang::CpiAccount https://docs.rs/anchor-lang/0.13.2/anchor_lang/struct.CpiAccount.html
anchor_lang::prelude::Pubkey https://docs.rs/anchor-lang/0.13.2/anchor_lang/prelude/struct.Pubkey.html
From above docs, Pubkey type does not have method to convert from Pubkey to AccountInfo type!
How can I do this? Please advise. Thank you so much!
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
Hi Henry, I downloaded your code then installed some nodeJs packages, then fixed the Anchor.toml file, then when I run "anchor test", it can compile the program no problem, but then it failed:
Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: custom program error: 0x63
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. (tests/dog-money.js:52:5)
tests/dog-money.js:52 has code: await program.rpc.initializeUser(amount, nonce, {
could you please help? Thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.