bridge_ui: use relayer fee schedule

This commit is contained in:
Evan Gray 2022-04-12 22:05:02 -04:00 committed by Evan Gray
parent 0a7168c107
commit 39bc7d1ca8
3 changed files with 45 additions and 76 deletions

View File

@ -1,14 +1,4 @@
import {
ChainId,
CHAIN_ID_AVAX,
CHAIN_ID_BSC,
CHAIN_ID_ETH,
CHAIN_ID_FANTOM,
CHAIN_ID_OASIS,
CHAIN_ID_POLYGON,
CHAIN_ID_SOLANA,
CHAIN_ID_TERRA,
} from "@certusone/wormhole-sdk";
import { ChainId, CHAIN_ID_ETH } from "@certusone/wormhole-sdk";
import { hexToNativeString } from "@certusone/wormhole-sdk/lib/esm/utils";
import axios from "axios";
import { useEffect, useMemo, useState } from "react";
@ -19,8 +9,11 @@ import {
selectTransferSourceParsedTokenAccount,
} from "../store/selectors";
import { getCoinGeckoURL, RELAYER_COMPARE_ASSET } from "../utils/consts";
import useRelayersAvailable, { RelayerTokenInfo } from "./useRelayersAvailable";
import { evmEstimatesByContract } from "./useTransactionFees";
import useRelayersAvailable, {
FeeScheduleEntryFlat,
FeeScheduleEntryPercent,
RelayerTokenInfo,
} from "./useRelayersAvailable";
export function getRelayAssetInfo(
originChain: ChainId,
@ -59,8 +52,6 @@ function isRelayable(
);
}
const ETH_SAFETY_TOLERANCE = 1.25;
export type RelayerInfo = {
isRelayable: boolean;
isRelayingAvailable: boolean;
@ -70,37 +61,32 @@ export type RelayerInfo = {
};
function calculateFeeUsd(
info: RelayerTokenInfo,
comparisonAssetPrice: number,
targetChain: ChainId,
gasPrice?: number
) {
let feeUsd = 0;
if (targetChain === CHAIN_ID_SOLANA) {
feeUsd = 2;
} else if (targetChain === CHAIN_ID_ETH) {
if (!gasPrice) {
feeUsd = 0; //catch this error elsewhere
} else {
// Number should be safe as long as we don't modify highGasEstimate to be in the BigInt range
feeUsd =
((Number(evmEstimatesByContract.transfer.highGasEstimate) * gasPrice) /
1000000000) *
comparisonAssetPrice *
ETH_SAFETY_TOLERANCE;
if (info?.feeSchedule) {
try {
if (info.feeSchedule[targetChain]?.type === "flat") {
feeUsd = (info.feeSchedule[targetChain] as FeeScheduleEntryFlat).feeUsd;
} else if (info.feeSchedule[targetChain]?.type === "percent") {
const entry = info.feeSchedule[targetChain] as FeeScheduleEntryPercent;
if (!gasPrice) {
feeUsd = 0; //catch this error elsewhere
} else {
// Number should be safe as long as we don't modify highGasEstimate to be in the BigInt range
feeUsd =
((Number(entry.gasEstimate) * gasPrice) / 1000000000) *
comparisonAssetPrice *
entry.feePercent;
}
}
} catch (e) {
console.error("Error determining relayer fee");
}
} else if (targetChain === CHAIN_ID_TERRA) {
feeUsd = 2;
} else if (targetChain === CHAIN_ID_BSC) {
feeUsd = 2;
} else if (targetChain === CHAIN_ID_POLYGON) {
feeUsd = 0.5;
} else if (targetChain === CHAIN_ID_AVAX) {
feeUsd = 2;
} else if (targetChain === CHAIN_ID_OASIS) {
feeUsd = 0.5;
} else if (targetChain === CHAIN_ID_FANTOM) {
feeUsd = 0.5;
}
return feeUsd;
@ -291,6 +277,7 @@ function useRelayerInfo(
relayerInfo.data
);
const feeUsd = calculateFeeUsd(
relayerInfo.data,
comparisonAssetPrice,
targetChain,
gasPrice
@ -306,7 +293,7 @@ function useRelayerInfo(
isFetching: false,
receivedAt: null,
data: {
isRelayable: relayable,
isRelayable: relayable && feeUsd > 0,
isRelayingAvailable: true,
feeUsd: usdString,
feeFormatted: feeFormatted,

View File

@ -7,7 +7,7 @@ import { DataWrapper } from "../store/helpers";
import { selectRelayerTokenInfo } from "../store/selectors";
import {
errorRelayerTokenInfo,
fetchTerraTokenMap,
fetchRelayerTokenInfo,
receiveRelayerTokenInfo,
} from "../store/tokenSlice";
import { RELAYER_INFO_URL } from "../utils/consts";
@ -21,9 +21,23 @@ export type Relayer = {
name?: string;
url?: string;
};
export type FeeScheduleEntryFlat = {
type: "flat";
feeUsd: number;
};
export type FeeScheduleEntryPercent = {
type: "percent";
feePercent: number;
gasEstimate: number;
};
export type FeeSchedule = {
// ChainId as a string
[key: string]: FeeScheduleEntryFlat | FeeScheduleEntryPercent;
};
export type RelayerTokenInfo = {
supportedTokens?: RelayToken[];
relayers?: Relayer[];
feeSchedule?: FeeSchedule;
};
const useRelayersAvailable = (
@ -47,14 +61,14 @@ const useRelayersAvailable = (
};
const getRelayersAvailable = (dispatch: Dispatch) => {
dispatch(fetchTerraTokenMap());
dispatch(fetchRelayerTokenInfo());
axios.get(RELAYER_INFO_URL).then(
(response) => {
dispatch(receiveRelayerTokenInfo(response.data as RelayerTokenInfo));
},
(error) => {
dispatch(
errorRelayerTokenInfo("Failed to retrieve the Terra Token List.")
errorRelayerTokenInfo("Failed to retrieve the relayer token info.")
);
}
);

View File

@ -1156,39 +1156,6 @@ export const getIsTransferDisabled = (
export const LUNA_ADDRESS = "uluna";
export const UST_ADDRESS = "uusd";
export type RelayAsset = {
chain: ChainId;
address: string;
coinGeckoId: string;
};
// export const RELAYER_SUPPORTED_ASSETS: RelayAsset[] =
// CLUSTER === "mainnet"
// ? [{ chain: CHAIN_ID_SOLANA, address: WSOL_ADDRESS, coinGeckoId: "solana" }]
// : CLUSTER === "testnet"
// ? [{ chain: CHAIN_ID_SOLANA, address: WSOL_ADDRESS, coinGeckoId: "solana" }]
// : [
// {
// chain: CHAIN_ID_SOLANA,
// address: WSOL_ADDRESS,
// coinGeckoId: "solana",
// },
// { chain: CHAIN_ID_ETH, address: WETH_ADDRESS, coinGeckoId: "ethereum" },
// {
// chain: CHAIN_ID_TERRA,
// address: LUNA_ADDRESS,
// coinGeckoId: "terra-luna",
// },
// {
// chain: CHAIN_ID_TERRA,
// address: UST_ADDRESS,
// coinGeckoId: "terrausd",
// },
// {
// chain: CHAIN_ID_BSC,
// address: WETH_ADDRESS,
// coinGeckoId: "binancecoin",
// },
// ];
export type RelayerCompareAsset = {
[key in ChainId]: string;
@ -1202,6 +1169,7 @@ export const RELAYER_COMPARE_ASSET: RelayerCompareAsset = {
[CHAIN_ID_AVAX]: "avalanche-2",
[CHAIN_ID_OASIS]: "oasis-network",
[CHAIN_ID_FANTOM]: "fantom",
// [CHAIN_ID_AURORA]: "ethereum", // Aurora uses bridged ether
} as RelayerCompareAsset;
export const getCoinGeckoURL = (coinGeckoId: string) =>
`https://api.coingecko.com/api/v3/simple/price?ids=${coinGeckoId}&vs_currencies=usd`;