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

v4 guides: mint position #743

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Changes from 1 commit
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
Next Next commit
mint position guide
saucepoint committed Sep 10, 2024

Verified

This commit was signed with the committer’s verified signature.
commit c876be0d27bdda706a6738e212b8abe8bdee5acd
Original file line number Diff line number Diff line change
@@ -1,3 +1,94 @@
---
title: Mint Position
---
---

Similar to Uniswap v3, liquidity positions are minted as ERC-721 tokens and depend on a *periphery* contract.
v4's `PositionManager` contract will facilitate liquidity management

### Context

Please note that `PositionManager` is a command-based contract, where integrators will be encoding commands and their corresponding
parameters.

### Setup

Install [foundry](https://book.getfoundry.sh)

Add v4-periphery as a dependency to your project

```bash
forge install uniswap/v4-periphery
```

# Guide

Below is a step-by-step guide for minting a v4 liquidity position, in *solidity*

### 1. Import and define `IPositionManager`

```solidity
import {IPositionManager} from "v4-periphery/src/interfaces/IPositionManager.sol";

// inside a contract, test, or foundry script:
IPositionManager posm = IPositionManager(<address>);
```

### 2. Encode Actions

To mint a position, two actions are required:

* mint operation - the creation of the liquidity position
* settle pair - the two tokens to be paid by msg.sender

```solidity
import {Actions} from "v4-periphery/src/libraries/Actions.sol";

bytes memory actions = abi.encodePacked(Actions.MINT_POSITION, Actions.SETTLE_PAIR);
```

### 3. Encode Parameters

```solidity
bytes[] memory params = new bytes[](2);
```

The `MINT_POSITION` action requires the following parameters:

* `poolKey` - the `PoolKey` where the liquidity will be added to
* `tickLower` - _int24_, the lower tick boundary of the position
* `tickUpper` - _int24_, the upper tick boundary of the position
* `liquidity`- _uint256_, the amount of liquidity units to mint
* `amount0Max` - _uint256_, the maximum amount of currency0 msg.sender is willing to pay
* `amount1Max` - _uint256_, the maximum amount of currency1 msg.sender is willing to pay
* `recipient` - _address_, the address that will receive the liquidity position (ERC-721)
* `hookData` - _bytes_, arbitrary data that will be forwarded to hook functions

```solidity
params[0] = abi.encode(poolKey, tickLower, tickUpper, liquidity, amount0Max, amount1Max, recipient, hookData);
```

The `SETTLE_PAIR` action requires the following parameters:

* `currency0` - _Currency_, one of the tokens to be paid by msg.sender
* `currency1` - _Currency_, the other token to be paid by msg.sender

```solidity
params[1] = abi.encode(currency0, currency1);
```

### 4. Submit Call

The entrypoint for all liquidity operations is `modifyLiquidities()`

```solidity
uint256 deadline = block.timestamp + 60;

posm.modifyLiquidities(
abi.encode(actions, params),
deadline
);
```

## Additional notes:

* To obtain balance changes, callers should read token balances before and after the `.modifyLiquidities()` call