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

Complete milestone 2 without ui #3

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/UniswapV3Quoter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.14;

import "./UniswapV3Pool.sol";
import "./interfaces/IUniswapV3Pool.sol";

contract UniswapV3Quoter {
// 在用户真正兑换之前,根据输入的代币数量计算能够得到的代币数量
// 为了计算swap数量,会初始化一个真正的swap过程,并在回调函数中中断,来获取pool合约计算的数量

struct QuoteParams {
address pool;
uint256 amountIn;
bool zeroForOne;
}

// quote函数模拟一个swap的过程,计算swap出的数量,适用于任何pool
// 调用的pool.callback函数预计会revert,但quote函数不会revert
// 在回调函数中主动实现revert会保证quote不会修改pool合约的状态,但是从客户端(Ethers.js和Web3.js等)调用quote将会触发一个交易
// 为了解决这个问题,我们需要强制库进行静态调用(static call)
function quote(QuoteParams memory params)
public
returns (
uint256 amountOut,
uint160 sqrtPriceX96After,
int24 tickAfter
)
{
try
UniswapV3Pool(params.pool).swap(
address(this),
params.zeroForOne,
params.amountIn,
abi.encode(params.pool)
)
{} catch (bytes memory reason) {
return abi.decode(reason, (uint256, uint160, int24));
}
}

function uniswapV3SwapCallback(
int256 amount0Delta,
int256 amount1Delta,
bytes memory data
) external view {
address pool = abi.decode(data, (address));

// 兑换后的代币数量
uint256 amountOut = amount0Delta > 0
? uint256(-amount1Delta)
: uint256(-amount0Delta);

// 兑换后的价格
(uint160 sqrtPriceX96After, int24 tickAfter) = IUniswapV3Pool(pool).slot0();

// 在内联汇编(Yul)中保存这些值并revert
assembly {
let ptr := mload(0x40)
mstore(ptr, amountOut)
mstore(add(ptr, 0x20), sqrtPriceX96After)
mstore(add(ptr, 0x40), tickAfter)
revert(ptr, 96)
}
}
}
13 changes: 13 additions & 0 deletions src/interfaces/IUniswapV3Pool.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.14;

interface IUniswapV3Pool {
function slot0() external view returns (uint160 sqrtPriceX96, int24 tick);

function swap(
address recipient,
bool zeroForOne,
uint256 amountSpecified,
bytes calldata data
) external returns (int256, int256);
}