Jupiter Swap via Flash-Fill
Integrate your program with Jupiter Swap without the limitations of CPI via the "Flash-Fill" approach.
Flash-Fill is one of two approaches to integrate Jupiter swap with your protocol. The other approach is CPI.
Our team engineered "flash-fill" to allow developers and integrators to utilize the full potential of Jupiter swap with their programs.
In order to get the best prices and maximum returned amount for any swaps, Jupiter splits and routes an order across multiple DEXes in a single transaction to minimize price impact while prioritizing routes with the lowest prices. Transactions on Solana has a maximum of 1232 bytes. With lookup tables, the Jupiter transaction can pack more accounts (32 bytes per account) into one transaction. As such, because the CPI approach has no the ability to use lookup tables to minimize the size of each account, CPI calls to swap via Jupiter would generally fail.
note: when using Jupiter's API, you can set maxAccounts to reduce the number of accounts.
Here we are showing an example on how to utilize Jupiter Swap via Flash Fill to swap from any tokens to SOL even if the user doesn't have enough SOL. You can even allow a third-party payer if the user doesn't have any SOL at all.
A note of caution Unlike a typical flash loan, the repayment is in a different mint from the loan. As such, there is no easy way to ensure that the repayment amount is appropriate, do take extra measures to minimize the surface area for exploits. For e.g. making the instruction permissioned to trusted admins or utilizing a price oracle etc+.
The exact implementation is protocol specific and, hence, beyond the scope of this guide.
How this works?
For a Flash Fill to work, the transaction will be composed of these instructions:
- Borrow enough SOL for opening the wSOL account from this program.
- Create the wSOL account for the borrower.
- Swap X token to wSOL.
- Close the wSOL account and send it to the borrower.
- Repay the SOL for opening the wSOL account back to this program.
Here is the transaction on chain on how this works.