diff --git a/src/bridges/xtoken-v3.ts b/src/bridges/xtoken-v3.ts index a030312b0..fbad03937 100644 --- a/src/bridges/xtoken-v3.ts +++ b/src/bridges/xtoken-v3.ts @@ -1,6 +1,7 @@ import { BridgeConstructorArgs, GetFeeArgs, Token, TransferOptions } from "@/types"; import { BaseBridge } from "."; import { Address, Hex, TransactionReceipt, encodeFunctionData } from "viem"; +import { fetchMsgportFeeAndParams } from "@/utils"; export class XTokenV3Bridge extends BaseBridge { constructor(args: BridgeConstructorArgs) { @@ -90,8 +91,6 @@ export class XTokenV3Bridge extends BaseBridge { this.contract && this.sourceToken && this.targetToken && - this.sourceChain && - this.targetChain && this.sourcePublicClient ) { const message = @@ -113,15 +112,14 @@ export class XTokenV3Bridge extends BaseBridge { args: [BigInt(this.sourceChain.id), sourceMessager, targetMessager, message], }); - const feeData = await fetch( - `https://msgport-api.darwinia.network/ormp_ext/fee?from_chain_id=${this.sourceChain.id}&to_chain_id=${this.targetChain.id}&payload=${payload}&from_address=${sourceMessager}&to_address=${targetMessager}&refund_address=${sender}`, + return fetchMsgportFeeAndParams( + this.sourceChain.id, + this.targetChain.id, + sourceMessager, + targetMessager, + sender, + payload, ); - const feeJson = await feeData.json(); - if (feeData.ok && feeJson.code === 0) { - const fee = BigInt(feeJson.data.fee); - const extParams = feeJson.data.params as Hex; - return { fee, extParams }; - } } } diff --git a/src/utils/misc.ts b/src/utils/misc.ts index 30098f8a6..a2b6e8927 100644 --- a/src/utils/misc.ts +++ b/src/utils/misc.ts @@ -1,5 +1,6 @@ import { FEE_RATE_BASE, FEE_RATE_MAX, FEE_RATE_MIN } from "@/config/constant"; import { RecordResult } from "@/types/graphql"; +import { Address, Hex } from "viem"; export function parseRecordResult(result: RecordResult) { switch (result) { @@ -41,3 +42,22 @@ export function formatFeeRate(rate: number) { export function isValidFeeRate(rate: number) { return FEE_RATE_MIN <= rate && rate <= FEE_RATE_MAX; } + +export async function fetchMsgportFeeAndParams( + sourceChainId: number, + targetChainId: number, + sourceMessager: Address, + targetMessager: Address, + sender: Address, + payload: Hex, +) { + const feeData = await fetch( + `https://msgport-api.darwinia.network/ormp_ext/fee?from_chain_id=${sourceChainId}&to_chain_id=${targetChainId}&payload=${payload}&from_address=${sourceMessager}&to_address=${targetMessager}&refund_address=${sender}`, + ); + const feeJson = await feeData.json(); + if (feeData.ok && feeJson.code === 0) { + const fee = BigInt(feeJson.data.fee); + const extParams = feeJson.data.params as Hex; + return { fee, extParams }; + } +}