From 80fa9cfe210a13c5a5c69940c0d43c5097f85371 Mon Sep 17 00:00:00 2001 From: Szymon Lesisz Date: Fri, 11 May 2018 18:29:27 +0200 Subject: [PATCH] BigNumber.js flowtype + removed ./utils/reducerUtils --- src/js/actions/SendFormActions.js | 9 ++---- src/js/actions/TrezorConnectActions.js | 1 - src/js/actions/Web3Actions.js | 23 +++++++------- .../wallet/account/AbstractAccount.js | 6 ++-- .../wallet/aside/AccountSelection.js | 4 +-- src/js/components/wallet/summary/Summary.js | 1 - src/js/flowtype/bignumber.js | 30 ++++++++++--------- src/js/reducers/AccountsReducer.js | 8 +++++ src/js/reducers/TrezorConnectReducer.js | 7 +++-- src/js/utils/ethUtils.js | 7 +++-- src/js/utils/reducerUtils.js | 18 ----------- 11 files changed, 51 insertions(+), 63 deletions(-) delete mode 100644 src/js/utils/reducerUtils.js diff --git a/src/js/actions/SendFormActions.js b/src/js/actions/SendFormActions.js index 5fecb4c1..eff65ed3 100644 --- a/src/js/actions/SendFormActions.js +++ b/src/js/actions/SendFormActions.js @@ -194,10 +194,7 @@ export const init = (): ThunkAction => { if (!selected) return; const web3instance: ?Web3Instance = getState().web3.find(w3 => w3.network === urlParams.network); - if (!web3instance) { - // no backend for this network - return; - } + if (!web3instance) return; // TODO: check if there are some unfinished tx in localStorage @@ -269,11 +266,11 @@ export const validation = (): ThunkAction => { // corner-case: when same derivation path is used on different networks const currentNetworkAccount = savedAccounts.find(a => a.network === accountState.network); if (currentNetworkAccount) { - const device: ?TrezorDevice = findDevice(getState().connect, currentNetworkAccount.deviceID, currentNetworkAccount.deviceState); + const device: ?TrezorDevice = findDevice(getState().connect.devices, currentNetworkAccount.deviceID, currentNetworkAccount.deviceState); if (!device) return; infos.address = `${ device.instanceLabel } Account #${ (currentNetworkAccount.index + 1) }`; } else { - const device: ?TrezorDevice = findDevice(getState().connect, savedAccounts[0].deviceID, savedAccounts[0].deviceState); + const device: ?TrezorDevice = findDevice(getState().connect.devices, savedAccounts[0].deviceID, savedAccounts[0].deviceState); if (!device) return; warnings.address = `Looks like it's ${ device.instanceLabel } Account #${ (savedAccounts[0].index + 1) } address of ${ savedAccounts[0].network.toUpperCase() } network`; } diff --git a/src/js/actions/TrezorConnectActions.js b/src/js/actions/TrezorConnectActions.js index 603575a5..8b686da3 100644 --- a/src/js/actions/TrezorConnectActions.js +++ b/src/js/actions/TrezorConnectActions.js @@ -10,7 +10,6 @@ import * as WALLET from './constants/wallet'; import { push } from 'react-router-redux'; import * as DiscoveryActions from './DiscoveryActions'; import { resolveAfter } from '../utils/promiseUtils'; -import { getAccounts } from '../utils/reducerUtils'; import { findSelectedDevice } from '../reducers/TrezorConnectReducer'; diff --git a/src/js/actions/Web3Actions.js b/src/js/actions/Web3Actions.js index f356f8cb..7f7bac2c 100644 --- a/src/js/actions/Web3Actions.js +++ b/src/js/actions/Web3Actions.js @@ -3,6 +3,7 @@ import Web3 from 'web3'; import HDKey from 'hdkey'; + import EthereumjsUtil from 'ethereumjs-util'; import EthereumjsTx from 'ethereumjs-tx'; import TrezorConnect from 'trezor-connect'; @@ -19,8 +20,8 @@ import type { AsyncAction, } from '../flowtype'; import type { ContractFactory, EstimateGasOptions } from 'web3'; +import type BigNumber from 'bignumber.js'; -import type { BigNumber } from 'bignumber.js'; import type { Account } from '../reducers/AccountsReducer'; import type { PendingTx } from '../reducers/PendingTxReducer'; import type { Web3Instance } from '../reducers/Web3Reducer'; @@ -267,7 +268,7 @@ export function getTokenBalance(token: Token): AsyncAction { const web3 = web3instance.web3; const contract = web3instance.erc20.at(token.address); - contract.balanceOf(token.ethAddress, (error: ?Error, balance: ?BigNumber) => { + contract.balanceOf(token.ethAddress, (error: Error, balance: BigNumber) => { if (balance) { const newBalance: string = balance.dividedBy( Math.pow(10, token.decimals) ).toString(); if (newBalance !== token.balance) { @@ -343,9 +344,9 @@ export const getTransaction = (web3: Web3, txid: string): Promise => { -export const getBalanceAsync = (web3: Web3, address: string): Promise => { +export const getBalanceAsync = (web3: Web3, address: string): Promise => { return new Promise((resolve, reject) => { - web3.eth.getBalance(address, (error, result) => { + web3.eth.getBalance(address, (error: Error, result: BigNumber) => { if (error) { reject(error); } else { @@ -361,10 +362,10 @@ export const getTokenBalanceAsync = (erc20: ContractFactory, token: Token): Prom return new Promise((resolve, reject) => { const contract = erc20.at(token.address); - contract.balanceOf(token.ethAddress, (error: ?Error, balance: ?BigNumber) => { + contract.balanceOf(token.ethAddress, (error: Error, balance: BigNumber) => { if (error) { reject(error); - } else if (balance) { + } else { const newBalance: string = balance.dividedBy( Math.pow(10, token.decimals) ).toString(); resolve(newBalance); } @@ -399,23 +400,23 @@ export const getTokenInfoAsync = (erc20: ContractFactory, address: string): Prom decimals: 0 }; - contract.name.call((error: ?Error, name: ?string) => { + contract.name.call((error: Error, name: string) => { if (error) { resolve(null); return; - } else if (name) { + } else { info.name = name; } - contract.symbol.call((error: ?Error, symbol: ?string) => { + contract.symbol.call((error: Error, symbol: string) => { if (error) { resolve(null); return; - } else if (symbol) { + } else { info.symbol = symbol; } - contract.decimals.call((error: ?Error, decimals: ?BigNumber) => { + contract.decimals.call((error: Error, decimals: BigNumber) => { if (decimals) { info.decimals = decimals.toNumber(); resolve(info); diff --git a/src/js/components/wallet/account/AbstractAccount.js b/src/js/components/wallet/account/AbstractAccount.js index 0915e422..6f619c43 100644 --- a/src/js/components/wallet/account/AbstractAccount.js +++ b/src/js/components/wallet/account/AbstractAccount.js @@ -3,7 +3,7 @@ import React, { Component } from 'react'; import { Notification } from '../../common/Notification'; -import { findDevice } from '../../../utils/reducerUtils'; +import { findDevice } from '../../../reducers/TrezorConnectReducer'; // import * as AbstractAccountActions from '../../actions/AbstractAccountActions'; import { default as AbstractAccountActions } from '../../../actions/AbstractAccountActions'; @@ -55,7 +55,7 @@ export default class AbstractAccount

extends Component d.deviceState === device.state && d.network === accountState.network); // if (!discovery) return; @@ -102,8 +102,6 @@ export default class AbstractAccount

extends ComponentDevice with state {accountState.deviceState} not found); } diff --git a/src/js/components/wallet/aside/AccountSelection.js b/src/js/components/wallet/aside/AccountSelection.js index cc72a019..70d97aa9 100644 --- a/src/js/components/wallet/aside/AccountSelection.js +++ b/src/js/components/wallet/aside/AccountSelection.js @@ -5,7 +5,7 @@ import React, { PureComponent } from 'react'; import { Link, NavLink } from 'react-router-dom'; import BigNumber from 'bignumber.js'; -import { getAccounts } from '../../../utils/reducerUtils'; +import { findDeviceAccounts } from '../../../reducers/AccountsReducer'; import { findSelectedDevice } from '../../../reducers/TrezorConnectReducer'; import Loader from '../../common/LoaderCircle'; import Tooltip from 'rc-tooltip'; @@ -29,7 +29,7 @@ const AccountSelection = (props: Props): ?React$Element => { const fiatRate = props.fiat.find(f => f.network === selectedCoin.network); - const deviceAddresses: Array = getAccounts(accounts, selected, location.state.network); + const deviceAddresses: Array = findDeviceAccounts(accounts, selected, location.state.network); let selectedAccounts = deviceAddresses.map((address, i) => { // const url: string = `${baseUrl}/network/${location.state.network}/address/${i}`; const url: string = location.pathname.replace(/address+\/([0-9]*)/, `address/${i}`); diff --git a/src/js/components/wallet/summary/Summary.js b/src/js/components/wallet/summary/Summary.js index 57d05637..2149817c 100644 --- a/src/js/components/wallet/summary/Summary.js +++ b/src/js/components/wallet/summary/Summary.js @@ -11,7 +11,6 @@ import AbstractAccount from '../account/AbstractAccount'; import { Notification } from '../../common/Notification'; import SummaryDetails from './SummaryDetails.js'; import SummaryTokens from './SummaryTokens.js'; -import { findDevice } from '../../../utils/reducerUtils'; import type { Props } from './index'; import type { AccountState } from '../account/AbstractAccount'; diff --git a/src/js/flowtype/bignumber.js b/src/js/flowtype/bignumber.js index 0853e3c6..488831aa 100644 --- a/src/js/flowtype/bignumber.js +++ b/src/js/flowtype/bignumber.js @@ -1,5 +1,5 @@ declare module 'bignumber.js' { - declare type $npm$big$number$object = number | string | BigNumber + declare type $npm$big$number$object = number | string | T_BigNumber declare type $npm$cmp$result = -1 | 0 | 1 declare type DIGIT = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 declare type ROUND_DOWN = 0 @@ -8,7 +8,7 @@ declare module 'bignumber.js' { declare type ROUND_UP = 3 declare type RM = ROUND_DOWN | ROUND_HALF_UP | ROUND_HALF_EVEN | ROUND_UP - declare class BigNumber { + declare class T_BigNumber { // Properties static DP: number; static RM: RM; @@ -20,14 +20,14 @@ declare module 'bignumber.js' { s: -1 | 1; // Constructors - static (value: $npm$big$number$object): BigNumber; - constructor(value: $npm$big$number$object): BigNumber; + static (value: $npm$big$number$object): T_BigNumber; + constructor(value: $npm$big$number$object): T_BigNumber; // Methods abs(): BigNumber; cmp(n: $npm$big$number$object): $npm$cmp$result; - div(n: $npm$big$number$object): BigNumber; - dividedBy(n: $npm$big$number$object): BigNumber; + div(n: $npm$big$number$object): T_BigNumber; + dividedBy(n: $npm$big$number$object): T_BigNumber; eq(n: $npm$big$number$object): boolean; gt(n: $npm$big$number$object): boolean; greaterThan(n: $npm$big$number$object): boolean; @@ -36,20 +36,22 @@ declare module 'bignumber.js' { lessThan(n: $npm$big$number$object): boolean; lte(n: $npm$big$number$object): boolean; lessThanOrEqualTo(n: $npm$big$number$object): boolean; - minus(n: $npm$big$number$object): BigNumber; - mod(n: $npm$big$number$object): BigNumber; - plus(n: $npm$big$number$object): BigNumber; + minus(n: $npm$big$number$object): T_BigNumber; + mod(n: $npm$big$number$object): T_BigNumber; + plus(n: $npm$big$number$object): T_BigNumber; pow(exp: number): BigNumber; - round(dp: ?number, rm: ?RM): BigNumber; - sqrt(): BigNumber; - times(n: $npm$big$number$object): BigNumber; + round(dp: ?number, rm: ?RM): T_BigNumber; + sqrt(): T_BigNumber; + times(n: $npm$big$number$object): T_BigNumber; toExponential(dp: ?number): string; toFixed(dp: ?number): string; toPrecision(sd: ?number): string; - toString(): string; + toString(format?: number): string; + toNumber(): number; valueOf(): string; toJSON(): string; } - declare module.exports: typeof BigNumber + //declare module.exports: typeof T_BigNumber + declare export default typeof T_BigNumber; } \ No newline at end of file diff --git a/src/js/reducers/AccountsReducer.js b/src/js/reducers/AccountsReducer.js index 5b60c6ec..150b0bc6 100644 --- a/src/js/reducers/AccountsReducer.js +++ b/src/js/reducers/AccountsReducer.js @@ -31,6 +31,14 @@ export const findAccount = (state: State, index: number, deviceState: string, ne return state.find(a => a.deviceState === deviceState && a.index === index && a.network === network); } +export const findDeviceAccounts = (state: State, device: TrezorDevice, network: string): Array => { + if (network) { + return state.filter((addr) => addr.deviceState === device.state && addr.network === network); + } else { + return state.filter((addr) => addr.deviceState === device.state); + } +} + const createAccount = (state: State, action: AccountCreateAction): State => { // TODO check with device_id diff --git a/src/js/reducers/TrezorConnectReducer.js b/src/js/reducers/TrezorConnectReducer.js index 0be85da7..2e8f5bc7 100644 --- a/src/js/reducers/TrezorConnectReducer.js +++ b/src/js/reducers/TrezorConnectReducer.js @@ -55,9 +55,10 @@ export const findSelectedDevice = (state: State): ?TrezorDevice => { }); } -export const findDevice = (state: State, deviceId: string, deviceState: string): ?TrezorDevice => { - return state.devices.find(d => { - if (d.features && d.features.device_id === deviceId && d.state === deviceState){ +export const findDevice = (devices: Array, deviceId: string, deviceState: string, instance: ?number): ?TrezorDevice => { + return devices.find(d => { + // TODO: && (instance && d.instance === instance) + if (d.features && d.features.device_id === deviceId && d.state === deviceState) { return true; } return false; diff --git a/src/js/utils/ethUtils.js b/src/js/utils/ethUtils.js index de8dc16e..4e2a1ecb 100644 --- a/src/js/utils/ethUtils.js +++ b/src/js/utils/ethUtils.js @@ -8,10 +8,11 @@ export const decimalToHex = (dec: number): string => { } export const hexToDecimal = (hex: number): string => { - return new BigNumber(sanitizeHex(hex)).toString(); + const sanitized: ?string = sanitizeHex(hex); + return !sanitized ? 'null' : new BigNumber(sanitized).toString(); } -export const sanitizeHex = (hex: number): ?string => { +export const sanitizeHex = (hex: number | string): ?string => { if (typeof hex !== 'string') return null; hex = hex.substring(0, 2) === '0x' ? hex.substring(2) : hex; if (hex === '') return ''; @@ -31,5 +32,5 @@ export const strip = (str: string): string => { } export const calcGasPrice = (price: BigNumber, limit: string): string => { - return price.times(limit); + return price.times(limit).toString(); } \ No newline at end of file diff --git a/src/js/utils/reducerUtils.js b/src/js/utils/reducerUtils.js deleted file mode 100644 index 97278f8b..00000000 --- a/src/js/utils/reducerUtils.js +++ /dev/null @@ -1,18 +0,0 @@ -/* @flow */ -'use strict'; - -import type { TrezorDevice } from '../flowtype'; - -export const getAccounts = (accounts: Array, device: any, network: ?string): Array => { - if (network) { - return accounts.filter((addr) => addr.deviceState === device.state && addr.network === network); - } else { - return accounts.filter((addr) => addr.deviceState === device.state); - } - -} - -// Public method used in components to find device by state and device_id -export const findDevice = (devices: Array, state: ?string, deviceId: ?string, instance: ?number): ?TrezorDevice => { - return devices.find(d => d.state === state && d.features && d.features.device_id === deviceId && d.instance === instance); -} \ No newline at end of file