Browse Source

Cleanup

develop
Ezerous 4 years ago
parent
commit
07dab8e12b
  1. 3
      .gitignore
  2. 4
      README.md
  3. 2
      package.json
  4. 2
      src/Drizzle.js
  5. 2
      src/DrizzleContract.js
  6. 5
      src/accountBalances/accountBalancesActions.js
  7. 4
      src/accountBalances/accountBalancesMiddleware.js
  8. 4
      src/accountBalances/accountBalancesReducer.js
  9. 23
      src/accountBalances/accountBalancesSaga.js
  10. 4
      src/accountBalances/constants.js
  11. 28
      src/accounts/accountsActions.js
  12. 10
      src/accounts/accountsMiddleware.js
  13. 6
      src/accounts/accountsReducer.js
  14. 18
      src/accounts/accountsSaga.js
  15. 4
      src/accounts/constants.js
  16. 0
      src/blocks/blockActions.js
  17. 2
      src/blocks/blocksReducer.js
  18. 2
      src/blocks/blocksSaga.js
  19. 3
      src/contracts/contractsSaga.js
  20. 6
      src/drizzle-middleware.js
  21. 0
      src/drizzleStatus/drizzleActions.js
  22. 2
      src/drizzleStatus/drizzleStatusReducer.js
  23. 5
      src/drizzleStatus/drizzleStatusSaga.js
  24. 12
      src/index.js
  25. 2
      src/rootSaga.js
  26. 2
      src/transactions/transactionStackReducer.js
  27. 0
      src/transactions/transactionsActions.js
  28. 2
      src/transactions/transactionsReducer.js
  29. 1
      src/web3/web3Saga.js
  30. 1133
      yarn.lock

3
.gitignore

@ -10,3 +10,6 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# npm
*.tgz

4
README.md

@ -1,3 +1,7 @@
# @ezerous/drizzle
[![Version](https://img.shields.io/npm/v/@ezerous/drizzle.svg)](https://www.npmjs.com/package/@ezerous/drizzle)
[![Dependencies](https://img.shields.io/david/Ezerous/@ezerous/drizzle.svg)](https://david-dm.org/Ezerous/drizzle)
[![License](https://img.shields.io/npm/l/@ezerous/drizzle.svg)](https://www.npmjs.com/package/@ezerous/drizzle)
A reactive data-store for web3 and smart contracts. A modified version of [@drizzle/store](https://github.com/trufflesuite/drizzle/tree/develop/packages/store).

2
package.json

@ -11,6 +11,6 @@
"is-plain-object": "4.1.1",
"redux": "4.0.5",
"redux-saga": "1.1.3",
"web3": "1.2.6"
"web3": "1.2.11"
}
}

2
src/Drizzle.js

@ -3,7 +3,7 @@ import defaultOptions from './defaultOptions'
import merge from './mergeOptions'
import DrizzleContract from './DrizzleContract'
import * as ContractActions from './contracts/constants'
import * as DrizzleActions from './drizzleStatus/constants'
import * as DrizzleActions from './drizzleStatus/drizzleActions'
// Load as promise so that async Drizzle initialization can still resolve
var isEnvReadyPromise = new Promise((resolve, reject) => {

2
src/DrizzleContract.js

@ -1,5 +1,5 @@
import * as ContractActions from './contracts/constants'
import * as TransactionsActions from './transactions/constants'
import * as TransactionsActions from './transactions/transactionsActions'
import { isGetterFunction, isSetterFunction } from './contractStateUtils'
class DrizzleContract {

5
src/accountBalances/accountBalancesActions.js

@ -1,4 +1,7 @@
import { ACCOUNT_BALANCES_FETCHING } from './constants'
export const ACCOUNT_BALANCE_FETCHED = 'ACCOUNT_BALANCE_FETCHED'
export const ACCOUNT_BALANCES_FETCHED = 'ACCOUNT_BALANCES_FETCHED'
export const ACCOUNT_BALANCES_FETCHING = 'ACCOUNT_BALANCES_FETCHING'
export const ACCOUNT_BALANCES_FAILED = 'ACCOUNT_BALANCES_FAILED'
export function accountBalancesFetching (web3) {
return {

4
src/accountBalances/accountBalancesMiddleware.js

@ -1,6 +1,6 @@
import {WEB3_INITIALIZED} from '../web3/constants'
import { ACCOUNTS_FETCHED } from '../accounts/constants'
import { WEB3_INITIALIZED } from '../web3/constants'
import { accountBalancesFetching } from './accountBalancesActions'
import { ACCOUNTS_FETCHED } from '../accounts/accountsActions'
export const accountBalancesMiddleware = web3 => store => next => action => {
const { type } = action;

4
src/accountBalances/accountBalancesReducer.js

@ -1,9 +1,9 @@
import * as AccountBalancesActions from './constants'
import { ACCOUNT_BALANCE_FETCHED } from './accountBalancesActions'
const initialState = {}
const accountBalancesReducer = (state = initialState, action) => {
if (action.type === AccountBalancesActions.ACCOUNT_BALANCE_FETCHED) {
if (action.type === ACCOUNT_BALANCE_FETCHED) {
return {
...state,
[action.account]: action.accountBalance

23
src/accountBalances/accountBalancesSaga.js

@ -1,6 +1,10 @@
import { call, put, select, takeLatest } from 'redux-saga/effects'
import * as AccountBalancesActions from './constants'
import { ACCOUNT_BALANCES_FETCHING } from './constants'
import {
ACCOUNT_BALANCES_FAILED,
ACCOUNT_BALANCE_FETCHED,
ACCOUNT_BALANCES_FETCHED,
ACCOUNT_BALANCES_FETCHING
} from './accountBalancesActions'
export function * getAccountBalances (action) {
const accounts = yield select(getAccountsState)
@ -13,18 +17,17 @@ export function * getAccountBalances (action) {
try {
for (let i in accounts) {
account = accounts[i]
let 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 })
yield put({ type: ACCOUNT_BALANCE_FETCHED, account, accountBalance });
}
yield put({ type: ACCOUNT_BALANCES_FETCHED });
} catch (error) {
yield put({ type: AccountBalancesActions.ACCOUNT_BALANCE_FAILED, error })
console.error('Error fetching account ' + account + ' balance:')
console.error(error)
yield put({ type: ACCOUNT_BALANCES_FAILED, error });
console.error('Error fetching account ' + account + ' balance:');
console.error(error);
}
yield put({ type: AccountBalancesActions.ACCOUNT_BALANCES_FETCHED })
}
export const getAccountsState = state => state.accounts

4
src/accountBalances/constants.js

@ -1,4 +0,0 @@
export const ACCOUNT_BALANCE_FETCHED = 'ACCOUNT_BALANCE_FETCHED'
export const ACCOUNT_BALANCE_FAILED = 'ACCOUNT_BALANCE_FAILED'
export const ACCOUNT_BALANCES_FETCHED = 'ACCOUNT_BALANCES_FETCHED'
export const ACCOUNT_BALANCES_FETCHING = 'ACCOUNT_BALANCES_FETCHING'

28
src/accounts/accountsActions.js

@ -1,27 +1,17 @@
import * as AccountsActions from './constants'
export const ACCOUNTS_FETCHING = 'ACCOUNTS_FETCHING'
export const ACCOUNTS_FETCHED = 'ACCOUNTS_FETCHED'
export const ACCOUNTS_FAILED = 'ACCOUNTS_FAILED'
export const ACCOUNTS_LISTENING = 'ACCOUNTS_LISTENING'
export function accountsFetching (results) {
export function accountsFetched (accounts) {
return {
type: AccountsActions.ACCOUNTS_FETCHING,
type: ACCOUNTS_FETCHED,
accounts
}
}
export function accountsFetched (results) {
export function accountsListening () {
return {
type: AccountsActions.ACCOUNTS_FETCHED,
accounts: results
}
}
export function accountsListening (results) {
return {
type: AccountsActions.ACCOUNTS_LISTENING,
}
}
export function accountsFailed (error) {
return {
type: AccountsActions.ACCOUNTS_FAILED,
error
type: ACCOUNTS_LISTENING,
}
}

10
src/accounts/accountsMiddleware.js

@ -8,10 +8,14 @@ export const accountsMiddleware = () => store => next => action => {
if(!window.ethereum)
console.warn('No Metamask detected, not subscribed to account changes!')
else {
store.dispatch(accountsListening());
window.ethereum.on('accountsChanged', function (accounts) {
store.dispatch(accountsFetched(accounts));
const { web3 } = action;
window.ethereum.on('accountsChanged', accounts => {
// For some reason accounts here are returned with lowercase letters, so we need to patch them
let patchedAccounts = Array.from(accounts);
patchedAccounts.forEach((account, i) => patchedAccounts[i] = web3.utils.toChecksumAddress(account));
store.dispatch(accountsFetched(patchedAccounts));
});
store.dispatch(accountsListening());
}
}
return next(action)

6
src/accounts/accountsReducer.js

@ -1,13 +1,13 @@
import * as AccountsActions from './constants'
import { ACCOUNTS_FETCHED, ACCOUNTS_FETCHING } from './accountsActions'
const initialState = {}
const accountsReducer = (state = initialState, action) => {
if (action.type === AccountsActions.ACCOUNTS_FETCHING) {
if (action.type === ACCOUNTS_FETCHING) {
return state
}
if (action.type === AccountsActions.ACCOUNTS_FETCHED) {
if (action.type === ACCOUNTS_FETCHED) {
return Object.assign({}, state, action.accounts)
}

18
src/accounts/accountsSaga.js

@ -1,29 +1,23 @@
import { call, put, takeLatest } from 'redux-saga/effects'
import * as AccountsActions from './constants'
import { call, put } from 'redux-saga/effects'
import { ACCOUNTS_FAILED, ACCOUNTS_FETCHED, ACCOUNTS_FETCHING } from './accountsActions'
/*
* Fetch Accounts List
* Manually Fetch Accounts List
*/
export function * getAccounts (action) {
const web3 = action.web3
try {
yield put({ type: ACCOUNTS_FETCHING });
const accounts = yield call(web3.eth.getAccounts)
if (!accounts)
throw 'No accounts found!'
yield put({ type: AccountsActions.ACCOUNTS_FETCHED, accounts })
yield put({ type: ACCOUNTS_FETCHED, accounts })
} catch (error) {
yield put({ type: AccountsActions.ACCOUNTS_FAILED, error })
yield put({ type: ACCOUNTS_FAILED, error })
console.error('Error fetching accounts:')
console.error(error)
}
}
function * accountsSaga () {
yield takeLatest(AccountsActions.ACCOUNTS_FETCHING, getAccounts)
}
export default accountsSaga

4
src/accounts/constants.js

@ -1,4 +0,0 @@
export const ACCOUNTS_FETCHING = 'ACCOUNTS_FETCHING'
export const ACCOUNTS_FETCHED = 'ACCOUNTS_FETCHED'
export const ACCOUNTS_FAILED = 'ACCOUNTS_FAILED'
export const ACCOUNTS_LISTENING = 'ACCOUNTS_LISTENING'

0
src/blocks/constants.js → src/blocks/blockActions.js

2
src/blocks/blocksReducer.js

@ -1,4 +1,4 @@
import * as BlocksActions from './constants'
import * as BlocksActions from './blockActions'
const initialState = {}

2
src/blocks/blocksSaga.js

@ -1,6 +1,6 @@
import { END, eventChannel } from 'redux-saga'
import { call, put, take, takeEvery, takeLatest, all } from 'redux-saga/effects'
import * as BlocksActions from './constants'
import * as BlocksActions from './blockActions'
/*
* Listen for Blocks

3
src/contracts/contractsSaga.js

@ -1,12 +1,11 @@
import { END, eventChannel } from 'redux-saga'
import { call, put, select, take, takeEvery } from 'redux-saga/effects'
import * as ContractActions from './constants'
import * as TransactionsActions from '../transactions/constants'
import * as TransactionsActions from '../transactions/transactionsActions'
/*
* Events
*/
export function createContractEventChannel ({
contract,
eventName,

6
src/drizzle-middleware.js

@ -1,6 +1,6 @@
import * as DrizzleActions from './drizzleStatus/constants'
import * as AccountsActions from './accounts/constants'
import * as DrizzleActions from './drizzleStatus/drizzleActions'
import * as ContractActions from './contracts/constants'
import { ACCOUNTS_FETCHED } from './accounts/accountsActions'
export const drizzleMiddleware = drizzleInstance => store => next => action => {
const { type } = action
@ -10,7 +10,7 @@ export const drizzleMiddleware = drizzleInstance => store => next => action => {
}
if (
type === AccountsActions.ACCOUNTS_FETCHED &&
type === ACCOUNTS_FETCHED &&
drizzleInstance &&
drizzleInstance.contractList.length
) {

0
src/drizzleStatus/constants.js → src/drizzleStatus/drizzleActions.js

2
src/drizzleStatus/drizzleStatusReducer.js

@ -1,4 +1,4 @@
import * as DrizzleActions from './constants'
import * as DrizzleActions from './drizzleActions'
const initialState = {
initialized: false

5
src/drizzleStatus/drizzleStatusSaga.js

@ -4,9 +4,8 @@ import { call, put, takeLatest } from 'redux-saga/effects'
import { initializeWeb3, getNetworkId } from '../web3/web3Saga'
import { getAccounts } from '../accounts/accountsSaga'
import { getAccountBalances } from '../accountBalances/accountBalancesSaga'
import * as DrizzleActions from './constants'
import * as BlocksActions from '../blocks/constants'
import * as AccountsActions from '../accounts/constants'
import * as DrizzleActions from './drizzleActions'
import * as BlocksActions from '../blocks/blockActions'
import { NETWORK_IDS, NETWORK_MISMATCH } from '../web3/constants'

12
src/index.js

@ -2,8 +2,9 @@ import Drizzle from './Drizzle.js'
import { generateStore } from './generateStore'
import { generateContractsInitialState } from './contractStateUtils'
// Events
// Actions
import * as EventActions from './contracts/constants'
import * as AccountActions from './accounts/accountsActions'
// Reducers
import accountsReducer from './accounts/accountsReducer'
@ -21,7 +22,6 @@ import accountsMiddleware from './accounts/accountsMiddleware'
import accountBalancesMiddleware from './accountBalances/accountBalancesMiddleware'
// Sagas
import accountsSaga from './accounts/accountsSaga'
import accountBalancesSaga from './accountBalances/accountBalancesSaga'
import blocksSaga from './blocks/blocksSaga'
import contractsSaga from './contracts/contractsSaga'
@ -45,13 +45,17 @@ const drizzleMiddlewares = [
]
const drizzleSagas = [
accountsSaga,
accountBalancesSaga,
blocksSaga,
contractsSaga,
drizzleStatusSaga
]
const drizzleActions = {
AccountActions,
EventActions
}
export {
Drizzle,
generateContractsInitialState,
@ -59,5 +63,5 @@ export {
drizzleReducers,
drizzleMiddlewares,
drizzleSagas,
EventActions
drizzleActions
}

2
src/rootSaga.js

@ -1,11 +1,9 @@
import accountsSaga from './accounts/accountsSaga'
import accountBalancesSaga from './accountBalances/accountBalancesSaga'
import blocksSaga from './blocks/blocksSaga'
import contractsSaga from './contracts/contractsSaga'
import drizzleStatusSaga from './drizzleStatus/drizzleStatusSaga'
export default [
accountsSaga,
accountBalancesSaga,
blocksSaga,
contractsSaga,

2
src/transactions/transactionStackReducer.js

@ -1,4 +1,4 @@
import * as TransactionsActions from './constants'
import * as TransactionsActions from './transactionsActions'
const initialState = []

0
src/transactions/constants.js → src/transactions/transactionsActions.js

2
src/transactions/transactionsReducer.js

@ -1,4 +1,4 @@
import * as TransactionsActions from './constants'
import * as TransactionsActions from './transactionsActions'
const initialState = {}

1
src/web3/web3Saga.js

@ -13,6 +13,7 @@ export function * initializeWeb3 (options) {
if (options.customProvider) {
yield put({ type: Action.WEB3_INITIALIZED, web3: options.customProvider })
return options.customProvider
}

1133
yarn.lock

File diff suppressed because it is too large
Loading…
Cancel
Save