From d4c9db093c9e7d6611c2209ba20afe22d06c3dcc Mon Sep 17 00:00:00 2001 From: Szymon Lesisz Date: Thu, 13 Sep 2018 19:21:40 +0200 Subject: [PATCH] updateGasPrice on block mined --- src/actions/BlockchainActions.js | 4 +- src/actions/Web3Actions.js | 84 +++++++------------------------- 2 files changed, 20 insertions(+), 68 deletions(-) diff --git a/src/actions/BlockchainActions.js b/src/actions/BlockchainActions.js index de319eb5..6dc45c39 100644 --- a/src/actions/BlockchainActions.js +++ b/src/actions/BlockchainActions.js @@ -91,6 +91,8 @@ export const onBlockMined = (network: string): PromiseAction => async (dis // try to resolve pending transactions await dispatch( Web3Actions.resolvePendingTransactions(network) ); + await dispatch( Web3Actions.updateGasPrice(network) ); + const accounts: Array = getState().accounts.filter(a => a.network === network); if (accounts.length > 0) { // find out which account changed @@ -98,7 +100,7 @@ export const onBlockMined = (network: string): PromiseAction => async (dis accounts, coin: network, }); - + if (response.success) { response.payload.forEach((a, i) => { if (a.transactions > 0) { diff --git a/src/actions/Web3Actions.js b/src/actions/Web3Actions.js index 9050cd85..60b132bb 100644 --- a/src/actions/Web3Actions.js +++ b/src/actions/Web3Actions.js @@ -137,57 +137,6 @@ export const initWeb3 = (network: string, urlIndex: number = 0): PromiseAction => async (dispatch: Dispatch, getState: GetState): Promise => { - - // 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 => async (dispatch: Dispatch, getState: GetState): Promise => { const instance: Web3Instance = await dispatch( initWeb3(network) ); const balance = await instance.web3.eth.getBalance(address); @@ -312,24 +261,25 @@ export const getCurrentGasPrice = (network: string): PromiseAction => as if (instance) { return EthereumjsUnits.convert(instance.gasPrice, 'wei', 'gwei'); } else { - return "0"; + throw "0"; } +} - // const index: number = getState().web3.findIndex(w3 => w3.network === network); - - // const web3instance = getState().web3[index]; - // const web3 = web3instance.web3; - // web3.eth.getGasPrice((error, gasPrice) => { - // if (!error) { - // if (web3instance.gasPrice && web3instance.gasPrice.toString() !== gasPrice.toString()) { - // dispatch({ - // type: WEB3.GAS_PRICE_UPDATED, - // network, - // gasPrice, - // }); - // } - // } - // }); +export const updateGasPrice = (network: string): PromiseAction => async (dispatch: Dispatch, getState: GetState): Promise => { + try { + const instance = await dispatch( initWeb3(network) ); + const gasPrice = await instance.web3.eth.getGasPrice(); + if (instance.gasPrice !== gasPrice) { + dispatch({ + type: WEB3.GAS_PRICE_UPDATED, + network, + gasPrice + }); + } + } catch (e) { + // silent action + // nothing happens if this fails + } }