Integration: Flash Fill

Jupiter Swap via Flash-Fill

Use Flash Fill to integrate your program with Jupiter Swap. This allows you to bypass the limitations of Cross Program Invocation (CPI) via the "Flash-Fill" approach. Flash-Fill is one of two methods to integrate Jupiter Swap with your protocol; the other approach is through Cross Program Invocation (CPI).

The Jupiter team engineered Flash-Fill to enable developers and integrators to fully leverage the potential of Jupiter Swap within their programs.

📘

Why Flash-Fill?

To achieve the best prices and highest returns on swaps, Jupiter divides and routes an order across multiple DEXes in a single transaction, minimizing price impact by prioritizing the lowest-cost routes. Solana transactions are limited to 1,232 bytes of storage. By using lookup tables, Jupiter can include more accounts (32 bytes per account) in one transaction. However, the CPI method cannot use lookup tables to reduce the size of each account, so CPI calls to swap via Jupiter typically fail.

Flash-Fill allows the use of Versioned Transactions in combination with Address Lookup Tables, reducing the "size" of each account—something that is not possible with the CPI approach.

Note: When using Jupiter's API, you can set maxAccounts to reduce the number of accounts.

Example

This transaction is an example of how to utilize Jupiter Swap via Flash-Fill. In this case, we use Flash-Fill to swap any token to SOL, even if the user doesn't have enough SOL. You can also allow a third-party payer to cover the transaction fees if the user has no SOL at all.

This approach ensures that users can complete swaps without needing to hold SOL upfront, making it more accessible and flexible.

❗️

Caution

Unlike a typical flash loan, the repayment is in a different mint from the loan. As such, there is no straightforward way to ensure the repayment amount is appropriate. Extra precautions should be taken to minimize the potential for exploits. For instance, you could make the instruction permissioned for trusted admins or utilize a price oracle to verify the repayment.

The exact implementation is protocol-specific and beyond the scope of this guide.

How Does This Work?

For Flash Fill to function, the transaction will include the following steps:

  1. Borrow enough SOL from this program to open the wSOL account.
  2. Create the wSOL account for the borrower.
  3. Swap X token to wSOL.
  4. Close the wSOL account and send the wSOL to the borrower.
  5. Repay the SOL borrowed to open the wSOL account back to the program.

Code Repo

Here is the GitHub repository: https://github.com/jup-ag/sol-swap-flash-fill. Check out both the program code and the client code.

You can also view the transaction on-chain to see how this works.