1

I am building a swap like app that uses raydium but I'm facing alot of problems.

I copied someone's code and tried to change it a little bit but something is quite off.

Whenever I try to sell the whole amount of token inside my token account - it sells only a portion of it. it sometimes sells 10% and other times 99% but never 100% as I try to.

I'm not really sure how to use the Raydium sdk and there is no where to read or learn about it really besides reading the code in github which I already tried, Please help.

code:

async calcAmountOut(poolKeys: LiquidityPoolKeys, rawAmountIn: number, swapInDirection: boolean) {
    const poolInfo = await Liquidity.fetchInfo({ connection, poolKeys });

    let currencyInMint = poolKeys.baseMint;
    let currencyInDecimals = poolInfo.baseDecimals;
    let currencyOutMint = poolKeys.quoteMint;
    let currencyOutDecimals = poolInfo.quoteDecimals;

    if (!swapInDirection) {
        currencyInMint = poolKeys.quoteMint;
        currencyInDecimals = poolInfo.quoteDecimals;
        currencyOutMint = poolKeys.baseMint;
        currencyOutDecimals = poolInfo.baseDecimals;
    }

    const currencyIn = new Token(TOKEN_PROGRAM_ID, currencyInMint, currencyInDecimals);
    const amountIn = new TokenAmount(currencyIn, rawAmountIn, false);
    const currencyOut = new Token(TOKEN_PROGRAM_ID, currencyOutMint, currencyOutDecimals);
    const slippage = new Percent(0, 100); // 5% slippage

    const { amountOut, minAmountOut, currentPrice, executionPrice, priceImpact, fee } = Liquidity.computeAmountOut({
        poolKeys,
        poolInfo,
        amountIn,
        currencyOut,
        slippage,
    });

    return {
        amountIn,
        amountOut,
        minAmountOut,
        currentPrice,
        executionPrice,
        priceImpact,
        fee,
    };
}



async getOwnerTokenAccounts(keypair: Keypair): Promise<TokenAccount[]> {
    return (
        await connection.getTokenAccountsByOwner(keypair.publicKey, {
            programId: TOKEN_PROGRAM_ID,
        })
    ).value.map(i => ({
        pubkey: i.pubkey,
        programId: i.account.owner,
        accountInfo: SPL_ACCOUNT_LAYOUT.decode(i.account.data),
    }));
}



async getSwapTransaction(
    keypair: Keypair,
    toToken: string,
    amount: number,
    poolKeys: LiquidityPoolKeys,
    fixedSide: 'in' | 'out' = 'in',
): Promise<VersionedTransaction> {
    const directionIn: boolean = poolKeys.quoteMint.toString() == toToken;
    const { minAmountOut, amountIn } = await this.calcAmountOut(poolKeys, amount, directionIn);

    const userTokenAccounts = await this.getOwnerTokenAccounts(keypair);
    const swapTransaction = await Liquidity.makeSwapInstructionSimple({
        connection,
        makeTxVersion: TxVersion.V0,
        poolKeys,
        userKeys: {
            tokenAccounts: userTokenAccounts,
            owner: keypair.publicKey,
        },
        amountIn,
        amountOut: minAmountOut,
        fixedSide,
        config: {
            bypassAssociatedCheck: false,
        },
    });

    const recentBlockhashForSwap = await connection.getLatestBlockhash();
    const instructions = swapTransaction.innerTransactions[0].instructions.filter(Boolean);

    const versionedTransaction = new VersionedTransaction(
        new TransactionMessage({
            payerKey: keypair.publicKey,
            recentBlockhash: recentBlockhashForSwap.blockhash,
            instructions: instructions,
        }).compileToV0Message(),
    );

    versionedTransaction.sign([keypair]);

    return versionedTransaction;
}



async sendVersionedTransaction(tx: VersionedTransaction) {
    return await connection.sendTransaction(tx, {
        skipPreflight: true,
        maxRetries: 2,
    });
}
1
  • It's a bit tough to get Raydium support in the Solana Stack Exchange unfortunately, so be sure to reach out to any other server or help that you can.
    – Jon C
    Commented Mar 13 at 12:53

1 Answer 1

0

get Token Accounts and filter by mint address you want to sell then take the amount then use it as amount in

const tokenAccounts = await getTokenAccounts(solanaConnection, wallet.publicKey, commitment);
      console.log(tokenAccounts)
var tokenAccount = tokenAccounts.find((v: any) => v.accountInfo.mint.toString() == baseMint.toString()) // basemint means spl token mint address
    console.log('tokenAccount->', tokenAccount)

const AmountIn = tokenAccount.accountInfo.amount

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.