Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: account receive gas limit #23

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions src/common/utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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,
);
}
83 changes: 56 additions & 27 deletions src/xchain/modules/folks-account.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -12,7 +9,6 @@ import {
assertSpokeChainSupported,
getSignerGenericAddress,
getSpokeChain,
getSpokeChainAdapterAddress,
} from "../../common/utils/chain.js";
import {
buildMessageToSend,
Expand All @@ -26,8 +22,8 @@ import type {
InviteAddressMessageData,
MessageAdapters,
MessageBuilderParams,
UnregisterAddressMessageData,
OptionalFeeParams,
UnregisterAddressMessageData,
} from "../../common/types/message.js";
import type {
PrepareAcceptInviteAddressCall,
Expand Down Expand Up @@ -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,
);

Expand Down Expand Up @@ -140,7 +123,7 @@ export const prepare = {
folksChainIdToInvite,
addressToInvite,
};
const messageToSend = buildMessageToSend(folksChain.chainType, {
const messageBuilderParams: MessageBuilderParams = {
userAddress,
accountId,
adapters,
Expand All @@ -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:
Expand Down Expand Up @@ -188,7 +186,7 @@ export const prepare = {
chainType: folksChain.chainType,
});

const messageToSend = buildMessageToSend(folksChain.chainType, {
const messageBuilderParams: MessageBuilderParams = {
userAddress,
accountId,
adapters,
Expand All @@ -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:
Expand Down Expand Up @@ -241,7 +254,8 @@ export const prepare = {
const data: UnregisterAddressMessageData = {
folksChainIdToUnregister,
};
const messageToSend = buildMessageToSend(folksChain.chainType, {

const messageBuilderParams: MessageBuilderParams = {
userAddress,
accountId,
adapters,
Expand All @@ -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:
Expand Down