Adding & Removing Liquidity

This section explains how to modify a liquidity position by adding or removing liquidity on the Storyhunt V3 protocol.

Introduction

This section explains how to modify a liquidity position by adding or removing liquidity on the Storyhunt V3 protocol. Liquidity positions are represented as non-fungible tokens. This guide demonstrates adding and removing liquidity for the USDC - DAI pair using the NonfungiblePositionManager class. The inputs include the tokens, pool fee, and fractions to adjust the position's liquidity.

Adding Liquidity to a Position

To add liquidity, construct a modified position by calculating the amount to increase based on the current position:

const fractionToAdd: number = ...;

const amount0Increased: JSBI = fromReadableAmount(
    readableAmount0 * fractionToAdd,
    token0.decimals
);
const amount1Increased: JSBI = fromReadableAmount(
    readableAmount1 * fractionToAdd,
    token1.decimals
);

const positionToIncreaseBy = constructPosition(
    amount0Increased,
    amount1Increased
);

Here, fromReadableAmount() converts the token amount to its smallest unit (e.g., 1 WIP = 1e18 Wei).

Create the constructPosition function to build a Position object:

Next, create an options object of type AddLiquidityOptions:

Use the NonfungiblePositionManager to get the calldata and value for the transaction:

Construct and execute the transaction:

Removing Liquidity from a Position

To remove liquidity, start by constructing a position identical to the minted one:

Create a RemoveLiquidityOptions object:

The collectOptions parameter allows optional fee collection:

Use removeCallParameters to get the calldata and value for the transaction:

Construct and execute the transaction:

Next Steps

Now that you can mint and modify a position, check out how to collect fees from the position!