Swap sell Raydium api
Why when im buy token its ok
But when i want to swap i get error
Token $PENGU
{ id: “d563cefe-e1ff-4272-ad6e-ca34f5066f6c”, success: false,
version: “V1”, msg: “UNKNOWN_ERROR”, }
import { Transaction, VersionedTransaction, sendAndConfirmTransaction, TransactionSignature, Connection, PublicKey } from '@solana/web3.js';
import { NATIVE_MINT } from '@solana/spl-token';
import axios from 'axios';
import { fetchTokenAccountData, getKepair } from './utils';
import { API_URLS, publicKey, Raydium } from '@raydium-io/raydium-sdk-v2';
import config from './config.json';
import { sleep, getConnection, getRandomFloat } from './utils';
import global_config from '../token_config.json';
import BN from 'bn.js';
interface SwapCompute {
id: string;
success: true;
version: 'V0' | 'V1';
openTime?: undefined;
msg: undefined;
data: {
swapType: 'BaseIn' | 'BaseOut';
inputMint: string;
inputAmount: string;
outputMint: string;
outputAmount: string;
otherAmountThreshold: string;
slippageBps: number;
priceImpactPct: number;
routePlan: {
poolId: string;
inputMint: string;
outputMint: string;
feeMint: string;
feeRate: number;
feeAmount: string;
}[];
};
}
const _tokenAccounts : { [owner : string] : any } = {};
export const apiSwap = async (mnemonic: string, data: Record<string, any>, connection : Connection, buy : Boolean) => {
const owner = getKepair(mnemonic);
let inputMint: string;
let outputMint: string;
let amount: BN;
let tokenAccounts = _tokenAccounts[owner.publicKey.toBase58()];
if (buy) {
inputMint = NATIVE_MINT.toBase58();
outputMint = global_config.tokenToSwap;
amount = getRandomFloat(config.buyAmountSol[0], config.buyAmountSol[1]);
console.log(`[${mnemonic}] buying ${amount} SOL`);
amount = new BN(Math.floor(amount * 10 ** 9));
} else {
inputMint = global_config.tokenToSwap;
outputMint = NATIVE_MINT.toBase58();
if (config.sellSwapFull) {
if(!tokenAccounts) {
tokenAccounts = await fetchTokenAccountData(connection, owner);
}
// Находим аккаунт токена, который нужно продать
const inputTokenAcc = tokenAccounts.find((a) => a.mint.toBase58() === inputMint);
if (!inputTokenAcc) {
console.error('Token account not found for the token you want to swap.');
return;
}
// Получаем баланс токена
amount = new BN(inputTokenAcc.amount);
console.log(`[${mnemonic}] selling ${amount / 10 ** 9} tokens`);
} else {
amount = getRandomFloat(config.sellAmountToken[0], config.sellAmountToken[1]);
console.log(`[${mnemonic}] selling ${amount} tokens`);
amount = new BN(Math.floor(amount * 10 ** 9));
}
}
const slippage = 0.5;
const txVersion: string = 'V0';
const isV0Tx = txVersion === 'V0';
const [isInputSol, isOutputSol] = [inputMint === NATIVE_MINT.toBase58(), outputMint === NATIVE_MINT.toBase58()];
if(!tokenAccounts) {
tokenAccounts = await fetchTokenAccountData(connection, owner);
}
const inputTokenAcc = tokenAccounts.find((a) => a.mint.toBase58() === inputMint)
//?.publicKey;
const outputTokenAcc = tokenAccounts.find((a) => a.mint.toBase58() === outputMint)
//?.publicKey;
const { data: sellResponse } = await axios.get<SwapCompute>(
`${API_URLS.SWAP_HOST}/compute/swap-base-out?inputMint=${inputMint}&outputMint=${NATIVE_MINT.toBase58()}&amount=${"1500".toString()}&slippageBps=${slippage * 100}&txVersion=${txVersion}`
);
const { data: swapTransactions } = await axios.post<{
id: string;
version: string;
success: boolean;
data: { transaction: string }[];
}>(`${API_URLS.SWAP_HOST}/transaction/swap-base-out`, {
computeUnitPriceMicroLamports: String(data.data.default.h),
swapResponse: sellResponse,
txVersion,
wallet: owner.publicKey.toBase58(),
wrapSol: false,
unwrapSol: true,
inputAccount: inputTokenAcc?.mint.toBase58(),
outputAccount: NATIVE_MINT.toBase58(),
});
console.log(swapTransactions)
// const { data: swapResponse } = await axios.get<SwapCompute>(
// `${API_URLS.SWAP_HOST}/compute/swap-base-in?inputMint=${inputMint}&outputMint=${outputMint}&amount=${amount.toString()}&slippageBps=${slippage * 100}&txVersion=${txVersion}`
// );
// const { data: swapTransactions } = await axios.post<{
// id: string;
// version: string;
// success: boolean;
// data: { transaction: string }[];
// }>(`${API_URLS.SWAP_HOST}/transaction/swap-base-in`, {
// computeUnitPriceMicroLamports: String(data.data.default.h),
// swapResponse,
// txVersion,
// wallet: owner.publicKey.toBase58(),
// wrapSol: true,
// //isInputSol,
// unwrapSol: false,
// //isOutputSol,
// inputAccount: isInputSol ? undefined : inputTokenAcc?.mint.toBase58(),
// outputAccount: isOutputSol ? undefined : outputTokenAcc?.mint.toBase58(),
// });
// Добавьте эту проверку
// if (!swapTransactions.data || !Array.isArray(swapTransactions.data)) {
// console.error('Invalid transaction data received from the API');
// console.error('Server Response:', swapTransactions);
// return;
// }
const allTxBuf = swapTransactions.data.map((tx) => Buffer.from(tx.transaction, 'base64'));
const allTransactions = allTxBuf.map((txBuf) =>
isV0Tx ? VersionedTransaction.deserialize(txBuf) : Transaction.from(txBuf)
);
console.log(`[${mnemonic}] total ${allTransactions.length} transactions`);
let idx = 0;
if (!isV0Tx) {
for (const tx of allTransactions) {
const transaction = tx as Transaction;
transaction.sign(owner);
try {
const txId = await sendAndConfirmTransaction(connection, transaction, [owner], { skipPreflight: true });
console.log(`[${mnemonic}] ${++idx} transaction confirmed, txId: ${txId}`);
} catch (err) {
if (err instanceof Error && err.message.includes('Signature 3JD8yaVYQuawnhkAJp17mbrpo7QAknvnwYif8QAicavfRcNFM4KPfQvYuWj5JmAwZ6hSqsiJ8iULJ4z32bHnNnxk has expired: block height exceeded.')) {
console.log(`[${mnemonic}] ${++idx} transaction sending failed, trying again...`);
await sleep(1000);
continue;
}
throw err;
}
}
} else {
for (const tx of allTransactions) {
const transaction = tx as VersionedTransaction;
transaction.sign([owner]);
let transcationSended = false;
// while(!transcationSended) {
try {
const txId = await connection.sendTransaction(tx as VersionedTransaction, { skipPreflight: false });
const { lastValidBlockHeight, blockhash } = await connection.getLatestBlockhash({
commitment: 'processed',
});
console.log(`[${mnemonic}] ${++idx} transaction sending..., txId: ${txId}`);
await connection.confirmTransaction(
{
blockhash,
lastValidBlockHeight,
signature: txId,
},
'processed'
);
console.log(`[${mnemonic}] ${++idx} transaction confirmed`);
// transcationSended = true;
} catch (err) {
console.log(err)
if (err instanceof Error && err.message.includes('Signature has expired: block height exceeded.')) {
await sleep(1000);
continue;
}
}
// }
}
}
sleep(config.sleepTime);
};
export default apiSwap;