🔄Swapping
Integrate Ekubo protocol to provide better prices to swappers or to perform arbitrage
To swap on Ekubo, you must called ICore#lock
. Ekubo's core contract will call back into your contract with the data you pass, via IYourContract#locked
. In your lock callback, you must execute the swap, and pay the input, and withdraw the output in no particular order.
Note you can do multiple swaps in a single callback and you only have to pay any differences.
Example code
The below code locks the Ekubo core contract and calls swap, then pays for the input.
Ekubo Protocol supports exact-output swaps, e.g. "buy 1 ETH, compute the input amount," by specifying a negative amount of the desired output token.
If you are making multiple trades against Ekubo, you will see better gas efficiency by utilizing only a single lock.
Instead of withdrawing a delta, you can also save the delta for later using #save
. Later, you can load it to pay for swaps using #load
. This completely avoids token transfers, which can protect you from undesirable token behavior such as fee-on-transfer. Note you must give a user another different token to represent the saved balance.
Note on Extensions
Extensions can modify pool state before you get to swap against it. Some extensions will update positions just-in-time before the swap, and others will front-run a swap with their own.
There are 2 ways to integrate extensions as a swapper:
read the code for the extension and support it by off-chain simulation
use a quoter contract to simulate swaps across pools, which always includes extension behavior
With the latter approach, you will always be susceptible to per-block changes in behavior of the extension, however this is no different from exposure to other front-runners. You should always check the amounts received from a swap are better than some slippage-adjusted amount to protect the user from misbehaving extensions, in the exact same way you would protect against front running.
Last updated