1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-27 21:00:13 +01:00
metamask-extension/ui/app/pages/swaps/swaps.util.js

647 lines
16 KiB
JavaScript
Raw Normal View History

import log from 'loglevel';
import BigNumber from 'bignumber.js';
import abi from 'human-standard-token-abi';
import { isValidAddress } from 'ethereumjs-util';
import {
ETH_SWAPS_TOKEN_OBJECT,
METASWAP_API_HOST,
} from '../../helpers/constants/swaps';
import {
calcTokenValue,
calcTokenAmount,
} from '../../helpers/utils/token-util';
2020-11-03 00:41:28 +01:00
import {
constructTxParams,
toPrecisionWithoutTrailingZeros,
} from '../../helpers/utils/util';
2020-11-03 00:41:28 +01:00
import {
decimalToHex,
getValueFromWeiHex,
} from '../../helpers/utils/conversions.util';
import { subtractCurrencies } from '../../helpers/utils/conversion-util';
import { formatCurrency } from '../../helpers/utils/confirm-tx.util';
import fetchWithCache from '../../helpers/utils/fetch-with-cache';
2020-10-06 20:28:38 +02:00
import { calcGasTotal } from '../send/send.utils';
2020-10-06 20:28:38 +02:00
2020-11-03 00:41:28 +01:00
const TOKEN_TRANSFER_LOG_TOPIC_HASH =
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef';
2020-10-06 20:28:38 +02:00
const CACHE_REFRESH_ONE_HOUR = 3600000;
2020-10-06 20:28:38 +02:00
const getBaseApi = function (type) {
switch (type) {
case 'trade':
return `${METASWAP_API_HOST}/trades?`;
2020-10-06 20:28:38 +02:00
case 'tokens':
return `${METASWAP_API_HOST}/tokens`;
2020-10-06 20:28:38 +02:00
case 'topAssets':
return `${METASWAP_API_HOST}/topAssets`;
2020-10-06 20:28:38 +02:00
case 'featureFlag':
return `${METASWAP_API_HOST}/featureFlag`;
2020-10-06 20:28:38 +02:00
case 'aggregatorMetadata':
return `${METASWAP_API_HOST}/aggregatorMetadata`;
Switch gas price estimation in swaps to metaswap-api /gasPrices (#9599) Adds swaps-gas-customization-modal and utilize in swaps Remove swaps specific code from gas-modal-page-container/ Remove slow estimate data from swaps-gas-customization-modal.container Use average as lower safe price limit in swaps-gas-customization-modal Lint fix Fix up unit tests Update ui/app/ducks/swaps/swaps.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Remove stale properties from gas-modal-page-container.component.js Replace use of isCustomPrice safe with isCustomSwapsGasPriceSafe, in swaps-gas-customization-modal Remove use of averageIsSafe in isCustomPriceSafe function Stop calling resetCustomGasState in swaps Refactor 'setter' type actions and creators to 'event based', for swaps slice custom gas logic Replace use of advanced-tab-content.component with advanceGasInputs in swaps gas customization component Add validation for the gasPrices endpoint swaps custom gas price should be considered safe if >= to average Update renderDataSummary unit test Lint fix Remove customOnHideOpts for swapsGasCustomizationModal in modal.js Better handling for swaps gas price loading and failure states Improve semantics: isCustomSwapsGasPriceSafe renamed to isCustomSwapsGasPriceUnSafe Mutate state directly in swaps gas slice reducer Remove unused params More reliable tracking of speed setting for Gas Fees Changed metrics event Lint fix Throw error when fetchSwapsGasPrices response is invalid add disableSave and customTotalSupplement to swaps-gas-customization container return Update ui/app/ducks/swaps/swaps.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Improve error handling in fetchMetaSwapsGasPriceEstimates Remove metricsEvent from swaps-gas-customization-modal context Base check of gas speed type in swaps-gas-customization-modal on gasEstimateType Improve naming of variable and functions use to set customPriceIsSafe prop of AdvancedGasInputs in swaps-gas-customization-modal Simplify sinon spy/stub code in gas-price-button-group-component.test.js Remove unnecessary getSwapsFallbackGasPrice call in swaps-gas-customization-modal Remove use of getSwapsTradeTxParams and clean up related gas price logic in swaps Improve validator of SWAP_GAS_PRICE_VALIDATOR Ensure default tradeValue
2020-11-04 17:14:08 +01:00
case 'gasPrices':
return `${METASWAP_API_HOST}/gasPrices`;
case 'refreshTime':
return `${METASWAP_API_HOST}/quoteRefreshRate`;
2020-10-06 20:28:38 +02:00
default:
throw new Error('getBaseApi requires an api call type');
2020-10-06 20:28:38 +02:00
}
};
2020-10-06 20:28:38 +02:00
const validHex = (string) => Boolean(string?.match(/^0x[a-f0-9]+$/u));
const truthyString = (string) => Boolean(string?.length);
2020-11-03 00:41:28 +01:00
const truthyDigitString = (string) =>
truthyString(string) && Boolean(string.match(/^\d+$/u));
2020-10-06 20:28:38 +02:00
const QUOTE_VALIDATORS = [
{
property: 'trade',
type: 'object',
2020-11-03 00:41:28 +01:00
validator: (trade) =>
trade &&
validHex(trade.data) &&
isValidAddress(trade.to) &&
isValidAddress(trade.from) &&
truthyString(trade.value),
2020-10-06 20:28:38 +02:00
},
{
property: 'approvalNeeded',
type: 'object',
2020-11-03 00:41:28 +01:00
validator: (approvalTx) =>
2020-10-06 20:28:38 +02:00
approvalTx === null ||
2020-11-03 00:41:28 +01:00
(approvalTx &&
2020-10-06 20:28:38 +02:00
validHex(approvalTx.data) &&
isValidAddress(approvalTx.to) &&
2020-11-03 00:41:28 +01:00
isValidAddress(approvalTx.from)),
2020-10-06 20:28:38 +02:00
},
{
property: 'sourceAmount',
type: 'string',
validator: truthyDigitString,
},
{
property: 'destinationAmount',
type: 'string',
validator: truthyDigitString,
},
{
property: 'sourceToken',
type: 'string',
validator: isValidAddress,
},
{
property: 'destinationToken',
type: 'string',
validator: isValidAddress,
},
{
property: 'aggregator',
type: 'string',
validator: truthyString,
},
{
property: 'aggType',
type: 'string',
validator: truthyString,
},
{
property: 'error',
type: 'object',
validator: (error) => error === null || typeof error === 'object',
},
{
property: 'averageGas',
type: 'number',
},
{
property: 'maxGas',
type: 'number',
},
{
property: 'gasEstimate',
type: 'number|undefined',
validator: (gasEstimate) => gasEstimate === undefined || gasEstimate > 0,
},
];
2020-10-06 20:28:38 +02:00
const TOKEN_VALIDATORS = [
{
property: 'address',
type: 'string',
validator: isValidAddress,
},
{
property: 'symbol',
type: 'string',
validator: (string) => truthyString(string) && string.length <= 12,
},
{
property: 'decimals',
type: 'string|number',
validator: (string) => Number(string) >= 0 && Number(string) <= 36,
},
];
2020-10-06 20:28:38 +02:00
const TOP_ASSET_VALIDATORS = TOKEN_VALIDATORS.slice(0, 2);
2020-10-06 20:28:38 +02:00
const AGGREGATOR_METADATA_VALIDATORS = [
{
property: 'color',
type: 'string',
validator: (string) => Boolean(string.match(/^#[A-Fa-f0-9]+$/u)),
},
{
property: 'title',
type: 'string',
validator: truthyString,
},
{
property: 'icon',
type: 'string',
validator: (string) => Boolean(string.match(/^data:image/u)),
},
];
2020-10-06 20:28:38 +02:00
Switch gas price estimation in swaps to metaswap-api /gasPrices (#9599) Adds swaps-gas-customization-modal and utilize in swaps Remove swaps specific code from gas-modal-page-container/ Remove slow estimate data from swaps-gas-customization-modal.container Use average as lower safe price limit in swaps-gas-customization-modal Lint fix Fix up unit tests Update ui/app/ducks/swaps/swaps.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Remove stale properties from gas-modal-page-container.component.js Replace use of isCustomPrice safe with isCustomSwapsGasPriceSafe, in swaps-gas-customization-modal Remove use of averageIsSafe in isCustomPriceSafe function Stop calling resetCustomGasState in swaps Refactor 'setter' type actions and creators to 'event based', for swaps slice custom gas logic Replace use of advanced-tab-content.component with advanceGasInputs in swaps gas customization component Add validation for the gasPrices endpoint swaps custom gas price should be considered safe if >= to average Update renderDataSummary unit test Lint fix Remove customOnHideOpts for swapsGasCustomizationModal in modal.js Better handling for swaps gas price loading and failure states Improve semantics: isCustomSwapsGasPriceSafe renamed to isCustomSwapsGasPriceUnSafe Mutate state directly in swaps gas slice reducer Remove unused params More reliable tracking of speed setting for Gas Fees Changed metrics event Lint fix Throw error when fetchSwapsGasPrices response is invalid add disableSave and customTotalSupplement to swaps-gas-customization container return Update ui/app/ducks/swaps/swaps.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Improve error handling in fetchMetaSwapsGasPriceEstimates Remove metricsEvent from swaps-gas-customization-modal context Base check of gas speed type in swaps-gas-customization-modal on gasEstimateType Improve naming of variable and functions use to set customPriceIsSafe prop of AdvancedGasInputs in swaps-gas-customization-modal Simplify sinon spy/stub code in gas-price-button-group-component.test.js Remove unnecessary getSwapsFallbackGasPrice call in swaps-gas-customization-modal Remove use of getSwapsTradeTxParams and clean up related gas price logic in swaps Improve validator of SWAP_GAS_PRICE_VALIDATOR Ensure default tradeValue
2020-11-04 17:14:08 +01:00
const isValidDecimalNumber = (string) =>
!isNaN(string) && string.match(/^[.0-9]+$/u) && !isNaN(parseFloat(string));
Switch gas price estimation in swaps to metaswap-api /gasPrices (#9599) Adds swaps-gas-customization-modal and utilize in swaps Remove swaps specific code from gas-modal-page-container/ Remove slow estimate data from swaps-gas-customization-modal.container Use average as lower safe price limit in swaps-gas-customization-modal Lint fix Fix up unit tests Update ui/app/ducks/swaps/swaps.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Remove stale properties from gas-modal-page-container.component.js Replace use of isCustomPrice safe with isCustomSwapsGasPriceSafe, in swaps-gas-customization-modal Remove use of averageIsSafe in isCustomPriceSafe function Stop calling resetCustomGasState in swaps Refactor 'setter' type actions and creators to 'event based', for swaps slice custom gas logic Replace use of advanced-tab-content.component with advanceGasInputs in swaps gas customization component Add validation for the gasPrices endpoint swaps custom gas price should be considered safe if >= to average Update renderDataSummary unit test Lint fix Remove customOnHideOpts for swapsGasCustomizationModal in modal.js Better handling for swaps gas price loading and failure states Improve semantics: isCustomSwapsGasPriceSafe renamed to isCustomSwapsGasPriceUnSafe Mutate state directly in swaps gas slice reducer Remove unused params More reliable tracking of speed setting for Gas Fees Changed metrics event Lint fix Throw error when fetchSwapsGasPrices response is invalid add disableSave and customTotalSupplement to swaps-gas-customization container return Update ui/app/ducks/swaps/swaps.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Improve error handling in fetchMetaSwapsGasPriceEstimates Remove metricsEvent from swaps-gas-customization-modal context Base check of gas speed type in swaps-gas-customization-modal on gasEstimateType Improve naming of variable and functions use to set customPriceIsSafe prop of AdvancedGasInputs in swaps-gas-customization-modal Simplify sinon spy/stub code in gas-price-button-group-component.test.js Remove unnecessary getSwapsFallbackGasPrice call in swaps-gas-customization-modal Remove use of getSwapsTradeTxParams and clean up related gas price logic in swaps Improve validator of SWAP_GAS_PRICE_VALIDATOR Ensure default tradeValue
2020-11-04 17:14:08 +01:00
const SWAP_GAS_PRICE_VALIDATOR = [
{
property: 'SafeGasPrice',
type: 'string',
validator: isValidDecimalNumber,
},
{
property: 'ProposeGasPrice',
type: 'string',
validator: isValidDecimalNumber,
},
{
property: 'FastGasPrice',
type: 'string',
validator: isValidDecimalNumber,
},
];
Switch gas price estimation in swaps to metaswap-api /gasPrices (#9599) Adds swaps-gas-customization-modal and utilize in swaps Remove swaps specific code from gas-modal-page-container/ Remove slow estimate data from swaps-gas-customization-modal.container Use average as lower safe price limit in swaps-gas-customization-modal Lint fix Fix up unit tests Update ui/app/ducks/swaps/swaps.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Remove stale properties from gas-modal-page-container.component.js Replace use of isCustomPrice safe with isCustomSwapsGasPriceSafe, in swaps-gas-customization-modal Remove use of averageIsSafe in isCustomPriceSafe function Stop calling resetCustomGasState in swaps Refactor 'setter' type actions and creators to 'event based', for swaps slice custom gas logic Replace use of advanced-tab-content.component with advanceGasInputs in swaps gas customization component Add validation for the gasPrices endpoint swaps custom gas price should be considered safe if >= to average Update renderDataSummary unit test Lint fix Remove customOnHideOpts for swapsGasCustomizationModal in modal.js Better handling for swaps gas price loading and failure states Improve semantics: isCustomSwapsGasPriceSafe renamed to isCustomSwapsGasPriceUnSafe Mutate state directly in swaps gas slice reducer Remove unused params More reliable tracking of speed setting for Gas Fees Changed metrics event Lint fix Throw error when fetchSwapsGasPrices response is invalid add disableSave and customTotalSupplement to swaps-gas-customization container return Update ui/app/ducks/swaps/swaps.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Improve error handling in fetchMetaSwapsGasPriceEstimates Remove metricsEvent from swaps-gas-customization-modal context Base check of gas speed type in swaps-gas-customization-modal on gasEstimateType Improve naming of variable and functions use to set customPriceIsSafe prop of AdvancedGasInputs in swaps-gas-customization-modal Simplify sinon spy/stub code in gas-price-button-group-component.test.js Remove unnecessary getSwapsFallbackGasPrice call in swaps-gas-customization-modal Remove use of getSwapsTradeTxParams and clean up related gas price logic in swaps Improve validator of SWAP_GAS_PRICE_VALIDATOR Ensure default tradeValue
2020-11-04 17:14:08 +01:00
2020-11-03 00:41:28 +01:00
function validateData(validators, object, urlUsed) {
2020-10-06 20:28:38 +02:00
return validators.every(({ property, type, validator }) => {
const types = type.split('|');
2020-10-06 20:28:38 +02:00
2020-11-03 00:41:28 +01:00
const valid =
types.some((_type) => typeof object[property] === _type) &&
(!validator || validator(object[property]));
2020-10-06 20:28:38 +02:00
if (!valid) {
2020-11-03 00:41:28 +01:00
log.error(
`response to GET ${urlUsed} invalid for property ${property}; value was:`,
object[property],
'| type was: ',
typeof object[property],
);
2020-10-06 20:28:38 +02:00
}
return valid;
});
2020-10-06 20:28:38 +02:00
}
2020-11-03 00:41:28 +01:00
export async function fetchTradesInfo({
2020-10-06 20:28:38 +02:00
slippage,
sourceToken,
sourceDecimals,
destinationToken,
value,
fromAddress,
exchangeList,
}) {
const urlParams = {
destinationToken,
sourceToken,
sourceAmount: calcTokenValue(value, sourceDecimals).toString(10),
slippage,
timeout: 10000,
walletAddress: fromAddress,
};
2020-10-06 20:28:38 +02:00
if (exchangeList) {
urlParams.exchangeList = exchangeList;
2020-10-06 20:28:38 +02:00
}
const queryString = new URLSearchParams(urlParams).toString();
const tradeURL = `${getBaseApi('trade')}${queryString}`;
2020-11-03 00:41:28 +01:00
const tradesResponse = await fetchWithCache(
tradeURL,
{ method: 'GET' },
{ cacheRefreshTime: 0, timeout: 15000 },
);
2020-10-06 20:28:38 +02:00
const newQuotes = tradesResponse.reduce((aggIdTradeMap, quote) => {
2020-11-03 00:41:28 +01:00
if (
quote.trade &&
!quote.error &&
validateData(QUOTE_VALIDATORS, quote, tradeURL)
) {
2020-10-06 20:28:38 +02:00
const constructedTrade = constructTxParams({
to: quote.trade.to,
from: quote.trade.from,
data: quote.trade.data,
amount: decimalToHex(quote.trade.value),
gas: decimalToHex(quote.maxGas),
});
2020-10-06 20:28:38 +02:00
let { approvalNeeded } = quote;
2020-10-06 20:28:38 +02:00
if (approvalNeeded) {
approvalNeeded = constructTxParams({
...approvalNeeded,
});
2020-10-06 20:28:38 +02:00
}
return {
...aggIdTradeMap,
[quote.aggregator]: {
...quote,
slippage,
trade: constructedTrade,
approvalNeeded,
},
};
2020-10-06 20:28:38 +02:00
}
return aggIdTradeMap;
}, {});
2020-10-06 20:28:38 +02:00
return newQuotes;
2020-10-06 20:28:38 +02:00
}
2020-11-03 00:41:28 +01:00
export async function fetchTokens() {
const tokenUrl = getBaseApi('tokens');
2020-11-03 00:41:28 +01:00
const tokens = await fetchWithCache(
tokenUrl,
{ method: 'GET' },
{ cacheRefreshTime: CACHE_REFRESH_ONE_HOUR },
);
const filteredTokens = tokens.filter((token) => {
2020-11-03 00:41:28 +01:00
return (
validateData(TOKEN_VALIDATORS, token, tokenUrl) &&
token.address !== ETH_SWAPS_TOKEN_OBJECT.address
);
});
filteredTokens.push(ETH_SWAPS_TOKEN_OBJECT);
return filteredTokens;
2020-10-06 20:28:38 +02:00
}
2020-11-03 00:41:28 +01:00
export async function fetchAggregatorMetadata() {
const aggregatorMetadataUrl = getBaseApi('aggregatorMetadata');
2020-11-03 00:41:28 +01:00
const aggregators = await fetchWithCache(
aggregatorMetadataUrl,
{ method: 'GET' },
{ cacheRefreshTime: CACHE_REFRESH_ONE_HOUR },
);
const filteredAggregators = {};
2020-10-06 20:28:38 +02:00
for (const aggKey in aggregators) {
2020-11-03 00:41:28 +01:00
if (
validateData(
AGGREGATOR_METADATA_VALIDATORS,
aggregators[aggKey],
aggregatorMetadataUrl,
)
) {
filteredAggregators[aggKey] = aggregators[aggKey];
2020-10-06 20:28:38 +02:00
}
}
return filteredAggregators;
2020-10-06 20:28:38 +02:00
}
2020-11-03 00:41:28 +01:00
export async function fetchTopAssets() {
const topAssetsUrl = getBaseApi('topAssets');
2020-11-03 00:41:28 +01:00
const response = await fetchWithCache(
topAssetsUrl,
{ method: 'GET' },
{ cacheRefreshTime: CACHE_REFRESH_ONE_HOUR },
);
2020-10-06 20:28:38 +02:00
const topAssetsMap = response.reduce((_topAssetsMap, asset, index) => {
if (validateData(TOP_ASSET_VALIDATORS, asset, topAssetsUrl)) {
return { ..._topAssetsMap, [asset.address]: { index: String(index) } };
2020-10-06 20:28:38 +02:00
}
return _topAssetsMap;
}, {});
return topAssetsMap;
2020-10-06 20:28:38 +02:00
}
2020-11-03 00:41:28 +01:00
export async function fetchSwapsFeatureLiveness() {
const status = await fetchWithCache(
getBaseApi('featureFlag'),
{ method: 'GET' },
{ cacheRefreshTime: 600000 },
);
return status?.active;
2020-10-06 20:28:38 +02:00
}
export async function fetchSwapsQuoteRefreshTime() {
const response = await fetchWithCache(
getBaseApi('refreshTime'),
{ method: 'GET' },
{ cacheRefreshTime: 600000 },
);
// We presently use milliseconds in the UI
if (typeof response?.seconds === 'number' && response.seconds > 0) {
return response.seconds * 1000;
}
throw new Error(
`MetaMask - refreshTime provided invalid response: ${response}`,
);
}
2020-11-03 00:41:28 +01:00
export async function fetchTokenPrice(address) {
const query = `contract_addresses=${address}&vs_currencies=eth`;
2020-10-06 20:28:38 +02:00
2020-11-03 00:41:28 +01:00
const prices = await fetchWithCache(
`https://api.coingecko.com/api/v3/simple/token_price/ethereum?${query}`,
{ method: 'GET' },
{ cacheRefreshTime: 60000 },
);
return prices && prices[address]?.eth;
2020-10-06 20:28:38 +02:00
}
2020-11-03 00:41:28 +01:00
export async function fetchTokenBalance(address, userAddress) {
const tokenContract = global.eth.contract(abi).at(address);
2020-10-06 20:28:38 +02:00
const tokenBalancePromise = tokenContract
? tokenContract.balanceOf(userAddress)
: Promise.resolve();
const usersToken = await tokenBalancePromise;
return usersToken;
2020-10-06 20:28:38 +02:00
}
Switch gas price estimation in swaps to metaswap-api /gasPrices (#9599) Adds swaps-gas-customization-modal and utilize in swaps Remove swaps specific code from gas-modal-page-container/ Remove slow estimate data from swaps-gas-customization-modal.container Use average as lower safe price limit in swaps-gas-customization-modal Lint fix Fix up unit tests Update ui/app/ducks/swaps/swaps.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Remove stale properties from gas-modal-page-container.component.js Replace use of isCustomPrice safe with isCustomSwapsGasPriceSafe, in swaps-gas-customization-modal Remove use of averageIsSafe in isCustomPriceSafe function Stop calling resetCustomGasState in swaps Refactor 'setter' type actions and creators to 'event based', for swaps slice custom gas logic Replace use of advanced-tab-content.component with advanceGasInputs in swaps gas customization component Add validation for the gasPrices endpoint swaps custom gas price should be considered safe if >= to average Update renderDataSummary unit test Lint fix Remove customOnHideOpts for swapsGasCustomizationModal in modal.js Better handling for swaps gas price loading and failure states Improve semantics: isCustomSwapsGasPriceSafe renamed to isCustomSwapsGasPriceUnSafe Mutate state directly in swaps gas slice reducer Remove unused params More reliable tracking of speed setting for Gas Fees Changed metrics event Lint fix Throw error when fetchSwapsGasPrices response is invalid add disableSave and customTotalSupplement to swaps-gas-customization container return Update ui/app/ducks/swaps/swaps.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Improve error handling in fetchMetaSwapsGasPriceEstimates Remove metricsEvent from swaps-gas-customization-modal context Base check of gas speed type in swaps-gas-customization-modal on gasEstimateType Improve naming of variable and functions use to set customPriceIsSafe prop of AdvancedGasInputs in swaps-gas-customization-modal Simplify sinon spy/stub code in gas-price-button-group-component.test.js Remove unnecessary getSwapsFallbackGasPrice call in swaps-gas-customization-modal Remove use of getSwapsTradeTxParams and clean up related gas price logic in swaps Improve validator of SWAP_GAS_PRICE_VALIDATOR Ensure default tradeValue
2020-11-04 17:14:08 +01:00
export async function fetchSwapsGasPrices() {
const gasPricesUrl = getBaseApi('gasPrices');
Switch gas price estimation in swaps to metaswap-api /gasPrices (#9599) Adds swaps-gas-customization-modal and utilize in swaps Remove swaps specific code from gas-modal-page-container/ Remove slow estimate data from swaps-gas-customization-modal.container Use average as lower safe price limit in swaps-gas-customization-modal Lint fix Fix up unit tests Update ui/app/ducks/swaps/swaps.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Remove stale properties from gas-modal-page-container.component.js Replace use of isCustomPrice safe with isCustomSwapsGasPriceSafe, in swaps-gas-customization-modal Remove use of averageIsSafe in isCustomPriceSafe function Stop calling resetCustomGasState in swaps Refactor 'setter' type actions and creators to 'event based', for swaps slice custom gas logic Replace use of advanced-tab-content.component with advanceGasInputs in swaps gas customization component Add validation for the gasPrices endpoint swaps custom gas price should be considered safe if >= to average Update renderDataSummary unit test Lint fix Remove customOnHideOpts for swapsGasCustomizationModal in modal.js Better handling for swaps gas price loading and failure states Improve semantics: isCustomSwapsGasPriceSafe renamed to isCustomSwapsGasPriceUnSafe Mutate state directly in swaps gas slice reducer Remove unused params More reliable tracking of speed setting for Gas Fees Changed metrics event Lint fix Throw error when fetchSwapsGasPrices response is invalid add disableSave and customTotalSupplement to swaps-gas-customization container return Update ui/app/ducks/swaps/swaps.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Improve error handling in fetchMetaSwapsGasPriceEstimates Remove metricsEvent from swaps-gas-customization-modal context Base check of gas speed type in swaps-gas-customization-modal on gasEstimateType Improve naming of variable and functions use to set customPriceIsSafe prop of AdvancedGasInputs in swaps-gas-customization-modal Simplify sinon spy/stub code in gas-price-button-group-component.test.js Remove unnecessary getSwapsFallbackGasPrice call in swaps-gas-customization-modal Remove use of getSwapsTradeTxParams and clean up related gas price logic in swaps Improve validator of SWAP_GAS_PRICE_VALIDATOR Ensure default tradeValue
2020-11-04 17:14:08 +01:00
const response = await fetchWithCache(
gasPricesUrl,
{ method: 'GET' },
{ cacheRefreshTime: 15000 },
);
Switch gas price estimation in swaps to metaswap-api /gasPrices (#9599) Adds swaps-gas-customization-modal and utilize in swaps Remove swaps specific code from gas-modal-page-container/ Remove slow estimate data from swaps-gas-customization-modal.container Use average as lower safe price limit in swaps-gas-customization-modal Lint fix Fix up unit tests Update ui/app/ducks/swaps/swaps.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Remove stale properties from gas-modal-page-container.component.js Replace use of isCustomPrice safe with isCustomSwapsGasPriceSafe, in swaps-gas-customization-modal Remove use of averageIsSafe in isCustomPriceSafe function Stop calling resetCustomGasState in swaps Refactor 'setter' type actions and creators to 'event based', for swaps slice custom gas logic Replace use of advanced-tab-content.component with advanceGasInputs in swaps gas customization component Add validation for the gasPrices endpoint swaps custom gas price should be considered safe if >= to average Update renderDataSummary unit test Lint fix Remove customOnHideOpts for swapsGasCustomizationModal in modal.js Better handling for swaps gas price loading and failure states Improve semantics: isCustomSwapsGasPriceSafe renamed to isCustomSwapsGasPriceUnSafe Mutate state directly in swaps gas slice reducer Remove unused params More reliable tracking of speed setting for Gas Fees Changed metrics event Lint fix Throw error when fetchSwapsGasPrices response is invalid add disableSave and customTotalSupplement to swaps-gas-customization container return Update ui/app/ducks/swaps/swaps.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Improve error handling in fetchMetaSwapsGasPriceEstimates Remove metricsEvent from swaps-gas-customization-modal context Base check of gas speed type in swaps-gas-customization-modal on gasEstimateType Improve naming of variable and functions use to set customPriceIsSafe prop of AdvancedGasInputs in swaps-gas-customization-modal Simplify sinon spy/stub code in gas-price-button-group-component.test.js Remove unnecessary getSwapsFallbackGasPrice call in swaps-gas-customization-modal Remove use of getSwapsTradeTxParams and clean up related gas price logic in swaps Improve validator of SWAP_GAS_PRICE_VALIDATOR Ensure default tradeValue
2020-11-04 17:14:08 +01:00
const responseIsValid = validateData(
SWAP_GAS_PRICE_VALIDATOR,
response,
gasPricesUrl,
);
Switch gas price estimation in swaps to metaswap-api /gasPrices (#9599) Adds swaps-gas-customization-modal and utilize in swaps Remove swaps specific code from gas-modal-page-container/ Remove slow estimate data from swaps-gas-customization-modal.container Use average as lower safe price limit in swaps-gas-customization-modal Lint fix Fix up unit tests Update ui/app/ducks/swaps/swaps.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Remove stale properties from gas-modal-page-container.component.js Replace use of isCustomPrice safe with isCustomSwapsGasPriceSafe, in swaps-gas-customization-modal Remove use of averageIsSafe in isCustomPriceSafe function Stop calling resetCustomGasState in swaps Refactor 'setter' type actions and creators to 'event based', for swaps slice custom gas logic Replace use of advanced-tab-content.component with advanceGasInputs in swaps gas customization component Add validation for the gasPrices endpoint swaps custom gas price should be considered safe if >= to average Update renderDataSummary unit test Lint fix Remove customOnHideOpts for swapsGasCustomizationModal in modal.js Better handling for swaps gas price loading and failure states Improve semantics: isCustomSwapsGasPriceSafe renamed to isCustomSwapsGasPriceUnSafe Mutate state directly in swaps gas slice reducer Remove unused params More reliable tracking of speed setting for Gas Fees Changed metrics event Lint fix Throw error when fetchSwapsGasPrices response is invalid add disableSave and customTotalSupplement to swaps-gas-customization container return Update ui/app/ducks/swaps/swaps.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Improve error handling in fetchMetaSwapsGasPriceEstimates Remove metricsEvent from swaps-gas-customization-modal context Base check of gas speed type in swaps-gas-customization-modal on gasEstimateType Improve naming of variable and functions use to set customPriceIsSafe prop of AdvancedGasInputs in swaps-gas-customization-modal Simplify sinon spy/stub code in gas-price-button-group-component.test.js Remove unnecessary getSwapsFallbackGasPrice call in swaps-gas-customization-modal Remove use of getSwapsTradeTxParams and clean up related gas price logic in swaps Improve validator of SWAP_GAS_PRICE_VALIDATOR Ensure default tradeValue
2020-11-04 17:14:08 +01:00
if (!responseIsValid) {
throw new Error(`${gasPricesUrl} response is invalid`);
Switch gas price estimation in swaps to metaswap-api /gasPrices (#9599) Adds swaps-gas-customization-modal and utilize in swaps Remove swaps specific code from gas-modal-page-container/ Remove slow estimate data from swaps-gas-customization-modal.container Use average as lower safe price limit in swaps-gas-customization-modal Lint fix Fix up unit tests Update ui/app/ducks/swaps/swaps.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Remove stale properties from gas-modal-page-container.component.js Replace use of isCustomPrice safe with isCustomSwapsGasPriceSafe, in swaps-gas-customization-modal Remove use of averageIsSafe in isCustomPriceSafe function Stop calling resetCustomGasState in swaps Refactor 'setter' type actions and creators to 'event based', for swaps slice custom gas logic Replace use of advanced-tab-content.component with advanceGasInputs in swaps gas customization component Add validation for the gasPrices endpoint swaps custom gas price should be considered safe if >= to average Update renderDataSummary unit test Lint fix Remove customOnHideOpts for swapsGasCustomizationModal in modal.js Better handling for swaps gas price loading and failure states Improve semantics: isCustomSwapsGasPriceSafe renamed to isCustomSwapsGasPriceUnSafe Mutate state directly in swaps gas slice reducer Remove unused params More reliable tracking of speed setting for Gas Fees Changed metrics event Lint fix Throw error when fetchSwapsGasPrices response is invalid add disableSave and customTotalSupplement to swaps-gas-customization container return Update ui/app/ducks/swaps/swaps.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Improve error handling in fetchMetaSwapsGasPriceEstimates Remove metricsEvent from swaps-gas-customization-modal context Base check of gas speed type in swaps-gas-customization-modal on gasEstimateType Improve naming of variable and functions use to set customPriceIsSafe prop of AdvancedGasInputs in swaps-gas-customization-modal Simplify sinon spy/stub code in gas-price-button-group-component.test.js Remove unnecessary getSwapsFallbackGasPrice call in swaps-gas-customization-modal Remove use of getSwapsTradeTxParams and clean up related gas price logic in swaps Improve validator of SWAP_GAS_PRICE_VALIDATOR Ensure default tradeValue
2020-11-04 17:14:08 +01:00
}
const {
SafeGasPrice: safeLow,
ProposeGasPrice: average,
FastGasPrice: fast,
} = response;
Switch gas price estimation in swaps to metaswap-api /gasPrices (#9599) Adds swaps-gas-customization-modal and utilize in swaps Remove swaps specific code from gas-modal-page-container/ Remove slow estimate data from swaps-gas-customization-modal.container Use average as lower safe price limit in swaps-gas-customization-modal Lint fix Fix up unit tests Update ui/app/ducks/swaps/swaps.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Remove stale properties from gas-modal-page-container.component.js Replace use of isCustomPrice safe with isCustomSwapsGasPriceSafe, in swaps-gas-customization-modal Remove use of averageIsSafe in isCustomPriceSafe function Stop calling resetCustomGasState in swaps Refactor 'setter' type actions and creators to 'event based', for swaps slice custom gas logic Replace use of advanced-tab-content.component with advanceGasInputs in swaps gas customization component Add validation for the gasPrices endpoint swaps custom gas price should be considered safe if >= to average Update renderDataSummary unit test Lint fix Remove customOnHideOpts for swapsGasCustomizationModal in modal.js Better handling for swaps gas price loading and failure states Improve semantics: isCustomSwapsGasPriceSafe renamed to isCustomSwapsGasPriceUnSafe Mutate state directly in swaps gas slice reducer Remove unused params More reliable tracking of speed setting for Gas Fees Changed metrics event Lint fix Throw error when fetchSwapsGasPrices response is invalid add disableSave and customTotalSupplement to swaps-gas-customization container return Update ui/app/ducks/swaps/swaps.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Improve error handling in fetchMetaSwapsGasPriceEstimates Remove metricsEvent from swaps-gas-customization-modal context Base check of gas speed type in swaps-gas-customization-modal on gasEstimateType Improve naming of variable and functions use to set customPriceIsSafe prop of AdvancedGasInputs in swaps-gas-customization-modal Simplify sinon spy/stub code in gas-price-button-group-component.test.js Remove unnecessary getSwapsFallbackGasPrice call in swaps-gas-customization-modal Remove use of getSwapsTradeTxParams and clean up related gas price logic in swaps Improve validator of SWAP_GAS_PRICE_VALIDATOR Ensure default tradeValue
2020-11-04 17:14:08 +01:00
return {
safeLow,
average,
fast,
};
Switch gas price estimation in swaps to metaswap-api /gasPrices (#9599) Adds swaps-gas-customization-modal and utilize in swaps Remove swaps specific code from gas-modal-page-container/ Remove slow estimate data from swaps-gas-customization-modal.container Use average as lower safe price limit in swaps-gas-customization-modal Lint fix Fix up unit tests Update ui/app/ducks/swaps/swaps.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Remove stale properties from gas-modal-page-container.component.js Replace use of isCustomPrice safe with isCustomSwapsGasPriceSafe, in swaps-gas-customization-modal Remove use of averageIsSafe in isCustomPriceSafe function Stop calling resetCustomGasState in swaps Refactor 'setter' type actions and creators to 'event based', for swaps slice custom gas logic Replace use of advanced-tab-content.component with advanceGasInputs in swaps gas customization component Add validation for the gasPrices endpoint swaps custom gas price should be considered safe if >= to average Update renderDataSummary unit test Lint fix Remove customOnHideOpts for swapsGasCustomizationModal in modal.js Better handling for swaps gas price loading and failure states Improve semantics: isCustomSwapsGasPriceSafe renamed to isCustomSwapsGasPriceUnSafe Mutate state directly in swaps gas slice reducer Remove unused params More reliable tracking of speed setting for Gas Fees Changed metrics event Lint fix Throw error when fetchSwapsGasPrices response is invalid add disableSave and customTotalSupplement to swaps-gas-customization container return Update ui/app/ducks/swaps/swaps.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Improve error handling in fetchMetaSwapsGasPriceEstimates Remove metricsEvent from swaps-gas-customization-modal context Base check of gas speed type in swaps-gas-customization-modal on gasEstimateType Improve naming of variable and functions use to set customPriceIsSafe prop of AdvancedGasInputs in swaps-gas-customization-modal Simplify sinon spy/stub code in gas-price-button-group-component.test.js Remove unnecessary getSwapsFallbackGasPrice call in swaps-gas-customization-modal Remove use of getSwapsTradeTxParams and clean up related gas price logic in swaps Improve validator of SWAP_GAS_PRICE_VALIDATOR Ensure default tradeValue
2020-11-04 17:14:08 +01:00
}
2020-11-03 00:41:28 +01:00
export function getRenderableNetworkFeesForQuote(
tradeGas,
approveGas,
gasPrice,
currentCurrency,
conversionRate,
tradeValue,
sourceSymbol,
sourceAmount,
) {
2020-11-03 00:41:28 +01:00
const totalGasLimitForCalculation = new BigNumber(tradeGas || '0x0', 16)
.plus(approveGas || '0x0', 16)
.toString(16);
const gasTotalInWeiHex = calcGasTotal(totalGasLimitForCalculation, gasPrice);
2020-10-06 20:28:38 +02:00
const nonGasFee = new BigNumber(tradeValue, 16)
.minus(sourceSymbol === 'ETH' ? sourceAmount : 0, 10)
.toString(16);
const totalWeiCost = new BigNumber(gasTotalInWeiHex, 16)
.plus(nonGasFee, 16)
.toString(16);
2020-10-06 20:28:38 +02:00
const ethFee = getValueFromWeiHex({
value: totalWeiCost,
2020-10-06 20:28:38 +02:00
toDenomination: 'ETH',
numberOfDecimals: 5,
});
2020-10-06 20:28:38 +02:00
const rawNetworkFees = getValueFromWeiHex({
value: totalWeiCost,
2020-10-06 20:28:38 +02:00
toCurrency: currentCurrency,
conversionRate,
numberOfDecimals: 2,
});
const formattedNetworkFee = formatCurrency(rawNetworkFees, currentCurrency);
2020-10-06 20:28:38 +02:00
return {
rawNetworkFees,
rawEthFee: ethFee,
feeInFiat: formattedNetworkFee,
feeInEth: `${ethFee} ETH`,
nonGasFee,
};
2020-10-06 20:28:38 +02:00
}
2020-11-03 00:41:28 +01:00
export function quotesToRenderableData(
quotes,
gasPrice,
conversionRate,
currentCurrency,
approveGas,
tokenConversionRates,
) {
2020-10-06 20:28:38 +02:00
return Object.values(quotes).map((quote) => {
const {
destinationAmount = 0,
sourceAmount = 0,
sourceTokenInfo,
destinationTokenInfo,
slippage,
aggType,
aggregator,
gasEstimateWithRefund,
averageGas,
fee,
trade,
} = quote;
2020-11-03 00:41:28 +01:00
const sourceValue = calcTokenAmount(
sourceAmount,
sourceTokenInfo.decimals,
).toString(10);
2020-11-03 00:41:28 +01:00
const destinationValue = calcTokenAmount(
destinationAmount,
destinationTokenInfo.decimals,
).toPrecision(8);
2020-10-06 20:28:38 +02:00
const {
feeInFiat,
rawNetworkFees,
rawEthFee,
feeInEth,
} = getRenderableNetworkFeesForQuote(
2020-11-03 00:41:28 +01:00
gasEstimateWithRefund || decimalToHex(averageGas || 800000),
2020-10-06 20:28:38 +02:00
approveGas,
gasPrice,
currentCurrency,
conversionRate,
trade.value,
sourceTokenInfo.symbol,
sourceAmount,
);
2020-10-06 20:28:38 +02:00
const slippageMultiplier = new BigNumber(100 - slippage).div(100);
2020-11-03 00:41:28 +01:00
const minimumAmountReceived = new BigNumber(destinationValue)
.times(slippageMultiplier)
.toFixed(6);
2020-11-03 00:41:28 +01:00
const tokenConversionRate =
tokenConversionRates[destinationTokenInfo.address];
2020-11-03 00:41:28 +01:00
const ethValueOfTrade =
destinationTokenInfo.symbol === 'ETH'
? calcTokenAmount(
destinationAmount,
destinationTokenInfo.decimals,
2020-11-03 00:41:28 +01:00
).minus(rawEthFee, 10)
: new BigNumber(tokenConversionRate || 0, 10)
.times(
calcTokenAmount(destinationAmount, destinationTokenInfo.decimals),
2020-11-03 00:41:28 +01:00
10,
)
.minus(rawEthFee, 10);
2020-10-06 20:28:38 +02:00
let liquiditySourceKey;
let renderedSlippage = slippage;
2020-10-06 20:28:38 +02:00
if (aggType === 'AGG') {
liquiditySourceKey = 'swapAggregator';
2020-10-06 20:28:38 +02:00
} else if (aggType === 'RFQ') {
liquiditySourceKey = 'swapRequestForQuotation';
renderedSlippage = 0;
2020-10-06 20:28:38 +02:00
} else if (aggType === 'DEX') {
liquiditySourceKey = 'swapDecentralizedExchange';
2020-10-06 20:28:38 +02:00
} else {
liquiditySourceKey = 'swapUnknown';
2020-10-06 20:28:38 +02:00
}
return {
aggId: aggregator,
amountReceiving: `${destinationValue} ${destinationTokenInfo.symbol}`,
destinationTokenDecimals: destinationTokenInfo.decimals,
destinationTokenSymbol: destinationTokenInfo.symbol,
destinationTokenValue: formatSwapsValueForDisplay(destinationValue),
destinationIconUrl: destinationTokenInfo.iconUrl,
2020-10-06 20:28:38 +02:00
isBestQuote: quote.isBestQuote,
liquiditySourceKey,
feeInEth,
detailedNetworkFees: `${feeInEth} (${feeInFiat})`,
networkFees: feeInFiat,
quoteSource: aggType,
rawNetworkFees,
slippage: renderedSlippage,
sourceTokenDecimals: sourceTokenInfo.decimals,
sourceTokenSymbol: sourceTokenInfo.symbol,
sourceTokenValue: sourceValue,
sourceTokenIconUrl: sourceTokenInfo.iconUrl,
2020-10-06 20:28:38 +02:00
ethValueOfTrade,
minimumAmountReceived,
metaMaskFee: fee,
};
});
2020-10-06 20:28:38 +02:00
}
2020-11-03 00:41:28 +01:00
export function getSwapsTokensReceivedFromTxMeta(
tokenSymbol,
txMeta,
tokenAddress,
accountAddress,
tokenDecimals,
approvalTxMeta,
) {
const txReceipt = txMeta?.txReceipt;
2020-10-06 20:28:38 +02:00
if (tokenSymbol === 'ETH') {
2020-11-03 00:41:28 +01:00
if (
!txReceipt ||
!txMeta ||
!txMeta.postTxBalance ||
!txMeta.preTxBalance
) {
return null;
2020-10-06 20:28:38 +02:00
}
let approvalTxGasCost = '0x0';
if (approvalTxMeta && approvalTxMeta.txReceipt) {
2020-11-03 00:41:28 +01:00
approvalTxGasCost = calcGasTotal(
approvalTxMeta.txReceipt.gasUsed,
approvalTxMeta.txParams.gasPrice,
);
}
const gasCost = calcGasTotal(txReceipt.gasUsed, txMeta.txParams.gasPrice);
2020-11-03 00:41:28 +01:00
const totalGasCost = new BigNumber(gasCost, 16)
.plus(approvalTxGasCost, 16)
.toString(16);
2020-11-03 00:41:28 +01:00
const preTxBalanceLessGasCost = subtractCurrencies(
txMeta.preTxBalance,
totalGasCost,
{
aBase: 16,
bBase: 16,
toNumericBase: 'hex',
},
);
2020-11-03 00:41:28 +01:00
const ethReceived = subtractCurrencies(
txMeta.postTxBalance,
preTxBalanceLessGasCost,
{
aBase: 16,
bBase: 16,
fromDenomination: 'WEI',
toDenomination: 'ETH',
toNumericBase: 'dec',
numberOfDecimals: 6,
},
);
return ethReceived;
2020-10-06 20:28:38 +02:00
}
const txReceiptLogs = txReceipt?.logs;
2020-10-06 20:28:38 +02:00
if (txReceiptLogs && txReceipt?.status !== '0x0') {
const tokenTransferLog = txReceiptLogs.find((txReceiptLog) => {
2020-11-03 00:41:28 +01:00
const isTokenTransfer =
txReceiptLog.topics &&
txReceiptLog.topics[0] === TOKEN_TRANSFER_LOG_TOPIC_HASH;
const isTransferFromGivenToken = txReceiptLog.address === tokenAddress;
2020-11-03 00:41:28 +01:00
const isTransferFromGivenAddress =
txReceiptLog.topics &&
txReceiptLog.topics[2] &&
txReceiptLog.topics[2].match(accountAddress.slice(2));
2020-11-03 00:41:28 +01:00
return (
isTokenTransfer &&
isTransferFromGivenToken &&
isTransferFromGivenAddress
);
});
2020-10-06 20:28:38 +02:00
return tokenTransferLog
? toPrecisionWithoutTrailingZeros(
2020-11-03 00:41:28 +01:00
calcTokenAmount(tokenTransferLog.data, tokenDecimals).toString(10),
6,
)
: '';
2020-10-06 20:28:38 +02:00
}
return null;
2020-10-06 20:28:38 +02:00
}
2020-11-03 00:41:28 +01:00
export function formatSwapsValueForDisplay(destinationAmount) {
let amountToDisplay = toPrecisionWithoutTrailingZeros(destinationAmount, 12);
if (amountToDisplay.match(/e[+-]/u)) {
amountToDisplay = new BigNumber(amountToDisplay).toFixed();
}
return amountToDisplay;
}