UPD: add code
This is swap function:
async function execSwap(input) {
const myPublicKey = myKeyPair.publicKey
let targetPoolInfo;
while (true) {
await sleepTime(15000);
try {
targetPoolInfo = await formatAmmKeysById(input.targetPool);
if (targetPoolInfo) {
break; // If successful, exit the loop
}
} catch (error) {
console.error('pool not found, retrying...');
}
}
let poolKeys = jsonInfo2PoolKeys(targetPoolInfo);
const minAmountOut = new TokenAmount(input.outputToken, 1);
const walletTokenAccounts = await getWalletTokenAccount(connection, myPublicKey)
console.log(myPublicKey)
try {
const instruction = await Liquidity.makeSwapInstructionSimple({
connection,
poolKeys,
userKeys: {
tokenAccounts: walletTokenAccounts,
owner: myPublicKey
},
amountIn: input.inputTokenAmount,
amountOut: minAmountOut,
fixedSide: 'in',
makeTxVersion,
})
} catch (error) {
console.error(error);
}
This is "main.js":
const inputToken = quoteToken; // WSOL
const outputToken = baseToken; // custom token
const inputTokenAmount = new TokenAmount(inputToken, 10**8);
const slippage = new Percent(1, 100);
const wallet = fromKeypair;
console.log(wallet)
const res = await execSwap({
targetPool,
inputTokenAmount,
outputToken,
slippage,
wallet
});