From 8c8738f849adc42d212cb5238859fd4d82fb80bb Mon Sep 17 00:00:00 2001 From: Giuseppe Palazzolo <38705839+palace22@users.noreply.github.com> Date: Mon, 23 Sep 2024 18:10:40 +0200 Subject: [PATCH] Fix: override token data on send token action (#170) Fixed override token data on send token action --- .changeset/neat-kids-promise.md | 5 +++ src/chains/evm/common/utils/message.ts | 10 +++--- src/chains/evm/hub/utils/message.ts | 46 +++++++++++++++++++------- src/common/types/message.ts | 12 +++++-- src/common/utils/messages.ts | 2 +- src/xchain/modules/folks-loan.ts | 19 +++++++++-- 6 files changed, 72 insertions(+), 22 deletions(-) create mode 100644 .changeset/neat-kids-promise.md diff --git a/.changeset/neat-kids-promise.md b/.changeset/neat-kids-promise.md new file mode 100644 index 0000000..dc5fc9e --- /dev/null +++ b/.changeset/neat-kids-promise.md @@ -0,0 +1,5 @@ +--- +"@folks-finance/xchain-sdk": patch +--- + +Fixed override token data on send token action diff --git a/src/chains/evm/common/utils/message.ts b/src/chains/evm/common/utils/message.ts index 70979fa..d8932da 100644 --- a/src/chains/evm/common/utils/message.ts +++ b/src/chains/evm/common/utils/message.ts @@ -27,7 +27,7 @@ import type { MessageParams, MessageToSend, OptionalFeeParams, - SendTokenExtraArgs, + OverrideTokenData, } from "../../../../common/types/message.js"; import type { FolksHubTokenType, FolksSpokeTokenType } from "../../../../common/types/token.js"; import type { CCIPAny2EvmMessage } from "../types/gmp.js"; @@ -394,7 +394,7 @@ export function buildEvmMessageToSend( handler, payload: buildMessagePayload(Action.SendToken, accountId, userAddress, data), finalityLevel: FINALITY.FINALISED, - extraArgs: buildSendTokenExtraArgsWhenAdding(extraArgs.recipient, extraArgs.token, extraArgs.amount), + extraArgs: buildSendTokenExtraArgsWhenRemoving(extraArgs.recipient, extraArgs.token, extraArgs.amount), }; return message; } @@ -479,8 +479,8 @@ export async function estimateEvmCcipDataGasLimit( }); } -export function getSendTokenStateOverride(folksChainId: FolksChainId, extraArgs: SendTokenExtraArgs) { - const { folksTokenId, amount, recipient, token } = extraArgs; +export function getSendTokenStateOverride(folksChainId: FolksChainId, extraArgs: OverrideTokenData) { + const { folksTokenId, amount, address, token } = extraArgs; if (token.type === TokenType.CIRCLE || token.type === TokenType.ERC20) { const erc20Address = convertFromGenericAddress(token.address, ChainType.EVM); return getBalanceOfStateOverride([ @@ -488,7 +488,7 @@ export function getSendTokenStateOverride(folksChainId: FolksChainId, extraArgs: erc20Address, stateDiff: [ { - owner: convertFromGenericAddress(recipient, ChainType.EVM), + owner: convertFromGenericAddress(address, ChainType.EVM), folksChainId, folksTokenId, tokenType: token.type, diff --git a/src/chains/evm/hub/utils/message.ts b/src/chains/evm/hub/utils/message.ts index 095dd35..5dab90b 100644 --- a/src/chains/evm/hub/utils/message.ts +++ b/src/chains/evm/hub/utils/message.ts @@ -15,13 +15,16 @@ import { } from "../../../../common/utils/messages.js"; import { getFolksTokenIdFromPool } from "../../../../common/utils/token.js"; import { FolksCore } from "../../../../xchain/core/folks-core.js"; +import { buildSendTokenExtraArgsWhenRemoving } from "../../common/utils/message.js"; +import { getHubTokenData } from "./chain.js"; import { getBridgeRouterHubContract } from "./contract.js"; import type { GenericAddress } from "../../../../common/types/address.js"; import type { NetworkType } from "../../../../common/types/chain.js"; import type { MessageBuilderParams, + OverrideTokenData, Payload, ReceiveTokenAction, ReversibleHubAction, @@ -62,10 +65,9 @@ export async function getHubRetryMessageExtraArgsAndAdapterFees( const spokeChain = getSpokeChain(payloadData.receiverFolksChainId, network); const spokeTokenData = getSpokeTokenData(spokeChain, folksTokenId); - const hubSpokeChain = getSpokeChain(hubChain.folksChainId, network); - const hubSpokeTokenData = getSpokeTokenData(hubSpokeChain, folksTokenId); + const hubTokenData = getHubTokenData(folksTokenId, network); - if (hubSpokeTokenData.token.type === TokenType.CIRCLE) + if (hubTokenData.token.type === TokenType.CIRCLE) assertAdapterSupportsTokenMessage(payloadData.receiverFolksChainId, returnAdapterId); else assertAdapterSupportsDataMessage(payloadData.receiverFolksChainId, returnAdapterId); @@ -74,10 +76,16 @@ export async function getHubRetryMessageExtraArgsAndAdapterFees( }; const returnExtraArgs: SendTokenExtraArgs = { folksTokenId, - token: hubSpokeTokenData.token, + token: hubTokenData.token, recipient: spokeTokenData.spokeAddress, amount: payloadData.amount, }; + const overrideTokenData: OverrideTokenData = { + folksTokenId, + token: spokeTokenData.token, + address: spokeTokenData.spokeAddress, + amount: payloadData.amount, + }; const returnMessageBuilderParams: MessageBuilderParams = { userAddress, accountId, @@ -91,6 +99,7 @@ export async function getHubRetryMessageExtraArgsAndAdapterFees( handler: spokeTokenData.spokeAddress, data: returnData, extraArgs: returnExtraArgs, + overrideData: overrideTokenData, }; const returnGasLimit = await estimateAdapterReceiveGasLimit( hubChain.folksChainId, @@ -105,6 +114,7 @@ export async function getHubRetryMessageExtraArgsAndAdapterFees( const messageToSend = buildMessageToSend(ChainType.EVM, returnMessageBuilderParams, { gasLimit: returnGasLimit, }); + const adapterFees = await bridgeRouter.read.getSendFee([messageToSend]); return { @@ -134,10 +144,9 @@ export async function getHubReverseMessageExtraArgsAndAdapterFees( const spokeChain = getSpokeChain(message.sourceChainId, network); const spokeTokenData = getSpokeTokenData(spokeChain, folksTokenId); - const hubSpokeChain = getSpokeChain(hubChain.folksChainId, network); - const hubSpokeTokenData = getSpokeTokenData(hubSpokeChain, folksTokenId); + const hubTokenData = getHubTokenData(folksTokenId, network); - if (hubSpokeTokenData.token.type === TokenType.CIRCLE) + if (hubTokenData.token.type === TokenType.CIRCLE) assertAdapterSupportsTokenMessage(message.sourceChainId, returnAdapterId); else assertAdapterSupportsDataMessage(message.sourceChainId, returnAdapterId); @@ -146,10 +155,16 @@ export async function getHubReverseMessageExtraArgsAndAdapterFees( }; const returnExtraArgs: SendTokenExtraArgs = { folksTokenId, - token: hubSpokeTokenData.token, + token: hubTokenData.token, recipient: spokeTokenData.spokeAddress, amount: payloadData.amount, }; + const overrideTokenData: OverrideTokenData = { + folksTokenId, + token: spokeTokenData.token, + address: spokeTokenData.spokeAddress, + amount: payloadData.amount, + }; const returnMessageBuilderParams: MessageBuilderParams = { userAddress, accountId, @@ -163,6 +178,7 @@ export async function getHubReverseMessageExtraArgsAndAdapterFees( handler: spokeTokenData.spokeAddress, data: returnData, extraArgs: returnExtraArgs, + overrideData: overrideTokenData, }; const returnGasLimit = await estimateAdapterReceiveGasLimit( hubChain.folksChainId, @@ -176,11 +192,17 @@ export async function getHubReverseMessageExtraArgsAndAdapterFees( const bridgeRouter = getBridgeRouterHubContract(provider, hubChain.bridgeRouterAddress); const messageToSend = buildMessageToSend(ChainType.EVM, returnMessageBuilderParams, { gasLimit: returnGasLimit, - receiverValue: 0n, - returnGasLimit: 0n, }); - const adapterFees = await bridgeRouter.read.getSendFee([messageToSend]); - + const adapterFees = await bridgeRouter.read.getSendFee([ + { + ...messageToSend, + extraArgs: buildSendTokenExtraArgsWhenRemoving( + spokeTokenData.spokeAddress, + hubTokenData.token, + payloadData.amount, + ), + }, + ]); return { adapterFees, extraArgs: { diff --git a/src/common/types/message.ts b/src/common/types/message.ts index 6aa4855..4f5472f 100644 --- a/src/common/types/message.ts +++ b/src/common/types/message.ts @@ -2,7 +2,7 @@ import type { GenericAddress } from "./address.js"; import type { FolksChainId } from "./chain.js"; import type { AccountId, LoanId, LoanName, Nonce } from "./lending.js"; import type { LoanTypeId } from "./module.js"; -import type { FolksTokenId, FolksSpokeTokenType } from "./token.js"; +import type { FolksTokenId, FolksSpokeTokenType, FolksHubTokenType } from "./token.js"; import type { FINALITY, HUB_ACTIONS, @@ -214,11 +214,18 @@ export type RepayExtraArgs = { export type SendTokenExtraArgs = { folksTokenId: FolksTokenId; - token: FolksSpokeTokenType; + token: FolksHubTokenType; recipient: GenericAddress; amount: bigint; }; +export type OverrideTokenData = { + folksTokenId: FolksTokenId; + token: FolksSpokeTokenType; + address: GenericAddress; + amount: bigint; +}; + export type MessageDataMap = { [Action.AcceptInviteAddress]: DefaultMessageData; [Action.AddDelegate]: DefaultMessageData; @@ -337,6 +344,7 @@ export type SendTokenMessageDataParams = { action: Action.SendToken; data: SendTokenMessageData; extraArgs: SendTokenExtraArgs; + overrideData: OverrideTokenData; }; export type MessageDataParams = diff --git a/src/common/utils/messages.ts b/src/common/utils/messages.ts index f8a1f74..6bf0eb9 100644 --- a/src/common/utils/messages.ts +++ b/src/common/utils/messages.ts @@ -108,7 +108,7 @@ export async function estimateAdapterReceiveGasLimit( let stateOverride: StateOverride = []; if (messageBuilderParams.action === Action.SendToken) { stateOverride = stateOverride.concat( - getSendTokenStateOverride(destFolksChainId, messageBuilderParams.extraArgs), + getSendTokenStateOverride(destFolksChainId, messageBuilderParams.overrideData), ); } switch (adapterId) { diff --git a/src/xchain/modules/folks-loan.ts b/src/xchain/modules/folks-loan.ts index 1a8267b..efc32eb 100644 --- a/src/xchain/modules/folks-loan.ts +++ b/src/xchain/modules/folks-loan.ts @@ -43,6 +43,7 @@ import type { MessageAdapters, MessageBuilderParams, OptionalFeeParams, + OverrideTokenData, RepayExtraArgs, RepayMessageData, RepayWithCollateralMessageData, @@ -388,10 +389,16 @@ export const prepare = { }; const returnExtraArgs: SendTokenExtraArgs = { folksTokenId, - token: receiverSpokeTokenData.token, + token: hubTokenData.token, recipient: receiverSpokeTokenData.spokeAddress, amount, }; + const overrideData: OverrideTokenData = { + folksTokenId, + token: receiverSpokeTokenData.token, + address: receiverSpokeTokenData.spokeAddress, + amount, + }; const returnMessageBuilderParams: MessageBuilderParams = { userAddress, accountId, @@ -402,6 +409,7 @@ export const prepare = { handler: receiverSpokeTokenData.spokeAddress, data: returnData, extraArgs: returnExtraArgs, + overrideData, }; feeParams.returnGasLimit = await estimateAdapterReceiveGasLimit( hubChain.folksChainId, @@ -510,10 +518,16 @@ export const prepare = { }; const returnExtraArgs: SendTokenExtraArgs = { folksTokenId, - token: receiverSpokeTokenData.token, + token: hubTokenData.token, recipient: receiverSpokeTokenData.spokeAddress, amount, }; + const overrideData: OverrideTokenData = { + folksTokenId, + token: receiverSpokeTokenData.token, + address: receiverSpokeTokenData.spokeAddress, + amount, + }; const returnMessageBuilderParams: MessageBuilderParams = { userAddress, accountId, @@ -524,6 +538,7 @@ export const prepare = { handler: receiverSpokeTokenData.spokeAddress, data: returnData, extraArgs: returnExtraArgs, + overrideData, }; feeParams.returnGasLimit = await estimateAdapterReceiveGasLimit( hubChain.folksChainId,