diff --git a/packages/transaction-pay-controller/CHANGELOG.md b/packages/transaction-pay-controller/CHANGELOG.md index d089f19cda1..b5faa90c035 100644 --- a/packages/transaction-pay-controller/CHANGELOG.md +++ b/packages/transaction-pay-controller/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Resolve correct `networkClientId` for source chain in Relay execute flow ([#8492](https://github.com/MetaMask/core/pull/8492)) +- Stop double-counting subsidized fees in Relay quote target amounts ([#8488](https://github.com/MetaMask/core/pull/8488)) ## [19.2.0] diff --git a/packages/transaction-pay-controller/src/strategy/relay/relay-quotes.test.ts b/packages/transaction-pay-controller/src/strategy/relay/relay-quotes.test.ts index a264d75a73e..4432974b027 100644 --- a/packages/transaction-pay-controller/src/strategy/relay/relay-quotes.test.ts +++ b/packages/transaction-pay-controller/src/strategy/relay/relay-quotes.test.ts @@ -1779,6 +1779,36 @@ describe('Relay Quotes Utils', () => { }); }); + it('uses amountFormatted for subsidized fee when fee token is a stablecoin', async () => { + const quoteMock = cloneDeep(QUOTE_MOCK); + quoteMock.fees.subsidized = { + amount: '500000', + amountFormatted: '0.50', + amountUsd: '0.49', + currency: { + address: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48' as Hex, + chainId: 1, + decimals: 6, + }, + minimumAmount: '500000', + }; + + successfulFetchMock.mockResolvedValue({ + json: async () => quoteMock, + } as never); + + const result = await getRelayQuotes({ + messenger, + requests: [QUOTE_REQUEST_MOCK], + transaction: TRANSACTION_META_MOCK, + }); + + expect(result[0].fees.provider).toStrictEqual({ + usd: '0', + fiat: '0', + }); + }); + it('includes dust in quote', async () => { successfulFetchMock.mockResolvedValue({ json: async () => QUOTE_MOCK, @@ -2402,7 +2432,7 @@ describe('Relay Quotes Utils', () => { }); }); - it('adds subsidized fee to target amount fiat values when trade type is EXACT_INPUT', async () => { + it('does not add subsidized fee to target amount', async () => { const quoteMock = cloneDeep(QUOTE_MOCK); quoteMock.fees.subsidized = { amount: '500000000000000', @@ -2426,66 +2456,6 @@ describe('Relay Quotes Utils', () => { transaction: TRANSACTION_META_MOCK, }); - expect(result[0].targetAmount).toStrictEqual({ - usd: '1.73', - fiat: '3.46', - }); - }); - - it('uses amountFormatted for subsidized fee when fee token is a stablecoin', async () => { - const quoteMock = cloneDeep(QUOTE_MOCK); - quoteMock.fees.subsidized = { - amount: '500000', - amountFormatted: '0.50', - amountUsd: '0.49', - currency: { - address: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48' as Hex, - chainId: 1, - decimals: 6, - }, - minimumAmount: '500000', - }; - - successfulFetchMock.mockResolvedValue({ - json: async () => quoteMock, - } as never); - - const result = await getRelayQuotes({ - messenger, - requests: [{ ...QUOTE_REQUEST_MOCK, isMaxAmount: true }], - transaction: TRANSACTION_META_MOCK, - }); - - expect(result[0].targetAmount).toStrictEqual({ - usd: '1.73', - fiat: '3.46', - }); - }); - - it('does not add subsidized fee to target amount when trade type is not EXACT_INPUT', async () => { - const quoteMock = cloneDeep(QUOTE_MOCK); - quoteMock.fees.subsidized = { - amount: '500000000000000', - amountFormatted: '0.0005', - amountUsd: '0.50', - currency: { - address: '0xdef' as Hex, - chainId: 1, - decimals: 18, - }, - minimumAmount: '500000000000000', - }; - - successfulFetchMock.mockResolvedValue({ - json: async () => quoteMock, - } as never); - - const result = await getRelayQuotes({ - messenger, - requests: [QUOTE_REQUEST_MOCK], - transaction: TRANSACTION_META_MOCK, - }); - expect(result[0].targetAmount).toStrictEqual({ usd: '1.23', fiat: '2.46', diff --git a/packages/transaction-pay-controller/src/strategy/relay/relay-quotes.ts b/packages/transaction-pay-controller/src/strategy/relay/relay-quotes.ts index 976b9f4f633..e8f04bb3d9a 100644 --- a/packages/transaction-pay-controller/src/strategy/relay/relay-quotes.ts +++ b/packages/transaction-pay-controller/src/strategy/relay/relay-quotes.ts @@ -471,24 +471,10 @@ async function normalizeQuote( request.targetTokenAddress, ); - const additionalTargetAmountUsd = - quote.request.tradeType === 'EXACT_INPUT' - ? subsidizedFeeUsd - : new BigNumber(0); - - if (additionalTargetAmountUsd.gt(0)) { - log( - 'Including subsidized fee in target amount', - additionalTargetAmountUsd.toString(10), - ); - } - - const baseTargetAmountUsd = isTargetStablecoin + const targetAmountUsd = isTargetStablecoin ? new BigNumber(currencyOut.amountFormatted) : new BigNumber(currencyOut.amountUsd); - const targetAmountUsd = baseTargetAmountUsd.plus(additionalTargetAmountUsd); - const targetAmount = getFiatValueFromUsd(targetAmountUsd, usdToFiatRate); const metamask: RelayQuoteMetamask = {