Deep Understanding of Swap
Swap Logic Breakdown
The token swap logic in the Dyson Pair Contract is divided into two main functions: _swap0in
and _swap1in
, corresponding to swapping token0 for token1 and token1 for token0, respectively. Let's focus on the _swap1in
function to understand the core calculation logic.
_swap1in
Function
_swap1in
FunctionMAX_FEE_RATIO
is set to 2^64
.
Calculate Output:
Obtain the fee ratios,
feeRatio0
andfeeRatio1
, by using thegetFeeRatio()
function. This step updatesd the fee ratios based on the half-life mechanism. When a trade happens, the fee will increase immediately, and decrease as time goes on. The change will keep going until the trading fee reduces to zero.Fee1 calculation based on fee ratio for token1:
fee1 = uint(_feeRatio1) * input / MAX_FEE_RATIO
Adjust input by deducting the fee:
inputLessFee = input - fee1
Use the constant product formula
xy = k
to calculate the output:output = inputLessFee * reserve0 / (reserve1 + inputLessFee)
Increase
fee0
:After the swap, to account for changes in the pool ratios and mitigate arbitrage opportunities for liquidity providers, an additional fee (
fee0
) is introduced for theswap0in
operation.Calculate the added fee0 using:
feeRatioAdded = uint64(MAX_FEE_RATIO * output / reserve0)
This calculation is based on the proportion of the totaltoken0
in the pool represented by the tokens swapped out during the_swap0in
operation.Update the fee ratio for
token0
using:_updateFeeRatio0(_feeRatio0, feeRatioAdded);
This step involves utilizing the formulanewFee = 1 - (1 - x%)(1 - y%)
to calculate the new fee ratio, and the result is stored for future use. About this formula please refer to Trading Fee Calculation section in our white paper.
Conclusion
The Dyson Pair Contract's swap mechanism introduces a dynamic fee model and premium calculation to incentivize users based on their chosen lock times. The core swap logic, exemplified by the _swap1in
function, showcases the intricacies of fee calculation, input adjustment, and output determination. This approach provides users with a transparent and adaptive swapping experience, aligning incentives with market conditions and user preferences.
Last updated