swap
function:token0
and WETH is token1
. amount0Out
and amount1Out
specify the amount of DAI and WETH that the msg.sender
wants the pair to send to the to
address (one of these amounts may be 0). At this point you may be wondering how the contract receives tokens. For a typical (non-flash) swap, it’s actually the responsibility of msg.sender
to ensure that enough WETH or DAI has already been sent to the pair before swap
is called (in the context of trading, this is all handled neatly by a router contract). But when executing a flash swap, tokens do not need to be sent to the contract before calling swap
. Instead, they must be sent from within a callback function that the pair triggers on the to
address.data
parameter. Specifically, if data.length
equals 0, the contract assumes that payment has already been received, and simply transfers the tokens to the to
address. But, if data.length
is greater than 0, the contract transfers the tokens and then calls the following function on the to
address:data
parameter. It’s expected that data
will be abi.decode
d from within uniswapV2Call
. In the rare case where no data is required, callers should ensure that data.length
equals 1 (i.e. encode a single junk byte as bytes
), and then ignore this argument in uniswapV2Call
.uniswapV2Call
with the sender
argument set to the msg.sender
of the swap
. amount0
and amount1
are simply amount0Out
and amount1Out
.uniswapV2Call
functions:msg.sender
is an actual Swipeswap pair address.uniswapV2Call
, contracts must return enough tokens to the pair to make it whole. Specifically, this means that the product of the pair reserves after the swap, discounting all token amounts sent by 0.3% LP fee, must be greater than before.getAmountIn
pricing function should be used to calculate e.g., the amount of WETH that must be returned in exchange for the amount of DAI that was requested out.DAIReservePre - DAIWithdrawn + (DAIReturned * .997) >= DAIReservePre
(DAIReturned * .997) - DAIWithdrawn >= 0
DAIReturned >= DAIWithdrawn / .997
.003 / .997 ≈ 0.3009027%
.