1
0
mirror of https://github.com/trezor/trezor-wallet synced 2025-02-28 16:32:02 +00:00

updateGasPrice on block mined

This commit is contained in:
Szymon Lesisz 2018-09-13 19:21:40 +02:00
parent b8fff2a308
commit d4c9db093c
2 changed files with 20 additions and 68 deletions

View File

@ -91,6 +91,8 @@ export const onBlockMined = (network: string): PromiseAction<void> => async (dis
// 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) );
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) {
// find out which account changed // find out which account changed
@ -98,7 +100,7 @@ export const onBlockMined = (network: string): PromiseAction<void> => async (dis
accounts, accounts,
coin: network, coin: network,
}); });
if (response.success) { if (response.success) {
response.payload.forEach((a, i) => { response.payload.forEach((a, i) => {
if (a.transactions > 0) { if (a.transactions > 0) {

View File

@ -137,57 +137,6 @@ export const initWeb3 = (network: string, urlIndex: number = 0): PromiseAction<W
}); });
} }
const _onNewBlock = (instance: Web3Instance): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
// const latestBlock = await instance.web3.eth.getBlockNumber();
// dispatch({
// type: WEB3.BLOCK_UPDATED,
// network: instance.network,
// blockHash: latestBlock,
// });
// TODO: filter only current device
const accounts = getState().accounts.filter(a => a.network === instance.network);
for (const account of accounts) {
const nonce = await instance.web3.eth.getTransactionCount(account.address);
if (nonce !== account.nonce) {
dispatch(AccountsActions.setNonce(account.address, account.network, account.deviceState, nonce));
}
const balance = await instance.web3.eth.getBalance(account.address);
const newBalance = EthereumjsUnits.convert(balance, 'wei', 'ether');
if (newBalance !== account.balance) {
dispatch(AccountsActions.setBalance(
account.address,
account.network,
account.deviceState,
newBalance
));
}
}
const tokens = getState().tokens.filter(t => t.network === instance.network);
for (const token of tokens) {
const balance = await dispatch( getTokenBalance(token) );
console.warn("TOK BALAC", balance)
// const newBalance: string = balance.dividedBy(Math.pow(10, token.decimals)).toString(10);
if (balance !== token.balance) {
dispatch(TokenActions.setBalance(
token.address,
token.ethAddress,
balance,
));
}
}
// dispatch(getGasPrice(network));
}
export const discoverAccount = (address: string, network: string): PromiseAction<AccountDiscovery> => async (dispatch: Dispatch, getState: GetState): Promise<AccountDiscovery> => { export const discoverAccount = (address: string, network: string): PromiseAction<AccountDiscovery> => async (dispatch: Dispatch, getState: GetState): Promise<AccountDiscovery> => {
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);
@ -312,24 +261,25 @@ export const getCurrentGasPrice = (network: string): PromiseAction<string> => as
if (instance) { if (instance) {
return EthereumjsUnits.convert(instance.gasPrice, 'wei', 'gwei'); return EthereumjsUnits.convert(instance.gasPrice, 'wei', 'gwei');
} else { } else {
return "0"; throw "0";
} }
}
// const index: number = getState().web3.findIndex(w3 => w3.network === network); export const updateGasPrice = (network: string): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
try {
// const web3instance = getState().web3[index]; const instance = await dispatch( initWeb3(network) );
// const web3 = web3instance.web3; const gasPrice = await instance.web3.eth.getGasPrice();
// web3.eth.getGasPrice((error, gasPrice) => { if (instance.gasPrice !== gasPrice) {
// if (!error) { dispatch({
// if (web3instance.gasPrice && web3instance.gasPrice.toString() !== gasPrice.toString()) { type: WEB3.GAS_PRICE_UPDATED,
// dispatch({ network,
// type: WEB3.GAS_PRICE_UPDATED, gasPrice
// network, });
// gasPrice, }
// }); } catch (e) {
// } // silent action
// } // nothing happens if this fails
// }); }
} }