I am gonna buy and sell SPL tokens using Jupiter Quote API and Swap API. My code works with some tokens but I failed with some tokens. However, I can see lots of holders for those tokens on Solcan. I am not sure what is wrong.
This is main code snippet.
import { Wallet } from "@project-serum/anchor";
import axios from "axios";
const JUP_API = "https://quote-api.jup.ag/v6";
export const getSwapInfo = async (tokenA: string, tokenB: string, amount: number, slippageBps: number) => {
const res = await axios.get(`${JUP_API}/quote?inputMint=${tokenA}&outputMint=${tokenB}&amount=${amount}&slippageBps=${slippageBps}`);
const swapinfo = res.data;
return swapinfo;
// Get the serialized transactions to perform the swap
};
export const getSwapTransaction = async (quoteResponse: any, anchorWallet: Wallet) => {
const swapResponse = await axios.post(`${JUP_API}/swap`, {
// quoteResponse from /quote api
quoteResponse,
userPublicKey: anchorWallet.publicKey.toString(),
wrapAndUnwrapSol: true,
prioritizationFeeLamports: 200000, // or custom lamports: 1000
});
return swapResponse.data.swapTransaction;
};
const connection = new Connection(RPC_URL, "confirmed");
const tokenDecimals = await getTokenDecimals(connection, tokenAddress);
const slippageBps = parseInt(process.env.SLIPPAGEBPS || "") || 50;
let swapInfo: any;
swapInfo = await getSwapInfo(
NATIVE_MINT.toBase58(),
tokenAddress,
amount * LAMPORTS_PER_SOL,
slippageBps
);
const swapTransaction = await getSwapTransaction(swapInfo, anchorWallet);
const swapTransactionBuf = Buffer.from(swapTransaction, "base64");
const latestBlockHash = await connection.getLatestBlockhash();
const versionedTransaction =
VersionedTransaction.deserialize(swapTransactionBuf);
versionedTransaction.message.recentBlockhash = latestBlockHash.blockhash;
versionedTransaction.sign([anchorWallet.payer]);
I tried with your code and it worked well.
But if you have issues for some SPL tokens, I think you need to update Jupiter v6 API.
Jupiter v6 API is already old and you can use new API version.
Please check this link: https://station.jup.ag/docs/.
I tried with this API - "https://api.jup.ag/swap/v1" and it also worked well.
I hope this helps you.