mirror of
https://github.com/trezor/trezor-wallet
synced 2024-11-24 09:18:09 +00:00
eslint fixes
This commit is contained in:
parent
91f731c34e
commit
590e0ca642
@ -1,42 +1,28 @@
|
|||||||
/* @flow */
|
/* @flow */
|
||||||
|
|
||||||
import Web3 from 'web3';
|
|
||||||
import HDKey from 'hdkey';
|
|
||||||
|
|
||||||
import EthereumjsUtil from 'ethereumjs-util';
|
import EthereumjsUtil from 'ethereumjs-util';
|
||||||
import EthereumjsUnits from 'ethereumjs-units';
|
|
||||||
import EthereumjsTx from 'ethereumjs-tx';
|
|
||||||
import TrezorConnect from 'trezor-connect';
|
import TrezorConnect from 'trezor-connect';
|
||||||
import BigNumber from 'bignumber.js';
|
import BigNumber from 'bignumber.js';
|
||||||
|
|
||||||
import type { EstimateGasOptions } from 'web3';
|
|
||||||
import type { TransactionStatus, TransactionReceipt } from 'web3';
|
|
||||||
import { strip } from 'utils/ethUtils';
|
|
||||||
import * as BLOCKCHAIN from 'actions/constants/blockchain';
|
import * as BLOCKCHAIN from 'actions/constants/blockchain';
|
||||||
import * as WEB3 from 'actions/constants/web3';
|
|
||||||
import * as PENDING from 'actions/constants/pendingTx';
|
import * as PENDING from 'actions/constants/pendingTx';
|
||||||
|
|
||||||
import * as AccountsActions from './AccountsActions';
|
|
||||||
import * as Web3Actions from './Web3Actions';
|
|
||||||
|
|
||||||
import type {
|
import type {
|
||||||
TrezorDevice,
|
TrezorDevice,
|
||||||
Dispatch,
|
Dispatch,
|
||||||
GetState,
|
GetState,
|
||||||
Action,
|
|
||||||
AsyncAction,
|
|
||||||
PromiseAction,
|
PromiseAction,
|
||||||
ThunkAction,
|
|
||||||
} from 'flowtype';
|
} from 'flowtype';
|
||||||
import type { EthereumAccount } from 'trezor-connect';
|
import type { EthereumAccount } from 'trezor-connect';
|
||||||
import type { Token } from 'reducers/TokensReducer';
|
import type { Token } from 'reducers/TokensReducer';
|
||||||
import type { NetworkToken } from 'reducers/LocalStorageReducer';
|
import type { NetworkToken } from 'reducers/LocalStorageReducer';
|
||||||
|
import * as Web3Actions from './Web3Actions';
|
||||||
|
import * as AccountsActions from './AccountsActions';
|
||||||
|
|
||||||
export type BlockchainAction = {
|
export type BlockchainAction = {
|
||||||
type: typeof BLOCKCHAIN.READY,
|
type: typeof BLOCKCHAIN.READY,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const discoverAccount = (device: TrezorDevice, address: string, network: string): PromiseAction<EthereumAccount> => async (dispatch: Dispatch, getState: GetState): Promise<EthereumAccount> => {
|
export const discoverAccount = (device: TrezorDevice, address: string, network: string): PromiseAction<EthereumAccount> => async (dispatch: Dispatch): Promise<EthereumAccount> => {
|
||||||
// get data from connect
|
// get data from connect
|
||||||
// Temporary disabled, enable after trezor-connect@5.0.32 release
|
// Temporary disabled, enable after trezor-connect@5.0.32 release
|
||||||
const txs = await TrezorConnect.ethereumGetAccountInfo({
|
const txs = await TrezorConnect.ethereumGetAccountInfo({
|
||||||
@ -44,8 +30,8 @@ export const discoverAccount = (device: TrezorDevice, address: string, network:
|
|||||||
address,
|
address,
|
||||||
block: 0,
|
block: 0,
|
||||||
transactions: 0,
|
transactions: 0,
|
||||||
balance: "0",
|
balance: '0',
|
||||||
nonce: 0
|
nonce: 0,
|
||||||
},
|
},
|
||||||
coin: network,
|
coin: network,
|
||||||
});
|
});
|
||||||
@ -55,7 +41,7 @@ export const discoverAccount = (device: TrezorDevice, address: string, network:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// blockbook web3 fallback
|
// blockbook web3 fallback
|
||||||
const web3account = await dispatch( Web3Actions.discoverAccount(address, network) );
|
const web3account = await dispatch(Web3Actions.discoverAccount(address, network));
|
||||||
// return { transactions: txs.payload, ...web3account };
|
// return { transactions: txs.payload, ...web3account };
|
||||||
return {
|
return {
|
||||||
address,
|
address,
|
||||||
@ -66,22 +52,18 @@ export const discoverAccount = (device: TrezorDevice, address: string, network:
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getTokenInfo = (input: string, network: string): PromiseAction<NetworkToken> => async (dispatch: Dispatch, getState: GetState): Promise<NetworkToken> => {
|
export const getTokenInfo = (input: string, network: string): PromiseAction<NetworkToken> => async (dispatch: Dispatch): Promise<NetworkToken> => dispatch(Web3Actions.getTokenInfo(input, network));
|
||||||
return await dispatch( Web3Actions.getTokenInfo(input, network) );
|
|
||||||
}
|
|
||||||
|
|
||||||
export const getTokenBalance = (token: Token): PromiseAction<string> => async (dispatch: Dispatch, getState: GetState): Promise<string> => {
|
export const getTokenBalance = (token: Token): PromiseAction<string> => async (dispatch: Dispatch): Promise<string> => dispatch(Web3Actions.getTokenBalance(token));
|
||||||
return await dispatch( Web3Actions.getTokenBalance(token) );
|
|
||||||
}
|
|
||||||
|
|
||||||
export const getGasPrice = (network: string, defaultGasPrice: number): PromiseAction<BigNumber> => async (dispatch: Dispatch, getState: GetState): Promise<BigNumber> => {
|
export const getGasPrice = (network: string, defaultGasPrice: number): PromiseAction<BigNumber> => async (dispatch: Dispatch): Promise<BigNumber> => {
|
||||||
try {
|
try {
|
||||||
const gasPrice = await dispatch( Web3Actions.getCurrentGasPrice(network) );
|
const gasPrice = await dispatch(Web3Actions.getCurrentGasPrice(network));
|
||||||
return new BigNumber(gasPrice);
|
return new BigNumber(gasPrice);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return new BigNumber(defaultGasPrice);
|
return new BigNumber(defaultGasPrice);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
const estimateProxy: Array<Promise<string>> = [];
|
const estimateProxy: Array<Promise<string>> = [];
|
||||||
export const estimateGasLimit = (network: string, data: string, value: string, gasPrice: string): PromiseAction<string> => async (dispatch: Dispatch): Promise<string> => {
|
export const estimateGasLimit = (network: string, data: string, value: string, gasPrice: string): PromiseAction<string> => async (dispatch: Dispatch): Promise<string> => {
|
||||||
@ -114,9 +96,9 @@ export const onBlockMined = (coinInfo: any): PromiseAction<void> => async (dispa
|
|||||||
const network: string = coinInfo.shortcut.toLowerCase();
|
const network: string = coinInfo.shortcut.toLowerCase();
|
||||||
|
|
||||||
// try to resolve pending transactions
|
// try to resolve pending transactions
|
||||||
await dispatch( Web3Actions.resolvePendingTransactions(network) );
|
await dispatch(Web3Actions.resolvePendingTransactions(network));
|
||||||
|
|
||||||
await dispatch( Web3Actions.updateGasPrice(network) );
|
await dispatch(Web3Actions.updateGasPrice(network));
|
||||||
|
|
||||||
const accounts: Array<any> = getState().accounts.filter(a => a.network === network);
|
const accounts: Array<any> = getState().accounts.filter(a => a.network === network);
|
||||||
if (accounts.length > 0) {
|
if (accounts.length > 0) {
|
||||||
@ -130,34 +112,31 @@ export const onBlockMined = (coinInfo: any): PromiseAction<void> => async (dispa
|
|||||||
response.payload.forEach((a, i) => {
|
response.payload.forEach((a, i) => {
|
||||||
if (a.transactions > 0) {
|
if (a.transactions > 0) {
|
||||||
// load additional data from Web3 (balance, nonce, tokens)
|
// load additional data from Web3 (balance, nonce, tokens)
|
||||||
dispatch( Web3Actions.updateAccount(accounts[i], a, network) )
|
dispatch(Web3Actions.updateAccount(accounts[i], a, network));
|
||||||
} else {
|
} else {
|
||||||
// there are no new txs, just update block
|
// there are no new txs, just update block
|
||||||
dispatch( AccountsActions.update( { ...accounts[i], block: a.block }) );
|
dispatch(AccountsActions.update({ ...accounts[i], block: a.block }));
|
||||||
|
|
||||||
// HACK: since blockbook can't work with smart contracts for now
|
// HACK: since blockbook can't work with smart contracts for now
|
||||||
// try to update tokens balances added to this account using Web3
|
// try to update tokens balances added to this account using Web3
|
||||||
dispatch( Web3Actions.updateAccountTokens(accounts[i]) );
|
dispatch(Web3Actions.updateAccountTokens(accounts[i]));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|
||||||
// not used for now, waiting for fix in blockbook
|
// not used for now, waiting for fix in blockbook
|
||||||
export const onNotification = (payload: any): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
export const onNotification = (payload: any): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
||||||
|
|
||||||
// this event can be triggered multiple times
|
// this event can be triggered multiple times
|
||||||
// 1. check if pair [txid + address] is already in reducer
|
// 1. check if pair [txid + address] is already in reducer
|
||||||
const network: string = payload.coin.shortcut.toLowerCase();
|
const network: string = payload.coin.shortcut.toLowerCase();
|
||||||
const address: string = EthereumjsUtil.toChecksumAddress(payload.tx.address);
|
const address: string = EthereumjsUtil.toChecksumAddress(payload.tx.address);
|
||||||
const txInfo = await dispatch( Web3Actions.getPendingInfo(network, payload.tx.txid) );
|
const txInfo = await dispatch(Web3Actions.getPendingInfo(network, payload.tx.txid));
|
||||||
|
|
||||||
// const exists = getState().pending.filter(p => p.id === payload.tx.txid && p.address === address);
|
// const exists = getState().pending.filter(p => p.id === payload.tx.txid && p.address === address);
|
||||||
const exists = getState().pending.filter(p => {
|
const exists = getState().pending.filter(p => p.address === address);
|
||||||
return p.address === address;
|
|
||||||
});
|
|
||||||
if (exists.length < 1) {
|
if (exists.length < 1) {
|
||||||
if (txInfo) {
|
if (txInfo) {
|
||||||
dispatch({
|
dispatch({
|
||||||
@ -166,14 +145,14 @@ export const onNotification = (payload: any): PromiseAction<void> => async (disp
|
|||||||
type: 'send',
|
type: 'send',
|
||||||
id: payload.tx.txid,
|
id: payload.tx.txid,
|
||||||
network,
|
network,
|
||||||
currency: "tETH",
|
currency: 'tETH',
|
||||||
amount: txInfo.value,
|
amount: txInfo.value,
|
||||||
total: "0",
|
total: '0',
|
||||||
tx: {},
|
tx: {},
|
||||||
nonce: txInfo.nonce,
|
nonce: txInfo.nonce,
|
||||||
address,
|
address,
|
||||||
rejected: false
|
rejected: false,
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// tx info not found (yet?)
|
// tx info not found (yet?)
|
||||||
@ -186,18 +165,17 @@ export const onNotification = (payload: any): PromiseAction<void> => async (disp
|
|||||||
// });
|
// });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|
||||||
export const subscribe = (network: string): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
export const subscribe = (network: string): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
||||||
const addresses: Array<string> = getState().accounts.filter(a => a.network === network).map(a => a.address);
|
const addresses: Array<string> = getState().accounts.filter(a => a.network === network).map(a => a.address); // eslint-disable-line no-unused-vars
|
||||||
// $FlowIssue: trezor-connect@5.0.32
|
await TrezorConnect.blockchainSubscribe({
|
||||||
return await TrezorConnect.blockchainSubscribe({
|
|
||||||
// accounts: addresses,
|
// accounts: addresses,
|
||||||
accounts: [],
|
accounts: [],
|
||||||
coin: network
|
coin: network,
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
// Conditionally subscribe to blockchain backend
|
// Conditionally subscribe to blockchain backend
|
||||||
// called after TrezorConnect.init successfully emits TRANSPORT.START event
|
// called after TrezorConnect.init successfully emits TRANSPORT.START event
|
||||||
@ -207,26 +185,26 @@ export const init = (): PromiseAction<void> => async (dispatch: Dispatch, getSta
|
|||||||
if (getState().discovery.length > 0) {
|
if (getState().discovery.length > 0) {
|
||||||
// get unique networks
|
// get unique networks
|
||||||
const networks: Array<string> = [];
|
const networks: Array<string> = [];
|
||||||
getState().discovery.forEach(discovery => {
|
getState().discovery.forEach((discovery) => {
|
||||||
if (networks.indexOf(discovery.network) < 0) {
|
if (networks.indexOf(discovery.network) < 0) {
|
||||||
networks.push(discovery.network);
|
networks.push(discovery.network);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// subscribe
|
// subscribe
|
||||||
for (let i = 0; i < networks.length; i++) {
|
const results = networks.map(n => dispatch(subscribe(n)));
|
||||||
await dispatch( subscribe(networks[i]) );
|
// wait for all subscriptions
|
||||||
}
|
await Promise.all(results);
|
||||||
}
|
}
|
||||||
|
|
||||||
// continue wallet initialization
|
// continue wallet initialization
|
||||||
dispatch({
|
dispatch({
|
||||||
type: BLOCKCHAIN.READY
|
type: BLOCKCHAIN.READY,
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
// Handle BLOCKCHAIN.ERROR event from TrezorConnect
|
// Handle BLOCKCHAIN.ERROR event from TrezorConnect
|
||||||
// disconnect and remove Web3 webscocket instance if exists
|
// disconnect and remove Web3 webscocket instance if exists
|
||||||
export const error = (payload: any): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
export const error = (payload: any): PromiseAction<void> => async (dispatch: Dispatch): Promise<void> => {
|
||||||
dispatch( Web3Actions.disconnect(payload.coin) );
|
dispatch(Web3Actions.disconnect(payload.coin));
|
||||||
}
|
};
|
@ -1,15 +1,10 @@
|
|||||||
/* @flow */
|
/* @flow */
|
||||||
import Web3 from 'web3';
|
import Web3 from 'web3';
|
||||||
import HDKey from 'hdkey';
|
|
||||||
import BigNumber from 'bignumber.js';
|
import BigNumber from 'bignumber.js';
|
||||||
|
|
||||||
import EthereumjsUtil from 'ethereumjs-util';
|
|
||||||
import EthereumjsUnits from 'ethereumjs-units';
|
import EthereumjsUnits from 'ethereumjs-units';
|
||||||
import EthereumjsTx from 'ethereumjs-tx';
|
|
||||||
// import InputDataDecoder from 'ethereum-input-data-decoder';
|
// import InputDataDecoder from 'ethereum-input-data-decoder';
|
||||||
import TrezorConnect from 'trezor-connect';
|
import type { EstimateGasOptions } from 'web3';
|
||||||
import type { EstimateGasOptions, TransactionStatus, TransactionReceipt } from 'web3';
|
|
||||||
import { strip } from 'utils/ethUtils';
|
|
||||||
import * as WEB3 from 'actions/constants/web3';
|
import * as WEB3 from 'actions/constants/web3';
|
||||||
import * as PENDING from 'actions/constants/pendingTx';
|
import * as PENDING from 'actions/constants/pendingTx';
|
||||||
|
|
||||||
@ -17,13 +12,11 @@ import type {
|
|||||||
Dispatch,
|
Dispatch,
|
||||||
GetState,
|
GetState,
|
||||||
ThunkAction,
|
ThunkAction,
|
||||||
AsyncAction,
|
|
||||||
PromiseAction,
|
PromiseAction,
|
||||||
} from 'flowtype';
|
} from 'flowtype';
|
||||||
|
|
||||||
import type { EthereumAccount } from 'trezor-connect';
|
import type { EthereumAccount } from 'trezor-connect';
|
||||||
import type { Account } from 'reducers/AccountsReducer';
|
import type { Account } from 'reducers/AccountsReducer';
|
||||||
import type { PendingTx } from 'reducers/PendingTxReducer';
|
|
||||||
import type { Web3Instance } from 'reducers/Web3Reducer';
|
import type { Web3Instance } from 'reducers/Web3Reducer';
|
||||||
import type { Token } from 'reducers/TokensReducer';
|
import type { Token } from 'reducers/TokensReducer';
|
||||||
import type { NetworkToken } from 'reducers/LocalStorageReducer';
|
import type { NetworkToken } from 'reducers/LocalStorageReducer';
|
||||||
@ -52,86 +45,82 @@ export type Web3Action = {
|
|||||||
} | Web3UpdateBlockAction
|
} | Web3UpdateBlockAction
|
||||||
| Web3UpdateGasPriceAction;
|
| Web3UpdateGasPriceAction;
|
||||||
|
|
||||||
export const initWeb3 = (network: string, urlIndex: number = 0): PromiseAction<Web3Instance> => async (dispatch: Dispatch, getState: GetState): Promise<Web3Instance> => {
|
export const initWeb3 = (network: string, urlIndex: number = 0): PromiseAction<Web3Instance> => async (dispatch: Dispatch, getState: GetState): Promise<Web3Instance> => new Promise(async (resolve, reject) => {
|
||||||
return new Promise(async (resolve, reject) => {
|
// check if requested web was initialized before
|
||||||
// check if requested web was initialized before
|
const instance = getState().web3.find(w3 => w3.network === network);
|
||||||
const instance = getState().web3.find(w3 => w3.network === network);
|
if (instance && instance.web3.currentProvider.connected) {
|
||||||
if (instance && instance.web3.currentProvider.connected) {
|
resolve(instance);
|
||||||
resolve(instance);
|
return;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// requested web3 wasn't initialized or is disconnected
|
// requested web3 wasn't initialized or is disconnected
|
||||||
// initialize again
|
// initialize again
|
||||||
const { config, ERC20Abi } = getState().localStorage;
|
const { config, ERC20Abi } = getState().localStorage;
|
||||||
const coin = config.coins.find(c => c.network === network);
|
const coin = config.coins.find(c => c.network === network);
|
||||||
if (!coin) {
|
if (!coin) {
|
||||||
// coin not found
|
// coin not found
|
||||||
reject(new Error(`Network ${ network} not found in application config.`));
|
reject(new Error(`Network ${network} not found in application config.`));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get first url
|
// get first url
|
||||||
const url = coin.web3[ urlIndex ];
|
const url = coin.web3[urlIndex];
|
||||||
if (!url) {
|
if (!url) {
|
||||||
reject(new Error('Web3 backend is not responding'));
|
reject(new Error('Web3 backend is not responding'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const web3 = new Web3( new Web3.providers.WebsocketProvider(url) );
|
const web3 = new Web3(new Web3.providers.WebsocketProvider(url));
|
||||||
|
|
||||||
const onConnect = async () => {
|
const onConnect = async () => {
|
||||||
|
const latestBlock = await web3.eth.getBlockNumber();
|
||||||
|
const gasPrice = await web3.eth.getGasPrice();
|
||||||
|
|
||||||
const latestBlock = await web3.eth.getBlockNumber();
|
const newInstance = {
|
||||||
const gasPrice = await web3.eth.getGasPrice();
|
network,
|
||||||
|
web3,
|
||||||
|
chainId: coin.chainId,
|
||||||
|
erc20: new web3.eth.Contract(ERC20Abi),
|
||||||
|
latestBlock,
|
||||||
|
gasPrice,
|
||||||
|
};
|
||||||
|
|
||||||
const instance = {
|
dispatch({
|
||||||
network,
|
type: WEB3.CREATE,
|
||||||
web3,
|
instance: newInstance,
|
||||||
chainId: coin.chainId,
|
});
|
||||||
erc20: new web3.eth.Contract(ERC20Abi),
|
|
||||||
latestBlock,
|
|
||||||
gasPrice,
|
|
||||||
}
|
|
||||||
|
|
||||||
dispatch({
|
resolve(newInstance);
|
||||||
type: WEB3.CREATE,
|
};
|
||||||
instance,
|
|
||||||
});
|
|
||||||
|
|
||||||
resolve(instance);
|
const onEnd = async () => {
|
||||||
}
|
web3.currentProvider.reset();
|
||||||
|
const oldInstance = getState().web3.find(w3 => w3.network === network);
|
||||||
|
|
||||||
const onEnd = async () => {
|
if (oldInstance && oldInstance.web3.currentProvider.connected) {
|
||||||
|
// backend disconnects
|
||||||
web3.currentProvider.reset();
|
// dispatch({
|
||||||
const instance = getState().web3.find(w3 => w3.network === network);
|
// type: 'WEB3.DISCONNECT',
|
||||||
|
// network
|
||||||
if (instance && instance.web3.currentProvider.connected) {
|
// });
|
||||||
// backend disconnects
|
} else {
|
||||||
// dispatch({
|
// backend initialization error for given url, try next one
|
||||||
// type: 'WEB3.DISCONNECT',
|
try {
|
||||||
// network
|
const otherWeb3 = await dispatch(initWeb3(network, urlIndex + 1));
|
||||||
// });
|
resolve(otherWeb3);
|
||||||
} else {
|
} catch (error) {
|
||||||
// backend initialization error for given url, try next one
|
reject(error);
|
||||||
try {
|
|
||||||
const web3 = await dispatch( initWeb3(network, urlIndex + 1) );
|
|
||||||
resolve(web3);
|
|
||||||
} catch (error) {
|
|
||||||
reject(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
web3.currentProvider.on('connect', onConnect);
|
web3.currentProvider.on('connect', onConnect);
|
||||||
web3.currentProvider.on('end', onEnd);
|
web3.currentProvider.on('end', onEnd);
|
||||||
web3.currentProvider.on('error', onEnd);
|
web3.currentProvider.on('error', onEnd);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
export const discoverAccount = (address: string, network: string): PromiseAction<EthereumAccount> => async (dispatch: Dispatch, getState: GetState): Promise<EthereumAccount> => {
|
export const discoverAccount = (address: string, network: string): PromiseAction<EthereumAccount> => async (dispatch: Dispatch): Promise<EthereumAccount> => {
|
||||||
const instance: Web3Instance = await dispatch( initWeb3(network) );
|
const instance: Web3Instance = await dispatch(initWeb3(network));
|
||||||
const balance = await instance.web3.eth.getBalance(address);
|
const balance = await instance.web3.eth.getBalance(address);
|
||||||
const nonce = await instance.web3.eth.getTransactionCount(address);
|
const nonce = await instance.web3.eth.getTransactionCount(address);
|
||||||
return {
|
return {
|
||||||
@ -139,12 +128,12 @@ export const discoverAccount = (address: string, network: string): PromiseAction
|
|||||||
transactions: 0,
|
transactions: 0,
|
||||||
block: 0,
|
block: 0,
|
||||||
balance: EthereumjsUnits.convert(balance, 'wei', 'ether'),
|
balance: EthereumjsUnits.convert(balance, 'wei', 'ether'),
|
||||||
nonce
|
nonce,
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
|
|
||||||
export const resolvePendingTransactions = (network: string): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
export const resolvePendingTransactions = (network: string): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
||||||
const instance: Web3Instance = await dispatch( initWeb3(network) );
|
const instance: Web3Instance = await dispatch(initWeb3(network));
|
||||||
const pending = getState().pending.filter(p => p.network === network);
|
const pending = getState().pending.filter(p => p.network === network);
|
||||||
for (const tx of pending) {
|
for (const tx of pending) {
|
||||||
const status = await instance.web3.eth.getTransaction(tx.id);
|
const status = await instance.web3.eth.getTransaction(tx.id);
|
||||||
@ -170,13 +159,14 @@ export const resolvePendingTransactions = (network: string): PromiseAction<void>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
export const getPendingInfo = (network: string, txid: string): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
/*
|
||||||
const instance: Web3Instance = await dispatch( initWeb3(network) );
|
export const getPendingInfo = (network: string, txid: string): PromiseAction<void> => async (dispatch: Dispatch): Promise<void> => {
|
||||||
|
const instance: Web3Instance = await dispatch(initWeb3(network));
|
||||||
const tx = await instance.web3.eth.getTransaction(txid);
|
const tx = await instance.web3.eth.getTransaction(txid);
|
||||||
|
|
||||||
/*
|
|
||||||
if (tx.input !== "0x") {
|
if (tx.input !== "0x") {
|
||||||
// find token:
|
// find token:
|
||||||
// tx.to <= smart contract address
|
// tx.to <= smart contract address
|
||||||
@ -190,31 +180,33 @@ export const getPendingInfo = (network: string, txid: string): PromiseAction<voi
|
|||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
// return tx;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const getTxInput = (): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
// return tx;
|
||||||
const instance: Web3Instance = await dispatch( initWeb3("ropsten") );
|
};
|
||||||
|
|
||||||
|
export const getTxInput = (): PromiseAction<void> => async (dispatch: Dispatch): Promise<void> => {
|
||||||
|
const instance: Web3Instance = await dispatch(initWeb3('ropsten'));
|
||||||
// const inputData = instance.web3.utils.hexToAscii("0xa9059cbb00000000000000000000000073d0385f4d8e00c5e6504c6030f47bf6212736a80000000000000000000000000000000000000000000000000000000000000001");
|
// const inputData = instance.web3.utils.hexToAscii("0xa9059cbb00000000000000000000000073d0385f4d8e00c5e6504c6030f47bf6212736a80000000000000000000000000000000000000000000000000000000000000001");
|
||||||
// console.warn("input data!", inputData);
|
// console.warn("input data!", inputData);
|
||||||
}
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
|
export const updateAccount = (account: Account, newAccount: EthereumAccount, network: string): PromiseAction<void> => async (dispatch: Dispatch): Promise<void> => {
|
||||||
export const updateAccount = (account: Account, newAccount: EthereumAccount, network: string): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
const instance: Web3Instance = await dispatch(initWeb3(network));
|
||||||
const instance: Web3Instance = await dispatch( initWeb3(network) );
|
|
||||||
const balance = await instance.web3.eth.getBalance(account.address);
|
const balance = await instance.web3.eth.getBalance(account.address);
|
||||||
const nonce = await instance.web3.eth.getTransactionCount(account.address);
|
const nonce = await instance.web3.eth.getTransactionCount(account.address);
|
||||||
dispatch( AccountsActions.update( { ...account, ...newAccount, balance: EthereumjsUnits.convert(balance, 'wei', 'ether'), nonce }) );
|
dispatch(AccountsActions.update({
|
||||||
|
...account, ...newAccount, balance: EthereumjsUnits.convert(balance, 'wei', 'ether'), nonce,
|
||||||
|
}));
|
||||||
|
|
||||||
// update tokens for this account
|
// update tokens for this account
|
||||||
dispatch( updateAccountTokens(account) );
|
dispatch(updateAccountTokens(account));
|
||||||
}
|
};
|
||||||
|
|
||||||
export const updateAccountTokens = (account: Account): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
export const updateAccountTokens = (account: Account): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
||||||
const tokens = getState().tokens.filter(t => t.network === account.network && t.ethAddress === account.address);
|
const tokens = getState().tokens.filter(t => t.network === account.network && t.ethAddress === account.address);
|
||||||
for (const token of tokens) {
|
for (const token of tokens) {
|
||||||
const balance = await dispatch( getTokenBalance(token) );
|
const balance = await dispatch(getTokenBalance(token));
|
||||||
// const newBalance: string = balance.dividedBy(Math.pow(10, token.decimals)).toString(10);
|
// const newBalance: string = balance.dividedBy(Math.pow(10, token.decimals)).toString(10);
|
||||||
if (balance !== token.balance) {
|
if (balance !== token.balance) {
|
||||||
dispatch(TokenActions.setBalance(
|
dispatch(TokenActions.setBalance(
|
||||||
@ -224,10 +216,10 @@ export const updateAccountTokens = (account: Account): PromiseAction<void> => as
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
export const getTokenInfo = (address: string, network: string): PromiseAction<NetworkToken> => async (dispatch: Dispatch, getState: GetState): Promise<NetworkToken> => {
|
export const getTokenInfo = (address: string, network: string): PromiseAction<NetworkToken> => async (dispatch: Dispatch): Promise<NetworkToken> => {
|
||||||
const instance: Web3Instance = await dispatch( initWeb3(network) );
|
const instance: Web3Instance = await dispatch(initWeb3(network));
|
||||||
const contract = instance.erc20.clone();
|
const contract = instance.erc20.clone();
|
||||||
contract.options.address = address;
|
contract.options.address = address;
|
||||||
|
|
||||||
@ -243,40 +235,39 @@ export const getTokenInfo = (address: string, network: string): PromiseAction<Ne
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getTokenBalance = (token: Token): PromiseAction<string> => async (dispatch: Dispatch, getState: GetState): Promise<string> => {
|
export const getTokenBalance = (token: Token): PromiseAction<string> => async (dispatch: Dispatch): Promise<string> => {
|
||||||
const instance = await dispatch( initWeb3(token.network) );
|
const instance = await dispatch(initWeb3(token.network));
|
||||||
const contract = instance.erc20.clone();
|
const contract = instance.erc20.clone();
|
||||||
contract.options.address = token.address;
|
contract.options.address = token.address;
|
||||||
|
|
||||||
const balance = await contract.methods.balanceOf(token.ethAddress).call();
|
const balance = await contract.methods.balanceOf(token.ethAddress).call();
|
||||||
return new BigNumber(balance).dividedBy(Math.pow(10, token.decimals)).toString(10);
|
return new BigNumber(balance).dividedBy(10 ** token.decimals).toString(10);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getCurrentGasPrice = (network: string): PromiseAction<string> => async (dispatch: Dispatch, getState: GetState): Promise<string> => {
|
export const getCurrentGasPrice = (network: string): PromiseAction<string> => async (dispatch: Dispatch, getState: GetState): Promise<string> => {
|
||||||
const instance = getState().web3.find(w3 => w3.network === network);
|
const instance = getState().web3.find(w3 => w3.network === network);
|
||||||
if (instance) {
|
if (instance) {
|
||||||
return EthereumjsUnits.convert(instance.gasPrice, 'wei', 'gwei');
|
return EthereumjsUnits.convert(instance.gasPrice, 'wei', 'gwei');
|
||||||
} else {
|
|
||||||
throw "0";
|
|
||||||
}
|
}
|
||||||
}
|
return '0';
|
||||||
|
};
|
||||||
|
|
||||||
export const updateGasPrice = (network: string): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
export const updateGasPrice = (network: string): PromiseAction<void> => async (dispatch: Dispatch): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const instance = await dispatch( initWeb3(network) );
|
const instance = await dispatch(initWeb3(network));
|
||||||
const gasPrice = await instance.web3.eth.getGasPrice();
|
const gasPrice = await instance.web3.eth.getGasPrice();
|
||||||
if (instance.gasPrice !== gasPrice) {
|
if (instance.gasPrice !== gasPrice) {
|
||||||
dispatch({
|
dispatch({
|
||||||
type: WEB3.GAS_PRICE_UPDATED,
|
type: WEB3.GAS_PRICE_UPDATED,
|
||||||
network,
|
network,
|
||||||
gasPrice
|
gasPrice,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// silent action
|
// silent action
|
||||||
// nothing happens if this fails
|
// nothing happens if this fails
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|
||||||
export const estimateGasLimit = (network: string, $options: EstimateGasOptions): PromiseAction<string> => async (dispatch: Dispatch): Promise<string> => {
|
export const estimateGasLimit = (network: string, $options: EstimateGasOptions): PromiseAction<string> => async (dispatch: Dispatch): Promise<string> => {
|
||||||
@ -296,8 +287,8 @@ export const estimateGasLimit = (network: string, $options: EstimateGasOptions):
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const disconnect = (coinInfo: any): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
|
export const disconnect = (coinInfo: any): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
|
||||||
// incoming "coinInfo" from TrezorConnect is CoinInfo | EthereumNetwork type
|
// incoming "coinInfo" from TrezorConnect is CoinInfo | EthereumNetwork type
|
||||||
const network: string = coinInfo.shortcut.toLowerCase();
|
const network: string = coinInfo.shortcut.toLowerCase();
|
||||||
// check if Web3 was already initialized
|
// check if Web3 was already initialized
|
||||||
const instance = getState().web3.find(w3 => w3.network === network);
|
const instance = getState().web3.find(w3 => w3.network === network);
|
||||||
if (instance) {
|
if (instance) {
|
||||||
@ -308,7 +299,7 @@ export const disconnect = (coinInfo: any): ThunkAction => (dispatch: Dispatch, g
|
|||||||
// remove instance from reducer
|
// remove instance from reducer
|
||||||
dispatch({
|
dispatch({
|
||||||
type: WEB3.DISCONNECT,
|
type: WEB3.DISCONNECT,
|
||||||
instance
|
instance,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user