See Liquidity position fee for an example of how to obtain the current fee, payable in HBAR, for minting a new liquidity position or adding more liquidity to an existing one.
When working with HBAR, use the Wrapped HBAR token ID and include the HBAR amount in the setPayableAmount() method for the ContractExecuteTransaction call.
Function name: increaseLiquidity
⛽ Recommended gas: 330,000 gwei (~ $0.028 USD)
Struct Parameter Name
Description
uint256 tokenSN
The serial number of the token for which liquidity is being increased
uint256 amount0Desired
The maximum amount for the first token in its smallest unit
uint256 amount1Desired
The maximum amount for the second token in its smallest unit
uint256 amount0Min
The minimum amount for the first token in its smallest unit
uint256 amount1Min
The minimum amount for the second token in its smallest unit
uint deadline
Deadline in Unix seconds
Solidity Interface & Function Body
//INonfungiblePositionManager.solstructIncreaseLiquidityParams {uint256 tokenSN;uint256 amount0Desired;uint256 amount1Desired;uint256 amount0Min;uint256 amount1Min;uint256 deadline;}/// @notice Increases the amount of liquidity in a position, with tokens paid by the `msg.sender`/// @param params tokenSN The serial number of the token for which liquidity is being increased,/// amount0Desired The desired amount of token0 to be spent,/// amount1Desired The desired amount of token1 to be spent,/// amount0Min The minimum amount of token0 to spend, which serves as a slippage check,/// amount1Min The minimum amount of token1 to spend, which serves as a slippage check,/// deadline The time by which the transaction must be included to effect the change/// @return liquidity The new liquidity amount as a result of the increase/// @return amount0 The amount of token0 to acheive resulting liquidity/// @return amount1 The amount of token1 to acheive resulting liquidityfunctionincreaseLiquidity(IncreaseLiquidityParamscalldata params)externalpayablereturns (uint128 liquidity,uint256 amount0,uint256 amount1);
The following code demonstrates how to add more liquidity to an existing position.
See Fetch pool token ratio for an example how to retrieve the latest data construct the Pool object using the Uniswap SDK library.
See Fetch all pools to retrieve the pool address, token pairs, fee tier, token IDs and decimal places for the target pool of interest.
The refundETH function uses HBAR, but its name is derived from Uniswap on Ethereum. The name was retained to simplify integration for developers familiar with Uniswap tools. It is used to refund any excess HBAR when setting up a new liquidity position.
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';//Client pre-checks:// - Router contract has spender allowance for the input HTS tokens//Set one of Hedera's JSON RPC Relay as the providerconstprovider=newethers.JsonRpcProvider(hederaJsonRelayUrl,'', { batchMaxCount:1,//workaround for V6});//Load the ABI data for UniswapV3PoolconstpoolInterfaces=newethers.Interface(poolAbi);//Load the ABI data for NonfungiblePositionManagerconstnftManagerInterfaces=newethers.Interface(nftManagerAbi);//Construct the pool contractconstpoolContract=newethers.Contract(poolEvmAddress,poolInterfaces.fragments, provider);//Construct the NFT Manager contractconstnftManagerContract=newethers.Contract(nftManagerEvmAddress,nftManagerInterfaces.fragments, provider);//Get current slot0 and liquidity data from the poolconst [slot0,poolLiquidity] =awaitPromise.all([poolContract.slot0(),poolContract.liquidity()]);//Construct the pool using the latest dataconstpool=newPool( token0, token1, feeTier,slot0.sqrtPriceX96.toString(),poolLiquidity.toString(),Number(slot0.tick));//Get current position data for the given NFT token serial numberconstlp=awaitnftManagerContract.positions(tokenSN);constfeeTier=Number(lp.fee);consttickLower=Number(lp.tickLower);consttickUpper=Number(lp.tickUpper);//Construct the tokens//For Hedera chain id, see https://chainlist.org/?testnets=true&search=Hederaconsttoken0=newToken(hederaChainId, token0Address, token0Decimals);consttoken1=newToken(hederaChainId, token1Address, token1Decimals);//Get amount0 in token's smallest unit from user's input (input0)constamount0=BigNumber(input0).times(Math.pow(10, token0Decimals)).toFixed(0);//Construct a position using the SDK// - use fromAmount0() if amount0 needs to be exact// - use fromAmount1() if amount1 needs to be exact// - use fromAmounts() if amount0 and amount1 do not need to be exactconstposition=Position.fromAmount0({ pool: pool, tickUpper: tickUpper, tickLower: tickLower, amount0: amount0, useFullPrecision:true});//Get the mint amounts based on what the router will give usconstamount0Mint=position.mintAmounts.amount0.toString();constamount1Mint=position.mintAmounts.amount1.toString();//Calculate the minimum amounts factoring in the price slippage % and rangeconstpriceSlippagePercent=newPercent(1,100); //1% price slippageconstminAmounts=position.mintAmountsWithSlippage(priceSlippagePercent);constamount0Min=minAmounts.amount0.toString();constamount1Min=minAmounts.amount1.toString();//IncreaseLiquidityParams structconstparams= { tokenSN: tokenSN, fee: feeTier,//500, 1500, 3000 or 10000 tickLower: tickLower,//lower tick of the range tickUpper: tickUpper,//upper tick of the range amount0Desired: amount0Mint,//in smallest unit amount1Desired: amount1Mint,//in smallest unit amount0Min: amount0Min,//in smallest unit amount1Min: amount1Min,//in smallest unit deadline: deadline,//Unix seconds};//Construct encoded data for each functionconstincreaseLiquidityEncoded=nftManagerInterfaces.encodeFunctionData('increaseLiquidity', [params]); constrefundEthEncoded=nftManagerInterfaces.encodeFunctionData('refundETH');//Build encoded data for multicallconstencodedData=abiInterfaces.encodeFunctionData('multicall', [[mintEncoded, refundEthEncoded]]); constencodedDataAsUint8Array=hexToUint8Array(encodedData.substring(2));//Give spender allowance for both tokens to the NFT Manager contract if needed.//To avoid having to ask for allowance each time, request max allowance.//If the token is HBAR, no spender allowance is required.//Use Hedera's REST API to get current allowances for an account.awaityourGrantSpenderAllowanceFunc(..);//Execute the contract callconstresponse=awaitnewContractExecuteTransaction().setPayableAmount(inputHbar) //mint fee + HBAR token amount if used.setContractId(nftManagerContractId).setGas(gasGwei).setFunctionParameters(encodedDataAsUint8Array).execute(client);//Fetch the resultconstrecord=awaitresponse.getRecord(client); constresult=record.contractFunctionResult!;constresults=abiInterfaces.decodeFunctionResult('multicall',result.bytes)[0];constmintResult=abiInterfaces.decodeFunctionResult('increaseLiquidity', results[0]);//Retrieve the newly minted liquidity and amounts for informative purposesconstliquidity=BigNumber(mintResult.liquidity);constamount0=BigNumber(mintResult.amount0);constamount1=BigNumber(mintResult.amount1);