Implementing into estimateFees()
Define the createAdapterParams
function
This function prepares parameters for each chain to estimate fees. It takes three arguments: gasLimit
, nativeAmount
, and to
address. To address can remain defaulted to a null
address
viem
import { encodePacked } from 'viem'
const createAdapterParams = (gasLimit: bigint, nativeAmount: bigint, to: string) => {
return encodePacked(['uint16', 'uint256', 'uint256', 'address'], [2, gasLimit, nativeAmount, to as `0x${string}`])
}
Prepare LayerZero chainIDs
and adapterParamsEstimate
Create an array of LayerZero chain IDs lzIds
and an array of adapter parameters adapterParamsEstimate
for each chain you want to estimate fees for.
viem
// Prepare parameters for Arbitrum and Optimism
const arbitrumParamsEstimate = createAdapterParams(
BigInt(30_000),
parseEther('0.000002'),
'0x0000000000000000000000000000000000000000',
)
const arbitrumNovaParamsEstimate = createAdapterParams(
BigInt(30_000),
parseEther('0.000002'),
'0x0000000000000000000000000000000000000000',
)
// Prepare the final object to send to the estimateFees() function
const adapterParamsEstimate = [arbitrumParamsEstimate, arbitrumNovaParamsEstimate]
const lzIds = [110, 175] // Arbitrum and Arbitrum Nova LayerZero chain IDs
Call the estimateFees()
function and aggregate the fees
Use the lzIds
and the aggregated adapterParamsEstimate
array as arguments for the estimateFees()
function. Each fee result from the contract read is then aggregated into one BigInt which is used as a prop in the deposit()
function.
viem
import { readContract } from 'viem'
import { estimateFeesAbi } from './abis'
let lzFees: bigint
async function estimateFees(lzIds: number[], adapterParamsEstimate: `0x${string}`[]): Promise<bigint> {
const fees = (await client.readContract({
address: '0xbf94ed69281709958c8f60bc15cd1bb6badcd4a4',
abi: estimateFeesAbi,
functionName: 'estimateFees',
args: [lzIds, adapterParamsEstimate],
})) as bigint[]
// Aggregate the fees together to use in the `deposit()` function
const lzFees = fees.reduce((p, c) => p + c, BigInt(0))
return lzFees
}
estimateFees(lzIds, adapterParams)
.then((fees) => {
lzFees = fees
console.log(`The fees are: ${lzFees}`)
})
.catch((error) => console.error(error))