Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,19 @@ contract Exchange is ERC20 {
// We are charging a fee of `1%`
// Input amount with fee = (input amount - (1*(input amount)/100)) = ((input amount)*99)/100
uint256 inputAmountWithFee = inputAmount * 99;

// Because we need to follow the concept of `XY = K` curve
// We need to make sure (x + Δx) * (y - Δy) = x * y
// So the final formula is Δy = (y * Δx) / (x + Δx)
// So the final formula is Δy = (y * Δx) / (x + Δx) = numerator / denominator
// Δy in our case is `tokens to be received`
// Δx = ((input amount)*99)/100, x = inputReserve, y = outputReserve
// Δx = ((input amount)*99)/100 = inputAmountWithFee / 100
// x = inputReserve, y = outputReserve

// So by putting the values in the formulae you can get the numerator and denominator
// numerator = y * Δx = outputReserve * inputAmountWithFee / 100
// denominator = x + Δx = inputReserve + (inputAmountWithFee / 100) = [(inputReserve * 100) + inputAmountWithFee] / 100
// since both have 100 as base, we can remove it and thus:

uint256 numerator = inputAmountWithFee * outputReserve;
uint256 denominator = (inputReserve * 100) + inputAmountWithFee;
return numerator / denominator;
Expand Down