From 167eedde5cb428b16ba46848449eff29cc893088 Mon Sep 17 00:00:00 2001 From: Matt Holtzman Date: Fri, 25 Feb 2022 09:33:45 -0500 Subject: [PATCH] code review cleanup --- .npmrc | 4 + .../TxApproval/approvals/TokenSpend/index.js | 32 ++++---- .../TxApproval/approvals/index.js | 4 +- .../TransactionRequest/TxApproval/index.js | 76 ++++++++++--------- .../Requests/TransactionRequest/index.js | 3 - app/App/Panel/Main/Account/Requests/index.js | 8 +- app/App/Panel/Main/Account/index.js | 1 - main/index.js | 7 +- main/provider/index.ts | 5 +- main/tsconfig.json | 22 ------ test/main/provider/index.test.js | 2 +- 11 files changed, 72 insertions(+), 92 deletions(-) delete mode 100644 main/tsconfig.json diff --git a/.npmrc b/.npmrc index f824318dc..67f1b0d52 100644 --- a/.npmrc +++ b/.npmrc @@ -1 +1,5 @@ +# want to set this to true and only allow scripts to be run via the "allowScipts" +# configuration in package.json but electron-builder relies on postinstall scripts +# to build native dependencies so for the time being we provide the "--ignore-scripts" +# CLI parameter on install instead ignore-scripts = false diff --git a/app/App/Panel/Main/Account/Requests/TransactionRequest/TxApproval/approvals/TokenSpend/index.js b/app/App/Panel/Main/Account/Requests/TransactionRequest/TxApproval/approvals/TokenSpend/index.js index 4b05c35f0..05f4393fd 100644 --- a/app/App/Panel/Main/Account/Requests/TransactionRequest/TxApproval/approvals/TokenSpend/index.js +++ b/app/App/Panel/Main/Account/Requests/TransactionRequest/TxApproval/approvals/TokenSpend/index.js @@ -7,20 +7,24 @@ import link from '../../../../../../../../../../resources/link' import { ApprovalType } from '../../../../../../../../../../resources/constants' -const nFormat = (num, digits = 2) => { - num = Number(num) - const lookup = [ - { value: 1, symbol: '' }, - { value: 1e6, symbol: 'million' }, - { value: 1e9, symbol: 'billion' }, - { value: 1e12, symbol: 'trillion' }, - { value: 1e15, symbol: 'quadrillion' }, - { value: 1e18, symbol: 'quintillion' } - ] - const rx = /\.0+$|(\.[0-9]*[1-9])0+$/; - const item = lookup.slice().reverse().find(function(item) { return num >= item.value }) +const numberRegex = /\.0+$|(\.[0-9]*[1-9])0+$/ +const MAX_HEX = '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' + +const digitsLookup = [ + { value: 1, symbol: '' }, + { value: 1e6, symbol: 'million' }, + { value: 1e9, symbol: 'billion' }, + { value: 1e12, symbol: 'trillion' }, + { value: 1e15, symbol: 'quadrillion' }, + { value: 1e18, symbol: 'quintillion' } +] + +function nFormat (n, digits = 2) { + const num = Number(n) + const item = digitsLookup.slice().reverse().find(item => num >= item.value) + return item ? { - number: (num / item.value).toFixed(digits).replace(rx, '$1'), + number: (num / item.value).toFixed(digits).replace(numberRegex, '$1'), symbol: item.symbol } : { number: '0', @@ -28,8 +32,6 @@ const nFormat = (num, digits = 2) => { } } -const MAX_HEX = '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' - class TokenSpend extends React.Component { constructor (...args) { super(...args) diff --git a/app/App/Panel/Main/Account/Requests/TransactionRequest/TxApproval/approvals/index.js b/app/App/Panel/Main/Account/Requests/TransactionRequest/TxApproval/approvals/index.js index 6d306b5e2..f328ebe08 100644 --- a/app/App/Panel/Main/Account/Requests/TransactionRequest/TxApproval/approvals/index.js +++ b/app/App/Panel/Main/Account/Requests/TransactionRequest/TxApproval/approvals/index.js @@ -1,2 +1,2 @@ -export {default as BasicApproval} from './BasicApproval' -export {default as TokenSpend } from './TokenSpend' +export { default as BasicApproval } from './BasicApproval' +export { default as TokenSpend } from './TokenSpend' diff --git a/app/App/Panel/Main/Account/Requests/TransactionRequest/TxApproval/index.js b/app/App/Panel/Main/Account/Requests/TransactionRequest/TxApproval/index.js index 0c5b96810..8097a8102 100644 --- a/app/App/Panel/Main/Account/Requests/TransactionRequest/TxApproval/index.js +++ b/app/App/Panel/Main/Account/Requests/TransactionRequest/TxApproval/index.js @@ -6,8 +6,11 @@ import { ApprovalType } from '../../../../../../../../resources/constants' import { BasicApproval, TokenSpend } from './approvals' -class TxApproval extends React.Component { +const supportedApprovals = [ + ApprovalType.GasLimitApproval, ApprovalType.OtherChainApproval, ApprovalType.TokenSpendApproval +] +class TxApproval extends React.Component { approve (req, type, data = {}, cb = () => {}) { link.rpc('confirmRequestApproval', req, type, data, cb) } @@ -19,44 +22,43 @@ class TxApproval extends React.Component { render () { const { req, approval, allowOtherChain } = this.props - if (approval) { - - if (approval.type === ApprovalType.GasLimitApproval) { - return ( - - ) - } - - if (approval.type === ApprovalType.OtherChainApproval) { - if (!allowOtherChain || typeof allowOtherChain !== 'function') throw new Error('OtherChainApproval needs allowOtherChain') - return ( - - ) - } - - if (approval.type === ApprovalType.TokenSpendApproval) { - return ( - - ) - } + if (!supportedApprovals.includes(approval)) { + throw new Error(`attempted to create unsupported approval: ${JSON.stringify(approval)}`) + } + + if (approval.type === ApprovalType.GasLimitApproval) { + return ( + + ) } - return null + if (approval.type === ApprovalType.OtherChainApproval) { + if (!allowOtherChain || typeof allowOtherChain !== 'function') throw new Error('OtherChainApproval needs allowOtherChain') + return ( + + ) + } + + if (approval.type === ApprovalType.TokenSpendApproval) { + return ( + + ) + } } } diff --git a/app/App/Panel/Main/Account/Requests/TransactionRequest/index.js b/app/App/Panel/Main/Account/Requests/TransactionRequest/index.js index 8308b8ba3..7a77d2cb1 100644 --- a/app/App/Panel/Main/Account/Requests/TransactionRequest/index.js +++ b/app/App/Panel/Main/Account/Requests/TransactionRequest/index.js @@ -11,9 +11,6 @@ import link from '../../../../../../../resources/link' import TxBar from './TxBar' -// import TxFee from './TxFee' - - // New Tx import TxMain from './TxMain' import TxFeeNew from './TxFeeNew' diff --git a/app/App/Panel/Main/Account/Requests/index.js b/app/App/Panel/Main/Account/Requests/index.js index 8fc2cf59e..71f5478b0 100644 --- a/app/App/Panel/Main/Account/Requests/index.js +++ b/app/App/Panel/Main/Account/Requests/index.js @@ -87,15 +87,9 @@ class Requests extends React.Component { render () { const activeAccount = this.store('main.accounts', this.props.id) + const requests = Object.values(activeAccount.requests || {}) const signingDelay = isHardwareSigner(activeAccount) ? 200 : 1500 - let requests = activeAccount.requests || {} - requests = Object.keys(requests).map(key => requests[key]) - // .filter(req => { - // if (req.type === 'transaction') return this.props.addresses.map(a => a.toLowerCase()).indexOf(req && req.data ? req.data.from.toLowerCase() : null) > -1 - // return true - // }) - // transitionName='slideUp' transitionEnterTimeout={960} transitionLeaveTimeout={640} const normal = requests.filter(req => req.mode === 'normal') normal.sort((a, b) => { if (a.created > b.created) return -1 diff --git a/app/App/Panel/Main/Account/index.js b/app/App/Panel/Main/Account/index.js index bdccd394b..fbbf94300 100644 --- a/app/App/Panel/Main/Account/index.js +++ b/app/App/Panel/Main/Account/index.js @@ -847,7 +847,6 @@ class Account extends React.Component { // const status = this.props.status.charAt(0).toUpperCase() + this.props.status.substr(1) // if (this.state.accountHighlight === 'active') currentIndex = this.state.highlightIndex - // TODO: use active to render currently active account const { address, ensName, active } = this.store('main.accounts', this.props.id) const formattedAddress = address || '0x' diff --git a/main/index.js b/main/index.js index fc6828454..e83eb0f1c 100644 --- a/main/index.js +++ b/main/index.js @@ -170,12 +170,17 @@ ipcMain.on('tray:switchChain', (e, type, id, req) => { }) ipcMain.on('tray:addToken', (e, token, req) => { - if (token) store.addCustomTokens([token]) + if (token) { + log.info('adding custom token', token) + store.addCustomTokens([token]) + } accounts.resolveRequest(req) }) ipcMain.on('tray:removeToken', (e, token) => { if (token) { + log.info('removing custom token', token) + store.removeBalance(token.chainId, token.address) store.removeCustomTokens([token]) } diff --git a/main/provider/index.ts b/main/provider/index.ts index f7f9d74cb..68e287f6b 100644 --- a/main/provider/index.ts +++ b/main/provider/index.ts @@ -31,7 +31,6 @@ import { getType as getSignerType, Type as SignerType } from '../signers/Signer' import { populate as populateTransaction, usesBaseFee, maxFee, TransactionData } from '../transaction' import FrameAccount from '../accounts/Account' import { capitalize, arraysMatch } from '../../resources/utils' -import { Approval } from '../accounts/types' import { ApprovalType } from '../../resources/constants' const NATIVE_CURRENCY = '0x0000000000000000000000000000000000000000' @@ -564,7 +563,7 @@ export class Provider extends EventEmitter { log.error('error creating transaction', e) cb(e as Error) } - } + } sendTransaction (payload: RPC.SendTransaction.Request, res: RPCRequestCallback) { const txParams = payload.params[0] @@ -577,7 +576,7 @@ export class Provider extends EventEmitter { const newTx = { ...txParams, - chainId: (txChain || targetChain) as string + chainId: txChain || (targetChain as string) } const currentAccount = accounts.current() diff --git a/main/tsconfig.json b/main/tsconfig.json deleted file mode 100644 index 27c24ef9c..000000000 --- a/main/tsconfig.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "compilerOptions": { - "target": "es2021", - "module": "commonjs", - "lib": ["es2021", "dom"], - "strict": true, - "esModuleInterop": true, - "skipLibCheck": true, - "forceConsistentCasingInFileNames": true, - "resolveJsonModule": true, - "allowJs": true, - - "rootDir": ".", - "outDir": "../compiled", - "typeRoots": ["../node_modules/@types", "../@types"] - }, - "include": ["./**/*.ts", "./**/*.js"], - "exclude": ["**/__mocks__/*"], - "references": [ - { "path": ".." } - ] -} diff --git a/test/main/provider/index.test.js b/test/main/provider/index.test.js index d9f074917..68c91271d 100644 --- a/test/main/provider/index.test.js +++ b/test/main/provider/index.test.js @@ -3,7 +3,7 @@ import accounts from '../../../main/accounts' import connection from '../../../main/chains' import store from '../../../main/store' import chainConfig from '../../../main/chains/config' -import { weiToHex, gweiToHex, capitalize } from '../../../resources/utils' +import { weiToHex, gweiToHex } from '../../../resources/utils' import { Type as SignerType } from '../../../main/signers/Signer' import { validate as validateUUID } from 'uuid'