Swap Steps
Last updated
Last updated
A Sovereign Pool swap performs the following steps:
Lock all state modifying functions in the pool.
Basic checks on _swapParams
:
_swapParams.amountIn
cannot be 0.
_swapParams.recipient
must be non-zero.
_swapParams.swapTokenOut
cannot be zero address nor input token.
In case the pool is meant to hold reserves (sovereignVault == address(this)
), _swapParams.swapTokenOut
must be either token0
or token1
, meaning that only token0 ↔ token1 swaps are supported. Only if an external vault is specified (sovereignVault ≠ address(this)
) then multi-hop swaps are allowed (where _swapParams.swapTokenOut
could differ from token0
or token1
).
If _verifierModule
is non-empty , the Verifier Module is called with the relevant input data. If invalid, it triggers a revert, otherwise, the swap continues and caches any return data from _verifierModule
.
If Swap Fee Module is set, it gets called with the relevant input data to return the swap fee (since we do not yet know how much tokenIn liquidity will be filled, this fee will be applied after calling getLiquidityQuote
). If Swap Fee Module is not set, a constant and immutable pool fee will be applied. Alternatively, both can be ignored and fees can be designed into a Liquidity Module's internal pricing.
LM is called using getLiquidityQuote
with all relevant inputs, including return data from Verifier Module if applicable, and returns the liquidity quote information.
Consistency checks for the liquidity quote are applied. The transaction reverts if one or more of the following are broken:
Transfer the input token from msg.sender
into sovereignVault
. Moreover, in the case where sovereignVault == address(this)
, the input token is a rebase token, and pool manager fees bips for input token are non-zero: we transfer a portion of the input token amount to the poolManager
as a fee. This is done to simplify accounting of rebase tokens at the pool level.
Update pool’s state variables, including: reserves of token0
and token1
(in case of non-rebase tokens), and feePoolManager0
or feePoolManager1
(in case of non-rebase tokens).
Update Oracle Module’s state with the latest swap data: amountInUsed
, amountOut
and effectiveFee
.
Transfer funds to _swapParams.recipient
. If liquidity is stored in the pool the funds are transferred directly using ERC20 transfer()
, otherwise sovereignVault
is expected to approve the pool before hand, which then transfers funds to _swapParams.recipient
to via ERC-20 transferFrom(...)
Perform the following callbacks on swap end:
All state modifying functions in the pool are unlocked.