Collecting Fees
This section explains how to collect fees from a liquidity position on the Storyhunt V3 protocol.
Introduction
This section explains how to collect fees from a liquidity position on the Storyhunt V3 protocol. Liquidity positions accrue fees from trades made within the positionโs price range. Using the NonfungiblePositionManager contract, we can collect these fees. The inputs include the tokens pooled, pool fee, and maximum accrued fees to collect for each token.
Setting Up Fee Collection
To begin, fetch the position details from the NonfungiblePositionManager contract to determine the fees owed:
import { ethers } from 'ethers';
import JSBI from 'jsbi';
const nfpmContract = new ethers.Contract(
NONFUNGIBLE_POSITION_MANAGER_ADDRESS,
INONFUNGIBLE_POSITION_MANAGER.abi,
provider
);
const position = await nfpmContract.positions(positionId);Next, construct a CollectOptions object to specify the fee collection details:
import { CurrencyAmount } from '@storyhunt/sdk-core';
const collectOptions: CollectOptions = {
tokenId: positionId,
expectedCurrencyOwed0: CurrencyAmount.fromRawAmount(
CurrentConfig.tokens.token0,
JSBI.BigInt(position.tokensOwed0)
),
expectedCurrencyOwed1: CurrencyAmount.fromRawAmount(
CurrentConfig.tokens.token1,
JSBI.BigInt(position.tokensOwed1)
),
recipient: address,
};Here:
tokenId: The ID of the position.expectedCurrencyOwed0andexpectedCurrencyOwed1: The maximum amounts of each token expected to collect.recipient: The wallet address to receive the collected fees.
You can fetch the tokensOwed0 and tokensOwed1 values using the positions function of the NonfungiblePositionManager contract:
Submitting the Fee Collection Transaction
Use the NonfungiblePositionManager to retrieve the call parameters required for the fee collection transaction:
Construct and execute the transaction:
After the transaction is confirmed, the balances of the pooled tokens (e.g., USDC and WIP) will increase if fees have been accrued.