Deep Understanding of Swap
Swap Logic Breakdown
_swap1in Function
_swap1in Functionfunction _swap1in(uint input, uint minOutput) internal returns (uint fee, uint output) {
// ... (omitting details for brevity)
// Fee calculation based on fee ratio for token1
fee = uint(_feeRatio1) * input / MAX_FEE_RATIO;
// Adjust input by deducting the fee
uint inputLessFee = input - fee;
// Calculate the output using the invariant formula
output = inputLessFee * reserve0 / (reserve1 + inputLessFee);
// Ensure the minimum output requirement is met
require(output >= minOutput, "slippage");
// Update fee ratio for token0 based on the output proportion of the reserve0w
uint64 feeRatioAdded = uint64(output * MAX_FEE_RATIO / reserve0);
_updateFeeRatio1(_feeRatio1, feeRatioAdded);
}Conclusion
Last updated