Skip to content

Example for deposit()

Example code for calling the deposit() function.

viem
import { encodePacked, parseEther, http, createWalletClient, publicActions, formatEther } from 'viem'
import { optimism } from 'viem/chains'
import { privateKeyToAccount } from 'viem/accounts'
import { estimateFeesAbi, lzDepositAbi } from './abis'
const account = privateKeyToAccount('0x...')
 
const client = createWalletClient({
  account,
  chain: optimism,
  transport: http(),
}).extend(publicActions)
 
const lzIds = [110, 175] // Arbitrum and Arbitrum Nova LayerZero chain IDs
// Define the createOptimizedAdapterParams function
const createOptimizedAdapterParams = (dstChainId: bigint, nativeAmount: bigint) => {
  return (dstChainId << BigInt(240)) | nativeAmount
}
 
// Prepare parameters for Arbitrum and Optimism
const arbitrumParamsDeposit = createOptimizedAdapterParams(BigInt(110), parseEther('0.000002'))
const arbitrumNovaParamsDeposit = createOptimizedAdapterParams(BigInt(175), parseEther('0.000002'))
 
// Prepare the final object to send to the deposit() function
const adapterParamsDeposit = [arbitrumParamsDeposit, arbitrumNovaParamsDeposit]
;(async () => {
  const lzFees = await estimateFees(lzIds, adapterParamsEstimate)
  console.log(`The fees are: ${lzFees}`)
  console.log(`Parsed Fees`, formatEther(lzFees))
 
  // Prepare the contract write configuration
  const { request } = await client.simulateContract({
    address: '0xbf94ed69281709958c8f60bc15cd1bb6badcd4a4',
    abi: lzDepositAbi,
    functionName: 'deposit',
    value: lzFees,
    args: [adapterParamsDeposit, account.address],
  })
 
  // Call the deposit() function
  await client.writeContract(request)
})().catch((error) => console.error(error))