From 8fa72ab93c1c8b86f64b1692bfd65c73915ad679 Mon Sep 17 00:00:00 2001 From: Micaela Estabillo Date: Thu, 21 Nov 2024 11:18:25 -0800 Subject: [PATCH] chore: include gasFees in metadata --- ui/ducks/bridge/selectors.test.ts | 49 ++++++++++++++++--------- ui/ducks/bridge/selectors.ts | 11 ++++-- ui/pages/bridge/types.ts | 1 + ui/pages/bridge/utils/quote.test.ts | 55 ++++++++++++++--------------- ui/pages/bridge/utils/quote.ts | 23 ++---------- 5 files changed, 72 insertions(+), 67 deletions(-) diff --git a/ui/ducks/bridge/selectors.test.ts b/ui/ducks/bridge/selectors.test.ts index b26de01e8fa3..b92c8e60e4f0 100644 --- a/ui/ducks/bridge/selectors.test.ts +++ b/ui/ducks/bridge/selectors.test.ts @@ -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'), @@ -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'), @@ -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'), @@ -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, @@ -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, { @@ -774,7 +789,7 @@ describe('Bridge selectors', () => { }, ], }, - }); + ); const { activeQuote, recommendedQuote, sortedQuotes } = getBridgeQuotes( state as never, @@ -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], { @@ -809,7 +825,7 @@ describe('Bridge selectors', () => { }, ], }, - }); + ); const { activeQuote, recommendedQuote, sortedQuotes } = getBridgeQuotes( state as never, @@ -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, { @@ -851,14 +868,14 @@ describe('Bridge selectors', () => { }, ], }, - metamaskStateOverrides: { + { currencyRates: { ETH: { conversionRate: 2524.25, }, }, }, - }); + ); const { activeQuote, recommendedQuote, sortedQuotes } = getBridgeQuotes( state as never, diff --git a/ui/ducks/bridge/selectors.ts b/ui/ducks/bridge/selectors.ts index f51c0ed60fca..4f55c76c669e 100644 --- a/ui/ducks/bridge/selectors.ts +++ b/ui/ducks/bridge/selectors.ts @@ -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'; @@ -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) @@ -240,6 +246,7 @@ const _getQuotesWithMetadata = createDeepEqualSelector( sentAmount, totalNetworkFee, adjustedReturn, + gasFee, swapRate: calcSwapRate(sentAmount.amount, toTokenAmount.amount), cost: calcCost(adjustedReturn.fiat, sentAmount.fiat), }; diff --git a/ui/pages/bridge/types.ts b/ui/pages/bridge/types.ts index 0c6eb8288797..61143bb9de68 100644 --- a/ui/pages/bridge/types.ts +++ b/ui/pages/bridge/types.ts @@ -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 diff --git a/ui/pages/bridge/utils/quote.test.ts b/ui/pages/bridge/utils/quote.test.ts index e6287a33e141..eec342517f83 100644 --- a/ui/pages/bridge/utils/quote.test.ts +++ b/ui/pages/bridge/utils/quote.test.ts @@ -5,7 +5,8 @@ import { calcSentAmount, calcSwapRate, calcToAmount, - calcTotalNetworkFee, + calcTotalGasFee, + calcRelayerFee, formatEtaInMinutes, } from './quote'; @@ -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 }, @@ -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); }, @@ -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 }, @@ -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); }, diff --git a/ui/pages/bridge/utils/quote.ts b/ui/pages/bridge/utils/quote.ts index 6b179fe33712..2fff7e9c1b18 100644 --- a/ui/pages/bridge/utils/quote.ts +++ b/ui/pages/bridge/utils/quote.ts @@ -77,7 +77,7 @@ export const calcSentAmount = ( }; }; -const calcRelayerFee = ( +export const calcRelayerFee = ( bridgeQuote: QuoteResponse, nativeExchangeRate?: number, ) => { @@ -101,7 +101,7 @@ const calcRelayerFee = ( }; }; -const calcTotalGasFee = ( +export const calcTotalGasFee = ( bridgeQuote: QuoteResponse & L1GasFees, estimatedBaseFeeInDecGwei: string, maxPriorityFeePerGasInDecGwei: string, @@ -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,