Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pool swap接口开发 #100

Closed
wants to merge 0 commits into from
Closed

pool swap接口开发 #100

wants to merge 0 commits into from

Conversation

gpteth
Copy link

@gpteth gpteth commented Jul 16, 2024

pool swap接口开发

Copy link

vercel bot commented Jul 16, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
wtf-dapp ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 20, 2024 4:19pm

@yutingzhao1991
Copy link
Collaborator

代码主要先往 https://github.com/WTFAcademy/WTF-Dapp/tree/main/demo-contract 这里提,保证工程可以跑起来

@yutingzhao1991
Copy link
Collaborator

这个课程的代码不用单独放到课程的 code 里面了,直接放到 demo-contract 里面就行,因为这个已经是最终的代码了,而且量比较大,直接在工程里面维护会比较好。

function liquidity() external view returns (uint128);

function positions(
int8 positionType
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在之前的讨论中 #94 为了让设计更合理,以及简化教程,positionType 已经被移除了。逻辑需要调整。一个 Pool 里面的流动性都是同一个价格范围内的。

#101 这个 PR 里面把之前漏掉删除的一些 positionType 删掉了

@yutingzhao1991
Copy link
Collaborator

@gpteth 代码有些冲突了

@@ -0,0 +1,2323 @@
// SPDX-License-Identifier: GPL-2.0-or-later
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个文件是不是误提交了,合约代码应该都是在 demo-contract 里面吧?

) external;
}

interface IPool {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里怎么重复定义了一个 IPool,而不是用 interfaces/IPool.sol

// 记录 token1 的每单位流动性所获取的手续费
uint256 public feeGrowthGlobal1X128;

int24 public tickSpacing;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码格式有些问题,中间有多余空格。下面几行也有同样的问题。

int24 public tickSpacing;

address public _factory;
// address public factory;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// address public factory;

int24 tickUpper_
) external override {}
) external override {
require(slot0.sqrtPriceX96 == 0, 'AI');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI 是什么的缩写?语义是不是可以更具体一点。不过我看 uni 的代码也是这么写的 https://github.com/Uniswap/v3-core/blob/main/contracts/UniswapV3Pool.sol#L272

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看了下,应该是 'AI' (Already Initialized)。的意思,感觉课程的话可以不用考虑节约 gas ,直接用 Already Initialized 这个语义更好,代码更容易懂。或者注释一下也行。


// emit Initialize(sqrtPriceX96, tick);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没用的代码就直接删掉吧

) external override {
require(slot0.sqrtPriceX96 == 0, 'AI');

int24 tick = TickMath.getTickAtSqrtRatio(sqrtPriceX96);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

针对这些代码里面可以多一些注释,这样新手看代码学的时候会更好理解


function sqrtPriceX96() external view override returns (uint160) {}
struct Slot0 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有点好奇 uni 这个 struct 为什么要取一个 Slot0 的名字?为啥有一个 0

function mint(
address recipient,
int8 positionType,
int24 tickLower,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在我们的设计里面,mint 方法应该没有 tickLower 和 tickUpper 了,这两个是在 Pool 上面的。

});

// 当剩余可交易金额为零,或交易后价格达到了限定的价格之后才退出循环
while (state.amountSpecifiedRemaining != 0 && state.sqrtPriceX96 != sqrtPriceLimitX96) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你这个应该还是整体参考了 Uniswap V3 的代码吧,V3 代码比较复杂。所以我们课程设计的时候做了调整。每个 Pool 都定义了价格上下限,不会出现一个 Pool 里面有不同价格区间的流动性。所以应该也不需要这样循环处理,你可以认为整个 Pool 里面的流动性都是在同一个价格区间内的,所以只要整个 Pool 没有超出价格就可以交易。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好OK

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有空可以帮忙 review 下 #107 ,完善了设计上的一些细节

@yutingzhao1991
Copy link
Collaborator

代码有冲突了


function token1() external view override returns (address) {}
function transferETH(address _address, uint256 amount) public returns (bool) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我们这个课程目前不需要实现原生代币的交易,只需要支持 ERC20 就行

@yutingzhao1991
Copy link
Collaborator

@gpteth #117 swap 写好了,感兴趣帮忙 review 也可以。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants