Open
Description
import {
CurveCalculator,
Raydium,
TxVersion,
} from "@raydium-io/raydium-sdk-v2";
import { Connection, PublicKey } from "@solana/web3.js";
import { BN } from "bn.js";
const connection = new Connection(
"<SOLANA_RPC_API",
{
commitment: "confirmed",
}
);
const owner = new PublicKey("<SOLANA_PUBLIC_KEY>");
const raydium = await Raydium.load({
connection,
owner,
});
interface SwapParams {
poolId: string;
from: string;
amount: string;
}
export async function getCpmmSwapTx({ poolId, from, amount }: SwapParams) {
const { poolInfo, rpcData } = await raydium.cpmm.getPoolInfoFromRpc(poolId);
const { baseReserve, quoteReserve, configInfo } = rpcData;
const baseIn = from === poolInfo.mintA.address;
const amountBN = new BN(amount);
const swapResult = CurveCalculator.swap(
amountBN,
baseIn ? baseReserve : quoteReserve,
baseIn ? quoteReserve : baseReserve,
configInfo.tradeFeeRate
);
const { transaction } = await raydium.cpmm.swap({
poolInfo,
baseIn,
swapResult,
inputAmount: amountBN,
txVersion: TxVersion.V0,
slippage: 0.01,
config: {
associatedOnly: false,
checkCreateATAOwner: true,
},
});
return transaction;
}
Caught error when rayidum.cpmm.swap
:
Error: ["user do not have token account",{"mintA":"WSOL","mintB":"TRUMP","mintATokenAcc":"2rkZ45PrZgYVjNr8Sau5eBvnyargmc3pyEidrfW9fno7","mintAUseSOLBalance":true,"mintBUseSOLBalance":false,"associatedOnly":true}]
I don’t want to execute a separate transaction to create the associated account. I hope it can be automatically created during the swap execution.
Is it possible to achieve it? If so, how should it be implemented?
Metadata
Metadata
Assignees
Labels
No labels