import { BigNumber } from 'ethers'; import config from './config/config'; import { getWalletConfigByChainId } from './utils/chainUtils'; import { LayerZeroUltraLightNode } from './contracts/LayerZeroUltraLightNode'; import { OmniteLayerZeroBridgeReceiver } from './contracts/OmniteLayerZeroBridgeReceiver'; export class LayerZero { /** * Returns estimated layer zero fee for given payload * * @param {string} sourceChainId * @param {string} targetChainId * @param {string} contractAddress * @param {string} payload * @param {BigNumber} gas? * @returns Promise */ static estimateFees = async ( sourceChainId: string, targetChainId: string, contractAddress: string, payload: string, gas?: BigNumber ): Promise => { const targetChainConfig = getWalletConfigByChainId(targetChainId); if (!targetChainConfig) { throw new Error('No bridge address'); } const result = await LayerZeroUltraLightNode.estimateFees( sourceChainId, targetChainConfig.layerZeroChainId, contractAddress, payload, gas || config.defaults.deploymentGas ); return BigNumber.from(result.nativeFee.toString()).mul(200).div(100); }; /** * Returns layer zero estimated gas for given payload * * @param {string} sourceChainId * @param {string} signerAddress * @param {string} targetChainId * @param {string} payload */ static estimateReceiveGas = async ( sourceChainId: string, signerAddress: string, targetChainId: string, payload: string ) => { const sourceChainConfig = getWalletConfigByChainId(sourceChainId); if (!sourceChainConfig) { throw new Error('Invalid source chain config'); } // TODO fix estimation const estimatedGasLimitResponse = await OmniteLayerZeroBridgeReceiver.estimateReceiveGas( targetChainId, sourceChainConfig.layerZeroChainId, signerAddress, payload ); let estimatedGasLimit = BigNumber.from(estimatedGasLimitResponse); if (estimatedGasLimit.lte(100000)) { estimatedGasLimit = BigNumber.from(100000); } return estimatedGasLimit.mul(300).div(100); }; }