I wrote a staking contract at https://beta.solpg.io/672789d1cffcf4b13384d42c, but when I use JS to call the contract's Deposit function, I always fail to initialize the account. Is there a problem with my contract? Is the Token Program of Soon the same as the one on Solana? I modified the code according to the error message, but it didn't work.
js:
const anchor = require('@project-serum/anchor');
const { SystemProgram, Keypair, PublicKey ,TokenProgram} = anchor.web3;
const TOKEN_PROGRAM_ID = anchor.utils.token.TOKEN_PROGRAM_ID;
// Soon is almost the same as Solana
const connection = new anchor.web3.Connection("https://rpc.devnet.soo.network/rpc", "confirmed");
const secretKey = "xxxx..."
const walletKeypair = Keypair.fromSecretKey(secretKey);
const wallet = new anchor.Wallet(walletKeypair);
const provider = new anchor.AnchorProvider(connection, wallet, { preflightCommitment: "confirmed" });
anchor.setProvider(provider);
const programId = new anchor.web3.PublicKey("H4Sx4E6j7Kob8hN2tnZBhDLEq6ZPfsXEwZvzuwAFR3s4");
const program = new anchor.Program(idl, programId, provider);
// wallet is xxxMint Authority
// https://explorer.devnet.soo.network/address/GiiAqBbfpZxEsXVLxFDofXdV5trLrF6Xm9iSZ6doZmSs
const xxxMint = new PublicKey("GiiAqBbfpZxEsXVLxFDofXdV5trLrF6Xm9iSZ6doZmSs");
async function deposit(userKeypair,mainKeypair,amount) {
const [userStakeAccount,bump1] = await anchor.web3.PublicKey.findProgramAddressSync(
[Buffer.from("stake"),userKeypair.publicKey.toBuffer()],
program.programId
);
const [userTokenAccount,bump2] = await anchor.web3.PublicKey.findProgramAddressSync(
[Buffer.from("token"),userKeypair.publicKey.toBuffer()],
program.programId
);
const senderTokenAccount = await getAssociatedTokenAddress(
xxxMint, // The mint of the token
userKeypair.publicKey // The owner of the ATA
);
const senderAccountInfo = await connection.getAccountInfo(senderTokenAccount);
if (!senderAccountInfo) {
throw new Error(`Sender token account ${senderTokenAccount.toString()} does not exist.`);
}
console.log("Sender Token Account:", senderTokenAccount.toString());
const txSignature = await program.methods
.deposit(new anchor.BN(amount))
.accounts({
mainAccount: mainKeypair.publicKey,
userStakeAccount: userStakeAccount,
userTokenAccount: userTokenAccount,
senderTokenAccount: senderTokenAccount,
owner: userKeypair.publicKey,
xxxMint: xxxMint,
tokenProgram: TOKEN_PROGRAM_ID,
systemProgram: SystemProgram.programId,
})
.signers([userKeypair])
.rpc();
console.log("Transaction signature", txSignature);
}
spl_amount = 10
real_amount = spl_amount * 1e9
const userKeypair = walletKeypair
deposit(userKeypair,mainKeypair,real_amount).then(() => {
console.log("Deposited successfully.");
}).catch(err => {
console.error("Failed to deposit:", err);
});
Error Message:
The given account is owned by a different program than expected.',
'Program log: Left:',
'Program log: 11111111111111111111111111111111',
'Program log: Right:',
'Program log: TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'
],
logs: [
'Program H4Sx4E6j7Kob8hN2tnZBhDLEq6ZPfsXEwZvzuwAFR3s4 invoke [1]',
'Program log: Instruction: Deposit',
'Program log: AnchorError caused by account: sender_token_account. Error Code: AccountOwnedByWrongProgram. Error Number: 3007. Error Message: The given account is owned by a different program than expected.',
'Program log: Left:',
'Program log: 11111111111111111111111111111111',
'Program log: Right:',
'Program log: TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
'Program H4Sx4E6j7Kob8hN2tnZBhDLEq6ZPfsXEwZvzuwAFR3s4 consumed 5199 of 200000 compute units',
'Program H4Sx4E6j7Kob8hN2tnZBhDLEq6ZPfsXEwZvzuwAFR3s4 failed: custom program error: 0xbbf'
],
error: {
errorCode: { code: 'AccountOwnedByWrongProgram', number: 3007 },
errorMessage: 'The given account is owned by a different program than expected',
comparedValues: [
[PublicKey [PublicKey(11111111111111111111111111111111)]],
[PublicKey [PublicKey(TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA)]]
],
origin: 'sender_token_account'
},
_programErrorStack: ProgramErrorStack {
stack: [
[PublicKey [PublicKey(H4Sx4E6j7Kob8hN2tnZBhDLEq6ZPfsXEwZvzuwAFR3s4)]]
]
}