Skip to content

Commit

Permalink
fix: loan module estimate fees (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
palace22 authored Jun 4, 2024
1 parent 87b8ff3 commit a7e24c3
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 103 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"typescript-eslint": "^7.9.0"
},
"dependencies": {
"viem": "^2.10.5"
"viem": "^2.13.2"
},
"config": {
"commitizen": {
Expand Down
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 20 additions & 8 deletions src/chains/evm/common/utils/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,21 @@ import type {
import type { CCIPAny2EvmMessage } from "../types/gmp.js";
import type { Client, Hex } from "viem";

export const DEFAULT_MESSAGE_PARAMS = (
adapters: MessageAdapters,
): MessageParams => ({
export const buildMessageParams = ({
adapters,
receiverValue = BigInt(0),
gasLimit = BigInt(0),
returnGasLimit = BigInt(0),
}: {
adapters: MessageAdapters;
receiverValue?: bigint;
gasLimit?: bigint;
returnGasLimit?: bigint;
}): MessageParams => ({
...adapters,
receiverValue: BigInt(0),
gasLimit: BigInt(0),
returnGasLimit: BigInt(0),
receiverValue,
gasLimit,
returnGasLimit,
});

export function buildMessagePayload(
Expand Down Expand Up @@ -202,7 +210,7 @@ export function buildEvmMessageToSend(
extraArgs,
} = messageToSendBuilderParams;
const data = buildEvmMessageData(messageToSendBuilderParams);
const params = { ...DEFAULT_MESSAGE_PARAMS(adapters), ...feeParams };
const params = buildMessageParams({ adapters, ...feeParams });
switch (action) {
case Action.CreateAccount: {
const message: MessageToSend = {
Expand Down Expand Up @@ -414,7 +422,11 @@ export async function estimateEvmWormholeDataGasLimit(
sourceWormholeChainId,
messageId,
],
{ value: receiverValue, account: wormholeRelayer },
{
value: receiverValue,
account: wormholeRelayer,
stateOverride: [{ address: wormholeRelayer, balance: receiverValue }],
},
);
}

Expand Down
83 changes: 43 additions & 40 deletions src/chains/evm/hub/modules/folks-hub-loan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getSpokeTokenData,
} from "../../../../common/utils/chain.js";
import {
DEFAULT_MESSAGE_PARAMS,
buildMessageParams,
buildMessagePayload,
buildSendTokenExtraArgsWhenRemoving,
} from "../../common/utils/message.js";
Expand All @@ -27,57 +27,60 @@ import type { AccountId } from "../../../../common/types/lending.js";
import type {
MessageAdapters,
MessageToSend,
OptionalFeeParams,
AdapterType,
} from "../../../../common/types/message.js";
import type { FolksTokenId } from "../../../../common/types/token.js";
import type { Client } from "viem";

export function getSendTokenAdapterFees(
export async function getSendTokenAdapterFees(
provider: Client,
network: NetworkType,
accountId: AccountId,
folksTokenId: FolksTokenId,
amount: bigint,
receiverFolksChainId: FolksChainId,
adapters: MessageAdapters,
): () => Promise<bigint> {
return async (): Promise<bigint> => {
const hubChain = getHubChain(network);
const hubTokenData = getHubTokenData(folksTokenId, network);
const hubBridgeRouter = getBridgeRouterHubContract(
provider,
hubChain.bridgeRouterAddress,
);
feeParams: OptionalFeeParams = {},
): Promise<bigint> {
const hubChain = getHubChain(network);
const hubTokenData = getHubTokenData(folksTokenId, network);
const hubBridgeRouter = getBridgeRouterHubContract(
provider,
hubChain.bridgeRouterAddress,
);

const spokeChain = getSpokeChain(receiverFolksChainId, network);
const spokeTokenData = getSpokeTokenData(spokeChain, folksTokenId);
const spokeChain = getSpokeChain(receiverFolksChainId, network);
const spokeTokenData = getSpokeTokenData(spokeChain, folksTokenId);

// construct return message
const { returnAdapterId } = adapters;
const returnParams = DEFAULT_MESSAGE_PARAMS({
adapterId: returnAdapterId,
returnAdapterId,
});
const returnMessage: MessageToSend = {
params: returnParams,
sender: hubChain.hubAddress,
destinationChainId: receiverFolksChainId,
handler: getRandomGenericAddress(),
payload: buildMessagePayload(
Action.SendToken,
accountId,
getRandomGenericAddress(),
convertNumberToBytes(amount, UINT256_LENGTH),
),
finalityLevel: FINALITY.FINALISED,
extraArgs: buildSendTokenExtraArgsWhenRemoving(
hubTokenData.tokenType,
spokeTokenData.spokeAddress,
getHubTokenAddress(hubTokenData),
amount,
),
};

// get return adapter fee
return await hubBridgeRouter.read.getSendFee([returnMessage]);
// construct return message
const returnParams = buildMessageParams({
adapters: {
adapterId: adapters.returnAdapterId,
returnAdapterId: 0 as AdapterType,
},
gasLimit: feeParams.returnGasLimit,
});
const returnMessage: MessageToSend = {
params: returnParams,
sender: hubChain.hubAddress,
destinationChainId: receiverFolksChainId,
handler: getRandomGenericAddress(),
payload: buildMessagePayload(
Action.SendToken,
accountId,
getRandomGenericAddress(),
convertNumberToBytes(amount, UINT256_LENGTH),
),
finalityLevel: FINALITY.FINALISED,
extraArgs: buildSendTokenExtraArgsWhenRemoving(
hubTokenData.tokenType,
spokeTokenData.spokeAddress,
getHubTokenAddress(hubTokenData),
amount,
),
};

// get return adapter fee
return await hubBridgeRouter.read.getSendFee([returnMessage]);
}
39 changes: 35 additions & 4 deletions src/common/utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async function estimateAdapterReceiveGasLimit(
destFolksChainId: FolksChainId,
destFolksChainProvider: FolksProvider,
network: NetworkType,
adapters: MessageAdapters,
adapterId: AdapterType,
sourceAdapterAddress: GenericAddress,
destAdapterAddress: GenericAddress,
messageBuilderParams: MessageBuilderParams,
Expand All @@ -53,7 +53,7 @@ async function estimateAdapterReceiveGasLimit(
const destFolksChain = getFolksChain(destFolksChainId, network);
switch (destFolksChain.chainType) {
case ChainType.EVM:
switch (adapters.adapterId) {
switch (adapterId) {
case AdapterType.WORMHOLE_DATA: {
const sourceWormholeChainId =
getWormholeData(sourceFolksChainId).wormholeChainId;
Expand Down Expand Up @@ -103,7 +103,7 @@ async function estimateAdapterReceiveGasLimit(
throw new Error("Not implemented yet: AdapterType.CCIP_TOKEN case");
}
default:
return exhaustiveCheck(adapters.adapterId);
return exhaustiveCheck(adapterId);
}

default:
Expand Down Expand Up @@ -135,11 +135,42 @@ export async function estimateReceiveGasLimit(
hubChain.folksChainId,
hubProvider,
folksChain.network,
adapters,
adapters.adapterId,
sourceAdapterAddress,
destAdapterAddress,
messageBuilderParams,
receiverValue,
returnGasLimit,
);
}

export async function estimateReturnReceiveGasLimit(
receiverFolksChainProvider: FolksProvider,
receiverFolksChain: FolksChain,
hubChain: HubChain,
adapters: MessageAdapters,
messageBuilderParams: MessageBuilderParams,
) {
const sourceAdapterAddress = getHubChainAdapterAddress(
receiverFolksChain.network,
adapters.returnAdapterId,
);
const destAdapterAddress = getSpokeChainAdapterAddress(
receiverFolksChain.folksChainId,
receiverFolksChain.network,
adapters.returnAdapterId,
);

return await estimateAdapterReceiveGasLimit(
hubChain.folksChainId,
receiverFolksChain.folksChainId,
receiverFolksChainProvider,
receiverFolksChain.network,
adapters.returnAdapterId,
sourceAdapterAddress,
destAdapterAddress,
messageBuilderParams,
BigInt(0),
BigInt(0),
);
}
Loading

0 comments on commit a7e24c3

Please sign in to comment.