diff --git a/content/JakeDu/24.md b/content/JakeDu/24.md index dc203732..788d1cdf 100644 --- a/content/JakeDu/24.md +++ b/content/JakeDu/24.md @@ -15,3 +15,22 @@ eg: 可乐价值1u,然后有个池子,我们放进去10u和10瓶可乐,k #### 去中心化交易所 在去中心化交易所中,使用的自动做市商的算法是恒定乘积,保证了价格的动态变化。 k = x * y。 + +getAmountOut精准输入计算输出的方式: +```solidity + // 由于x与y是固定比例,所以x增加的比例 * y的数量,就可以求出y的数量。 + uint amountInWithFee = amountIn.mul(997); + uint numerator = amountInWithFee.mul(reserveOut); + uint denominator = reserveIn.mul(1000).add(amountInWithFee); + amountOut = numerator / denominator; + +``` + +swap通过输出的token计算是否满足输入的方式: + 通过计算拿到一次swap输入的token数量,然后拿到总余额中扣除一次手续费千分之三的x的balance乘y的balance,得到的k值应该要大于或者等于之前的k值。 +```solidity +uint balance0Adjusted = balance0.mul(1000).sub(amount0In.mul(3)); +// balance0 = reserve + inputTokenNumber +// reserve * 1000 + amount0In * 1000 - amount0In * 3 +// balance0 * 1000 - amount0In * 3 +```