Minting a Position

This section explains how to create (or mint) a liquidity position on the Storyhunt V3 protocol.

Introduction

This section explains how to create (or mint) a liquidity position on the Storyhunt V3 protocol. In Storyhunt V3, liquidity positions are represented using non-fungible tokens. This guide demonstrates minting a liquidity position for the USDC - DAI pair, including transferring token approvals, creating a pool instance, calculating the position, and executing the minting transaction.

Giving Approval to Transfer Tokens

The first step is to approve the protocolโ€™s NonfungiblePositionManager contract to transfer your tokens. Since both USDC and DAI are ERC20 tokens, their contracts need approval to transfer funds on your behalf:

import { ethers, BigNumber } from 'ethers';

async function getTokenTransferApproval(address: string, amount: BigNumber) {
    const provider = new ethers.providers.JsonRpcProvider(rpcUrl);

    const tokenContract = new ethers.Contract(
        address,
        ERC20_ABI,
        provider
    );

    return tokenContract.approve(
        NONFUNGIBLE_POSITION_MANAGER_CONTRACT_ADDRESS,
        amount
    );
}

const token0Approval = await getTokenTransferApproval(token0Address, amount0);
const token1Approval = await getTokenTransferApproval(token1Address, amount1);

Creating a Pool Instance

After approving token transfers, gather the required pool data to instantiate the Pool class. Compute the pool address using the unique identifiers: the two tokens and the pool fee.

Retrieve pool data by creating a reference to the poolโ€™s smart contract:

Create the pool instance:

Calculating a Position from Input Tokens

Using the pool instance, create a Position class to define the price range for providing liquidity:

Configuring and Executing the Minting Transaction

Pass the Position instance to the NonfungiblePositionManager class to generate the calldata for minting a new position. Use the MintOptions object to configure transaction parameters:

Create and send the transaction:

The transaction mints a new position NFT, representing your liquidity in the specified pool.

Next Steps

After minting a position, you can manage it by adding or removing liquidity as needed. Explore advanced features to optimize your liquidity management in the Storyhunt V3 protocol.