Next Swap
Unlike AMMs, in Mean Finance swappers cannot specify how much they'd like to swap. These amounts are determined by the positions created for the different pairs. As a result, the first step towards executing a swap would be to understand which tokens need to be provided, and how big is the reward.
Time Until Next Swap
As we explained before, all positions have a swap interval that determines when they can be swapped. So it could happen that, at some point, a pair has no swaps to execute. If that is the case, trying to execute swap
for only that pair would end up reverting. That's why you should check when the next swap will be available.
To do so, you can call secondsUntilNextSwap
on the Companion. This functions takes a list of pairs, and returns the amount of seconds until the next swap is available for each of those pairs.
Next Swap's Information
When secondsUntilNextSwap
returns 0 for a pair, it means that you can execute a swap right now. If that's the case, you can call getNextSwapInfo
to get all information regarding this swap, and determine if you want to execute it.
IfsecondsUntilNextSwap
is not 0, you can still callgetNextSwapInfo
, but the result will be zero-ed and meaningless.
The Parameters
Calling getNextSwapInfo
or swap
is not as straight forward as we'd like. This is mainly because Solidity has restrictions when it comes to non-storage mappings. So, in order to lower the gas costs, we expect the parameters to be already organized in a particular way.
If you look at the function's definition, you'll see that it takes two parameters that reference what is being swapped: tokens
and pairs
. The idea is the following, let's assume that you'd like to understand how a swap with the following pairs would look like:
WBTC/DAI
WBTC/USDC
YFI/WETH
The fist thing to do, would be to build a list of all the tokens involved, and sort them by address. So, since:
DAI =
0x6b175474e89094c44da98b954eedeac495271d0f
WETH =
0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
USDC =
0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48
WBTC =
0x2260fac5e5542a773aa44fbcfedf7c193bc2c599
YFI =
0x0bc529c00C6401aEF6D220BE8C6Ea1667F6Ad93e
The list would look like this:
Once the list is built, we need to associate the pairs by their indexes on the list. Take into account that this list also needs to be sorted. Based on the example, it would look like this:
Once you have these two parameters built, you can call the Hub.
A little help from the Periphery
We know this is fairly complicated, so we've tried to make it a little bit simpler. We've built a library that takes a list of pairs { address tokenA; address tokenB }
and builds these parameters for you.
We've also added a function on the Companion that takes this easier format, generates the parameters and calls the hub for you:
Please notice that while this new call is perfect for off-chain queries, the generation of the parameters can be expensive for on-chain purposes. So please consider both the Hub's and the Companion's versions when integrating.
The Result
When calling getNextSwapInfo
, you will get a struct called SwapInfo
as a result. It will contain all information necessary about the swap, from the ratio between tokens in the same pair, to how much the swapper needs to provide, and how much they'll get as a reward. The struct looks like this:
Last updated