Skip to content

Commit

Permalink
Fix: override token data on send token action (#170)
Browse files Browse the repository at this point in the history
Fixed override token data on send token action
  • Loading branch information
palace22 authored Sep 23, 2024
1 parent 1ca77b0 commit 8c8738f
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/neat-kids-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@folks-finance/xchain-sdk": patch
---

Fixed override token data on send token action
10 changes: 5 additions & 5 deletions src/chains/evm/common/utils/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -479,16 +479,16 @@ 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([
{
erc20Address,
stateDiff: [
{
owner: convertFromGenericAddress(recipient, ChainType.EVM),
owner: convertFromGenericAddress(address, ChainType.EVM),
folksChainId,
folksTokenId,
tokenType: token.type,
Expand Down
46 changes: 34 additions & 12 deletions src/chains/evm/hub/utils/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand All @@ -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,
Expand All @@ -91,6 +99,7 @@ export async function getHubRetryMessageExtraArgsAndAdapterFees(
handler: spokeTokenData.spokeAddress,
data: returnData,
extraArgs: returnExtraArgs,
overrideData: overrideTokenData,
};
const returnGasLimit = await estimateAdapterReceiveGasLimit(
hubChain.folksChainId,
Expand All @@ -105,6 +114,7 @@ export async function getHubRetryMessageExtraArgsAndAdapterFees(
const messageToSend = buildMessageToSend(ChainType.EVM, returnMessageBuilderParams, {
gasLimit: returnGasLimit,
});

const adapterFees = await bridgeRouter.read.getSendFee([messageToSend]);

return {
Expand Down Expand Up @@ -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);

Expand All @@ -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,
Expand All @@ -163,6 +178,7 @@ export async function getHubReverseMessageExtraArgsAndAdapterFees(
handler: spokeTokenData.spokeAddress,
data: returnData,
extraArgs: returnExtraArgs,
overrideData: overrideTokenData,
};
const returnGasLimit = await estimateAdapterReceiveGasLimit(
hubChain.folksChainId,
Expand All @@ -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: {
Expand Down
12 changes: 10 additions & 2 deletions src/common/types/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -337,6 +344,7 @@ export type SendTokenMessageDataParams = {
action: Action.SendToken;
data: SendTokenMessageData;
extraArgs: SendTokenExtraArgs;
overrideData: OverrideTokenData;
};

export type MessageDataParams =
Expand Down
2 changes: 1 addition & 1 deletion src/common/utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 17 additions & 2 deletions src/xchain/modules/folks-loan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import type {
MessageAdapters,
MessageBuilderParams,
OptionalFeeParams,
OverrideTokenData,
RepayExtraArgs,
RepayMessageData,
RepayWithCollateralMessageData,
Expand Down Expand Up @@ -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,
Expand All @@ -402,6 +409,7 @@ export const prepare = {
handler: receiverSpokeTokenData.spokeAddress,
data: returnData,
extraArgs: returnExtraArgs,
overrideData,
};
feeParams.returnGasLimit = await estimateAdapterReceiveGasLimit(
hubChain.folksChainId,
Expand Down Expand Up @@ -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,
Expand All @@ -524,6 +538,7 @@ export const prepare = {
handler: receiverSpokeTokenData.spokeAddress,
data: returnData,
extraArgs: returnExtraArgs,
overrideData,
};
feeParams.returnGasLimit = await estimateAdapterReceiveGasLimit(
hubChain.folksChainId,
Expand Down

0 comments on commit 8c8738f

Please sign in to comment.