Skip to content

Commit

Permalink
chore: include gasFees in metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
micaelae committed Nov 22, 2024
1 parent 4356162 commit 8fa72ab
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 67 deletions.
49 changes: 33 additions & 16 deletions ui/ducks/bridge/selectors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,10 @@ describe('Bridge selectors', () => {
fiat: new BigNumber('13.8444372'),
amount: new BigNumber('13.98428'),
},
gasFee: {
amount: new BigNumber('7.141025952e-8'),
fiat: new BigNumber('7.141025952e-8'),
},
totalNetworkFee: {
fiat: new BigNumber('0.00100007141025952'),
amount: new BigNumber('0.00100007141025952'),
Expand Down Expand Up @@ -599,6 +603,10 @@ describe('Bridge selectors', () => {
fiat: new BigNumber('13.8444372'),
amount: new BigNumber('13.98428'),
},
gasFee: {
amount: new BigNumber('7.141025952e-8'),
fiat: new BigNumber('7.141025952e-8'),
},
totalNetworkFee: {
fiat: new BigNumber('0.00100007141025952'),
amount: new BigNumber('0.00100007141025952'),
Expand Down Expand Up @@ -671,6 +679,10 @@ describe('Bridge selectors', () => {
fiat: new BigNumber('13.8444372'),
amount: new BigNumber('13.98428'),
},
gasFee: {
amount: new BigNumber('7.141025952e-8'),
fiat: new BigNumber('7.141025952e-8'),
},
totalNetworkFee: {
fiat: new BigNumber('0.00100007141025952'),
amount: new BigNumber('0.00100007141025952'),
Expand Down Expand Up @@ -721,9 +733,11 @@ describe('Bridge selectors', () => {
});

it('should sort quotes by adjustedReturn', () => {
const state = createBridgeMockStore({
bridgeStateOverrides: { quotes: mockBridgeQuotesNativeErc20 },
});
const state = createBridgeMockStore(
{},
{},
{ quotes: mockBridgeQuotesNativeErc20 },
);

const { activeQuote, recommendedQuote, sortedQuotes } = getBridgeQuotes(
state as never,
Expand Down Expand Up @@ -759,9 +773,10 @@ describe('Bridge selectors', () => {
});

it('should sort quotes by ETA', () => {
const state = createBridgeMockStore({
bridgeSliceOverrides: { sortOrder: SortOrder.ETA_ASC },
bridgeStateOverrides: {
const state = createBridgeMockStore(
{},
{ sortOrder: SortOrder.ETA_ASC },
{
quotes: [
...mockBridgeQuotesNativeErc20,
{
Expand All @@ -774,7 +789,7 @@ describe('Bridge selectors', () => {
},
],
},
});
);

const { activeQuote, recommendedQuote, sortedQuotes } = getBridgeQuotes(
state as never,
Expand All @@ -793,9 +808,10 @@ describe('Bridge selectors', () => {
});

it('should recommend 2nd cheapest quote if ETA exceeds 1 hour', () => {
const state = createBridgeMockStore({
bridgeSliceOverrides: { sortOrder: SortOrder.COST_ASC },
bridgeStateOverrides: {
const state = createBridgeMockStore(
{},
{ sortOrder: SortOrder.COST_ASC },
{
quotes: [
mockBridgeQuotesNativeErc20[1],
{
Expand All @@ -809,7 +825,7 @@ describe('Bridge selectors', () => {
},
],
},
});
);

const { activeQuote, recommendedQuote, sortedQuotes } = getBridgeQuotes(
state as never,
Expand All @@ -831,13 +847,14 @@ describe('Bridge selectors', () => {
});

it('should recommend 2nd fastest quote if adjustedReturn is less than 80% of cheapest quote', () => {
const state = createBridgeMockStore({
bridgeSliceOverrides: {
const state = createBridgeMockStore(
{},
{
sortOrder: SortOrder.ETA_ASC,
toTokenExchangeRate: 0.998781,
toNativeExchangeRate: 0.354073,
},
bridgeStateOverrides: {
{
quotes: [
...mockBridgeQuotesNativeErc20,
{
Expand All @@ -851,14 +868,14 @@ describe('Bridge selectors', () => {
},
],
},
metamaskStateOverrides: {
{
currencyRates: {
ETH: {
conversionRate: 2524.25,
},
},
},
});
);

const { activeQuote, recommendedQuote, sortedQuotes } = getBridgeQuotes(
state as never,
Expand Down
11 changes: 9 additions & 2 deletions ui/ducks/bridge/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ import {
import {
calcAdjustedReturn,
calcCost,
calcRelayerFee,
calcSentAmount,
calcSwapRate,
calcToAmount,
calcTotalNetworkFee,
calcTotalGasFee,
isNativeAddress,
} from '../../pages/bridge/utils/quote';
import { decGWEIToHexWEI } from '../../../shared/modules/conversion.utils';
Expand Down Expand Up @@ -217,12 +218,17 @@ const _getQuotesWithMetadata = createDeepEqualSelector(
): (QuoteResponse & QuoteMetadata)[] => {
const newQuotes = quotes.map((quote: QuoteResponse) => {
const toTokenAmount = calcToAmount(quote.quote, toTokenExchangeRate);
const totalNetworkFee = calcTotalNetworkFee(
const gasFee = calcTotalGasFee(
quote,
estimatedBaseFeeInDecGwei,
maxPriorityFeePerGasInDecGwei,
nativeExchangeRate,
);
const relayerFee = calcRelayerFee(quote, nativeExchangeRate);
const totalNetworkFee = {
amount: gasFee.amount.plus(relayerFee.amount),
fiat: gasFee.fiat?.plus(relayerFee.fiat || '0') ?? null,
};
const sentAmount = calcSentAmount(
quote.quote,
isNativeAddress(quote.quote.srcAsset.address)
Expand All @@ -240,6 +246,7 @@ const _getQuotesWithMetadata = createDeepEqualSelector(
sentAmount,
totalNetworkFee,
adjustedReturn,
gasFee,
swapRate: calcSwapRate(sentAmount.amount, toTokenAmount.amount),
cost: calcCost(adjustedReturn.fiat, sentAmount.fiat),
};
Expand Down
1 change: 1 addition & 0 deletions ui/pages/bridge/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type L1GasFees = {
// Values derived from the quote response
// fiat values are calculated based on the user's selected currency
export type QuoteMetadata = {
gasFee: { amount: BigNumber; fiat: BigNumber | null };
totalNetworkFee: { amount: BigNumber; fiat: BigNumber | null }; // gasFees + relayerFees
toTokenAmount: { amount: BigNumber; fiat: BigNumber | null };
adjustedReturn: { fiat: BigNumber | null }; // destTokenAmount - totalNetworkFee
Expand Down
55 changes: 27 additions & 28 deletions ui/pages/bridge/utils/quote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
calcSentAmount,
calcSwapRate,
calcToAmount,
calcTotalNetworkFee,
calcTotalGasFee,
calcRelayerFee,
formatEtaInMinutes,
} from './quote';

Expand Down Expand Up @@ -155,7 +156,7 @@ describe('Bridge quote utils', () => {
undefined,
],
])(
'calcTotalNetworkFee: fromToken is %s',
'calcTotalGasFee and calcRelayerFee: fromToken is %s',
(
_: string,
srcAsset: { decimals: number; address: string },
Expand All @@ -165,18 +166,17 @@ describe('Bridge quote utils', () => {
approvalGasLimit?: number,
) => {
const feeData = { metabridge: { amount: 0 } };
const result = calcTotalNetworkFee(
{
trade: { value, gasLimit: 1092677 },
approval: approvalGasLimit
? { gasLimit: approvalGasLimit }
: undefined,
quote: { srcAsset, srcTokenAmount, feeData },
} as never,
'0.00010456',
'0.0001',
2517.42,
);
const quote = {
trade: { value, gasLimit: 1092677 },
approval: approvalGasLimit ? { gasLimit: approvalGasLimit } : undefined,
quote: { srcAsset, srcTokenAmount, feeData },
} as never;
const gasFee = calcTotalGasFee(quote, '0.00010456', '0.0001', 2517.42);
const relayerFee = calcRelayerFee(quote, 2517.42);
const result = {
amount: gasFee.amount.plus(relayerFee.amount),
fiat: gasFee.fiat?.plus(relayerFee.fiat || '0') ?? null,
};
expect(result.amount?.toString()).toStrictEqual(amount);
expect(result.fiat?.toString()).toStrictEqual(fiat);
},
Expand Down Expand Up @@ -225,7 +225,7 @@ describe('Bridge quote utils', () => {
undefined,
],
])(
'calcTotalNetworkFee: fromToken is %s with l1GasFee',
'calcTotalGasFee and calcRelayerFee: fromToken is %s with l1GasFee',
(
_: string,
srcAsset: { decimals: number; address: string },
Expand All @@ -235,19 +235,18 @@ describe('Bridge quote utils', () => {
approvalGasLimit?: number,
) => {
const feeData = { metabridge: { amount: 0 } };
const result = calcTotalNetworkFee(
{
trade: { value, gasLimit: 1092677 },
approval: approvalGasLimit
? { gasLimit: approvalGasLimit }
: undefined,
quote: { srcAsset, srcTokenAmount, feeData },
l1GasFeesInHexWei: '0x25F63418AA4',
} as never,
'0.00010456',
'0.0001',
2517.42,
);
const quote = {
trade: { value, gasLimit: 1092677 },
approval: approvalGasLimit ? { gasLimit: approvalGasLimit } : undefined,
quote: { srcAsset, srcTokenAmount, feeData },
l1GasFeesInHexWei: '0x25F63418AA4',
} as never;
const gasFee = calcTotalGasFee(quote, '0.00010456', '0.0001', 2517.42);
const relayerFee = calcRelayerFee(quote, 2517.42);
const result = {
amount: gasFee.amount.plus(relayerFee.amount),
fiat: gasFee.fiat?.plus(relayerFee.fiat || '0') ?? null,
};
expect(result.amount?.toString()).toStrictEqual(amount);
expect(result.fiat?.toString()).toStrictEqual(fiat);
},
Expand Down
23 changes: 2 additions & 21 deletions ui/pages/bridge/utils/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const calcSentAmount = (
};
};

const calcRelayerFee = (
export const calcRelayerFee = (
bridgeQuote: QuoteResponse,
nativeExchangeRate?: number,
) => {
Expand All @@ -101,7 +101,7 @@ const calcRelayerFee = (
};
};

const calcTotalGasFee = (
export const calcTotalGasFee = (
bridgeQuote: QuoteResponse & L1GasFees,
estimatedBaseFeeInDecGwei: string,
maxPriorityFeePerGasInDecGwei: string,
Expand Down Expand Up @@ -140,25 +140,6 @@ const calcTotalGasFee = (
};
};

export const calcTotalNetworkFee = (
bridgeQuote: QuoteResponse & L1GasFees,
estimatedBaseFeeInDecGwei: string,
maxPriorityFeePerGasInDecGwei: string,
nativeExchangeRate?: number,
) => {
const normalizedGasFee = calcTotalGasFee(
bridgeQuote,
estimatedBaseFeeInDecGwei,
maxPriorityFeePerGasInDecGwei,
nativeExchangeRate,
);
const normalizedRelayerFee = calcRelayerFee(bridgeQuote, nativeExchangeRate);
return {
amount: normalizedGasFee.amount.plus(normalizedRelayerFee.amount),
fiat: normalizedGasFee.fiat?.plus(normalizedRelayerFee.fiat || '0') ?? null,
};
};

export const calcAdjustedReturn = (
destTokenAmountInFiat: BigNumber | null,
totalNetworkFeeInFiat: BigNumber | null,
Expand Down

0 comments on commit 8fa72ab

Please sign in to comment.