Skip to content

Commit

Permalink
Helper fetchMsgportFeeAndParams
Browse files Browse the repository at this point in the history
  • Loading branch information
JayJay1024 committed Dec 25, 2023
1 parent 4fa16fe commit 5c8480c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/bridges/xtoken-v3.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -90,8 +91,6 @@ export class XTokenV3Bridge extends BaseBridge {
this.contract &&
this.sourceToken &&
this.targetToken &&
this.sourceChain &&
this.targetChain &&
this.sourcePublicClient
) {
const message =
Expand All @@ -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 };
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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 };
}
}

0 comments on commit 5c8480c

Please sign in to comment.