mirror of
https://github.com/trezor/trezor-wallet
synced 2024-11-24 09:18:09 +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;
|
if (!selected) return;
|
||||||
|
|
||||||
const web3instance: ?Web3Instance = getState().web3.find(w3 => w3.network === urlParams.network);
|
const web3instance: ?Web3Instance = getState().web3.find(w3 => w3.network === urlParams.network);
|
||||||
if (!web3instance) {
|
if (!web3instance) return;
|
||||||
// no backend for this network
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: check if there are some unfinished tx in localStorage
|
// 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
|
// corner-case: when same derivation path is used on different networks
|
||||||
const currentNetworkAccount = savedAccounts.find(a => a.network === accountState.network);
|
const currentNetworkAccount = savedAccounts.find(a => a.network === accountState.network);
|
||||||
if (currentNetworkAccount) {
|
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;
|
if (!device) return;
|
||||||
infos.address = `${ device.instanceLabel } Account #${ (currentNetworkAccount.index + 1) }`;
|
infos.address = `${ device.instanceLabel } Account #${ (currentNetworkAccount.index + 1) }`;
|
||||||
} else {
|
} 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;
|
if (!device) return;
|
||||||
warnings.address = `Looks like it's ${ device.instanceLabel } Account #${ (savedAccounts[0].index + 1) } address of ${ savedAccounts[0].network.toUpperCase() } network`;
|
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 { push } from 'react-router-redux';
|
||||||
import * as DiscoveryActions from './DiscoveryActions';
|
import * as DiscoveryActions from './DiscoveryActions';
|
||||||
import { resolveAfter } from '../utils/promiseUtils';
|
import { resolveAfter } from '../utils/promiseUtils';
|
||||||
import { getAccounts } from '../utils/reducerUtils';
|
|
||||||
import { findSelectedDevice } from '../reducers/TrezorConnectReducer';
|
import { findSelectedDevice } from '../reducers/TrezorConnectReducer';
|
||||||
|
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
import Web3 from 'web3';
|
import Web3 from 'web3';
|
||||||
import HDKey from 'hdkey';
|
import HDKey from 'hdkey';
|
||||||
|
|
||||||
import EthereumjsUtil from 'ethereumjs-util';
|
import EthereumjsUtil from 'ethereumjs-util';
|
||||||
import EthereumjsTx from 'ethereumjs-tx';
|
import EthereumjsTx from 'ethereumjs-tx';
|
||||||
import TrezorConnect from 'trezor-connect';
|
import TrezorConnect from 'trezor-connect';
|
||||||
@ -19,8 +20,8 @@ import type {
|
|||||||
AsyncAction,
|
AsyncAction,
|
||||||
} from '../flowtype';
|
} from '../flowtype';
|
||||||
import type { ContractFactory, EstimateGasOptions } from 'web3';
|
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 { Account } from '../reducers/AccountsReducer';
|
||||||
import type { PendingTx } from '../reducers/PendingTxReducer';
|
import type { PendingTx } from '../reducers/PendingTxReducer';
|
||||||
import type { Web3Instance } from '../reducers/Web3Reducer';
|
import type { Web3Instance } from '../reducers/Web3Reducer';
|
||||||
@ -267,7 +268,7 @@ export function getTokenBalance(token: Token): AsyncAction {
|
|||||||
const web3 = web3instance.web3;
|
const web3 = web3instance.web3;
|
||||||
const contract = web3instance.erc20.at(token.address);
|
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) {
|
if (balance) {
|
||||||
const newBalance: string = balance.dividedBy( Math.pow(10, token.decimals) ).toString();
|
const newBalance: string = balance.dividedBy( Math.pow(10, token.decimals) ).toString();
|
||||||
if (newBalance !== token.balance) {
|
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) => {
|
return new Promise((resolve, reject) => {
|
||||||
web3.eth.getBalance(address, (error, result) => {
|
web3.eth.getBalance(address, (error: Error, result: BigNumber) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
reject(error);
|
reject(error);
|
||||||
} else {
|
} else {
|
||||||
@ -361,10 +362,10 @@ export const getTokenBalanceAsync = (erc20: ContractFactory, token: Token): Prom
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
|
||||||
const contract = erc20.at(token.address);
|
const contract = erc20.at(token.address);
|
||||||
contract.balanceOf(token.ethAddress, (error: ?Error, balance: ?BigNumber) => {
|
contract.balanceOf(token.ethAddress, (error: Error, balance: BigNumber) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
reject(error);
|
reject(error);
|
||||||
} else if (balance) {
|
} else {
|
||||||
const newBalance: string = balance.dividedBy( Math.pow(10, token.decimals) ).toString();
|
const newBalance: string = balance.dividedBy( Math.pow(10, token.decimals) ).toString();
|
||||||
resolve(newBalance);
|
resolve(newBalance);
|
||||||
}
|
}
|
||||||
@ -399,23 +400,23 @@ export const getTokenInfoAsync = (erc20: ContractFactory, address: string): Prom
|
|||||||
decimals: 0
|
decimals: 0
|
||||||
};
|
};
|
||||||
|
|
||||||
contract.name.call((error: ?Error, name: ?string) => {
|
contract.name.call((error: Error, name: string) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
resolve(null);
|
resolve(null);
|
||||||
return;
|
return;
|
||||||
} else if (name) {
|
} else {
|
||||||
info.name = name;
|
info.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
contract.symbol.call((error: ?Error, symbol: ?string) => {
|
contract.symbol.call((error: Error, symbol: string) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
resolve(null);
|
resolve(null);
|
||||||
return;
|
return;
|
||||||
} else if (symbol) {
|
} else {
|
||||||
info.symbol = symbol;
|
info.symbol = symbol;
|
||||||
}
|
}
|
||||||
|
|
||||||
contract.decimals.call((error: ?Error, decimals: ?BigNumber) => {
|
contract.decimals.call((error: Error, decimals: BigNumber) => {
|
||||||
if (decimals) {
|
if (decimals) {
|
||||||
info.decimals = decimals.toNumber();
|
info.decimals = decimals.toNumber();
|
||||||
resolve(info);
|
resolve(info);
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
import React, { Component } from 'react';
|
import React, { Component } from 'react';
|
||||||
import { Notification } from '../../common/Notification';
|
import { Notification } from '../../common/Notification';
|
||||||
import { findDevice } from '../../../utils/reducerUtils';
|
import { findDevice } from '../../../reducers/TrezorConnectReducer';
|
||||||
|
|
||||||
// import * as AbstractAccountActions from '../../actions/AbstractAccountActions';
|
// import * as AbstractAccountActions from '../../actions/AbstractAccountActions';
|
||||||
import { default 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;
|
const accountState = props.abstractAccount;
|
||||||
if (!accountState) return;
|
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;
|
if (!device) return;
|
||||||
const discovery = props.discovery.find(d => d.deviceState === device.state && d.network === accountState.network);
|
const discovery = props.discovery.find(d => d.deviceState === device.state && d.network === accountState.network);
|
||||||
// if (!discovery) return;
|
// if (!discovery) return;
|
||||||
@ -102,8 +102,6 @@ export default class AbstractAccount<P> extends Component<Props & P, AccountStat
|
|||||||
discovery
|
discovery
|
||||||
} = this.state;
|
} = this.state;
|
||||||
|
|
||||||
// const device = findDevice(props.devices, accountState.deviceState, accountState.deviceId, accountState.deviceInstance);
|
|
||||||
|
|
||||||
if (!device) {
|
if (!device) {
|
||||||
return (<section>Device with state {accountState.deviceState} not found</section>);
|
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 { Link, NavLink } from 'react-router-dom';
|
||||||
import BigNumber from 'bignumber.js';
|
import BigNumber from 'bignumber.js';
|
||||||
|
|
||||||
import { getAccounts } from '../../../utils/reducerUtils';
|
import { findDeviceAccounts } from '../../../reducers/AccountsReducer';
|
||||||
import { findSelectedDevice } from '../../../reducers/TrezorConnectReducer';
|
import { findSelectedDevice } from '../../../reducers/TrezorConnectReducer';
|
||||||
import Loader from '../../common/LoaderCircle';
|
import Loader from '../../common/LoaderCircle';
|
||||||
import Tooltip from 'rc-tooltip';
|
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 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) => {
|
let selectedAccounts = deviceAddresses.map((address, i) => {
|
||||||
// const url: string = `${baseUrl}/network/${location.state.network}/address/${i}`;
|
// const url: string = `${baseUrl}/network/${location.state.network}/address/${i}`;
|
||||||
const url: string = location.pathname.replace(/address+\/([0-9]*)/, `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 { Notification } from '../../common/Notification';
|
||||||
import SummaryDetails from './SummaryDetails.js';
|
import SummaryDetails from './SummaryDetails.js';
|
||||||
import SummaryTokens from './SummaryTokens.js';
|
import SummaryTokens from './SummaryTokens.js';
|
||||||
import { findDevice } from '../../../utils/reducerUtils';
|
|
||||||
|
|
||||||
import type { Props } from './index';
|
import type { Props } from './index';
|
||||||
import type { AccountState } from '../account/AbstractAccount';
|
import type { AccountState } from '../account/AbstractAccount';
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
declare module 'bignumber.js' {
|
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 $npm$cmp$result = -1 | 0 | 1
|
||||||
declare type DIGIT = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
|
declare type DIGIT = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
|
||||||
declare type ROUND_DOWN = 0
|
declare type ROUND_DOWN = 0
|
||||||
@ -8,7 +8,7 @@ declare module 'bignumber.js' {
|
|||||||
declare type ROUND_UP = 3
|
declare type ROUND_UP = 3
|
||||||
declare type RM = ROUND_DOWN | ROUND_HALF_UP | ROUND_HALF_EVEN | ROUND_UP
|
declare type RM = ROUND_DOWN | ROUND_HALF_UP | ROUND_HALF_EVEN | ROUND_UP
|
||||||
|
|
||||||
declare class BigNumber {
|
declare class T_BigNumber {
|
||||||
// Properties
|
// Properties
|
||||||
static DP: number;
|
static DP: number;
|
||||||
static RM: RM;
|
static RM: RM;
|
||||||
@ -20,14 +20,14 @@ declare module 'bignumber.js' {
|
|||||||
s: -1 | 1;
|
s: -1 | 1;
|
||||||
|
|
||||||
// Constructors
|
// Constructors
|
||||||
static (value: $npm$big$number$object): BigNumber;
|
static (value: $npm$big$number$object): T_BigNumber;
|
||||||
constructor(value: $npm$big$number$object): BigNumber;
|
constructor(value: $npm$big$number$object): T_BigNumber;
|
||||||
|
|
||||||
// Methods
|
// Methods
|
||||||
abs(): BigNumber;
|
abs(): BigNumber;
|
||||||
cmp(n: $npm$big$number$object): $npm$cmp$result;
|
cmp(n: $npm$big$number$object): $npm$cmp$result;
|
||||||
div(n: $npm$big$number$object): BigNumber;
|
div(n: $npm$big$number$object): T_BigNumber;
|
||||||
dividedBy(n: $npm$big$number$object): BigNumber;
|
dividedBy(n: $npm$big$number$object): T_BigNumber;
|
||||||
eq(n: $npm$big$number$object): boolean;
|
eq(n: $npm$big$number$object): boolean;
|
||||||
gt(n: $npm$big$number$object): boolean;
|
gt(n: $npm$big$number$object): boolean;
|
||||||
greaterThan(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;
|
lessThan(n: $npm$big$number$object): boolean;
|
||||||
lte(n: $npm$big$number$object): boolean;
|
lte(n: $npm$big$number$object): boolean;
|
||||||
lessThanOrEqualTo(n: $npm$big$number$object): boolean;
|
lessThanOrEqualTo(n: $npm$big$number$object): boolean;
|
||||||
minus(n: $npm$big$number$object): BigNumber;
|
minus(n: $npm$big$number$object): T_BigNumber;
|
||||||
mod(n: $npm$big$number$object): BigNumber;
|
mod(n: $npm$big$number$object): T_BigNumber;
|
||||||
plus(n: $npm$big$number$object): BigNumber;
|
plus(n: $npm$big$number$object): T_BigNumber;
|
||||||
pow(exp: number): BigNumber;
|
pow(exp: number): BigNumber;
|
||||||
round(dp: ?number, rm: ?RM): BigNumber;
|
round(dp: ?number, rm: ?RM): T_BigNumber;
|
||||||
sqrt(): BigNumber;
|
sqrt(): T_BigNumber;
|
||||||
times(n: $npm$big$number$object): BigNumber;
|
times(n: $npm$big$number$object): T_BigNumber;
|
||||||
toExponential(dp: ?number): string;
|
toExponential(dp: ?number): string;
|
||||||
toFixed(dp: ?number): string;
|
toFixed(dp: ?number): string;
|
||||||
toPrecision(sd: ?number): string;
|
toPrecision(sd: ?number): string;
|
||||||
toString(): string;
|
toString(format?: number): string;
|
||||||
|
toNumber(): number;
|
||||||
valueOf(): string;
|
valueOf(): string;
|
||||||
toJSON(): 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);
|
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 => {
|
const createAccount = (state: State, action: AccountCreateAction): State => {
|
||||||
|
|
||||||
// TODO check with device_id
|
// 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 => {
|
export const findDevice = (devices: Array<TrezorDevice>, deviceId: string, deviceState: string, instance: ?number): ?TrezorDevice => {
|
||||||
return state.devices.find(d => {
|
return devices.find(d => {
|
||||||
if (d.features && d.features.device_id === deviceId && d.state === deviceState){
|
// TODO: && (instance && d.instance === instance)
|
||||||
|
if (d.features && d.features.device_id === deviceId && d.state === deviceState) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@ -8,10 +8,11 @@ export const decimalToHex = (dec: number): string => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const hexToDecimal = (hex: 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;
|
if (typeof hex !== 'string') return null;
|
||||||
hex = hex.substring(0, 2) === '0x' ? hex.substring(2) : hex;
|
hex = hex.substring(0, 2) === '0x' ? hex.substring(2) : hex;
|
||||||
if (hex === '') return '';
|
if (hex === '') return '';
|
||||||
@ -31,5 +32,5 @@ export const strip = (str: string): string => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const calcGasPrice = (price: BigNumber, limit: 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