token0and WETH is
amount1Outspecify the amount of DAI and WETH that the
msg.senderwants the pair to send to the
toaddress (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.senderto ensure that enough WETH or DAI has already been sent to the pair before
swapis 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
dataparameter. Specifically, if
data.lengthequals 0, the contract assumes that payment has already been received, and simply transfers the tokens to the
toaddress. But, if
data.lengthis greater than 0, the contract transfers the tokens and then calls the following function on the
dataparameter. It’s expected that
abi.decoded from within
uniswapV2Call. In the rare case where no data is required, callers should ensure that
data.lengthequals 1 (i.e. encode a single junk byte as
bytes), and then ignore this argument in
senderargument set to the
msg.senderis 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.
getAmountInpricing 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%.