2021-08-31 01:01:59 +02:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import { Provider } from 'react-redux';
|
|
|
|
import configureStore from '../../../store/store';
|
|
|
|
import testData from '../../../../.storybook/test-data';
|
|
|
|
import { formatETHFee } from '../../../helpers/utils/formatters';
|
|
|
|
import {
|
|
|
|
addHexes,
|
|
|
|
getValueFromWeiHex,
|
|
|
|
getWeiHexFromDecimalValue,
|
|
|
|
sumHexWEIsToRenderableFiat,
|
|
|
|
} from '../../../helpers/utils/conversions.util';
|
|
|
|
import { ETH } from '../../../helpers/constants/common';
|
|
|
|
import { calcGasTotal, isBalanceSufficient } from '../../send/send.utils';
|
|
|
|
import { conversionLessThan } from '../../../../shared/modules/conversion.utils';
|
|
|
|
import GasModalPageContainer from './swaps-gas-customization-modal.component';
|
|
|
|
// Using Test Data For Redux
|
|
|
|
const store = configureStore(testData);
|
|
|
|
|
|
|
|
export default {
|
2021-12-01 20:27:57 +01:00
|
|
|
title: 'Pages/Swaps/GasModalPageContainer',
|
2021-09-15 20:55:48 +02:00
|
|
|
id: __filename,
|
2022-03-30 15:46:37 +02:00
|
|
|
component: GasModalPageContainer,
|
|
|
|
argTypes: {
|
|
|
|
sendAmountArg: {
|
|
|
|
name: 'Send Amount (this should be static)',
|
|
|
|
control: { type: 'number', min: 0, step: 0.01 },
|
|
|
|
},
|
|
|
|
walletBalance: {
|
|
|
|
name: 'Wallet Balance (this should be static)',
|
|
|
|
control: { type: 'number', min: 0, step: 0.01 },
|
|
|
|
},
|
|
|
|
averageGasPrice: {
|
|
|
|
name: 'Average Gas Price',
|
|
|
|
control: { type: 'number', min: 0, step: 0.01 },
|
|
|
|
},
|
|
|
|
insufficientBalance: {
|
|
|
|
table: {
|
|
|
|
disable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
gasPriceButtonGroupProps: {
|
|
|
|
table: {
|
|
|
|
disable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
infoRowProps: {
|
|
|
|
table: {
|
|
|
|
disable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
onSubmit: {
|
|
|
|
table: {
|
|
|
|
disable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
cancelAndClose: {
|
|
|
|
table: {
|
|
|
|
disable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
showCustomPriceTooLowWarning: {
|
|
|
|
table: {
|
|
|
|
disable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
disableSave: {
|
|
|
|
table: {
|
|
|
|
disable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
customGasLimitMessage: {
|
|
|
|
table: {
|
|
|
|
disable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
usdConversionRate: {
|
|
|
|
table: {
|
|
|
|
disable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
customGasPrice: {
|
|
|
|
table: {
|
|
|
|
disable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
customGasLimit: {
|
|
|
|
table: {
|
|
|
|
disable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
setSwapsCustomizationModalLimit: {
|
|
|
|
table: {
|
|
|
|
disable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
setSwapsCustomizationModalPrice: {
|
|
|
|
table: {
|
|
|
|
disable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
customTotalSupplement: {
|
|
|
|
table: {
|
|
|
|
disable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
gasEstimateLoadingHasFailed: {
|
|
|
|
table: {
|
|
|
|
disable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
minimumGasLimit: {
|
|
|
|
table: {
|
|
|
|
disable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: {
|
|
|
|
sendAmountArg: 0.01,
|
|
|
|
walletBalance: 10,
|
|
|
|
averageGasPrice: 2,
|
|
|
|
},
|
2021-08-31 01:01:59 +02:00
|
|
|
decorators: [(story) => <Provider store={store}>{story()}</Provider>],
|
|
|
|
};
|
|
|
|
|
|
|
|
// Convert Hex Wei to Decimal ETH
|
|
|
|
const sumHexWEIsToRenderableEth = (hexWEIs, currencySymbol = 'ETH') => {
|
|
|
|
const hexWEIsSum = hexWEIs.filter(Boolean).reduce(addHexes);
|
|
|
|
return formatETHFee(
|
|
|
|
getValueFromWeiHex({
|
|
|
|
value: hexWEIsSum,
|
|
|
|
fromCurrency: currencySymbol,
|
|
|
|
toCurrency: currencySymbol,
|
|
|
|
numberOfDecimals: 6,
|
|
|
|
}),
|
|
|
|
currencySymbol,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-03-30 15:46:37 +02:00
|
|
|
export const DefaultStory = (args) => {
|
|
|
|
const { sendAmountArg, walletBalance, averageGasPrice } = args;
|
|
|
|
|
2021-08-31 01:01:59 +02:00
|
|
|
const hexWei = getWeiHexFromDecimalValue({
|
2022-03-30 15:46:37 +02:00
|
|
|
value: sendAmountArg,
|
2021-08-31 01:01:59 +02:00
|
|
|
fromCurrency: ETH,
|
|
|
|
fromDenomination: ETH,
|
|
|
|
});
|
|
|
|
|
|
|
|
// ETH Balance
|
|
|
|
const balanceHexWei = getWeiHexFromDecimalValue({
|
2022-03-30 15:46:37 +02:00
|
|
|
value: walletBalance,
|
2021-08-31 01:01:59 +02:00
|
|
|
fromCurrency: ETH,
|
|
|
|
fromDenomination: ETH,
|
|
|
|
});
|
|
|
|
|
|
|
|
const sendAmount = sumHexWEIsToRenderableEth([hexWei, '0x0']);
|
|
|
|
const [gasLimit, setGasLimit] = useState('5208');
|
|
|
|
const [gasPrice, setGasPrice] = useState('ee6b2800');
|
|
|
|
const [transactionFee, setTransactionFee] = useState('');
|
|
|
|
const [totalETH, setTotalETH] = useState('');
|
|
|
|
const [totalFiat, setTotalFiat] = useState('');
|
|
|
|
const [isInsufficientBalance, setIsInsufficientBalance] = useState(false);
|
|
|
|
|
|
|
|
const { metamask } = store.getState();
|
|
|
|
const { currentCurrency, conversionRate } = metamask;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
// Transfer Fee
|
|
|
|
const customGasTotal = calcGasTotal(gasLimit, gasPrice);
|
|
|
|
setTransactionFee(sumHexWEIsToRenderableEth(['0x0', customGasTotal]));
|
|
|
|
|
|
|
|
// New Total ETH
|
|
|
|
setTotalETH(sumHexWEIsToRenderableEth([hexWei, customGasTotal, '']));
|
|
|
|
|
|
|
|
// New Total Fiat
|
|
|
|
setTotalFiat(
|
|
|
|
sumHexWEIsToRenderableFiat(
|
|
|
|
[hexWei, customGasTotal, ''],
|
|
|
|
currentCurrency,
|
|
|
|
conversionRate,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check If Balance is Sufficient
|
|
|
|
setIsInsufficientBalance(
|
|
|
|
!isBalanceSufficient({
|
|
|
|
amount: hexWei,
|
|
|
|
gasTotal: customGasTotal,
|
|
|
|
balance: balanceHexWei,
|
|
|
|
conversionRate,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}, [
|
|
|
|
gasPrice,
|
|
|
|
gasLimit,
|
|
|
|
hexWei,
|
|
|
|
totalETH,
|
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
|
|
|
totalFiat,
|
|
|
|
balanceHexWei,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Check If Gas Price Is Too Low
|
|
|
|
const shouldShowCustomPriceTooLowWarning = () => {
|
|
|
|
const customGasPrice = gasPrice;
|
|
|
|
|
2022-03-30 15:46:37 +02:00
|
|
|
if (!customGasPrice || averageGasPrice === undefined) {
|
2021-08-31 01:01:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const customPriceRisksSwapFailure = conversionLessThan(
|
|
|
|
{
|
|
|
|
value: customGasPrice,
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
fromDenomination: 'WEI',
|
|
|
|
toDenomination: 'GWEI',
|
|
|
|
},
|
2022-03-30 15:46:37 +02:00
|
|
|
{ value: averageGasPrice, fromNumericBase: 'dec' },
|
2021-08-31 01:01:59 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return customPriceRisksSwapFailure;
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<GasModalPageContainer
|
|
|
|
insufficientBalance={isInsufficientBalance}
|
|
|
|
showCustomPriceTooLowWarning={shouldShowCustomPriceTooLowWarning()}
|
|
|
|
minimumGasLimit={21000}
|
|
|
|
infoRowProps={{
|
|
|
|
sendAmount,
|
|
|
|
transactionFee,
|
|
|
|
newTotalEth: totalETH,
|
|
|
|
newTotalFiat: totalFiat,
|
|
|
|
}}
|
|
|
|
gasPriceButtonGroupProps={{
|
|
|
|
buttonDataLoading: true,
|
|
|
|
}}
|
|
|
|
setSwapsCustomizationModalPrice={(newPrice) => {
|
|
|
|
setGasPrice(newPrice);
|
|
|
|
}}
|
|
|
|
setSwapsCustomizationModalLimit={(newLimit) => {
|
|
|
|
setGasLimit(newLimit);
|
|
|
|
}}
|
|
|
|
customGasPrice={gasPrice}
|
|
|
|
customGasLimit={gasLimit}
|
|
|
|
disableSave={
|
|
|
|
isInsufficientBalance || shouldShowCustomPriceTooLowWarning()
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
2021-12-01 20:27:57 +01:00
|
|
|
|
|
|
|
DefaultStory.storyName = 'Default';
|