Browse Source

Version 0.2.1

master v0.2.1
Ezerous 4 years ago
parent
commit
2b7fdd1b47
  1. 2
      README.md
  2. 2
      package.json
  3. 6
      src/Drizzle.js
  4. 3
      src/defaultOptions.js
  5. 9
      src/drizzle-middleware.js
  6. 2
      src/web3/web3Actions.js
  7. 11
      src/web3/web3Middleware.js
  8. 29
      src/web3/web3Saga.js

2
README.md

@ -1,7 +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)
[![Dependencies](https://img.shields.io/david/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

@ -1,6 +1,6 @@
{
"name": "@ezerous/drizzle",
"version": "0.2.0",
"version": "0.2.1",
"description": "A reactive data-store for web3 and smart contracts.",
"license": "MIT",
"author": "Ezerous <ezerous@gmail.com>",

6
src/Drizzle.js

@ -81,6 +81,12 @@ class Drizzle {
events
)
if (this.contracts[drizzleContract.contractName]) {
throw new Error(
`Contract already exists: ${drizzleContract.contractName}`
)
}
this.store.dispatch({ type: ContractActions.CONTRACT_INITIALIZING, contractConfig })
this.contracts[drizzleContract.contractName] = drizzleContract

3
src/defaultOptions.js

@ -3,7 +3,8 @@ const defaultOptions = {
fallback: {
type: 'ws',
url: 'ws://127.0.0.1:8545'
}
},
retryPermissionDialog: true
},
contracts: [],
events: {},

9
src/drizzle-middleware.js

@ -1,7 +1,6 @@
import * as DrizzleActions from './drizzleStatus/drizzleActions'
import * as ContractActions from './contracts/constants'
import { ACCOUNTS_FETCHED } from './accounts/accountsActions'
import { NETWORK_ID_CHANGED } from './web3/web3Actions'
export const drizzleMiddleware = drizzleInstance => store => next => action => {
const { type } = action
@ -10,14 +9,6 @@ export const drizzleMiddleware = drizzleInstance => store => next => action => {
drizzleInstance = action.drizzle
}
if (type === NETWORK_ID_CHANGED) {
store.dispatch({
type: DrizzleActions.DRIZZLE_INITIALIZING,
drizzle: drizzleInstance,
options: drizzleInstance.options
})
}
if (
type === ACCOUNTS_FETCHED &&
drizzleInstance &&

2
src/web3/web3Actions.js

@ -3,7 +3,6 @@ export const WEB3_INITIALIZED = 'WEB3_INITIALIZED'
export const WEB3_FAILED = 'WEB3_FAILED'
export const WEB3_USER_DENIED = 'WEB3_USER_DENIED'
export const NETWORK_ID_FETCHING = 'NETWORK_ID_FETCHING'
export const NETWORK_ID_FETCHED = 'NETWORK_ID_FETCHED'
export const NETWORK_ID_CHANGED = 'NETWORK_ID_CHANGED'
export const NETWORK_ID_FAILED = 'NETWORK_ID_FAILED'
@ -21,7 +20,6 @@ export const NETWORK_IDS = {
export function networkIdChanged (web3, networkId) {
return {
type: NETWORK_ID_CHANGED,
web3,
networkId
}
}

11
src/web3/web3Middleware.js

@ -1,23 +1,24 @@
import { networkIdChanged, WEB3_INITIALIZED } from './web3Actions'
export const web3Middleware = web3 => store => next => action => {
export const web3Middleware = () => store => next => action => {
const { type } = action
if (type === WEB3_INITIALIZED) {
if(!window.ethereum)
console.warn('No Metamask detected, not subscribed to network changes!')
else {
web3 = action.web3;
window.ethereum.on('networkChanged', (networkId) => {
// Warning: 'networkChanged' is deprecated (EIP-1193)
const storedNetworkId = store.getState().web3.networkId;
if(storedNetworkId && networkId !== storedNetworkId)
store.dispatch(networkIdChanged(web3, networkId));
if(storedNetworkId && (networkId !== storedNetworkId)){
store.dispatch(networkIdChanged(networkId)); // Just to be typical
window.location.reload();
}
});
}
}
return next(action)
}
const initializedMiddleware = web3Middleware(undefined)
const initializedMiddleware = web3Middleware()
export default initializedMiddleware

29
src/web3/web3Saga.js

@ -10,30 +10,27 @@ export function * initializeWeb3 (options) {
try {
let web3 = {}
if (options.customProvider) {
yield put({ type: Action.WEB3_INITIALIZED, web3: options.customProvider })
return options.customProvider
}
if (window.ethereum) {
const { ethereum } = window
web3 = new Web3(ethereum)
web3 = options.customProvider || new Web3(ethereum);
try {
// ethereum.enable() will return the selected account
// unless user opts out and then it will return undefined
const selectedAccount = yield call([ethereum, 'enable'])
yield call([ethereum, 'request'], { method: 'eth_requestAccounts' });
yield put({ type: Action.WEB3_INITIALIZED, web3 })
if (!selectedAccount) {
yield put({ type: Action.WEB3_USER_DENIED })
return
}
return web3
} catch (error) {
console.error(error)
yield put({ type: Action.WEB3_FAILED })
console.error(error);
if(error.code === 4001){
console.warn("User rejected MetaMask permission request");
yield put({ type: Action.WEB3_USER_DENIED });
if(options.retryPermissionDialog)
return yield call(initializeWeb3, options); // User rejected permission dialog, let's retry
}
else if (error.code === -32002)
console.warn('Please accept the pending MetaMask permission request');
yield put({ type: Action.WEB3_FAILED, error });
}
} else if (typeof window.web3 !== 'undefined') {
// Checking if Web3 has been injected by the browser (Mist/MetaMask)

Loading…
Cancel
Save