See Get user positions for details how to retrieve all positions of a user including fees earned if any.
Function name: collect⛽ Recommended gas: 300,000 gwei (~ $0.026 USD)
Struct Parameter Name
Description
uint256 tokenSN
The serial number of the token for which liquidity is being increased
address recipient
EVM address to receive the claimed swap fees
uint256 amount0Max
The maximum amount for the first token in its smallest unit
uint256 amount1Max
The maximum amount for the second token in its smallest unit
Copy
Ask AI
struct CollectParams { uint256 tokenSN; address recipient; uint128 amount0Max; uint128 amount1Max;}/// @notice Collects up to a maximum amount of fees owed to a specific position to the recipient/// @param params tokenSN The serial number of the NFT for which tokens are being collected,/// recipient The account that should receive the tokens,/// amount0Max The maximum amount of token0 to collect,/// amount1Max The maximum amount of token1 to collect/// @return amount0 The amount of fees collected in token0/// @return amount1 The amount of fees collected in token1function collect(CollectParams calldata params) external payable returns (uint256 amount0, uint256 amount1);
The following code demonstrates how to claim all swap fees from a pool.
When claiming fees from a pool that involves HBAR, include unwrapWHBAR in your multicall to convert the Wrapped HBAR (WHBAR) output token back into the native HBAR cryptocurrency.
import * as ethers from 'ethers'; //V6import { Pool, Position, nearestUsableTick, priceToClosestTick } from '@uniswap/v3-sdk';import { Fraction, Percent, Token, Price } from '@uniswap/sdk-core';import { ContractExecuteTransaction, .. } from '@hashgraph/sdk';//Set one of Hedera's JSON RPC Relay as the providerconst provider = new ethers.JsonRpcProvider(hederaJsonRelayUrl, '', { batchMaxCount: 1, //workaround for V6});//Load the ABI data for NonfungiblePositionManagerconst nftManagerInterfaces = new ethers.Interface(nftManagerAbi);//get max possible value for amount0Max and amount1Maxconst MAX_UINT128 = new BigNumber(2).pow(128).minus(1).toFixed(0);//CollectParams structconst params = { tokenSN: tokenSN, recipient: recipientAddress, amount0Max: MAX_UINT128, //collect max fees amount1Max: MAX_UINT128, //collect max fees};//Construct encoded data for each functionconst collectEncoded = nftManagerInterfaces.encodeFunctionData('collect', [params]); //Not needed if HBAR isn't include in the poolconst unwrapWHBAREncoded = nftManagerInterfaces.encodeFunctionData('unwrapWHBAR', [0, recipientAddress]);//Build encoded data for the multicallconst encodedData = nftManagerInterfaces.encodeFunctionData('multicall', [[collectEncoded, unwrapWHBAREncoded]]); const encodedDataAsUint8Array = hexToUint8Array(encodedData.substring(2));//Execute the paid contract callconst response = await new ContractExecuteTransaction() .setContractId(nftManagerContractId) .setGas(gasGwei) .setFunctionParameters(encodedDataAsUint8Array) .execute(client);//Fetch the resultconst record = await response.getRecord(client); const result = record.contractFunctionResult!;const results = nftManagerInterfaces.decodeFunctionResult('multicall', result.bytes)[0];const collectResult = nftManagerInterfaces.decodeFunctionResult('collect', results[0]);//Retrieve the collected amounts for informative purposesconst amount0 = BigNumber(collectResult.amount0);const amount1 = BigNumber(collectResult.amount1);