Skip to content

Commit

Permalink
Added native eth guide support (#824)
Browse files Browse the repository at this point in the history
  • Loading branch information
seeyijie authored Dec 5, 2024
1 parent 29caef0 commit 78f7656
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 29 deletions.
27 changes: 20 additions & 7 deletions docs/contracts/v4/guides/09-swap-routing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,32 @@ First, let’s define our function signature:

```solidity
function swapExactInputSingle(
PoolKey calldata key,
uint128 amountIn,
uint128 minAmountOut
PoolKey calldata key, // PoolKey struct that identifies the v4 pool
uint128 amountIn, // Exact amount of tokens to swap
uint128 minAmountOut // Minimum amount of output tokens expected
) external returns (uint256 amountOut) {
// Implementation will follow
}
```
**Important note:**

The function takes:
When swapping tokens involving native ETH, we use `Currency.wrap(address(0))` to represent ETH in the `PoolKey` struct.

- `key`: The PoolKey struct that identifies the v4 pool
- `amountIn`: The exact amount of tokens to swap
- `minAmountOut`: The minimum amount of output tokens expected
```solidity
struct PoolKey {
/// @notice The lower currency of the pool, sorted numerically.
/// For native ETH, Currency currency0 = Currency.wrap(address(0));
Currency currency0;
/// @notice The higher currency of the pool, sorted numerically
Currency currency1;
/// @notice The pool LP fee, capped at 1_000_000. If the highest bit is 1, the pool has a dynamic fee and must be exactly equal to 0x800000
uint24 fee;
/// @notice Ticks that involve positions must be a multiple of tick spacing
int24 tickSpacing;
/// @notice The hooks of the pool
IHooks hooks;
}
```

### 3.2: Encoding the Swap Command

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,5 @@ After cloning the repository, and installing foundry, developers can manually se
forge-std/=lib/v4-core/lib/forge-std/src/
permit2/=lib/v4-periphery/lib/permit2/
solmate/=lib/v4-core/lib/solmate/
v4-core/=lib/v4-core/
v4-periphery/=lib/v4-periphery/
```
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ The `MINT_POSITION` action requires the following parameters:
| `hookData` | _bytes_ | arbitrary data that will be forwarded to hook functions |

```solidity
Currency currency0 = Currency.wrap(<tokenAddress1>); // tokenAddress1 = 0 for native ETH
Currency currency1 = Currency.wrap(<tokenAddress2>);
PoolKey poolKey = PoolKey(currency0, currency1, 3000, 60, IHooks(hook));
params[0] = abi.encode(poolKey, tickLower, tickUpper, liquidity, amount0Max, amount1Max, recipient, hookData);
```

Expand All @@ -79,7 +83,9 @@ The entrypoint for all liquidity operations is `modifyLiquidities()`
```solidity
uint256 deadline = block.timestamp + 60;
posm.modifyLiquidities(
uint256 valueToPass = currency0.isAddressZero() ? amount0Max : 0;
posm.modifyLiquidities{value: valueToPass}(
abi.encode(actions, params),
deadline
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ The `SETTLE_PAIR` action requires the following parameters:
In the above case, the parameter encoding is:

```solidity
Currency currency0 = Currency.wrap(<tokenAddress1>); // tokenAddress1 = 0 for native ETH
Currency currency1 = Currency.wrap(<tokenAddress2>);
params[1] = abi.encode(currency0, currency1);
```

Expand Down Expand Up @@ -131,7 +133,10 @@ The entrypoint for all liquidity operations is `modifyLiquidities()`.

```solidity
uint256 deadline = block.timestamp + 60;
posm.modifyLiquidities(
uint256 valueToPass = currency0.isAddressZero() ? amount0Max : 0;
posm.modifyLiquidities{value: valueToPass}(
abi.encode(actions, params),
deadline
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ The `TAKE_PAIR` action requires the following parameters:
In the above case, the parameter encoding is:

```solidity
Currency currency0 = Currency.wrap(<tokenAddress1>); // tokenAddress1 = 0 for native ETH
Currency currency1 = Currency.wrap(<tokenAddress2>);
params[1] = abi.encode(currency0, currency1, recipient);
```

Expand All @@ -116,7 +118,10 @@ The entrypoint for all liquidity operations is `modifyLiquidities()`.

```solidity
uint256 deadline = block.timestamp + 60;
posm.modifyLiquidities(
uint256 valueToPass = currency0.isAddressZero() ? amount0Max : 0;
posm.modifyLiquidities{value: valueToPass}(
abi.encode(actions, params),
deadline
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ The `TAKE_PAIR` action requires the following parameters:
* `recipient` - _address_, destination of the fee revenue for both tokens

```solidity
Currency currency0 = Currency.wrap(<tokenAddress1>); // tokenAddress1 = 0 for native ETH
Currency currency1 = Currency.wrap(<tokenAddress2>);
params[1] = abi.encode(currency0, currency1, recipient);
```

Expand All @@ -73,7 +75,10 @@ The entrypoint for all liquidity operations is `modifyLiquidities()`.

```solidity
uint256 deadline = block.timestamp + 60;
posm.modifyLiquidities(
uint256 valueToPass = currency0.isAddressZero() ? amount0Max : 0;
posm.modifyLiquidities{value: valueToPass}(
abi.encode(actions, params),
deadline
);
Expand Down
37 changes: 20 additions & 17 deletions docs/contracts/v4/quickstart/03-swap.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,6 @@ In this step, we’re importing the necessary contracts and interfaces:
- `IPermit2`: This interface allows us to interact with the Permit2 contract, which provides enhanced token approval functionality.
- `StateLibrary`: This provides optimized functions for interacting with the PoolManager’s state. By using `StateLibrary`, we can more efficiently read and manipulate pool states, which is crucial for many operations in Uniswap v4.

**Important note:**

To use the Universal Router imports, you'll need to set up remappings in your project. This can be done using a `remappings.txt` file:

```
@uniswap/universal-router/=lib/universal-router/
[...]
```

## Step 2: Implement Token Approval with Permit2

`UniversalRouter` integrates with [Permit2](https://github.com/Uniswap/permit2), to enable users to have more safety, flexibility, and control over their ERC20 token approvals.
Expand Down Expand Up @@ -107,19 +97,32 @@ First, let’s define our function signature:

```solidity
function swapExactInputSingle(
PoolKey calldata key,
uint128 amountIn,
uint128 minAmountOut
PoolKey calldata key, // PoolKey struct that identifies the v4 pool
uint128 amountIn, // Exact amount of tokens to swap
uint128 minAmountOut // Minimum amount of output tokens expected
) external returns (uint256 amountOut) {
// Implementation will follow
}
```
**Important note:**

The function takes:
When swapping tokens involving native ETH, we use `Currency.wrap(address(0))` to represent ETH in the `PoolKey` struct.

- `key`: The [PoolKey](https://github.com/Uniswap/v4-core/blob/main/src/types/PoolKey.sol) struct that identifies the v4 pool
- `amountIn`: The exact amount of tokens to swap
- `minAmountOut`: The minimum amount of output tokens expected
```solidity
struct PoolKey {
/// @notice The lower currency of the pool, sorted numerically.
/// For native ETH, Currency currency0 = Currency.wrap(address(0));
Currency currency0;
/// @notice The higher currency of the pool, sorted numerically
Currency currency1;
/// @notice The pool LP fee, capped at 1_000_000. If the highest bit is 1, the pool has a dynamic fee and must be exactly equal to 0x800000
uint24 fee;
/// @notice Ticks that involve positions must be a multiple of tick spacing
int24 tickSpacing;
/// @notice The hooks of the pool
IHooks hooks;
}
```

### 3.2: Encoding the Swap Command

Expand Down

0 comments on commit 78f7656

Please sign in to comment.