3

I get this error when I am calling an instruction of a program from a client. I am using Seahorse library to write smart contract. The library transpiles python code to anchor rust program.

Here's the client code:

    it("add liquidity", async () => {
        const tokenAmountA = 400_000_000;
        const tokenAmountB = 400_000_000;

        const tx = await program.methods
            .addLiquidity(new anchor.BN(tokenAmountA), new anchor.BN(tokenAmountB))
            .accounts({
                user: userPk,
                pool: poolPk,
                tokenMintA: tokenMintAPk,
                tokenMintB: tokenMintBPk,
                userTokenAccountA: userTokenAccountAPk,
                userTokenAccountB: userTokenAccountBPk,
                poolTokenVaultA: poolTokenVaultAPk,
                poolTokenVaultB: poolTokenVaultBPk,
                lpTokenMint: lpTokenMintPk,
                userLpTokenAccount: userLPTokenAccountPk,
            })
            .signers([user])
            .rpc();

        console.log("Your transaction signature", tx);
    });

Here's the instruction code in transpiled rust language:

    #[derive(Accounts)]
    # [instruction (token_amount_a : u64 , token_amount_b : u64)]
    pub struct AddLiquidity<'info> {
        #[account(mut)]
        pub user: Signer<'info>,
        #[account(mut)]
        pub pool: Box<Account<'info, dot::program::Pool>>,
        #[account(mut)]
        pub token_mint_a: Box<Account<'info, Mint>>,
        #[account(mut)]
        pub token_mint_b: Box<Account<'info, Mint>>,
        #[account(mut)]
        pub user_token_account_a: Box<Account<'info, TokenAccount>>,
        #[account(mut)]
        pub user_token_account_b: Box<Account<'info, TokenAccount>>,
        #[account(mut)]
        pub pool_token_vault_a: Box<Account<'info, TokenAccount>>,
        #[account(mut)]
        pub pool_token_vault_b: Box<Account<'info, TokenAccount>>,
        #[account(mut)]
        pub lp_token_mint: Box<Account<'info, Mint>>,
        # [account (init , payer = user , seeds = ["lp-token-account" . as_bytes () . as_ref () , lp_token_mint . key () . as_ref () , user . key () . as_ref ()] , bump , token :: mint = lp_token_mint , token :: authority = user)]
        pub user_lp_token_account: Box<Account<'info, TokenAccount>>,
        pub rent: Sysvar<'info, Rent>,
        pub system_program: Program<'info, System>,
        pub token_program: Program<'info, Token>,
    }
    pub fn add_liquidity(
        ctx: Context<AddLiquidity>,
        token_amount_a: u64,
        token_amount_b: u64,
    ) -> Result<()> {
        let mut programs = HashMap::new();

        programs.insert(
            "system_program",
            ctx.accounts.system_program.to_account_info(),
        );

        programs.insert(
            "token_program",
            ctx.accounts.token_program.to_account_info(),
        );

        let programs_map = ProgramsMap(programs);
        let user = SeahorseSigner {
            account: &ctx.accounts.user,
            programs: &programs_map,
        };

        let pool = dot::program::Pool::load(&mut ctx.accounts.pool, &programs_map);
        let token_mint_a = SeahorseAccount {
            account: &ctx.accounts.token_mint_a,
            programs: &programs_map,
        };

        let token_mint_b = SeahorseAccount {
            account: &ctx.accounts.token_mint_b,
            programs: &programs_map,
        };

        let user_token_account_a = SeahorseAccount {
            account: &ctx.accounts.user_token_account_a,
            programs: &programs_map,
        };

        let user_token_account_b = SeahorseAccount {
            account: &ctx.accounts.user_token_account_b,
            programs: &programs_map,
        };

        let pool_token_vault_a = SeahorseAccount {
            account: &ctx.accounts.pool_token_vault_a,
            programs: &programs_map,
        };

        let pool_token_vault_b = SeahorseAccount {
            account: &ctx.accounts.pool_token_vault_b,
            programs: &programs_map,
        };

        let lp_token_mint = SeahorseAccount {
            account: &ctx.accounts.lp_token_mint,
            programs: &programs_map,
        };

        let user_lp_token_account = Empty {
            account: SeahorseAccount {
                account: &ctx.accounts.user_lp_token_account,
                programs: &programs_map,
            },
            bump: ctx.bumps.get("user_lp_token_account").map(|bump| *bump),
        };

        add_liquidity_handler(
            user.clone(),
            pool.clone(),
            token_mint_a.clone(),
            token_mint_b.clone(),
            user_token_account_a.clone(),
            user_token_account_b.clone(),
            pool_token_vault_a.clone(),
            pool_token_vault_b.clone(),
            lp_token_mint.clone(),
            user_lp_token_account.clone(),
            token_amount_a,
            token_amount_b,
        );

        dot::program::Pool::store(pool);

        return Ok(());
    }

I have passed the required parameters and account to the instructions. I don't understand why I am getting this error.

1 Answer 1

4

Can you do

 }).rpc({
  skipPreflight:true
})  

This will not simulate the tx and you could get better errors

1
  • 1
    Hi. Thanks for the suggestion to enable this feature. Using it, I was able to pinpoint the error in the program.
    – denimcodes
    Jan 20, 2023 at 5:15

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.