1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Standard reference to ETH_SWAPS_TOKEN_ADDRESS via ETH_SWAPS_TOKEN_OBJECT.address (#10613)

This commit is contained in:
Dan J Miller 2021-03-09 12:29:35 -03:30 committed by GitHub
parent 59f516ade2
commit c86ceacfd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 24 deletions

View File

@ -8,7 +8,7 @@ import { calcTokenAmount } from '../../../ui/app/helpers/utils/token-util';
import { calcGasTotal } from '../../../ui/app/pages/send/send.utils'; import { calcGasTotal } from '../../../ui/app/pages/send/send.utils';
import { conversionUtil } from '../../../ui/app/helpers/utils/conversion-util'; import { conversionUtil } from '../../../ui/app/helpers/utils/conversion-util';
import { import {
ETH_SWAPS_TOKEN_ADDRESS, ETH_SWAPS_TOKEN_OBJECT,
DEFAULT_ERC20_APPROVE_GAS, DEFAULT_ERC20_APPROVE_GAS,
QUOTES_EXPIRED_ERROR, QUOTES_EXPIRED_ERROR,
QUOTES_NOT_AVAILABLE_ERROR, QUOTES_NOT_AVAILABLE_ERROR,
@ -191,7 +191,7 @@ export default class SwapsController {
let approvalRequired = false; let approvalRequired = false;
if ( if (
fetchParams.sourceToken !== ETH_SWAPS_TOKEN_ADDRESS && fetchParams.sourceToken !== ETH_SWAPS_TOKEN_OBJECT.address &&
Object.values(newQuotes).length Object.values(newQuotes).length
) { ) {
const allowance = await this._getERC20Allowance( const allowance = await this._getERC20Allowance(
@ -552,7 +552,7 @@ export default class SwapsController {
// If the swap is from ETH, subtract the sourceAmount from the total cost. // If the swap is from ETH, subtract the sourceAmount from the total cost.
// Otherwise, the total fee is simply trade.value plus gas fees. // Otherwise, the total fee is simply trade.value plus gas fees.
const ethFee = const ethFee =
sourceToken === ETH_SWAPS_TOKEN_ADDRESS sourceToken === ETH_SWAPS_TOKEN_OBJECT.address
? conversionUtil( ? conversionUtil(
totalWeiCost.minus(sourceAmount, 10), // sourceAmount is in wei totalWeiCost.minus(sourceAmount, 10), // sourceAmount is in wei
{ {
@ -589,7 +589,9 @@ export default class SwapsController {
); );
const conversionRateForCalculations = const conversionRateForCalculations =
destinationToken === ETH_SWAPS_TOKEN_ADDRESS ? 1 : tokenConversionRate; destinationToken === ETH_SWAPS_TOKEN_OBJECT.address
? 1
: tokenConversionRate;
const overallValueOfQuoteForSorting = const overallValueOfQuoteForSorting =
conversionRateForCalculations === undefined conversionRateForCalculations === undefined
@ -616,7 +618,7 @@ export default class SwapsController {
}); });
const isBest = const isBest =
newQuotes[topAggId].destinationToken === ETH_SWAPS_TOKEN_ADDRESS || newQuotes[topAggId].destinationToken === ETH_SWAPS_TOKEN_OBJECT.address ||
Boolean(tokenConversionRates[newQuotes[topAggId]?.destinationToken]); Boolean(tokenConversionRates[newQuotes[topAggId]?.destinationToken]);
let savings = null; let savings = null;

View File

@ -9,7 +9,7 @@ import {
ROPSTEN_NETWORK_ID, ROPSTEN_NETWORK_ID,
MAINNET_NETWORK_ID, MAINNET_NETWORK_ID,
} from '../../../../shared/constants/network'; } from '../../../../shared/constants/network';
import { ETH_SWAPS_TOKEN_ADDRESS } from '../../../../ui/app/helpers/constants/swaps'; import { ETH_SWAPS_TOKEN_OBJECT } from '../../../../ui/app/helpers/constants/swaps';
import { createTestProviderTools } from '../../../stub/provider'; import { createTestProviderTools } from '../../../stub/provider';
import SwapsController, { import SwapsController, {
utils, utils,
@ -471,7 +471,7 @@ describe('SwapsController', function () {
getTopQuoteAndSavingsMockQuotes(), getTopQuoteAndSavingsMockQuotes(),
(quote) => ({ (quote) => ({
...quote, ...quote,
sourceToken: ETH_SWAPS_TOKEN_ADDRESS, sourceToken: ETH_SWAPS_TOKEN_OBJECT.address,
destinationToken: '0x1111111111111111111111111111111111111111', destinationToken: '0x1111111111111111111111111111111111111111',
trade: { value: '0x8ac7230489e80000' }, trade: { value: '0x8ac7230489e80000' },
}), }),
@ -480,42 +480,42 @@ describe('SwapsController', function () {
const expectedResultQuotes = { const expectedResultQuotes = {
[TEST_AGG_ID_1]: { [TEST_AGG_ID_1]: {
...baseExpectedResultQuotes[TEST_AGG_ID_1], ...baseExpectedResultQuotes[TEST_AGG_ID_1],
sourceToken: ETH_SWAPS_TOKEN_ADDRESS, sourceToken: ETH_SWAPS_TOKEN_OBJECT.address,
destinationToken: '0x1111111111111111111111111111111111111111', destinationToken: '0x1111111111111111111111111111111111111111',
trade: { value: '0x8ac7230489e80000' }, trade: { value: '0x8ac7230489e80000' },
overallValueOfQuote: '2.0195', overallValueOfQuote: '2.0195',
}, },
[TEST_AGG_ID_2]: { [TEST_AGG_ID_2]: {
...baseExpectedResultQuotes[TEST_AGG_ID_2], ...baseExpectedResultQuotes[TEST_AGG_ID_2],
sourceToken: ETH_SWAPS_TOKEN_ADDRESS, sourceToken: ETH_SWAPS_TOKEN_OBJECT.address,
destinationToken: '0x1111111111111111111111111111111111111111', destinationToken: '0x1111111111111111111111111111111111111111',
trade: { value: '0x8ac7230489e80000' }, trade: { value: '0x8ac7230489e80000' },
overallValueOfQuote: '1.9996', overallValueOfQuote: '1.9996',
}, },
[TEST_AGG_ID_3]: { [TEST_AGG_ID_3]: {
...baseExpectedResultQuotes[TEST_AGG_ID_3], ...baseExpectedResultQuotes[TEST_AGG_ID_3],
sourceToken: ETH_SWAPS_TOKEN_ADDRESS, sourceToken: ETH_SWAPS_TOKEN_OBJECT.address,
destinationToken: '0x1111111111111111111111111111111111111111', destinationToken: '0x1111111111111111111111111111111111111111',
trade: { value: '0x8ac7230489e80000' }, trade: { value: '0x8ac7230489e80000' },
overallValueOfQuote: '1.9698', overallValueOfQuote: '1.9698',
}, },
[TEST_AGG_ID_4]: { [TEST_AGG_ID_4]: {
...baseExpectedResultQuotes[TEST_AGG_ID_4], ...baseExpectedResultQuotes[TEST_AGG_ID_4],
sourceToken: ETH_SWAPS_TOKEN_ADDRESS, sourceToken: ETH_SWAPS_TOKEN_OBJECT.address,
destinationToken: '0x1111111111111111111111111111111111111111', destinationToken: '0x1111111111111111111111111111111111111111',
trade: { value: '0x8ac7230489e80000' }, trade: { value: '0x8ac7230489e80000' },
overallValueOfQuote: '1.94', overallValueOfQuote: '1.94',
}, },
[TEST_AGG_ID_5]: { [TEST_AGG_ID_5]: {
...baseExpectedResultQuotes[TEST_AGG_ID_5], ...baseExpectedResultQuotes[TEST_AGG_ID_5],
sourceToken: ETH_SWAPS_TOKEN_ADDRESS, sourceToken: ETH_SWAPS_TOKEN_OBJECT.address,
destinationToken: '0x1111111111111111111111111111111111111111', destinationToken: '0x1111111111111111111111111111111111111111',
trade: { value: '0x8ac7230489e80000' }, trade: { value: '0x8ac7230489e80000' },
overallValueOfQuote: '1.9102', overallValueOfQuote: '1.9102',
}, },
[TEST_AGG_ID_6]: { [TEST_AGG_ID_6]: {
...baseExpectedResultQuotes[TEST_AGG_ID_6], ...baseExpectedResultQuotes[TEST_AGG_ID_6],
sourceToken: ETH_SWAPS_TOKEN_ADDRESS, sourceToken: ETH_SWAPS_TOKEN_OBJECT.address,
destinationToken: '0x1111111111111111111111111111111111111111', destinationToken: '0x1111111111111111111111111111111111111111',
trade: { value: '0x8ac7230489e80000' }, trade: { value: '0x8ac7230489e80000' },
overallValueOfQuote: '1.8705', overallValueOfQuote: '1.8705',
@ -535,7 +535,7 @@ describe('SwapsController', function () {
getTopQuoteAndSavingsMockQuotes(), getTopQuoteAndSavingsMockQuotes(),
(quote) => ({ (quote) => ({
...quote, ...quote,
sourceToken: ETH_SWAPS_TOKEN_ADDRESS, sourceToken: ETH_SWAPS_TOKEN_OBJECT.address,
destinationToken: '0x1111111111111111111111111111111111111111', destinationToken: '0x1111111111111111111111111111111111111111',
trade: { value: '0x8ac7230489e80000' }, trade: { value: '0x8ac7230489e80000' },
}), }),
@ -546,7 +546,7 @@ describe('SwapsController', function () {
const expectedResultQuotes = { const expectedResultQuotes = {
[TEST_AGG_ID_1]: { [TEST_AGG_ID_1]: {
...baseExpectedResultQuotes[TEST_AGG_ID_1], ...baseExpectedResultQuotes[TEST_AGG_ID_1],
sourceToken: ETH_SWAPS_TOKEN_ADDRESS, sourceToken: ETH_SWAPS_TOKEN_OBJECT.address,
destinationToken: '0x1111111111111111111111111111111111111111', destinationToken: '0x1111111111111111111111111111111111111111',
trade: { value: '0x8b553ece48ec0000' }, trade: { value: '0x8b553ece48ec0000' },
overallValueOfQuote: '1.9795', overallValueOfQuote: '1.9795',
@ -554,7 +554,7 @@ describe('SwapsController', function () {
}, },
[TEST_AGG_ID_2]: { [TEST_AGG_ID_2]: {
...baseExpectedResultQuotes[TEST_AGG_ID_2], ...baseExpectedResultQuotes[TEST_AGG_ID_2],
sourceToken: ETH_SWAPS_TOKEN_ADDRESS, sourceToken: ETH_SWAPS_TOKEN_OBJECT.address,
destinationToken: '0x1111111111111111111111111111111111111111', destinationToken: '0x1111111111111111111111111111111111111111',
trade: { value: '0x8ac7230489e80000' }, trade: { value: '0x8ac7230489e80000' },
overallValueOfQuote: '1.9996', overallValueOfQuote: '1.9996',
@ -569,28 +569,28 @@ describe('SwapsController', function () {
}, },
[TEST_AGG_ID_3]: { [TEST_AGG_ID_3]: {
...baseExpectedResultQuotes[TEST_AGG_ID_3], ...baseExpectedResultQuotes[TEST_AGG_ID_3],
sourceToken: ETH_SWAPS_TOKEN_ADDRESS, sourceToken: ETH_SWAPS_TOKEN_OBJECT.address,
destinationToken: '0x1111111111111111111111111111111111111111', destinationToken: '0x1111111111111111111111111111111111111111',
trade: { value: '0x8ac7230489e80000' }, trade: { value: '0x8ac7230489e80000' },
overallValueOfQuote: '1.9698', overallValueOfQuote: '1.9698',
}, },
[TEST_AGG_ID_4]: { [TEST_AGG_ID_4]: {
...baseExpectedResultQuotes[TEST_AGG_ID_4], ...baseExpectedResultQuotes[TEST_AGG_ID_4],
sourceToken: ETH_SWAPS_TOKEN_ADDRESS, sourceToken: ETH_SWAPS_TOKEN_OBJECT.address,
destinationToken: '0x1111111111111111111111111111111111111111', destinationToken: '0x1111111111111111111111111111111111111111',
trade: { value: '0x8ac7230489e80000' }, trade: { value: '0x8ac7230489e80000' },
overallValueOfQuote: '1.94', overallValueOfQuote: '1.94',
}, },
[TEST_AGG_ID_5]: { [TEST_AGG_ID_5]: {
...baseExpectedResultQuotes[TEST_AGG_ID_5], ...baseExpectedResultQuotes[TEST_AGG_ID_5],
sourceToken: ETH_SWAPS_TOKEN_ADDRESS, sourceToken: ETH_SWAPS_TOKEN_OBJECT.address,
destinationToken: '0x1111111111111111111111111111111111111111', destinationToken: '0x1111111111111111111111111111111111111111',
trade: { value: '0x8ac7230489e80000' }, trade: { value: '0x8ac7230489e80000' },
overallValueOfQuote: '1.9102', overallValueOfQuote: '1.9102',
}, },
[TEST_AGG_ID_6]: { [TEST_AGG_ID_6]: {
...baseExpectedResultQuotes[TEST_AGG_ID_6], ...baseExpectedResultQuotes[TEST_AGG_ID_6],
sourceToken: ETH_SWAPS_TOKEN_ADDRESS, sourceToken: ETH_SWAPS_TOKEN_OBJECT.address,
destinationToken: '0x1111111111111111111111111111111111111111', destinationToken: '0x1111111111111111111111111111111111111111',
trade: { value: '0x8ac7230489e80000' }, trade: { value: '0x8ac7230489e80000' },
overallValueOfQuote: '1.8705', overallValueOfQuote: '1.8705',

View File

@ -1,6 +1,5 @@
// An address that the metaswap-api recognizes as ETH, in place of the token address that ERC-20 tokens have // An address that the metaswap-api recognizes as ETH, in place of the token address that ERC-20 tokens have
export const ETH_SWAPS_TOKEN_ADDRESS = const ETH_SWAPS_TOKEN_ADDRESS = '0x0000000000000000000000000000000000000000';
'0x0000000000000000000000000000000000000000';
export const ETH_SWAPS_TOKEN_OBJECT = { export const ETH_SWAPS_TOKEN_OBJECT = {
symbol: 'ETH', symbol: 'ETH',

View File

@ -1,5 +1,5 @@
import { TRANSACTION_CATEGORIES } from '../../../shared/constants/transaction'; import { TRANSACTION_CATEGORIES } from '../../../shared/constants/transaction';
import { ETH_SWAPS_TOKEN_ADDRESS } from '../helpers/constants/swaps'; import { ETH_SWAPS_TOKEN_OBJECT } from '../helpers/constants/swaps';
import { getSwapsTokensReceivedFromTxMeta } from '../pages/swaps/swaps.util'; import { getSwapsTokensReceivedFromTxMeta } from '../pages/swaps/swaps.util';
import { useTokenFiatAmount } from './useTokenFiatAmount'; import { useTokenFiatAmount } from './useTokenFiatAmount';
@ -30,7 +30,7 @@ export function useSwappedTokenValue(transactionGroup, currentAsset) {
const isViewingReceivedTokenFromSwap = const isViewingReceivedTokenFromSwap =
currentAsset?.symbol === primaryTransaction.destinationTokenSymbol || currentAsset?.symbol === primaryTransaction.destinationTokenSymbol ||
(currentAsset.address === ETH_SWAPS_TOKEN_ADDRESS && (currentAsset.address === ETH_SWAPS_TOKEN_OBJECT.address &&
primaryTransaction.destinationTokenSymbol === 'ETH'); primaryTransaction.destinationTokenSymbol === 'ETH');
const swapTokenValue = const swapTokenValue =