From 572a6842c53c649e6ce0914e7309739b4ec48502 Mon Sep 17 00:00:00 2001 From: liamaharon Date: Fri, 2 Nov 2018 13:28:57 +1100 Subject: [PATCH 1/2] Batch token balance requests --- src/containers/HubiiApiHoc/reducer.js | 4 + src/containers/HubiiApiHoc/saga.js | 45 +- .../HubiiApiHoc/tests/reducer.test.js | 13 + src/containers/WalletsOverview/index.js | 11 +- .../tests/__snapshots__/index.test.js.snap | 896 ------------------ src/translations/en.json | 4 +- src/translations/zh.json | 6 +- src/utils/rpcRequest.js | 55 ++ 8 files changed, 113 insertions(+), 921 deletions(-) create mode 100644 src/utils/rpcRequest.js diff --git a/src/containers/HubiiApiHoc/reducer.js b/src/containers/HubiiApiHoc/reducer.js index 3d421f71..3ddcf767 100644 --- a/src/containers/HubiiApiHoc/reducer.js +++ b/src/containers/HubiiApiHoc/reducer.js @@ -18,6 +18,7 @@ import { LOAD_TRANSACTIONS_SUCCESS, LOAD_TRANSACTIONS_ERROR, } from './constants'; +import { ADD_NEW_WALLET } from '../WalletHoc/constants'; export const initialState = fromJS({ transactions: {}, @@ -85,6 +86,9 @@ function hubiiApiHocReducer(state = initialState, action) { .setIn(['prices', 'loading'], true) .set('transactions', fromJS({})) .set('balances', fromJS({})); + case ADD_NEW_WALLET: + return state + .setIn(['balances', action.newWallet.address], fromJS({ loading: true, error: null, assets: [] })); default: return state; } diff --git a/src/containers/HubiiApiHoc/saga.js b/src/containers/HubiiApiHoc/saga.js index 9ff49c4c..8cf0d1c5 100644 --- a/src/containers/HubiiApiHoc/saga.js +++ b/src/containers/HubiiApiHoc/saga.js @@ -12,11 +12,11 @@ import { import { delay } from 'redux-saga'; import nahmii from 'nahmii-sdk'; +import ethers from 'ethers'; import BigNumber from 'bignumber.js'; -import { Contract } from 'ethers'; import request, { requestWalletAPI } from 'utils/request'; -import erc20Abi from 'utils/abi/erc20Standard'; +import rpcRequest from 'utils/rpcRequest'; import { CHANGE_NETWORK, INIT_NETWORK_ACTIVITY } from 'containers/App/constants'; import { makeSelectCurrentNetwork } from 'containers/App/selectors'; import { ADD_NEW_WALLET } from 'containers/WalletHoc/constants'; @@ -25,6 +25,7 @@ import { makeSelectWallets, } from 'containers/WalletHoc/selectors'; + import { LOAD_WALLET_BALANCES, LOAD_SUPPORTED_TOKENS_SUCCESS, } from './constants'; @@ -43,6 +44,8 @@ import { import { makeSelectSupportedAssets } from './selectors'; +// https://stackoverflow.com/questions/48228662/get-token-balance-with-ethereum-rpc +const BALANCE_OF_FUNCTION_ID = ('0x70a08231'); export function* loadWalletBalances({ address, noPoll, onlyEth }, _network) { const network = _network || (yield select(makeSelectCurrentNetwork())); let supportedAssets = (yield select(makeSelectSupportedAssets())).toJS(); @@ -58,17 +61,43 @@ export function* loadWalletBalances({ address, noPoll, onlyEth }, _network) { /** * temporarily fetching balances from node until the backend is fixed */ + // get ETH balance const ethBal = yield network.provider.getBalance(address); if (onlyEth) { yield put(loadWalletBalancesSuccess(address, [{ currency: 'ETH', address, decimals: 18, balance: ethBal }])); return; } - // get all contract addresses. remove last entry because it's ETH + + // get token balances, batching all the requests in an array and sending them all at once + // https://stackoverflow.com/questions/48228662/get-token-balance-with-ethereum-rpc const tokenContractAddresses = supportedAssets.assets.map((a) => a.currency).slice(0, -1); - const tokenBals = yield all( - tokenContractAddresses.map((addr) => new Contract(addr, erc20Abi, network.provider).balanceOf(address)) - ); + // the first provider in network.provider.providers in an Infura node, which supports RPC calls + const jsonRpcProvider = network.provider.providers[0]; + + // pad the 20 byte address to 32 bytes + const paddedAddr = ethers.utils.hexlify(ethers.utils.padZeros(address, 32)); + + // concat the balanceOf('address') function identifier to the padded address. this shows our intention to call the + // balanceOf method with address as the parameter + const dataArr = ethers.utils.concat([BALANCE_OF_FUNCTION_ID, paddedAddr]); + const data = ethers.utils.hexlify(dataArr); + + // send a batch of RPC requests asking for all token balances + // https://www.jsonrpc.org/specification#batch + const requestBatch = tokenContractAddresses.map((contractAddr) => { + const params = [{ from: address, to: contractAddr, data }, 'latest']; + return { + method: 'eth_call', + params, + id: 42, + jsonrpc: '2.0', + }; + }); + const response = yield rpcRequest(jsonRpcProvider.url, JSON.stringify(requestBatch)); + + // process and return the response + const tokenBals = response.map((item) => new BigNumber(item.result)); const formattedBalances = tokenBals.reduce((acc, bal, i) => { if (!bal.gt('0')) return acc; const { currency } = supportedAssets.assets[i]; @@ -78,8 +107,8 @@ export function* loadWalletBalances({ address, noPoll, onlyEth }, _network) { } catch (err) { yield put(loadWalletBalancesError(address, err)); } finally { - const ONE_MIN_SEC_IN_MS = 1000 * 60; - yield delay(ONE_MIN_SEC_IN_MS); + const TEN_SEC_IN_MS = 1000 * 10; + yield delay(TEN_SEC_IN_MS); } if (noPoll) break; } diff --git a/src/containers/HubiiApiHoc/tests/reducer.test.js b/src/containers/HubiiApiHoc/tests/reducer.test.js index 0cfedfc9..2fdeb30b 100644 --- a/src/containers/HubiiApiHoc/tests/reducer.test.js +++ b/src/containers/HubiiApiHoc/tests/reducer.test.js @@ -25,6 +25,7 @@ import { import hubiiApiHocReducer from '../reducer'; +import { addNewWallet } from '../../WalletHoc/actions'; describe('hubiiApiHocReducer', () => { let state; @@ -180,4 +181,16 @@ describe('hubiiApiHocReducer', () => { expect(hubiiApiHocReducer(testState, changeNetwork('some network'))).toEqual(expected); }); }); + + describe('a wallet is added', () => { + it('should reset its balance to loading state', () => { + const newWalletAddr = '0x00'; + const testState = state + .setIn(['balances', newWalletAddr], fromJS({ assets: ['123'] })); + const expected = state + .setIn(['balances', newWalletAddr], fromJS({ loading: true, error: null, assets: [] })); + + expect(hubiiApiHocReducer(testState, addNewWallet({ address: newWalletAddr }))).toEqual(expected); + }); + }); }); diff --git a/src/containers/WalletsOverview/index.js b/src/containers/WalletsOverview/index.js index 7ee1778c..ad8ab9ef 100644 --- a/src/containers/WalletsOverview/index.js +++ b/src/containers/WalletsOverview/index.js @@ -3,7 +3,7 @@ import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import { compose } from 'redux'; import { createStructuredSelector } from 'reselect'; -import { Row, Col, Alert } from 'antd'; +import { Row, Col } from 'antd'; import { injectIntl } from 'react-intl'; import { getBreakdown } from 'utils/wallet'; @@ -113,15 +113,6 @@ export class WalletsOverview extends React.PureComponent { // eslint-disable-lin {walletCards} - {formatMessage({ id: 'my_balances_notes' })} - } - type="info" - showIcon - style={{ margin: '2rem 0' }} - /> { diff --git a/src/containers/WalletsOverview/tests/__snapshots__/index.test.js.snap b/src/containers/WalletsOverview/tests/__snapshots__/index.test.js.snap index e3fbe8f3..3526b680 100644 --- a/src/containers/WalletsOverview/tests/__snapshots__/index.test.js.snap +++ b/src/containers/WalletsOverview/tests/__snapshots__/index.test.js.snap @@ -1184,21 +1184,6 @@ ShallowWrapper { /> - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> , , - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" -/>, ], "lg": 16, "md": 12, @@ -5803,25 +5758,6 @@ ShallowWrapper { ], "type": [Function], }, - Object { - "instance": null, - "key": undefined, - "nodeType": "class", - "props": Object { - "description": - my_balances_notes -, - "message": "why_balance_not_updated", - "showIcon": true, - "style": Object { - "margin": "2rem 0", - }, - "type": "info", - }, - "ref": null, - "rendered": null, - "type": [Function], - }, ], "type": [Function], }, @@ -6762,21 +6698,6 @@ ShallowWrapper { /> - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> , , - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" -/>, ], "lg": 16, "md": 12, @@ -11381,25 +11272,6 @@ ShallowWrapper { ], "type": [Function], }, - Object { - "instance": null, - "key": undefined, - "nodeType": "class", - "props": Object { - "description": - my_balances_notes -, - "message": "why_balance_not_updated", - "showIcon": true, - "style": Object { - "margin": "2rem 0", - }, - "type": "info", - }, - "ref": null, - "rendered": null, - "type": [Function], - }, ], "type": [Function], }, @@ -12617,21 +12489,6 @@ ShallowWrapper { /> - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> , , - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" -/>, ], "lg": 16, "md": 12, @@ -17236,25 +17063,6 @@ ShallowWrapper { ], "type": [Function], }, - Object { - "instance": null, - "key": undefined, - "nodeType": "class", - "props": Object { - "description": - my_balances_notes -, - "message": "why_balance_not_updated", - "showIcon": true, - "style": Object { - "margin": "2rem 0", - }, - "type": "info", - }, - "ref": null, - "rendered": null, - "type": [Function], - }, ], "type": [Function], }, @@ -18195,21 +18003,6 @@ ShallowWrapper { /> - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> , , - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" -/>, ], "lg": 16, "md": 12, @@ -22814,25 +22577,6 @@ ShallowWrapper { ], "type": [Function], }, - Object { - "instance": null, - "key": undefined, - "nodeType": "class", - "props": Object { - "description": - my_balances_notes -, - "message": "why_balance_not_updated", - "showIcon": true, - "style": Object { - "margin": "2rem 0", - }, - "type": "info", - }, - "ref": null, - "rendered": null, - "type": [Function], - }, ], "type": [Function], }, @@ -23046,21 +22790,6 @@ ShallowWrapper { add_wallet_tip - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> , , - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" -/>, ], "lg": 16, "md": 12, @@ -23236,25 +22935,6 @@ ShallowWrapper { }, "type": [Function], }, - Object { - "instance": null, - "key": undefined, - "nodeType": "class", - "props": Object { - "description": - my_balances_notes -, - "message": "why_balance_not_updated", - "showIcon": true, - "style": Object { - "margin": "2rem 0", - }, - "type": "info", - }, - "ref": null, - "rendered": null, - "type": [Function], - }, ], "type": [Function], }, @@ -23345,21 +23025,6 @@ ShallowWrapper { add_wallet_tip - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> , , - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" -/>, ], "lg": 16, "md": 12, @@ -23535,25 +23170,6 @@ ShallowWrapper { }, "type": [Function], }, - Object { - "instance": null, - "key": undefined, - "nodeType": "class", - "props": Object { - "description": - my_balances_notes -, - "message": "why_balance_not_updated", - "showIcon": true, - "style": Object { - "margin": "2rem 0", - }, - "type": "info", - }, - "ref": null, - "rendered": null, - "type": [Function], - }, ], "type": [Function], }, @@ -23809,21 +23425,6 @@ ShallowWrapper { add_wallet_tip - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> , , - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" -/>, ], "lg": 16, "md": 12, @@ -23999,25 +23570,6 @@ ShallowWrapper { }, "type": [Function], }, - Object { - "instance": null, - "key": undefined, - "nodeType": "class", - "props": Object { - "description": - my_balances_notes -, - "message": "why_balance_not_updated", - "showIcon": true, - "style": Object { - "margin": "2rem 0", - }, - "type": "info", - }, - "ref": null, - "rendered": null, - "type": [Function], - }, ], "type": [Function], }, @@ -24108,21 +23660,6 @@ ShallowWrapper { add_wallet_tip - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> , , - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" -/>, ], "lg": 16, "md": 12, @@ -24298,25 +23805,6 @@ ShallowWrapper { }, "type": [Function], }, - Object { - "instance": null, - "key": undefined, - "nodeType": "class", - "props": Object { - "description": - my_balances_notes -, - "message": "why_balance_not_updated", - "showIcon": true, - "style": Object { - "margin": "2rem 0", - }, - "type": "info", - }, - "ref": null, - "rendered": null, - "type": [Function], - }, ], "type": [Function], }, @@ -25612,21 +25100,6 @@ ShallowWrapper { /> - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> , , - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" -/>, ], "lg": 16, "md": 12, @@ -30271,25 +29714,6 @@ ShallowWrapper { ], "type": [Function], }, - Object { - "instance": null, - "key": undefined, - "nodeType": "class", - "props": Object { - "description": - my_balances_notes -, - "message": "why_balance_not_updated", - "showIcon": true, - "style": Object { - "margin": "2rem 0", - }, - "type": "info", - }, - "ref": null, - "rendered": null, - "type": [Function], - }, ], "type": [Function], }, @@ -31272,21 +30696,6 @@ ShallowWrapper { /> - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> , , - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" -/>, ], "lg": 16, "md": 12, @@ -35931,25 +35310,6 @@ ShallowWrapper { ], "type": [Function], }, - Object { - "instance": null, - "key": undefined, - "nodeType": "class", - "props": Object { - "description": - my_balances_notes -, - "message": "why_balance_not_updated", - "showIcon": true, - "style": Object { - "margin": "2rem 0", - }, - "type": "info", - }, - "ref": null, - "rendered": null, - "type": [Function], - }, ], "type": [Function], }, @@ -37234,21 +36594,6 @@ ShallowWrapper { /> - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> , , - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" -/>, ], "lg": 16, "md": 12, @@ -41853,25 +41168,6 @@ ShallowWrapper { ], "type": [Function], }, - Object { - "instance": null, - "key": undefined, - "nodeType": "class", - "props": Object { - "description": - my_balances_notes -, - "message": "why_balance_not_updated", - "showIcon": true, - "style": Object { - "margin": "2rem 0", - }, - "type": "info", - }, - "ref": null, - "rendered": null, - "type": [Function], - }, ], "type": [Function], }, @@ -42812,21 +42108,6 @@ ShallowWrapper { /> - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> , , - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" -/>, ], "lg": 16, "md": 12, @@ -47431,25 +46682,6 @@ ShallowWrapper { ], "type": [Function], }, - Object { - "instance": null, - "key": undefined, - "nodeType": "class", - "props": Object { - "description": - my_balances_notes -, - "message": "why_balance_not_updated", - "showIcon": true, - "style": Object { - "margin": "2rem 0", - }, - "type": "info", - }, - "ref": null, - "rendered": null, - "type": [Function], - }, ], "type": [Function], }, @@ -48692,21 +47924,6 @@ ShallowWrapper { /> - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> , , - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" -/>, ], "lg": 16, "md": 12, @@ -53311,25 +52498,6 @@ ShallowWrapper { ], "type": [Function], }, - Object { - "instance": null, - "key": undefined, - "nodeType": "class", - "props": Object { - "description": - my_balances_notes -, - "message": "why_balance_not_updated", - "showIcon": true, - "style": Object { - "margin": "2rem 0", - }, - "type": "info", - }, - "ref": null, - "rendered": null, - "type": [Function], - }, ], "type": [Function], }, @@ -54270,21 +53438,6 @@ ShallowWrapper { /> - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" - /> , , - - my_balances_notes - - } - message="why_balance_not_updated" - showIcon={true} - style={ - Object { - "margin": "2rem 0", - } - } - type="info" -/>, ], "lg": 16, "md": 12, @@ -58889,25 +58012,6 @@ ShallowWrapper { ], "type": [Function], }, - Object { - "instance": null, - "key": undefined, - "nodeType": "class", - "props": Object { - "description": - my_balances_notes -, - "message": "why_balance_not_updated", - "showIcon": true, - "style": Object { - "margin": "2rem 0", - }, - "type": "info", - }, - "ref": null, - "rendered": null, - "type": [Function], - }, ], "type": [Function], }, diff --git a/src/translations/en.json b/src/translations/en.json index fdfb956e..ea229b5d 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -175,7 +175,5 @@ "airdriip_registration_problem": "Sorry, something went wrong during registration: {message}", "airdriip_address_is_already_registered": "Address already registered, no further action is required", "airdriip_testnet_warning": "hubii core is currently connected to a nahmii test network. Registrations made on a test network will NOT qualify your wallet for the nahmii airdriip. To register for the real airdriip, you must connect hubii core to the mainnet by navigating to 'Settings', and changing the 'Network' option to 'Homestead [MAINNET]'.", - "warning": "Warning", - "why_balance_not_updated": "Why isn't my balance up to date?", - "my_balances_notes": "In this alpha build of hubii core, balances may take a few minutes to update. The more wallets you have imported, the slower balance updates will be. Until we improve this, we recommend having less than 4 wallets imported at any one time. If you want to double check a balance, we recommend using Etherscan." + "warning": "Warning" } diff --git a/src/translations/zh.json b/src/translations/zh.json index 8857bd28..ba9145e5 100644 --- a/src/translations/zh.json +++ b/src/translations/zh.json @@ -175,7 +175,5 @@ "airdriip_registration_problem": "抱歉,注册过程中发生错误:{message}", "airdriip_address_is_already_registered": "钱包地址已经被注册,无需进行更多操作", "airdriip_testnet_warning": "hubii core 当前连接的是 nahmii 测试网络。在测试网络的注册将无法获得 nahmii 空投。正确的注册方式,您应该在“设置”中连接到以太坊主网,选择“网络”选项为'Homestead [MAINNET]'。", - "warning": "警告", - "why_balance_not_updated": "为什么钱包余额没有同步?", - "my_balances_notes": "在此 alpha 版本中,可能需要几分钟的时间来同步钱包余额。钱包数量越多,需要同步的时间越长。 暂时,我们建议最多导入4个钱包。如果您想确认一下余额,我们建议使用 Etherscan。" -} \ No newline at end of file + "warning": "警告" +} diff --git a/src/utils/rpcRequest.js b/src/utils/rpcRequest.js new file mode 100644 index 00000000..590d46bc --- /dev/null +++ b/src/utils/rpcRequest.js @@ -0,0 +1,55 @@ +/** + * Modified version of the ethers.js providers.Provider.fetchJson, allowing batched RPC requests + */ +export default function (url, json) { + return new Promise((resolve, reject) => { + const request = new XMLHttpRequest(); + + if (json) { + request.open('POST', url, true); + request.setRequestHeader('Content-Type', 'application/json'); + } else { + request.open('GET', url, true); + } + + request.onreadystatechange = () => { + if (request.readyState !== 4) { return; } + + let result; + try { + result = JSON.parse(request.responseText); + } catch (error) { + const jsonError = new Error('invalid json response'); + jsonError.orginialError = error; + jsonError.responseText = request.responseText; + reject(jsonError); + return; + } + + if (request.status !== 200) { + const error = new Error(`invalid response - ${request.status}`); + error.statusCode = request.statusCode; + reject(error); + return; + } + + resolve(result); + }; + + request.onerror = (error) => { + reject(error); + }; + + try { + if (json) { + request.send(json); + } else { + request.send(); + } + } catch (error) { + const connectionError = new Error('connection error'); + connectionError.error = error; + reject(connectionError); + } + }); +} From dd27dd03b6b31007f455f18f8c10f270ce3ec16c Mon Sep 17 00:00:00 2001 From: liamaharon Date: Fri, 2 Nov 2018 13:34:46 +1100 Subject: [PATCH 2/2] Increase delay between balance refresh --- src/containers/HubiiApiHoc/saga.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/containers/HubiiApiHoc/saga.js b/src/containers/HubiiApiHoc/saga.js index 8cf0d1c5..fda66f1f 100644 --- a/src/containers/HubiiApiHoc/saga.js +++ b/src/containers/HubiiApiHoc/saga.js @@ -107,8 +107,8 @@ export function* loadWalletBalances({ address, noPoll, onlyEth }, _network) { } catch (err) { yield put(loadWalletBalancesError(address, err)); } finally { - const TEN_SEC_IN_MS = 1000 * 10; - yield delay(TEN_SEC_IN_MS); + const TWENTY_SEC_IN_MS = 1000 * 20; + yield delay(TWENTY_SEC_IN_MS); } if (noPoll) break; }