I use Jupiter V6 swap request to get a post request and it works well. My question is hot to add 1 more instruction with SOL transfer to my fee address? 0.1% of SOL for example
I've been trying to add it for a long hours and I haven't been successful, so I ask you to help me. Please give me any advice or tell me what am I doing wrong. Maybe I should use /swap-instructions instead? Or I should receive legacyTransaction and then just add 1 more instruction. Please help
const fetch = require('cross-fetch');
const { Wallet } = require('@project-serum/anchor');
const bs58 = require('bs58');
const { createJupiterApiClient } = require('@jup-ag/api');
const jupiterQuoteApi = createJupiterApiClient();
const transactionSenderAndConfirmationWaiter = require("../../utils/transactionSender.js").transactionSenderAndConfirmationWaiter;
const getSignature = require("../../utils/getSignature.js").getSignature;
const { feeAddress } = require('../../main/variables.js');
const feePublicKey = new PublicKey(feeAddress)
async function swapSolToToken(connection, requestBody) {
const wallet = new Wallet(Keypair.fromSecretKey(bs58.decode(requestBody.privateKey)));
const realSolAmount = requestBody.solAmount * 1000000000
// Swapping SOL to Token quote
const quoteResponse = await jupiterQuoteApi.quoteGet({
inputMint: "So11111111111111111111111111111111111111112",
outputMint: requestBody.tokenMint,
amount: realSolAmount,
// platformFeeBps: 10,
// asLegacyTransaction: true, // legacy transaction, default is versoined transaction
slippageBps: 2500
});
if (!quoteResponse) {
console.error("unable to quote");
return;
}
const swapResult = await jupiterQuoteApi.swapPost({
swapRequest: {
// quoteResponse from /quote api
quoteResponse: quoteResponse,
// user public key to be used for the swap
userPublicKey: wallet.publicKey.toBase58(),
// auto wrap and unwrap SOL. default is true
wrapAndUnwrapSol: true,
dynamicComputeUnitLimit: true,
prioritizationFeeLamports: "auto",
// prioritizationFeeLamports: {
// autoMultiplier: 2,
// },
// feeAccount is optional. Use if you want to charge a fee. feeBps must have been passed in /quote API.
// feeAccount: "fee_account_public_key"
},
});
// deserialize the transaction
const swapTransactionBuf = Buffer.from(swapResult.swapTransaction, 'base64');
var transaction = VersionedTransaction.deserialize(swapTransactionBuf);
console.log(transaction);
// sign the transaction
transaction.sign([wallet.payer]);
const signature = getSignature(transaction);
// We first simulate whether the transaction would be successful
const { value: simulatedTransactionResponse } =
await connection.simulateTransaction(transaction, {
replaceRecentBlockhash: true,
commitment: "processed",
});
const { err, logs } = simulatedTransactionResponse;
if (err) {
// Simulation error, we can check the logs for more details
// If you are getting an invalid account error, make sure that you have the input mint account to actually swap from.
console.error("Simulation Error:");
console.error({ err, logs });
return;
}
const serializedTransaction = Buffer.from(transaction.serialize());
const blockhash = transaction.message.recentBlockhash;
const transactionResponse = await transactionSenderAndConfirmationWaiter({
connection,
serializedTransaction,
blockhashWithExpiryBlockHeight: {
blockhash,
lastValidBlockHeight: swapResult.lastValidBlockHeight,
},
});
// If we are not getting a response back, the transaction has not confirmed.
if (!transactionResponse) {
console.error("Transaction not confirmed");
return;
}
if (transactionResponse.meta?.err) {
console.error(transactionResponse.meta?.err);
}
console.log(`https://solscan.io/tx/${signature}`);
return signature
}```