1

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)]]
    ]
  }
1
  • could you also share the instruction for this method and your Anchor.toml config
    – Jimii
    Commented Nov 4 at 9:26

1 Answer 1

1

The error is being caused by the senderTokenAccount.

If you are using the getAssociatedTokenAddressSync method to derive the token address and getting the error above, it likely means that the the account might not be initialized.

import { getAssociatedTokenAddressSync, } from "@solana/spl-token";

getAssociatedTokenAddressSync(mint, publicKey)
5
  • In the code, senderTokenAccount and senderAccountInfo are able to run successfully, which indicates that the ATA (Associated Token Account) has already been created. This ATA is the token account I created for the xxx token using the SPL Token CLI, and the wallet used is walletPair.
    – song xiao
    Commented Nov 4 at 8:53
  • The getAssociatedTokenAddress only derives the address from the given seeds i.e mint and user's publicKey. Whether the account is initialized or owned by the correct program is checked by the program
    – Jimii
    Commented Nov 4 at 9:34
  • I checked another ATA account and confirmed that the owner of the ATA account is the Token Program, but senderTokenAccount still reports the same error.
    – song xiao
    Commented Nov 5 at 9:43
  • does the error occur when you use a token from another cluster?
    – Jimii
    Commented Nov 5 at 14:30
  • Only this cluster is available on Soon.:rpc.devnet.soo.network/rpc
    – song xiao
    Commented Nov 6 at 8:30

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.