diff --git a/src/accountBalances/accountBalancesActions.js b/src/accountBalances/accountBalancesActions.js index 8a7df81..3b9152a 100644 --- a/src/accountBalances/accountBalancesActions.js +++ b/src/accountBalances/accountBalancesActions.js @@ -1,17 +1,8 @@ -const ACCOUNTS_FETCHING = 'ACCOUNTS_FETCHING' +import { ACCOUNT_BALANCES_FETCHING } from './constants' -export function accountsFetching (results) { +export function accountBalancesFetching (web3) { return { - type: ACCOUNTS_FETCHING, - payload: results - } -} - -const ACCOUNTS_FETCHED = 'ACCOUNTS_FETCHED' - -export function accountsFetched (results) { - return { - type: ACCOUNTS_FETCHED, - payload: results + type: ACCOUNT_BALANCES_FETCHING, + web3 } } diff --git a/src/accountBalances/accountBalancesMiddleware.js b/src/accountBalances/accountBalancesMiddleware.js new file mode 100644 index 0000000..0230ab5 --- /dev/null +++ b/src/accountBalances/accountBalancesMiddleware.js @@ -0,0 +1,21 @@ +import {WEB3_INITIALIZED} from '../web3/constants' +import { ACCOUNTS_FETCHED } from '../accounts/constants' +import { accountBalancesFetching } from './accountBalancesActions' + +export const accountBalancesMiddleware = web3 => store => next => action => { + const { type } = action; + + if (type === WEB3_INITIALIZED) + web3 = action.web3; + + if((type === ACCOUNTS_FETCHED) && web3){ + next(action); + store.dispatch(accountBalancesFetching(web3)); + return; + } + + return next(action); +} + +const initializedMiddleware = accountBalancesMiddleware(undefined) +export default initializedMiddleware diff --git a/src/accountBalances/accountBalancesSaga.js b/src/accountBalances/accountBalancesSaga.js index e889eea..88285f7 100644 --- a/src/accountBalances/accountBalancesSaga.js +++ b/src/accountBalances/accountBalancesSaga.js @@ -1,18 +1,20 @@ import { call, put, select, takeLatest } from 'redux-saga/effects' import * as AccountBalancesActions from './constants' +import { ACCOUNT_BALANCES_FETCHING } from './constants' export function * getAccountBalances (action) { const accounts = yield select(getAccountsState) const web3 = action.web3 - if (!accounts) { console.error('No accounts found while attempting to fetch balances!') } + let account; + try { for (let i in accounts) { - var account = accounts[i] - var accountBalance = yield call(web3.eth.getBalance, account) + account = accounts[i] + let accountBalance = yield call(web3.eth.getBalance, account) yield put({ type: AccountBalancesActions.ACCOUNT_BALANCE_FETCHED, account, accountBalance }) } @@ -28,7 +30,7 @@ export function * getAccountBalances (action) { export const getAccountsState = state => state.accounts function * accountBalancesSaga () { - yield takeLatest(AccountBalancesActions.ACCOUNT_BALANCES_FETCHING, getAccountBalances) + yield takeLatest(ACCOUNT_BALANCES_FETCHING, getAccountBalances) } export default accountBalancesSaga diff --git a/src/accounts/accountsMiddleware.js b/src/accounts/accountsMiddleware.js index 353669c..8a5b585 100644 --- a/src/accounts/accountsMiddleware.js +++ b/src/accounts/accountsMiddleware.js @@ -10,7 +10,6 @@ export const accountsMiddleware = () => store => next => action => { else { store.dispatch(accountsListening()); window.ethereum.on('accountsChanged', function (accounts) { - console.debug("Account changed") store.dispatch(accountsFetched(accounts)); }); } diff --git a/src/index.js b/src/index.js index 80644c1..5c758fa 100644 --- a/src/index.js +++ b/src/index.js @@ -18,6 +18,7 @@ import web3Reducer from './web3/web3Reducer' // Middleware import drizzleMiddleware from './drizzle-middleware' import accountsMiddleware from './accounts/accountsMiddleware' +import accountBalancesMiddleware from './accountBalances/accountBalancesMiddleware' // Sagas import accountsSaga from './accounts/accountsSaga' @@ -39,7 +40,8 @@ const drizzleReducers = { const drizzleMiddlewares = [ drizzleMiddleware, - accountsMiddleware + accountsMiddleware, + accountBalancesMiddleware ] const drizzleSagas = [ diff --git a/src/web3/web3Saga.js b/src/web3/web3Saga.js index aea1e37..f96b209 100644 --- a/src/web3/web3Saga.js +++ b/src/web3/web3Saga.js @@ -12,7 +12,7 @@ export function * initializeWeb3 (options) { let web3 = {} if (options.customProvider) { - yield put({ type: Action.WEB3_INITIALIZED }) + yield put({ type: Action.WEB3_INITIALIZED, web3: options.customProvider }) return options.customProvider } @@ -24,7 +24,7 @@ export function * initializeWeb3 (options) { // unless user opts out and then it will return undefined const selectedAccount = yield call([ethereum, 'enable']) - yield put({ type: Action.WEB3_INITIALIZED }) + yield put({ type: Action.WEB3_INITIALIZED, web3 }) if (!selectedAccount) { yield put({ type: Action.WEB3_USER_DENIED }) @@ -40,7 +40,7 @@ export function * initializeWeb3 (options) { // Checking if Web3 has been injected by the browser (Mist/MetaMask) // Use Mist/MetaMask's provider. web3 = new Web3(window.web3.currentProvider) - yield put({ type: Action.WEB3_INITIALIZED }) + yield put({ type: Action.WEB3_INITIALIZED, web3 }) return web3 } else if (options.fallback) { @@ -51,7 +51,7 @@ export function * initializeWeb3 (options) { options.fallback.url ) web3 = new Web3(provider) - yield put({ type: Action.WEB3_INITIALIZED }) + yield put({ type: Action.WEB3_INITIALIZED, web3 }) return web3 default: