Skip to content

Commit

Permalink
Update node. solidity103 lesson: 56. Author: JakeDu
Browse files Browse the repository at this point in the history
  • Loading branch information
Ling ye authored and Ling ye committed Oct 17, 2024
1 parent f4d3c17 commit 2b61e4d
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions content/JakeDu/24.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

0 comments on commit 2b61e4d

Please sign in to comment.