mirror of
https://github.com/trezor/trezor-wallet
synced 2024-11-24 01:08:27 +00:00
BigNumber.js flowtype + removed ./utils/reducerUtils
This commit is contained in:
parent
b0d7891f5d
commit
80fa9cfe21
@ -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`;
|
||||
}
|
||||
|
@ -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';
|
||||
|
||||
|
||||
|
@ -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<any> => {
|
||||
|
||||
|
||||
|
||||
export const getBalanceAsync = (web3: Web3, address: string): Promise<any> => {
|
||||
export const getBalanceAsync = (web3: Web3, address: string): Promise<BigNumber> => {
|
||||
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);
|
||||
|
@ -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<P> extends Component<Props & P, AccountStat
|
||||
const accountState = props.abstractAccount;
|
||||
if (!accountState) return;
|
||||
|
||||
const device = findDevice(props.devices, accountState.deviceState, accountState.deviceId, accountState.deviceInstance);
|
||||
const device = findDevice(props.devices, accountState.deviceId, accountState.deviceState, accountState.deviceInstance);
|
||||
if (!device) return;
|
||||
const discovery = props.discovery.find(d => d.deviceState === device.state && d.network === accountState.network);
|
||||
// if (!discovery) return;
|
||||
@ -102,8 +102,6 @@ export default class AbstractAccount<P> extends Component<Props & P, AccountStat
|
||||
discovery
|
||||
} = this.state;
|
||||
|
||||
// const device = findDevice(props.devices, accountState.deviceState, accountState.deviceId, accountState.deviceInstance);
|
||||
|
||||
if (!device) {
|
||||
return (<section>Device with state {accountState.deviceState} not found</section>);
|
||||
}
|
||||
|
@ -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<string> => {
|
||||
|
||||
const fiatRate = props.fiat.find(f => f.network === selectedCoin.network);
|
||||
|
||||
const deviceAddresses: Array<any> = getAccounts(accounts, selected, location.state.network);
|
||||
const deviceAddresses: Array<any> = 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}`);
|
||||
|
@ -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';
|
||||
|
@ -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;
|
||||
}
|
@ -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<Account> => {
|
||||
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
|
||||
|
@ -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<TrezorDevice>, 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;
|
||||
|
@ -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();
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
/* @flow */
|
||||
'use strict';
|
||||
|
||||
import type { TrezorDevice } from '../flowtype';
|
||||
|
||||
export const getAccounts = (accounts: Array<any>, device: any, network: ?string): Array<any> => {
|
||||
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<TrezorDevice>, state: ?string, deviceId: ?string, instance: ?number): ?TrezorDevice => {
|
||||
return devices.find(d => d.state === state && d.features && d.features.device_id === deviceId && d.instance === instance);
|
||||
}
|
Loading…
Reference in New Issue
Block a user