Need help with Raydium SDK
Need help with Raydium SDK
Loading saved threads...
user10900 · External communityPost link
External question — Solana Stack Exchange
Author: user10900
Original post: https://solana.stackexchange.com/questions/11779
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I am building a swap like app that uses Raydium but I'm facing a lot 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 nowhere to read or learn about it really besides reading the code in Github which I already tried.
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,
});
}
Quote
Report
muthu kumar · External communityPost link
External answer — Solana Stack Exchange
Author: muthu kumar
Original post: https://solana.stackexchange.com/a/12750
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
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
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External answer — Solana Stack Exchange Author: muthu kumar Source score (net votes, not local likes): 0 Original post: https://solana.stackexchange.com/a/12750 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. 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
Checking account access…