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 NameDescription
uint256 tokenSNThe serial number of the token for which liquidity is being increased
address recipientEVM address to receive the claimed swap fees
uint256 amount0MaxThe maximum amount for the first token in its smallest unit
uint256 amount1MaxThe maximum amount for the second token in its smallest unit
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 token1
function collect(CollectParams calldata params) external payable 
  returns (uint256 amount0, uint256 amount1);

Code Overview

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.

Resources:

Typescript
import * as ethers from 'ethers'; //V6
import { 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 provider
const provider = new ethers.JsonRpcProvider(hederaJsonRelayUrl, '', {
  batchMaxCount: 1, //workaround for V6
});

//Load the ABI data for NonfungiblePositionManager
const nftManagerInterfaces = new ethers.Interface(nftManagerAbi);

//get max possible value for amount0Max and amount1Max
const MAX_UINT128 = new BigNumber(2).pow(128).minus(1).toFixed(0);

//CollectParams struct
const params = {
  tokenSN: tokenSN,
  recipient: recipientAddress,    
  amount0Max: MAX_UINT128, //collect max fees
  amount1Max: MAX_UINT128, //collect max fees
};

//Construct encoded data for each function
const collectEncoded = nftManagerInterfaces.encodeFunctionData('collect', [params]);  

//Not needed if HBAR isn't include in the pool
const unwrapWHBAREncoded = nftManagerInterfaces.encodeFunctionData('unwrapWHBAR', [0, recipientAddress]);

//Build encoded data for the multicall
const encodedData = nftManagerInterfaces.encodeFunctionData('multicall', 
  [[collectEncoded, unwrapWHBAREncoded]]);  
const encodedDataAsUint8Array = hexToUint8Array(encodedData.substring(2));

//Execute the paid contract call
const response = await new ContractExecuteTransaction()
  .setContractId(nftManagerContractId)
  .setGas(gasGwei)
  .setFunctionParameters(encodedDataAsUint8Array)
  .execute(client);

//Fetch the result
const 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 purposes
const amount0 = BigNumber(collectResult.amount0);
const amount1 = BigNumber(collectResult.amount1);