uniswap
flashloan
fee
uniswap-v2
Refer to Uniswap v2 Whitepaper, each swap should satisfy following formula:
To simplify the calculation on-chain:
When we flash loan from Uniswap v2 pair, what is the amount including fee we should repay?
Suppose we flash loan amount of token : .
and are the amount of token and token to repay.
and are the balance of token before and after flash loan, respectively, and are the balance of token before and after flash loan.
The problem is to calculate using .
Since we don't flash loan token , so , , , we can simplify formula (2):
The relation between and is:
Using formula (4) and (5):
We can calculate :
In Solidity contract, to avoid rouding down error, we should add 1 to round up:
where repayAmount is , amountOut is .
So the flashloan fee of Uniswap v2 is: