Use Case
Let’s set the stage. You are selling a jupcake!!! to your customer and merchant might only accept in 1 USDC, but your customer only has 1 SOL. Well, you’re at the right place! By using the Legacy Swap API, merchant can let customer pay in SOL while merchant still receive USDC in order to complete the payment for a jupcake.- Customer has 1,000,000 SOL.
- Merchant sells 1 jupcake for 1 USDC.
- Use the Legacy Swap API to swap exactly 1 USDC output from Customer’s SOL.
- Merchant receives the 1 USDC, as planned!
Let’s Get Started
1. Setup
You will need slightly different imports and also remember to set up connection to an RPC. If you have not set up the other typical libraries or are familiar with the Legacy Swap API, please follow this Environment Setup and Get Quote and Swap guide.NOTEDo note that you will need to have already set up:
- A wallet in your machine to simulate yourself as the customer as the customer is the signer of the transaction (similar to how we set up in Environment Setup).
trackingAccount
is an additional Solana Account you can pass in to track only Jupiter transactions easily.
Set Up Accounts
Set Up destinationTokenAccount
One more thing you will need to set up! Later on, you will need to pass in destinationTokenAccount
which will be your token account for your preferred token payment mint. Do note that it is the merchant’s token account and it needs to be initialized.
2. Set swapMode
to ExactOut
in Quote
Next, the merchant have to Get Quote for the customer. We are using the ExactOut
mode because we know exactly how much output amount (1 USDC) the merchant want to receive but not sure how much input amount the customer should pay with.
By getting a quote first, the customer can know upfront the specific amount of input token before they approve and sign the transaction.
LIMITATIONS OF
ExactOut
Currently, there are some limitations as ExactOut
is not widely supported across all DEXes.- Supported DEXes are only Orca Whirlpool, Raydium CLMM, and Raydium CPMM.
- NOT ALL token pairs may be available.
amount
specified in the query parameter represents the outAmount
in the response and of course, swapMode: ExactOut
.
3. Set destinationTokenAccount
in Swap
The merchant then retrieves the serialized swap transaction, but the merchant need to specify the destinationTokenAccount
in the parameters — this will build the swap transaction to swap but send to the merchant’s specified token account which we defined earlier.
The destinationTokenAccount
should be the merchant’s token account to receive the payment in. Also do note that customerAccount
should be accounted for. You can refer to the Build Swap Transaction guide for other parameters to be passed in.
4. Prepare Transaction
We have walked through the steps here and explained some of the code, you can refer to Send Swap Transaction - Prepare Transaction. The main difference for payments is to ensure that the customer is the fee payer (the merchant can be generous and be the fee payer too!) and the signer.5. Send Transaction
We have walked through the steps here and explained some of the code, you can refer to Send Swap Transaction - Send Transaction. The main difference for payments is, you might want to try adjustingmaxRetries
to a higher count as it is not time sensitive and ideally this is used with tighter slippage and ensuring the inputMint
is not too unstable.
Do note that more retries will cause the user to wait slightly longer, so find the balance between the two. Read more here: https://solana.com/docs/advanced/retry.
- Token A swaps from the customer’s token account
- Token A swap to Token B
- Token B sends to the merchant’s token account