Skip to content

Commit

Permalink
code review cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mholtzman committed Feb 25, 2022
1 parent cf68b9d commit 167eedd
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 92 deletions.
4 changes: 4 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,31 @@ 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',
symbol: ''
}
}

const MAX_HEX = '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'

class TokenSpend extends React.Component {
constructor (...args) {
super(...args)
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -19,44 +22,43 @@ class TxApproval extends React.Component {
render () {
const { req, approval, allowOtherChain } = this.props

if (approval) {

if (approval.type === ApprovalType.GasLimitApproval) {
return (
<BasicApproval
req={req}
approval={approval}
onApprove={this.approve}
onDecline={this.decline}
/>
)
}

if (approval.type === ApprovalType.OtherChainApproval) {
if (!allowOtherChain || typeof allowOtherChain !== 'function') throw new Error('OtherChainApproval needs allowOtherChain')
return (
<BasicApproval
req={req}
approval={approval}
onApprove={allowOtherChain}
onDecline={this.decline}
/>
)
}

if (approval.type === ApprovalType.TokenSpendApproval) {
return (
<TokenSpend
req={req}
approval={approval}
onApprove={this.approve}
onDecline={this.decline}
/>
)
}
if (!supportedApprovals.includes(approval)) {
throw new Error(`attempted to create unsupported approval: ${JSON.stringify(approval)}`)
}

if (approval.type === ApprovalType.GasLimitApproval) {
return (
<BasicApproval
req={req}
approval={approval}
onApprove={this.approve}
onDecline={this.decline}
/>
)
}

return null
if (approval.type === ApprovalType.OtherChainApproval) {
if (!allowOtherChain || typeof allowOtherChain !== 'function') throw new Error('OtherChainApproval needs allowOtherChain')
return (
<BasicApproval
req={req}
approval={approval}
onApprove={allowOtherChain}
onDecline={this.decline}
/>
)
}

if (approval.type === ApprovalType.TokenSpendApproval) {
return (
<TokenSpend
req={req}
approval={approval}
onApprove={this.approve}
onDecline={this.decline}
/>
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
8 changes: 1 addition & 7 deletions app/App/Panel/Main/Account/Requests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion app/App/Panel/Main/Account/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
7 changes: 6 additions & 1 deletion main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
Expand Down
5 changes: 2 additions & 3 deletions main/provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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]
Expand All @@ -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()
Expand Down
22 changes: 0 additions & 22 deletions main/tsconfig.json

This file was deleted.

2 changes: 1 addition & 1 deletion test/main/provider/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 167eedd

Please sign in to comment.