Update 2:
Was having fun looking at the Jup.ag docs and noticed a page building off of what you where trying to do initially so I will be posting the code for this as well.
// Define the referral account public key (obtained from the referral dashboard)
const referralAccountPublicKey = new PublicKey("your_referral_account_public_key_here");
// Define the fee token account (use the correct mint according to your swap's input or output mint)
const feeTokenAccount = await PublicKey.findProgramAddressSync(
[Buffer.from("referral_ata"), referralAccountPublicKey.toBuffer(), requestBody.tokenMint.toBuffer()],
new PublicKey("REFER4ZgmyYx9c6He5XfaTMiGfdLwRnkV4RPp9t9iF3")
);
// Get a quote with platform fee included
const quoteResponse = await jupiterQuoteApi.quoteGet({
inputMint: "So11111111111111111111111111111111111111112",
outputMint: requestBody.tokenMint,
amount: requestBody.solAmount * 1000000000, // Real SOL amount in lamports
slippageBps: 2500,
platformFeeBps: 20 // Set your platform fee here in basis points
});
if (!quoteResponse) {
console.error("unable to quote");
return;
}
// Execute the swap with the fee account specified
const swapResult = await jupiterQuoteApi.swapPost({
swapRequest: {
quoteResponse: quoteResponse, // Quote response from /quote API
userPublicKey: wallet.publicKey.toBase58(), // User's public key for the swap
wrapAndUnwrapSol: true, // Auto wrap and unwrap SOL if necessary
dynamicComputeUnitLimit: true, // Dynamically adjust compute units
prioritizationFeeLamports: "auto", // Optionally set prioritization fees
feeAccount: feeTokenAccount[0].toBase58() // Specify the fee token account
},
});
// Deserialize the received transaction
const swapTransactionBuf = Buffer.from(swapResult.swapTransaction, 'base64');
var transaction = VersionedTransaction.deserialize(swapTransactionBuf);
// Sign and send the transaction
transaction.sign([wallet.payer]); // Ensure the wallet is properly set up and can sign transactions
const rawTransaction = transaction.serialize();
const txid = await sendAndConfirmRawTransaction(connection, Buffer.from(rawTransaction), {
skipPreflight: true,
commitment: 'confirmed',
maxRetries: 2
});
console.log(`Transaction successful: https://solscan.io/tx/${txid}`);
Here you need to ensure that your Referral Account has been created with Jup.ag in which the feeAccount will be derived from and matches the mint of the swap's output (for ExactIn) or input (for ExactOut).
platformFeeBps needs to be included in /quote API call and when calling the /swap endpoint, the feeAccount needs to be included there as well.
It is important to note that utilizing this approach that Jup.ag will take a 2.5% cut from the fee's you are collecting as well.