diff --git a/README.md b/README.md index e883978..df5a0ef 100644 --- a/README.md +++ b/README.md @@ -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;