1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-13 20:08:56 +00:00
trezor-wallet/src/js/actions/Web3Actions.js

459 lines
14 KiB
JavaScript
Raw Normal View History

2017-12-13 11:01:37 +00:00
/* @flow */
'use strict';
2018-05-05 11:52:03 +00:00
import Web3 from 'web3';
2017-12-13 11:01:37 +00:00
import HDKey from 'hdkey';
import EthereumjsUtil from 'ethereumjs-util';
import EthereumjsTx from 'ethereumjs-tx';
import TrezorConnect from 'trezor-connect';
import { strip } from '../utils/ethUtils';
2018-04-11 10:13:38 +00:00
import * as ADDRESS from './constants/address';
import * as WEB3 from './constants/web3';
2018-04-23 10:20:15 +00:00
import * as PENDING from './constants/pendingTx';
import * as AddressActions from '../actions/AddressActions';
2017-12-13 11:01:37 +00:00
2018-04-16 21:19:50 +00:00
import type {
Dispatch,
GetState,
Action,
AsyncAction,
} from '../flowtype';
2018-05-07 11:25:46 +00:00
import type { ContractFactory } from 'web3';
2018-04-16 21:19:50 +00:00
import type { Account } from '../reducers/AccountsReducer';
2018-04-23 10:20:15 +00:00
import type { PendingTx } from '../reducers/PendingTxReducer';
2018-05-05 11:52:03 +00:00
import type { Web3Instance } from '../reducers/Web3Reducer';
2018-04-16 21:19:50 +00:00
export type Web3Action = {
type: typeof WEB3.READY,
2018-04-23 10:20:15 +00:00
} | Web3CreateAction
| Web3UpdateBlockAction
2018-04-16 21:19:50 +00:00
| Web3UpdateGasPriceAction;
export type Web3CreateAction = {
type: typeof WEB3.CREATE,
network: string,
2018-05-07 11:25:46 +00:00
web3: Web3,
erc20: ContractFactory,
2018-04-16 21:19:50 +00:00
chainId: string;
};
2017-12-13 11:01:37 +00:00
2018-04-16 21:19:50 +00:00
export type Web3UpdateBlockAction = {
type: typeof WEB3.BLOCK_UPDATED,
network: string,
2018-05-07 11:25:46 +00:00
blockHash: string
2018-03-08 16:10:53 +00:00
};
2018-04-16 21:19:50 +00:00
export type Web3UpdateGasPriceAction = {
type: typeof WEB3.GAS_PRICE_UPDATED,
network: string,
2018-05-05 11:52:03 +00:00
gasPrice: string
2018-03-08 16:10:53 +00:00
};
2017-12-13 11:01:37 +00:00
2018-04-16 21:19:50 +00:00
export function init(web3: ?Web3, coinIndex: number = 0): AsyncAction {
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
2018-02-20 09:30:36 +00:00
const { config, ERC20Abi } = getState().localStorage;
2018-02-20 09:30:36 +00:00
const coin = config.coins[ coinIndex ];
if (!coin) {
// all instances done
dispatch({
type: WEB3.READY,
});
return;
}
const network = coin.network;
2018-02-20 09:30:36 +00:00
const urls = coin.backends[0].urls;
let web3host: string = urls[0];
if (web3) {
const currentHost = web3.currentProvider.host;
let currentHostIndex: number = urls.indexOf(currentHost);
if (currentHostIndex + 1 < urls.length) {
web3host = urls[currentHostIndex + 1];
} else {
2018-05-07 11:25:46 +00:00
console.error("TODO: Backend " + network + " not working", web3.currentProvider );
dispatch({
type: WEB3.CREATE,
network,
web3,
erc20: web3.eth.contract(ERC20Abi),
chainId: '0'
});
2018-02-20 09:30:36 +00:00
// try next coin
2018-05-07 11:25:46 +00:00
dispatch( init(null, coinIndex + 1) );
2018-02-20 09:30:36 +00:00
return;
}
}
//const instance = new Web3(window.web3.currentProvider);
const instance = new Web3( new Web3.providers.HttpProvider(web3host) );
// instance = new Web3( new Web3.providers.HttpProvider('https://pyrus2.ubiqscan.io') ); // UBQ
//instance = new Web3( new Web3.providers.HttpProvider('https://node.expanse.tech/') ); // EXP
//instance = new Web3( new Web3.providers.HttpProvider('http://10.34.0.91:8545/') );
//web3 = new Web3(new Web3.providers.HttpProvider("https://api.myetherapi.com/rop"));
//instance = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io2/QGyVKozSUEh2YhL4s2G4"));
//web3 = new Web3( new Web3.providers.HttpProvider("ws://34.230.234.51:30303") );
// initial check if backend is running
2018-05-05 11:52:03 +00:00
instance.eth.getGasPrice((error: Error, gasPrice: string) => {
2018-02-20 09:30:36 +00:00
if (error) {
// try different url
dispatch( init(instance, coinIndex) );
} else {
const erc20 = instance.eth.contract(ERC20Abi);
2018-02-20 09:30:36 +00:00
2017-12-13 11:01:37 +00:00
dispatch({
2018-02-20 09:30:36 +00:00
type: WEB3.CREATE,
network,
2018-02-20 09:30:36 +00:00
web3: instance,
erc20,
chainId: instance.version.network
});
dispatch({
type: WEB3.GAS_PRICE_UPDATED,
network,
2018-02-20 09:30:36 +00:00
gasPrice
});
// console.log("GET CHAIN", instance.version.network)
// instance.version.getWhisper((err, shh) => {
// console.log("-----whisperrr", error, shh)
// })
// const sshFilter = instance.ssh.filter('latest');
// sshFilter.watch((error, blockHash) => {
// console.warn("SSH", error, blockHash);
// });
//const shh = instance.shh.newIdentity();
const latestBlockFilter = instance.eth.filter('latest');
2018-05-05 11:52:03 +00:00
const onBlockMined = async (error: ?Error, blockHash: ?string) => {
2018-02-20 09:30:36 +00:00
if (error) {
2018-03-08 16:10:53 +00:00
window.setTimeout(() => {
// try again
2018-05-05 11:52:03 +00:00
onBlockMined(new Error("manually_triggered_error"), undefined);
2018-03-08 16:10:53 +00:00
}, 30000);
}
if (blockHash) {
dispatch({
type: WEB3.BLOCK_UPDATED,
network,
2018-03-08 16:10:53 +00:00
blockHash
});
2018-02-20 09:30:36 +00:00
}
// TODO: filter only current device
const accounts = getState().accounts.filter(a => a.network === network);
2018-02-20 09:30:36 +00:00
for (const addr of accounts) {
dispatch( getBalance(addr) );
2018-03-08 16:10:53 +00:00
dispatch( getNonce(addr) );
2018-02-20 09:30:36 +00:00
}
dispatch( getGasPrice(network) );
2018-02-20 09:30:36 +00:00
const pending = getState().pending.filter(p => p.network === network);
2018-03-08 16:10:53 +00:00
for (const tx of pending) {
dispatch( getTransactionReceipt(tx) );
}
}
latestBlockFilter.watch(onBlockMined);
2018-02-20 09:30:36 +00:00
// init next coin
dispatch( init(instance, coinIndex + 1) );
2017-12-13 11:01:37 +00:00
}
});
2018-02-20 09:30:36 +00:00
// let instance2 = new Web3( new Web3.providers.HttpProvider('https://pyrus2.ubiqscan.io') );
// console.log("INIT WEB3", instance, instance2);
// instance2.eth.getGasPrice((error, gasPrice) => {
// console.log("---gasss price from UBQ", gasPrice)
// });
}
}
2018-04-16 21:19:50 +00:00
// export function initContracts(): AsyncAction {
// return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
// const { web3, abi, tokens } = getState().web3;
// const contracts = [];
// for (let token of tokens) {
// contracts.push({
// contract: web3.eth.contract(abi).at(token.address),
// name: token.name,
// symbol: token.symbol,
// decimal: token.decimal
// });
// // web3.eth.contract(abi).at(token.address).balanceOf('0x98ead4bd2fbbb0cf0b49459aa0510ef53faa6cad', (e, r) => {
// // console.warn('contrR', e, r.toString(10));
// // });
// }
// const contract = web3.eth.contract(abi).at('0x58cda554935e4a1f2acbe15f8757400af275e084');
// contract.name.call((error, name) => {
// if (error) {
// // TODO: skip
// }
// contract.symbol.call((error, symbol) => {
// if (error) {
// // TODO: skip
// }
// contract.decimals.call((error, decimals) => {
// })
// });
2018-02-20 09:30:36 +00:00
2018-04-16 21:19:50 +00:00
// })
// }
// }
2017-12-13 11:01:37 +00:00
2018-04-16 21:19:50 +00:00
export function getGasPrice(network: string): AsyncAction {
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
2017-12-13 11:01:37 +00:00
const index: number = getState().web3.findIndex(w3 => w3.network === network);
2017-12-13 11:01:37 +00:00
const web3instance = getState().web3[ index ];
const web3 = web3instance.web3;
2018-02-20 09:30:36 +00:00
web3.eth.getGasPrice((error, gasPrice) => {
if (!error) {
if (web3instance.gasPrice && web3instance.gasPrice.toString() !== gasPrice.toString()) {
dispatch({
type: WEB3.GAS_PRICE_UPDATED,
network: network,
gasPrice
});
}
2018-02-20 09:30:36 +00:00
}
});
}
}
2017-12-13 11:01:37 +00:00
2018-04-16 21:19:50 +00:00
export function getBalance(account: Account): AsyncAction {
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
2017-12-13 11:01:37 +00:00
2018-04-16 21:19:50 +00:00
const web3instance = getState().web3.filter(w3 => w3.network === account.network)[0];
const web3: Web3 = web3instance.web3;
2017-12-13 11:01:37 +00:00
2018-04-16 21:19:50 +00:00
web3.eth.getBalance(account.address, (error, balance) => {
2018-02-20 09:30:36 +00:00
if (!error) {
const newBalance: string = web3.fromWei(balance.toString(), 'ether');
2018-04-16 21:19:50 +00:00
if (account.balance !== newBalance) {
2018-04-23 10:20:15 +00:00
dispatch(AddressActions.setBalance(
account.address,
account.network,
newBalance
));
2018-02-20 09:30:36 +00:00
// dispatch( loadHistory(addr) );
}
}
});
}
}
2017-12-13 11:01:37 +00:00
2018-04-16 21:19:50 +00:00
export function getNonce(account: Account): AsyncAction {
2018-03-08 16:10:53 +00:00
2018-04-16 21:19:50 +00:00
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
2018-03-08 16:10:53 +00:00
2018-04-16 21:19:50 +00:00
const web3instance = getState().web3.filter(w3 => w3.network === account.network)[0];
2018-03-08 16:10:53 +00:00
const web3 = web3instance.web3;
2018-04-23 10:20:15 +00:00
web3.eth.getTransactionCount(account.address, (error: Error, result: number) => {
2018-03-08 16:10:53 +00:00
if (!error) {
2018-04-16 21:19:50 +00:00
if (account.nonce !== result) {
2018-03-08 16:10:53 +00:00
dispatch({
type: ADDRESS.SET_NONCE,
2018-04-16 21:19:50 +00:00
address: account.address,
network: account.network,
2018-03-08 16:10:53 +00:00
nonce: result
});
}
}
});
}
}
2018-04-23 10:20:15 +00:00
export const getTransactionReceipt = (tx: PendingTx): AsyncAction => {
2018-04-16 21:19:50 +00:00
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
2018-03-08 16:10:53 +00:00
const web3instance = getState().web3.filter(w3 => w3.network === tx.network)[0];
2018-03-08 16:10:53 +00:00
const web3 = web3instance.web3;
2018-02-20 09:30:36 +00:00
//web3.eth.getTransactionReceipt(txid, (error, tx) => {
2018-03-08 16:10:53 +00:00
web3.eth.getTransaction(tx.id, (error, receipt) => {
if (receipt && receipt.blockNumber) {
web3.eth.getBlock(receipt.blockHash, (error, block) => {
2018-02-20 09:30:36 +00:00
dispatch({
2018-04-23 10:20:15 +00:00
type: PENDING.TX_RESOLVED,
2018-02-20 09:30:36 +00:00
tx,
2018-03-08 16:10:53 +00:00
receipt,
2018-02-20 09:30:36 +00:00
block
})
});
}
});
}
}
2018-04-16 21:19:50 +00:00
// export function updateLastBlock(hash: string): Action {
// return {
// type: 'web3__update_last_block',
// hash
// }
// }
2017-12-13 11:01:37 +00:00
2018-04-16 21:19:50 +00:00
export const getTransaction = (web3: Web3, txid: string): Promise<any> => {
2017-12-13 11:01:37 +00:00
return new Promise((resolve, reject) => {
web3.eth.getTransaction(txid, (error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});
}
2018-02-20 09:30:36 +00:00
2018-04-16 21:19:50 +00:00
export const getBalanceAsync = (web3: Web3, address: string): Promise<any> => {
2017-12-13 11:01:37 +00:00
return new Promise((resolve, reject) => {
web3.eth.getBalance(address, (error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});
}
2018-05-05 11:52:03 +00:00
export const getTokenBalanceAsync = (erc20: any, token: string, address: string): Promise<string> => {
2018-02-20 09:30:36 +00:00
return new Promise((resolve, reject) => {
const contr = erc20.at(token);
2018-04-23 10:20:15 +00:00
contr.balanceOf(address, (error: Error, result) => {
2018-02-20 09:30:36 +00:00
if (error) {
reject(error);
} else {
resolve(result);
}
});
});
}
2018-04-23 10:20:15 +00:00
export const getNonceAsync = (web3: Web3, address: string): Promise<number> => {
2017-12-13 11:01:37 +00:00
return new Promise((resolve, reject) => {
2018-04-23 10:20:15 +00:00
web3.eth.getTransactionCount(address, (error: Error, result: number) => {
2017-12-13 11:01:37 +00:00
if (error) {
reject(error);
} else {
resolve(result);
}
});
});
}
2018-02-20 09:30:36 +00:00
2018-04-16 21:19:50 +00:00
export const getTokenInfoAsync = (erc20: any, address: string): Promise<any> => {
2018-02-20 09:30:36 +00:00
return new Promise((resolve, reject) => {
2018-05-05 11:52:03 +00:00
const contract = erc20.at(address, (error, res) => {
console.warn("callack", error, res)
});
console.warn("AT", contract)
2018-02-20 09:30:36 +00:00
const info = {};
// TODO: handle errors
contract.name.call((e, name) => {
if (e) {
//resolve(null);
//return;
}
info.name = name;
contract.symbol.call((e, symbol) => {
if (e) {
resolve(null);
return;
}
info.symbol = symbol;
contract.decimals.call((e, decimals) => {
if (e) {
resolve(null);
return;
}
info.decimals = decimals.toString();
resolve(info);
});
})
});
});
}
2018-04-16 21:19:50 +00:00
export const estimateGas = (web3: Web3, gasOptions: any): Promise<any> => {
2017-12-13 11:01:37 +00:00
return new Promise((resolve, reject) => {
web3.eth.estimateGas(gasOptions, (error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
})
}
2018-04-16 21:19:50 +00:00
export const getGasPrice2 = (web3: Web3): Promise<any> => {
2017-12-13 11:01:37 +00:00
return new Promise((resolve, reject) => {
web3.eth.getGasPrice((error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
})
}
2018-04-16 21:19:50 +00:00
export const pushTx = (web3: Web3, tx: any): Promise<any> => {
2017-12-13 11:01:37 +00:00
return new Promise((resolve, reject) => {
web3.eth.sendRawTransaction(tx, (error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
})
}