-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reverse extra args completion (#158)
Resolve #140 - Added completion of reverse `extraArgs` when missing - Added check on reversible actions - Updated `SendTokenMessageData` to not contain `folksTokenId` - Added utils to decode `payload` and `data`
- Loading branch information
Showing
14 changed files
with
583 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@folks-finance/xchain-sdk": patch | ||
--- | ||
|
||
Added retry and reverse msg value estimation considering bridge router balance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@folks-finance/xchain-sdk": patch | ||
--- | ||
|
||
Added reverse extra args completion with gas limit estimation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@folks-finance/xchain-sdk": patch | ||
--- | ||
|
||
Updated SendTokenMessageData to not have the extraneous value folksTokenId |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@folks-finance/xchain-sdk": patch | ||
--- | ||
|
||
Added retry extra args completion with gas limit estimation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
import { RECEIVE_TOKEN_ACTIONS } from "../../../../common/constants/message.js"; | ||
import { ChainType } from "../../../../common/types/chain.js"; | ||
import { MessageDirection } from "../../../../common/types/gmp.js"; | ||
import { Action, AdapterType } from "../../../../common/types/message.js"; | ||
import { getSpokeChain, getSpokeTokenData } from "../../../../common/utils/chain.js"; | ||
import { | ||
buildMessageToSend, | ||
decodeMessagePayloadData, | ||
estimateAdapterReceiveGasLimit, | ||
} from "../../../../common/utils/messages.js"; | ||
import { getFolksTokenIdFromPool } from "../../../../common/utils/token.js"; | ||
import { FolksCore } from "../../../../xchain/core/folks-core.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, | ||
Payload, | ||
ReceiveTokenAction, | ||
ReversibleHubAction, | ||
SendTokenExtraArgs, | ||
SendTokenMessageData, | ||
} from "../../../../common/types/message.js"; | ||
import type { | ||
MessageReceived, | ||
RetryMessageExtraArgs, | ||
RetryMessageExtraArgsParams, | ||
ReverseMessageExtraArgs, | ||
ReverseMessageExtraArgsParams, | ||
} from "../../common/types/gmp.js"; | ||
import type { HubChain } from "../types/chain.js"; | ||
import type { Client as EVMProvider } from "viem"; | ||
|
||
export async function getHubRetryMessageExtraArgsAndAdapterFees( | ||
provider: EVMProvider, | ||
hubChain: HubChain, | ||
network: NetworkType, | ||
userAddress: GenericAddress, | ||
message: MessageReceived, | ||
extraArgsParams: RetryMessageExtraArgsParams, | ||
payload: Payload, | ||
): Promise<{ | ||
adapterFees: bigint; | ||
extraArgs: RetryMessageExtraArgs; | ||
}> { | ||
const returnAdapterId = extraArgsParams?.returnAdapterId ?? message.returnAdapterId; | ||
const { accountId, action, data } = payload; | ||
|
||
// @ts-expect-error: ts(2345) | ||
if (!RECEIVE_TOKEN_ACTIONS.includes(action)) | ||
return { adapterFees: 0n, extraArgs: { returnAdapterId, returnGasLimit: 0n } }; | ||
const payloadData = decodeMessagePayloadData(action as ReceiveTokenAction, data); | ||
|
||
const folksTokenId = getFolksTokenIdFromPool(payloadData.poolId); | ||
|
||
const spokeChain = getSpokeChain(payloadData.receiverFolksChainId, network); | ||
const spokeTokenData = getSpokeTokenData(spokeChain, folksTokenId); | ||
|
||
const returnData: SendTokenMessageData = { | ||
amount: payloadData.amount, | ||
}; | ||
const returnExtraArgs: SendTokenExtraArgs = { | ||
folksTokenId, | ||
token: spokeTokenData.token, | ||
recipient: spokeTokenData.spokeAddress, | ||
amount: payloadData.amount, | ||
}; | ||
const returnMessageBuilderParams: MessageBuilderParams = { | ||
userAddress, | ||
accountId, | ||
adapters: { | ||
adapterId: AdapterType.HUB, | ||
returnAdapterId: extraArgsParams?.returnAdapterId ?? message.returnAdapterId, | ||
}, | ||
action: Action.SendToken, | ||
sender: hubChain.hubAddress, | ||
destinationChainId: payloadData.receiverFolksChainId, | ||
handler: spokeTokenData.spokeAddress, | ||
data: returnData, | ||
extraArgs: returnExtraArgs, | ||
}; | ||
const returnGasLimit = await estimateAdapterReceiveGasLimit( | ||
hubChain.folksChainId, | ||
payloadData.receiverFolksChainId, | ||
FolksCore.getEVMProvider(payloadData.receiverFolksChainId), | ||
network, | ||
MessageDirection.HubToSpoke, | ||
returnMessageBuilderParams, | ||
); | ||
|
||
const bridgeRouter = getBridgeRouterHubContract(provider, hubChain.bridgeRouterAddress); | ||
const messageToSend = buildMessageToSend(ChainType.EVM, returnMessageBuilderParams); | ||
const adapterFees = await bridgeRouter.read.getSendFee([messageToSend]); | ||
|
||
return { | ||
adapterFees, | ||
extraArgs: { returnAdapterId, returnGasLimit }, | ||
}; | ||
} | ||
|
||
export async function getHubReverseMessageExtraArgsAndAdapterFees( | ||
provider: EVMProvider, | ||
hubChain: HubChain, | ||
network: NetworkType, | ||
userAddress: GenericAddress, | ||
message: MessageReceived, | ||
extraArgsParams: ReverseMessageExtraArgsParams, | ||
payload: Payload, | ||
): Promise<{ | ||
adapterFees: bigint; | ||
extraArgs: ReverseMessageExtraArgs; | ||
}> { | ||
const { action, data } = payload; | ||
const payloadData = decodeMessagePayloadData(action as ReversibleHubAction, data); | ||
|
||
const returnAdapterId = extraArgsParams?.returnAdapterId ?? message.returnAdapterId; | ||
const accountId = extraArgsParams?.accountId ?? payload.accountId; | ||
|
||
const folksTokenId = getFolksTokenIdFromPool(payloadData.poolId); | ||
|
||
const spokeChain = getSpokeChain(message.sourceChainId, network); | ||
const spokeTokenData = getSpokeTokenData(spokeChain, folksTokenId); | ||
|
||
const returnData: SendTokenMessageData = { | ||
amount: payloadData.amount, | ||
}; | ||
const returnExtraArgs: SendTokenExtraArgs = { | ||
folksTokenId, | ||
token: spokeTokenData.token, | ||
recipient: spokeTokenData.spokeAddress, | ||
amount: payloadData.amount, | ||
}; | ||
const returnMessageBuilderParams: MessageBuilderParams = { | ||
userAddress, | ||
accountId, | ||
adapters: { | ||
adapterId: AdapterType.HUB, | ||
returnAdapterId, | ||
}, | ||
action: Action.SendToken, | ||
sender: hubChain.hubAddress, | ||
destinationChainId: message.sourceChainId, | ||
handler: spokeTokenData.spokeAddress, | ||
data: returnData, | ||
extraArgs: returnExtraArgs, | ||
}; | ||
const returnGasLimit = await estimateAdapterReceiveGasLimit( | ||
hubChain.folksChainId, | ||
message.sourceChainId, | ||
FolksCore.getEVMProvider(message.sourceChainId), | ||
network, | ||
MessageDirection.HubToSpoke, | ||
returnMessageBuilderParams, | ||
); | ||
|
||
const bridgeRouter = getBridgeRouterHubContract(provider, hubChain.bridgeRouterAddress); | ||
const messageToSend = buildMessageToSend(ChainType.EVM, returnMessageBuilderParams); | ||
const adapterFees = await bridgeRouter.read.getSendFee([messageToSend]); | ||
|
||
return { | ||
adapterFees, | ||
extraArgs: { | ||
accountId, | ||
returnAdapterId, | ||
returnGasLimit, | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,12 @@ | ||
import { Action } from "../types/message.js"; | ||
|
||
export const FINALITY = { | ||
IMMEDIATE: BigInt(0), | ||
FINALISED: BigInt(1), | ||
} as const; | ||
|
||
export const REVERSIBLE_HUB_ACTIONS = [Action.CreateLoanAndDeposit, Action.Deposit, Action.Repay] as const; | ||
|
||
export const SEND_TOKEN_ACTIONS = [Action.CreateLoanAndDeposit, Action.Deposit, Action.Repay] as const; | ||
export const RECEIVE_TOKEN_ACTIONS = [Action.Withdraw, Action.Borrow] as const; | ||
export const HUB_ACTIONS = [Action.DepositFToken, Action.WithdrawFToken, Action.Liquidate, Action.SendToken] as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.