Skip to content

Commit

Permalink
feat: new course init contracts (WTFAcademy#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
yutingzhao1991 authored Jun 6, 2024
1 parent de6ad41 commit e431e1f
Show file tree
Hide file tree
Showing 22 changed files with 465 additions and 21 deletions.
6 changes: 3 additions & 3 deletions 02_NodeService/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

节点服务是 DApp 开发必不可少的服务。它是一个运行在区块链网络上的服务,它可以帮助你与区块链网络进行交互。在 DApp 开发中,我们需要通过节点服务来获取区块链的数据,发送交易等。

在以太坊网络中,我们可以通过 [ZAN](https://zan.top/)[Infura](https://infura.io/)[Alchemy](https://www.alchemy.com/) 等服务来获取节点服务。这些服务都提供了免费的节点服务,当然,它们也提供了付费的服务,如果你的 DApp 需要更高的性能,你可以考虑使用它们的付费服务。
在以太坊网络中,我们可以通过 [ZAN](https://zan.top?chInfo=wtf)[Infura](https://infura.io/)[Alchemy](https://www.alchemy.com/) 等服务来获取节点服务。这些服务都提供了免费的节点服务,当然,它们也提供了付费的服务,如果你的 DApp 需要更高的性能,你可以考虑使用它们的付费服务。

## 配置节点服务

这里以 [ZAN 的节点服务](https://zan.top/home/node-service)为例,指引你如何配置节点服务。
这里以 [ZAN 的节点服务](https://zan.top/home/node-service?chInfo=wtf)为例,指引你如何配置节点服务。

首先注册并登录 [https://zan.top](https://zan.top) 之后进入到节点服务的控制台 [https://zan.top/service/apikeys](https://zan.top/service/apikeys) 创建一个 Key,每个 Key 都有默认的免费额度,对于微型项目来说够用了,但是对于生产环境的项目来说,请结合实际情况购买节点服务。
首先注册并登录 [https://zan.top](https://zan.top?chInfo=wtf) 之后进入到节点服务的控制台 [https://zan.top/service/apikeys](https://zan.top/service/apikeys?chInfo=wtf) 创建一个 Key,每个 Key 都有默认的免费额度,对于微型项目来说够用了,但是对于生产环境的项目来说,请结合实际情况购买节点服务。

创建成功后你会看到如下的页面:

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions P101_InitContract/readme.md → P101_ContractsDesign/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function createAndInitializePoolIfNecessary(
) external payable returns (address pool);
```

完整的接口在 [IPoolManager](./code/interfaces/IPoolManager.sol) 中。
完整的接口在 [IPoolManager](./code/IPoolManager.sol) 中。

#### PositionManager

Expand Down Expand Up @@ -225,7 +225,7 @@ function collect(
) external returns (uint256 amount0, uint256 amount1);
```

完整的接口在 [IPositionManager](./code/interfaces/IPositionManager.sol) 中。
完整的接口在 [IPositionManager](./code/IPositionManager.sol) 中。

#### SwapRouter

Expand Down Expand Up @@ -302,7 +302,7 @@ function exactOutput(
) external payable returns (uint256 amountIn);
```

完整的接口在 [ISwapRouter](./code/interfaces/ISwapRouter.sol) 中。
完整的接口在 [ISwapRouter](./code/ISwapRouter.sol) 中。

#### Factory

Expand Down Expand Up @@ -340,7 +340,7 @@ function parameters()
returns (address factory, address token0, address token1, uint24 fee);
```

完整的接口在 [IFactory](./code/interfaces/IFactory.sol) 中。
完整的接口在 [IFactory](./code/IFactory.sol) 中。

#### Pool

Expand Down Expand Up @@ -474,4 +474,4 @@ interface ISwapCallback {
}
```

完整的接口在 [IPool.sol](./code/interfaces/IPool.sol) 中。
完整的接口在 [IPool.sol](./code/IPool.sol) 中。
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ contract Pool is IPool {
{}

function initialize(
uint160 sqrtPriceX96,
int24 tickLower,
int24 tickUpper
uint160 sqrtPriceX96_,
int24 tickLower_,
int24 tickUpper_
) external override {}

function mint(
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletions P102_InitContracts/code/interfaces/IFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.24;

interface IFactory {
function parameters()
external
view
returns (address factory, address token0, address token1, uint24 fee);

event PoolCreated(
address indexed token0,
address indexed token1,
uint24 indexed fee,
address pool
);

function getPool(
address tokenA,
address tokenB,
uint24 fee
) external view returns (address pool);

function createPool(
address tokenA,
address tokenB,
uint24 fee
) external returns (address pool);
}
110 changes: 110 additions & 0 deletions P102_InitContracts/code/interfaces/IPool.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.24;

interface IMintCallback {
function mintCallback(
uint256 amount0Owed,
uint256 amount1Owed,
bytes calldata data
) external;
}

interface ISwapCallback {
function swapCallback(
int256 amount0Delta,
int256 amount1Delta,
bytes calldata data
) external;
}

interface IPool {
function factory() external view returns (address);

function token0() external view returns (address);

function token1() external view returns (address);

function fee() external view returns (uint24);

function tickLower() external view returns (int24);

function tickUpper() external view returns (int24);

function sqrtPriceX96() external view returns (uint160);

function tick() external view returns (int24);

function liquidity() external view returns (uint128);

function positions(
int8 positionType
)
external
view
returns (uint128 _liquidity, uint128 tokensOwed0, uint128 tokensOwed1);

function initialize(
uint160 sqrtPriceX96,
int24 tickLower,
int24 tickUpper
) external;

event Mint(
address sender,
address indexed owner,
int8 indexed positionType,
uint128 amount,
uint256 amount0,
uint256 amount1
);

function mint(
address recipient,
int8 positionType,
uint128 amount,
bytes calldata data
) external returns (uint256 amount0, uint256 amount1);

event Collect(
address indexed owner,
address recipient,
int8 indexed positionType,
uint128 amount0,
uint128 amount1
);

function collect(
address recipient,
int8 positionType
) external returns (uint128 amount0, uint128 amount1);

event Burn(
address indexed owner,
int8 indexed positionType,
uint128 amount,
uint256 amount0,
uint256 amount1
);

function burn(
int8 positionType
) external returns (uint256 amount0, uint256 amount1);

event Swap(
address indexed sender,
address indexed recipient,
int256 amount0,
int256 amount1,
uint160 sqrtPriceX96,
uint128 liquidity,
int24 tick
);

function swap(
address recipient,
bool zeroForOne,
int256 amountSpecified,
uint160 sqrtPriceLimitX96,
bytes calldata data
) external returns (int256 amount0, int256 amount1);
}
51 changes: 51 additions & 0 deletions P102_InitContracts/code/interfaces/IPoolManager.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.24;
pragma abicoder v2;

interface IPoolManager {
struct PoolKey {
address token0;
address token1;
uint24 fee;
}

function getPools() external view returns (PoolKey[] memory pools);

function getTokens() external view returns (address[] memory tokens);

function getTokenPools(
address token
) external view returns (PoolKey[] memory pools);

struct PoolInfo {
// the current protocol fee as a percentage of the swap fee taken on withdrawal
// represented as an integer denominator (1/x)%
uint8 feeProtocol;
// tick range
int24 tickLower;
int24 tickUpper;
// the current tick
int24 tick;
// the current price
uint160 sqrtPriceX96;
}

function getPoolInfo(
address token0,
address token1,
uint24 fee
) external view returns (PoolInfo memory poolInfo);

struct CreateAndInitializeParams {
address token0;
address token1;
uint24 fee;
int24 tickLower;
int24 tickUpper;
uint160 sqrtPriceX96;
}

function createAndInitializePoolIfNecessary(
CreateAndInitializeParams calldata params
) external payable returns (address pool);
}
58 changes: 58 additions & 0 deletions P102_InitContracts/code/interfaces/IPositionManager.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.24;
pragma abicoder v2;

interface IPositionManager {
function getPositions(
address owner
) external view returns (uint256[] memory positionIds);

struct PositionInfo {
// address owner;
address token0;
address token1;
uint24 fee;
int128 liquidity;
// tick range
int24 tickLower;
int24 tickUpper;
uint256 tokensOwed0;
uint256 tokensOwed1;
}

function getPositionInfo(
uint256 positionId
) external view returns (PositionInfo memory positionInfo);

struct MintParams {
address token0;
address token1;
uint24 fee;
int8 positionType; // lower:-1; medium:0; upper:1
uint256 amount0Desired;
uint256 amount1Desired;
address recipient;
uint256 deadline;
}

function mint(
MintParams calldata params
)
external
payable
returns (
uint256 positionId,
uint128 liquidity,
uint256 amount0,
uint256 amount1
);

function burn(
uint256 positionId
) external returns (uint256 amount0, uint256 amount1);

function collect(
uint256 positionId,
address recipient
) external returns (uint256 amount0, uint256 amount1);
}
55 changes: 55 additions & 0 deletions P102_InitContracts/code/interfaces/ISwapRouter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.24;
pragma abicoder v2;

interface ISwapRouter {
struct ExactInputParams {
address tokenIn;
address tokenOut;
address recipient;
uint256 deadline;
uint256 amountIn;
uint256 amountOutMinimum;
uint160 sqrtPriceLimitX96;
}

function exactInput(
ExactInputParams calldata params
) external payable returns (uint256 amountOut);

struct ExactOutputParams {
address tokenIn;
address tokenOut;
address recipient;
uint256 deadline;
uint256 amountOut;
uint256 amountInMaximum;
uint160 sqrtPriceLimitX96;
}

function exactOutput(
ExactOutputParams calldata params
) external payable returns (uint256 amountIn);

struct QuoteExactInputParams {
address tokenIn;
address tokenOut;
uint256 amountIn;
uint160 sqrtPriceLimitX96;
}

function quoteExactInput(
QuoteExactInputParams memory params
) external returns (uint256 amountOut);

struct QuoteExactOutputParams {
address tokenIn;
address tokenOut;
uint256 amount;
uint160 sqrtPriceLimitX96;
}

function quoteExactOutput(
QuoteExactOutputParams memory params
) external returns (uint256 amountIn);
}
Binary file added P102_InitContracts/img/deploy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit e431e1f

Please sign in to comment.