Browse Source

Grab account balances automatically

develop
Ezerous 4 years ago
parent
commit
75f29c7c91
  1. 17
      src/accountBalances/accountBalancesActions.js
  2. 21
      src/accountBalances/accountBalancesMiddleware.js
  3. 10
      src/accountBalances/accountBalancesSaga.js
  4. 1
      src/accounts/accountsMiddleware.js
  5. 4
      src/index.js
  6. 8
      src/web3/web3Saga.js

17
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 { return {
type: ACCOUNTS_FETCHING, type: ACCOUNT_BALANCES_FETCHING,
payload: results web3
}
}
const ACCOUNTS_FETCHED = 'ACCOUNTS_FETCHED'
export function accountsFetched (results) {
return {
type: ACCOUNTS_FETCHED,
payload: results
} }
} }

21
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

10
src/accountBalances/accountBalancesSaga.js

@ -1,18 +1,20 @@
import { call, put, select, takeLatest } from 'redux-saga/effects' import { call, put, select, takeLatest } from 'redux-saga/effects'
import * as AccountBalancesActions from './constants' import * as AccountBalancesActions from './constants'
import { ACCOUNT_BALANCES_FETCHING } from './constants'
export function * getAccountBalances (action) { export function * getAccountBalances (action) {
const accounts = yield select(getAccountsState) const accounts = yield select(getAccountsState)
const web3 = action.web3 const web3 = action.web3
if (!accounts) { if (!accounts) {
console.error('No accounts found while attempting to fetch balances!') console.error('No accounts found while attempting to fetch balances!')
} }
let account;
try { try {
for (let i in accounts) { for (let i in accounts) {
var account = accounts[i] account = accounts[i]
var accountBalance = yield call(web3.eth.getBalance, account) let accountBalance = yield call(web3.eth.getBalance, account)
yield put({ type: AccountBalancesActions.ACCOUNT_BALANCE_FETCHED, account, accountBalance }) yield put({ type: AccountBalancesActions.ACCOUNT_BALANCE_FETCHED, account, accountBalance })
} }
@ -28,7 +30,7 @@ export function * getAccountBalances (action) {
export const getAccountsState = state => state.accounts export const getAccountsState = state => state.accounts
function * accountBalancesSaga () { function * accountBalancesSaga () {
yield takeLatest(AccountBalancesActions.ACCOUNT_BALANCES_FETCHING, getAccountBalances) yield takeLatest(ACCOUNT_BALANCES_FETCHING, getAccountBalances)
} }
export default accountBalancesSaga export default accountBalancesSaga

1
src/accounts/accountsMiddleware.js

@ -10,7 +10,6 @@ export const accountsMiddleware = () => store => next => action => {
else { else {
store.dispatch(accountsListening()); store.dispatch(accountsListening());
window.ethereum.on('accountsChanged', function (accounts) { window.ethereum.on('accountsChanged', function (accounts) {
console.debug("Account changed")
store.dispatch(accountsFetched(accounts)); store.dispatch(accountsFetched(accounts));
}); });
} }

4
src/index.js

@ -18,6 +18,7 @@ import web3Reducer from './web3/web3Reducer'
// Middleware // Middleware
import drizzleMiddleware from './drizzle-middleware' import drizzleMiddleware from './drizzle-middleware'
import accountsMiddleware from './accounts/accountsMiddleware' import accountsMiddleware from './accounts/accountsMiddleware'
import accountBalancesMiddleware from './accountBalances/accountBalancesMiddleware'
// Sagas // Sagas
import accountsSaga from './accounts/accountsSaga' import accountsSaga from './accounts/accountsSaga'
@ -39,7 +40,8 @@ const drizzleReducers = {
const drizzleMiddlewares = [ const drizzleMiddlewares = [
drizzleMiddleware, drizzleMiddleware,
accountsMiddleware accountsMiddleware,
accountBalancesMiddleware
] ]
const drizzleSagas = [ const drizzleSagas = [

8
src/web3/web3Saga.js

@ -12,7 +12,7 @@ export function * initializeWeb3 (options) {
let web3 = {} let web3 = {}
if (options.customProvider) { if (options.customProvider) {
yield put({ type: Action.WEB3_INITIALIZED }) yield put({ type: Action.WEB3_INITIALIZED, web3: options.customProvider })
return options.customProvider return options.customProvider
} }
@ -24,7 +24,7 @@ export function * initializeWeb3 (options) {
// unless user opts out and then it will return undefined // unless user opts out and then it will return undefined
const selectedAccount = yield call([ethereum, 'enable']) const selectedAccount = yield call([ethereum, 'enable'])
yield put({ type: Action.WEB3_INITIALIZED }) yield put({ type: Action.WEB3_INITIALIZED, web3 })
if (!selectedAccount) { if (!selectedAccount) {
yield put({ type: Action.WEB3_USER_DENIED }) 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) // Checking if Web3 has been injected by the browser (Mist/MetaMask)
// Use Mist/MetaMask's provider. // Use Mist/MetaMask's provider.
web3 = new Web3(window.web3.currentProvider) web3 = new Web3(window.web3.currentProvider)
yield put({ type: Action.WEB3_INITIALIZED }) yield put({ type: Action.WEB3_INITIALIZED, web3 })
return web3 return web3
} else if (options.fallback) { } else if (options.fallback) {
@ -51,7 +51,7 @@ export function * initializeWeb3 (options) {
options.fallback.url options.fallback.url
) )
web3 = new Web3(provider) web3 = new Web3(provider)
yield put({ type: Action.WEB3_INITIALIZED }) yield put({ type: Action.WEB3_INITIALIZED, web3 })
return web3 return web3
default: default:

Loading…
Cancel
Save