From f397b666d81c4caa69a8cc8960ae284302c5a0d9 Mon Sep 17 00:00:00 2001 From: Sergei Novikov Date: Thu, 14 Dec 2023 14:28:11 +0300 Subject: [PATCH] solution: reorganize components --- .../create-tx/CreateErc20ApproveTx.ts | 2 +- .../create-tx/CreateErc20ConvertTx.ts | 7 +- .../src/app/screen/Screen/Screen.tsx | 8 +- .../common/AmountField/AmountField.spec.tsx | 41 -- .../src/common/AmountField/AmountField.tsx | 136 ---- .../react-app/src/common/AmountField/index.ts | 1 - .../src/common/BtcConfirm/BtcConfirm.tsx | 82 --- .../react-app/src/common/BtcConfirm/RawTx.tsx | 38 - .../src/common/BtcConfirm/RawTxDetails.tsx | 146 ---- .../react-app/src/common/BtcConfirm/index.ts | 1 - .../BroadcastEthTx/BroadcastEthTx.spec.tsx | 75 -- .../BroadcastEthTx/BroadcastEthTx.tsx | 168 ----- .../src/transaction/BroadcastEthTx/index.ts | 1 - .../BroadcastTransaction.tsx | 0 .../display/blockchain/bitcoin.tsx | 0 .../display/blockchain/ethereum.tsx | 0 .../display/blockchain/index.ts | 0 .../BroadcastTransaction/display/common.tsx | 0 .../display/components/Actions.tsx | 0 .../display/components/BitcoinDecoded.tsx | 0 .../display/components/EthereumDecoded.tsx | 0 .../display/components/RawTx.tsx | 0 .../display/components/index.ts | 0 .../BroadcastTransaction/display/display.ts | 0 .../BroadcastTransaction/display/index.ts | 0 .../BroadcastTransaction/display/types.ts | 0 .../BroadcastTransaction/index.ts | 0 .../CreateRecoverTransaction.tsx | 662 ------------------ .../CreateRecoverTransaction/index.ts | 1 - .../CreateTransaction.tsx | 0 .../SetupTransaction/SetupTransaction.tsx | 2 +- .../flow/blockchain/bitcoin/index.ts | 0 .../flow/blockchain/bitcoin/modify/cancel.tsx | 0 .../flow/blockchain/bitcoin/modify/index.ts | 0 .../flow/blockchain/bitcoin/modify/modify.tsx | 2 +- .../blockchain/bitcoin/modify/speedup.tsx | 0 .../flow/blockchain/bitcoin/transfer.tsx | 0 .../blockchain/ethereum/erc20/approve.tsx | 6 +- .../blockchain/ethereum/erc20/convert.tsx | 2 +- .../flow/blockchain/ethereum/erc20/index.ts | 0 .../flow/blockchain/ethereum/index.ts | 0 .../blockchain/ethereum/modify/cancel.tsx | 0 .../flow/blockchain/ethereum/modify/index.ts | 0 .../blockchain/ethereum/modify/modify.tsx | 2 +- .../blockchain/ethereum/modify/speedup.tsx | 0 .../flow/blockchain/ethereum/transfer.tsx | 2 +- .../SetupTransaction/flow/blockchain/index.ts | 0 .../SetupTransaction/flow/common/index.ts | 0 .../SetupTransaction/flow/common/modify.tsx | 0 .../SetupTransaction/flow/common/transfer.tsx | 2 +- .../flow/components/Actions.tsx | 0 .../flow/components/Amount.tsx | 12 +- .../flow/components/ApproveAmount.tsx | 40 +- .../flow/components/BitcoinFee.tsx | 0 .../flow/components/EthereumFee.tsx | 2 +- .../SetupTransaction/flow/components/To.tsx | 2 +- .../SetupTransaction/flow/components/index.ts | 0 .../SetupTransaction/flow/flow.tsx | 0 .../SetupTransaction/flow/index.ts | 0 .../SetupTransaction/flow/types.ts | 2 +- .../SetupTransaction/index.ts | 0 .../SignTransaction/SignTransaction.tsx | 0 .../display/blockchain/bitcoin.tsx | 0 .../blockchain/ethereum/erc20/approve.tsx | 0 .../blockchain/ethereum/erc20/convert.tsx | 0 .../blockchain/ethereum/erc20/index.ts | 0 .../display/blockchain/ethereum/ethereum.tsx | 0 .../display/blockchain/ethereum/index.ts | 0 .../display/blockchain/index.ts | 0 .../SignTransaction/display/common.tsx | 0 .../display/components/Actions.tsx | 2 +- .../display/components/AddressPreview.tsx | 0 .../display/components/index.ts | 0 .../SignTransaction/display/display.ts | 0 .../SignTransaction/display/index.ts | 0 .../SignTransaction/display/types.ts | 0 .../SignTransaction/index.ts | 0 .../{CreateTransaction => }/index.ts | 0 .../WalletDetails/entry/EthereumEntryItem.tsx | 13 +- packages/store/src/screen/types.ts | 2 - 80 files changed, 64 insertions(+), 1398 deletions(-) delete mode 100644 packages/react-app/src/common/AmountField/AmountField.spec.tsx delete mode 100644 packages/react-app/src/common/AmountField/AmountField.tsx delete mode 100644 packages/react-app/src/common/AmountField/index.ts delete mode 100644 packages/react-app/src/common/BtcConfirm/BtcConfirm.tsx delete mode 100644 packages/react-app/src/common/BtcConfirm/RawTx.tsx delete mode 100644 packages/react-app/src/common/BtcConfirm/RawTxDetails.tsx delete mode 100644 packages/react-app/src/common/BtcConfirm/index.ts delete mode 100644 packages/react-app/src/transaction/BroadcastEthTx/BroadcastEthTx.spec.tsx delete mode 100644 packages/react-app/src/transaction/BroadcastEthTx/BroadcastEthTx.tsx delete mode 100644 packages/react-app/src/transaction/BroadcastEthTx/index.ts rename packages/react-app/src/transaction/{CreateTransaction => }/BroadcastTransaction/BroadcastTransaction.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/BroadcastTransaction/display/blockchain/bitcoin.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/BroadcastTransaction/display/blockchain/ethereum.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/BroadcastTransaction/display/blockchain/index.ts (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/BroadcastTransaction/display/common.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/BroadcastTransaction/display/components/Actions.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/BroadcastTransaction/display/components/BitcoinDecoded.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/BroadcastTransaction/display/components/EthereumDecoded.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/BroadcastTransaction/display/components/RawTx.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/BroadcastTransaction/display/components/index.ts (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/BroadcastTransaction/display/display.ts (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/BroadcastTransaction/display/index.ts (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/BroadcastTransaction/display/types.ts (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/BroadcastTransaction/index.ts (100%) delete mode 100644 packages/react-app/src/transaction/CreateRecoverTransaction/CreateRecoverTransaction.tsx delete mode 100644 packages/react-app/src/transaction/CreateRecoverTransaction/index.ts rename packages/react-app/src/transaction/{CreateTransaction => }/CreateTransaction.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/SetupTransaction.tsx (99%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/blockchain/bitcoin/index.ts (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/blockchain/bitcoin/modify/cancel.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/blockchain/bitcoin/modify/index.ts (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/blockchain/bitcoin/modify/modify.tsx (95%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/blockchain/bitcoin/modify/speedup.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/blockchain/bitcoin/transfer.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/blockchain/ethereum/erc20/approve.tsx (95%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/blockchain/ethereum/erc20/convert.tsx (98%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/blockchain/ethereum/erc20/index.ts (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/blockchain/ethereum/index.ts (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/blockchain/ethereum/modify/cancel.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/blockchain/ethereum/modify/index.ts (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/blockchain/ethereum/modify/modify.tsx (95%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/blockchain/ethereum/modify/speedup.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/blockchain/ethereum/transfer.tsx (96%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/blockchain/index.ts (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/common/index.ts (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/common/modify.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/common/transfer.tsx (97%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/components/Actions.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/components/Amount.tsx (81%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/components/ApproveAmount.tsx (67%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/components/BitcoinFee.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/components/EthereumFee.tsx (97%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/components/To.tsx (92%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/components/index.ts (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/flow.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/index.ts (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/flow/types.ts (96%) rename packages/react-app/src/transaction/{CreateTransaction => }/SetupTransaction/index.ts (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SignTransaction/SignTransaction.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SignTransaction/display/blockchain/bitcoin.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SignTransaction/display/blockchain/ethereum/erc20/approve.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SignTransaction/display/blockchain/ethereum/erc20/convert.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SignTransaction/display/blockchain/ethereum/erc20/index.ts (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SignTransaction/display/blockchain/ethereum/ethereum.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SignTransaction/display/blockchain/ethereum/index.ts (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SignTransaction/display/blockchain/index.ts (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SignTransaction/display/common.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SignTransaction/display/components/Actions.tsx (98%) rename packages/react-app/src/transaction/{CreateTransaction => }/SignTransaction/display/components/AddressPreview.tsx (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SignTransaction/display/components/index.ts (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SignTransaction/display/display.ts (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SignTransaction/display/index.ts (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SignTransaction/display/types.ts (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/SignTransaction/index.ts (100%) rename packages/react-app/src/transaction/{CreateTransaction => }/index.ts (100%) diff --git a/packages/core/src/transaction/workflow/create-tx/CreateErc20ApproveTx.ts b/packages/core/src/transaction/workflow/create-tx/CreateErc20ApproveTx.ts index 97e397b02..0e625fe65 100644 --- a/packages/core/src/transaction/workflow/create-tx/CreateErc20ApproveTx.ts +++ b/packages/core/src/transaction/workflow/create-tx/CreateErc20ApproveTx.ts @@ -174,7 +174,7 @@ export class CreateErc20ApproveTx implements Erc20ApproveTxDetails { if (TokenAmount.is(value)) { this._amount = value; } else { - this._amount = this.token.getAmount(value); + this._amount = this.token.getAmount(1).multiply(this.token.getUnits().top.multiplier).multiply(value); } this._target = ApproveTarget.MANUAL; diff --git a/packages/core/src/transaction/workflow/create-tx/CreateErc20ConvertTx.ts b/packages/core/src/transaction/workflow/create-tx/CreateErc20ConvertTx.ts index 291237503..e09469bcd 100644 --- a/packages/core/src/transaction/workflow/create-tx/CreateErc20ConvertTx.ts +++ b/packages/core/src/transaction/workflow/create-tx/CreateErc20ConvertTx.ts @@ -1,4 +1,4 @@ -import { BigAmount, CreateAmount } from '@emeraldpay/bigamount'; +import { BigAmount, CreateAmount, Units } from '@emeraldpay/bigamount'; import { WeiAny } from '@emeraldpay/bigamount-crypto'; import BigNumber from 'bignumber.js'; import { @@ -156,13 +156,14 @@ export class CreateErc20ConvertTx implements Erc20ConvertTxDetails { } else { const { asset, token } = this; - const { units } = this.amount; - let amount: BigAmount; + let units: Units; if (asset.toLowerCase() === token.address.toLowerCase()) { amount = token.getAmount(1); + units = token.getUnits(); } else { + units = this.amount.units; amount = new WeiAny(1, units); } diff --git a/packages/react-app/src/app/screen/Screen/Screen.tsx b/packages/react-app/src/app/screen/Screen/Screen.tsx index 565765d09..4536758c0 100644 --- a/packages/react-app/src/app/screen/Screen/Screen.tsx +++ b/packages/react-app/src/app/screen/Screen/Screen.tsx @@ -13,9 +13,7 @@ import ShowMessage from '../../../message/ShowMessage'; import SignMessage from '../../../message/SignMessage'; import ReceiveScreen from '../../../receive/ReceiveScreen'; import Settings from '../../../settings/Settings'; -import { BroadcastEthTx } from '../../../transaction/BroadcastEthTx'; -import CreateRecoverTransaction from '../../../transaction/CreateRecoverTransaction'; -import { CreateTransaction } from '../../../transaction/CreateTransaction'; +import { CreateTransaction } from '../../../transaction'; import TxDetails from '../../../transactions/TxDetails'; import WalletDetails from '../../../wallets/WalletDetails'; import WalletInfo from '../../../wallets/WalletInfo'; @@ -57,12 +55,8 @@ const Screen: React.FC = ({ restoreData, screenItem, term return ; case screen.Pages.ADDRESS_BOOK: return ; - case screen.Pages.BROADCAST_TX: - return ; case screen.Pages.CREATE_TX: return ; - case screen.Pages.CREATE_TX_RECOVER: - return ; case screen.Pages.CREATE_WALLET: return ; case screen.Pages.EDIT_ADDRESS: diff --git a/packages/react-app/src/common/AmountField/AmountField.spec.tsx b/packages/react-app/src/common/AmountField/AmountField.spec.tsx deleted file mode 100644 index b9924579b..000000000 --- a/packages/react-app/src/common/AmountField/AmountField.spec.tsx +++ /dev/null @@ -1,41 +0,0 @@ -import { WEIS, Wei } from '@emeraldpay/bigamount-crypto'; -import { Theme } from '@emeraldwallet/ui'; -import { ThemeProvider } from '@material-ui/core'; -import { mount, shallow } from 'enzyme'; -import * as React from 'react'; -import { Provider } from 'react-redux'; -import { createTestStore } from '../../testStore'; -import AmountField from './AmountField'; - -describe('AmountField', () => { - it('should renders without crash', () => { - const wrapper = shallow( - - - - - , - ); - - expect(wrapper).toBeDefined(); - }); - - it('should call onChangeAmount', () => { - const onChangeAmount = jest.fn(); - - const wrapper = mount( - - - - - , - ); - - wrapper.find('input').simulate('change', { target: { value: '567' } }); - - expect(onChangeAmount.mock.calls.length).toBe(1); - expect(typeof onChangeAmount.mock.calls[0][0]).toBe('object'); - expect(onChangeAmount.mock.calls[0][0].toString()).toBe('567 ETH'); - expect(onChangeAmount.mock.calls[0][0]).toEqual(new Wei('567', 'ETHER')); - }); -}); diff --git a/packages/react-app/src/common/AmountField/AmountField.tsx b/packages/react-app/src/common/AmountField/AmountField.tsx deleted file mode 100644 index aca58f7cf..000000000 --- a/packages/react-app/src/common/AmountField/AmountField.tsx +++ /dev/null @@ -1,136 +0,0 @@ -import { BigAmount, Units } from '@emeraldpay/bigamount'; -import { formatAmountValue } from '@emeraldwallet/core'; -import { Button, Input } from '@emeraldwallet/ui'; -import * as React from 'react'; -import { connect } from 'react-redux'; - -interface OwnProps { - amount?: BigAmount; - disabled?: boolean; - maxAmount?: BigAmount; - maxDisabled?: boolean; - fieldWidth?: number; - units: Units; - onChangeAmount(value: BigAmount): void; - onMaxClick(callback: (value: BigAmount) => void): void; -} - -interface DispatchProps { - onClear(): void; -} - -const AmountField: React.FC = ({ - amount, - maxAmount, - units, - disabled = false, - maxDisabled = false, - fieldWidth = 440, - onChangeAmount, - onClear, - onMaxClick, -}) => { - const changed = React.useRef(false); - - const [currentAmount, setCurrentAmount] = React.useState(amount == null ? '0' : formatAmountValue(amount)); - const [currentMaxAmount, setCurrentMaxAmount] = React.useState( - maxAmount == null ? null : formatAmountValue(maxAmount), - ); - - const [errorText, setErrorText] = React.useState(null); - - const handleAmountChange = ({ target: { value } }: React.ChangeEvent): void => { - const newAmount = value.trim(); - - changed.current = true; - - setCurrentAmount(newAmount); - - if (newAmount === '') { - setErrorText('Required'); - - onClear(); - - return; - } - - const valid = newAmount.match(/^\d*(\.\d+)?$/); - - if (!valid) { - setErrorText('Invalid number'); - - onClear(); - - return; - } - - try { - const parsed = parseFloat(newAmount); - - if (parsed < 0) { - setErrorText('Value must be positive number'); - - onClear(); - } else { - setErrorText(null); - - // Relative to the main unit. I.e. "20" should be 20 Ether, not 20 Wei - onChangeAmount(new BigAmount(1, units).multiply(units.top.multiplier).multiply(parsed)); - } - } catch (exception) { - setErrorText('Invalid value'); - - onClear(); - } - }; - - const handleMaxClick = (): void => { - onMaxClick((value) => { - const newAmount = formatAmountValue(value); - - changed.current = true; - - setCurrentAmount(newAmount); - setCurrentMaxAmount(newAmount); - - setErrorText(null); - }); - }; - - React.useEffect(() => { - if (!changed.current && amount != null) { - const newAmount = formatAmountValue(amount); - - if (newAmount !== currentAmount) { - setCurrentAmount(newAmount); - } - } - }, [amount, currentAmount]); - - return ( -
- - } - value={currentAmount} - onChange={handleAmountChange} - /> -
- ); -}; - -export default connect(null, (dispatch, { units, onChangeAmount }) => ({ - onClear() { - onChangeAmount(new BigAmount(0, units)); - }, -}))(AmountField); diff --git a/packages/react-app/src/common/AmountField/index.ts b/packages/react-app/src/common/AmountField/index.ts deleted file mode 100644 index 137ed9621..000000000 --- a/packages/react-app/src/common/AmountField/index.ts +++ /dev/null @@ -1 +0,0 @@ -export { default as AmountField } from './AmountField'; diff --git a/packages/react-app/src/common/BtcConfirm/BtcConfirm.tsx b/packages/react-app/src/common/BtcConfirm/BtcConfirm.tsx deleted file mode 100644 index ec999deca..000000000 --- a/packages/react-app/src/common/BtcConfirm/BtcConfirm.tsx +++ /dev/null @@ -1,82 +0,0 @@ -import { EntryId, EntryIdOp } from '@emeraldpay/emerald-vault-core'; -import { BlockchainCode } from '@emeraldwallet/core'; -import { screen } from '@emeraldwallet/store'; -import { Button, ButtonGroup, FormLabel, FormRow } from '@emeraldwallet/ui'; -import { createStyles } from '@material-ui/core'; -import { makeStyles } from '@material-ui/core/styles'; -import * as React from 'react'; -import { connect } from 'react-redux'; -import RawTx from './RawTx'; -import RawTxDetails from './RawTxDetails'; - -const useStyles = makeStyles( - createStyles({ - buttons: { - display: 'flex', - justifyContent: 'end', - width: '100%', - }, - }), -); - -interface OwnProps { - blockchain: BlockchainCode; - disabled?: boolean; - entryId: EntryId; - rawTx: string; - onConfirm(): void; -} - -interface DispatchProps { - onCancel(): void; -} - -const BtcConfirm: React.FC = ({ - entryId, - blockchain, - rawTx, - onCancel, - onConfirm, - disabled = false, -}) => { - const styles = useStyles(); - - const [showRaw, setShowRaw] = React.useState(false); - - return ( - <> - - {showRaw ? ( - <> - - Raw Tx - - - - -