diff --git a/src/common/utils/messages.ts b/src/common/utils/messages.ts index f7b993e..6806177 100644 --- a/src/common/utils/messages.ts +++ b/src/common/utils/messages.ts @@ -2,15 +2,18 @@ import { buildEvmMessageToSend, estimateEVMWormholeDataGasLimit, } from "../../chains/evm/common/utils/message.js"; +import { getHubChainAdapterAddress } from "../../chains/evm/hub/utils/chain.js"; import { exhaustiveCheck } from "../../utils/exhaustive-check.js"; import { WORMHOLE_DATA } from "../constants/gmp.js"; import { ChainType } from "../types/chain.js"; import { AdapterType } from "../types/message.js"; import { convertFromGenericAddress } from "./address.js"; -import { getFolksChain } from "./chain.js"; +import { getFolksChain, getSpokeChainAdapterAddress } from "./chain.js"; +import type { HubChain } from "../../chains/evm/hub/types/chain.js"; import type { + FolksChain, FolksChainId, GenericAddress, NetworkType, @@ -45,7 +48,7 @@ export function getWormholeData(folksChainId: FolksChainId): WormholeData { throw new Error(`Wormhole data not found for folksChainId: ${folksChainId}`); } -export async function estimateReceiveGasLimit( +async function estimateAdapterReceiveGasLimit( sourceFolksChainId: FolksChainId, destFolksChainId: FolksChainId, destFolksChainProvider: FolksProvider, @@ -54,8 +57,8 @@ export async function estimateReceiveGasLimit( sourceAdapterAddress: GenericAddress, destAdapterAddress: GenericAddress, messageBuilderParams: MessageBuilderParams, - receiverValue = BigInt(0), - returnGasLimit = BigInt(0), + receiverValue: bigint, + returnGasLimit: bigint, ) { const destFolksChain = getFolksChain(destFolksChainId, network); switch (destFolksChain.chainType) { @@ -103,3 +106,36 @@ export async function estimateReceiveGasLimit( return exhaustiveCheck(destFolksChain.chainType); } } + +export async function estimateReceiveGasLimit( + hubProvider: FolksProvider, + hubChain: HubChain, + folksChain: FolksChain, + adapters: MessageAdapters, + messageBuilderParams: MessageBuilderParams, + receiverValue = BigInt(0), + returnGasLimit = BigInt(0), +) { + const sourceAdapterAddress = getSpokeChainAdapterAddress( + folksChain.folksChainId, + folksChain.network, + adapters.adapterId, + ); + const destAdapterAddress = getHubChainAdapterAddress( + folksChain.network, + adapters.adapterId, + ); + + return await estimateAdapterReceiveGasLimit( + folksChain.folksChainId, + hubChain.folksChainId, + hubProvider, + folksChain.network, + adapters, + sourceAdapterAddress, + destAdapterAddress, + messageBuilderParams, + receiverValue, + returnGasLimit, + ); +} diff --git a/src/xchain/modules/folks-account.ts b/src/xchain/modules/folks-account.ts index 1b4099c..cb561b0 100644 --- a/src/xchain/modules/folks-account.ts +++ b/src/xchain/modules/folks-account.ts @@ -1,8 +1,5 @@ import { FolksHubAccount } from "../../chains/evm/hub/modules/index.js"; -import { - getHubChain, - getHubChainAdapterAddress, -} from "../../chains/evm/hub/utils/chain.js"; +import { getHubChain } from "../../chains/evm/hub/utils/chain.js"; import { FolksEvmAccount } from "../../chains/evm/spoke/modules/index.js"; import { ChainType } from "../../common/types/chain.js"; import { Action } from "../../common/types/message.js"; @@ -12,7 +9,6 @@ import { assertSpokeChainSupported, getSignerGenericAddress, getSpokeChain, - getSpokeChainAdapterAddress, } from "../../common/utils/chain.js"; import { buildMessageToSend, @@ -26,8 +22,8 @@ import type { InviteAddressMessageData, MessageAdapters, MessageBuilderParams, - UnregisterAddressMessageData, OptionalFeeParams, + UnregisterAddressMessageData, } from "../../common/types/message.js"; import type { PrepareAcceptInviteAddressCall, @@ -70,24 +66,11 @@ export const prepare = { }; const feeParams: OptionalFeeParams = {}; - const sourceAdapterAddress = getSpokeChainAdapterAddress( - folksChain.folksChainId, - folksChain.network, - adapters.adapterId, - ); - const destAdapterAddress = getHubChainAdapterAddress( - folksChain.network, - adapters.adapterId, - ); - feeParams.gasLimit = await estimateReceiveGasLimit( - folksChain.folksChainId, - hubChain.folksChainId, FolksCore.getHubProvider(), - folksChain.network, + hubChain, + folksChain, adapters, - sourceAdapterAddress, - destAdapterAddress, messageBuilderParams, ); @@ -140,7 +123,7 @@ export const prepare = { folksChainIdToInvite, addressToInvite, }; - const messageToSend = buildMessageToSend(folksChain.chainType, { + const messageBuilderParams: MessageBuilderParams = { userAddress, accountId, adapters, @@ -150,7 +133,22 @@ export const prepare = { handler: hubChain.hubAddress, data, extraArgs: "0x", - }); + }; + const feeParams: OptionalFeeParams = {}; + + feeParams.gasLimit = await estimateReceiveGasLimit( + FolksCore.getHubProvider(), + hubChain, + folksChain, + adapters, + messageBuilderParams, + ); + + const messageToSend = buildMessageToSend( + folksChain.chainType, + messageBuilderParams, + feeParams, + ); switch (folksChain.chainType) { case ChainType.EVM: @@ -188,7 +186,7 @@ export const prepare = { chainType: folksChain.chainType, }); - const messageToSend = buildMessageToSend(folksChain.chainType, { + const messageBuilderParams: MessageBuilderParams = { userAddress, accountId, adapters, @@ -198,7 +196,22 @@ export const prepare = { handler: hubChain.hubAddress, data: "0x", extraArgs: "0x", - }); + }; + const feeParams: OptionalFeeParams = {}; + + feeParams.gasLimit = await estimateReceiveGasLimit( + FolksCore.getHubProvider(), + hubChain, + folksChain, + adapters, + messageBuilderParams, + ); + + const messageToSend = buildMessageToSend( + folksChain.chainType, + messageBuilderParams, + feeParams, + ); switch (folksChain.chainType) { case ChainType.EVM: @@ -241,7 +254,8 @@ export const prepare = { const data: UnregisterAddressMessageData = { folksChainIdToUnregister, }; - const messageToSend = buildMessageToSend(folksChain.chainType, { + + const messageBuilderParams: MessageBuilderParams = { userAddress, accountId, adapters, @@ -251,7 +265,22 @@ export const prepare = { handler: hubChain.hubAddress, data, extraArgs: "0x", - }); + }; + const feeParams: OptionalFeeParams = {}; + + feeParams.gasLimit = await estimateReceiveGasLimit( + FolksCore.getHubProvider(), + hubChain, + folksChain, + adapters, + messageBuilderParams, + ); + + const messageToSend = buildMessageToSend( + folksChain.chainType, + messageBuilderParams, + feeParams, + ); switch (folksChain.chainType) { case ChainType.EVM: