Fetching Positions
This section demonstrates how to fetch liquidity positions on the Storyhunt V3 protocol.
Introduction
This section demonstrates how to fetch liquidity positions on the Storyhunt V3 protocol. Using the NonfungiblePositionManager contract, you can retrieve all positions associated with an address and fetch detailed data for each position.
Connecting to the NonfungiblePositionManager Contract
NonfungiblePositionManager ContractTo interact with the NonfungiblePositionManager contract, create an ethers.js contract instance:
import { ethers } from 'ethers';
import INONFUNGIBLE_POSITION_MANAGER from '@storyhunt/v3-periphery/artifacts/contracts/NonfungiblePositionManager.sol/NonfungiblePositionManager.json';
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
const nfpmContract = new ethers.Contract(
NONFUNGIBLE_POSITION_MANAGER_CONTRACT_ADDRESS,
INONFUNGIBLE_POSITION_MANAGER.abi,
provider
);Fetching Position IDs
Retrieve all position IDs for a specific address. Start by fetching the number of positions:
const numPositions = await nfpmContract.balanceOf(address);Iterate through the positions and fetch the IDs:
Fetching Position Info
Once you have the position IDs, fetch detailed information for each position using the positions function:
For this example, focus on values needed to interact with positions. Define an interface:
Fetch the position data:
Map the RPC response to the PositionInfo interface:
You now have an array of PositionInfo objects containing detailed data for all positions associated with the specified address.