diff --git a/app/package.json b/app/package.json index fd7c064..604998f 100644 --- a/app/package.json +++ b/app/package.json @@ -7,8 +7,6 @@ "url": "https://gitlab.com/Ezerous/Apella.git" }, "dependencies": { - "@drizzle-utils/get-contract-instance": "^0.1.6-alpha.0", - "@drizzle-utils/get-web3": "^0.1.6-alpha.0", "connected-react-router": "^6.3.1", "drizzle": "^1.3.3", "history": "^4.7.2", @@ -28,7 +26,8 @@ "redux": "^4.0.1", "redux-saga": "^0.16.2", "semantic-ui-react": "^0.85.0", - "uuid": "^3.3.2" + "uuid": "^3.3.2", + "web3": "1.0.0-beta.35" }, "scripts": { "start": "react-scripts start", diff --git a/app/src/containers/BoardContainer.js b/app/src/containers/BoardContainer.js index 9f78752..4c90ef2 100644 --- a/app/src/containers/BoardContainer.js +++ b/app/src/containers/BoardContainer.js @@ -1,7 +1,7 @@ import React, { Component } from 'react'; import { connect } from 'react-redux'; import { drizzle } from '../index'; -import { withRouter } from 'react-router' +import { withRouter } from 'react-router-dom' import { Header } from 'semantic-ui-react'; diff --git a/app/src/redux/sagas/drizzleUtilsSaga.js b/app/src/redux/sagas/drizzleUtilsSaga.js index 7bd241b..69d3ee2 100644 --- a/app/src/redux/sagas/drizzleUtilsSaga.js +++ b/app/src/redux/sagas/drizzleUtilsSaga.js @@ -1,5 +1,4 @@ -import getWeb3 from '@drizzle-utils/get-web3'; -import getContractInstance from '@drizzle-utils/get-contract-instance'; +import { getContractInstance, getWeb3 } from "../../utils/drizzleUtils"; import { call, put, takeLatest, select } from 'redux-saga/effects' import Forum from '../../contracts/Forum'; diff --git a/app/src/utils/drizzleUtils.js b/app/src/utils/drizzleUtils.js new file mode 100644 index 0000000..dcf87e5 --- /dev/null +++ b/app/src/utils/drizzleUtils.js @@ -0,0 +1,87 @@ +const Web3 = require("web3"); + +const resolveWeb3 = (resolve, options, isBrowser) => { + let provider; + + if (options.customProvider) { + // use custom provider from options object + provider = options.customProvider; + } else if (isBrowser && window.ethereum) { + // use `ethereum` object injected by MetaMask + provider = window.ethereum; + } else if (isBrowser && typeof window.web3 !== "undefined") { + // use injected web3 object by legacy dapp browsers + provider = window.web3.currentProvider; + } else if (options.fallbackProvider) { + // use fallback provider from options object + provider = options.fallbackProvider; + } else { + // connect to development blockchain from `truffle develop` + provider = new Web3.providers.HttpProvider("http://127.0.0.1:9545"); + } + + const web3 = new Web3(provider); + resolve(web3); +}; + +const getWeb3 = (options = {}) => + new Promise(resolve => { + // handle server-side and React Native environments + const isReactNative = + typeof navigator !== "undefined" && navigator.product === "ReactNative"; + const isNode = typeof window === "undefined"; + if (isNode || isReactNative) { + return resolveWeb3(resolve, options, false); + } + + // if page is ready, resolve for web3 immediately + if (document.readyState === `complete`) { + return resolveWeb3(resolve, options, true); + } + + // otherwise, resolve for web3 when page is done loading + return window.addEventListener("load", () => + resolveWeb3(resolve, options, true), + ); + }); + +const getContractInstance = (options = {}) => + new Promise(async (resolve, reject) => { + if (!options.web3) { + return reject(new Error("The options object with web3 is required.")); + } + + const { web3 } = options; + + let instance; + try { + if (options.artifact) { + // if artifact exists, attempt to get network ID and the deployed address + const { artifact } = options; + const networkId = await web3.eth.net.getId(); + const deployedNetwork = artifact.networks[networkId]; + + // if no deployed address is found, instantiate without the address + const address = deployedNetwork && deployedNetwork.address; + + instance = new web3.eth.Contract(artifact.abi, address); + } else if (options.abi) { + // otherwise, use passed-in ABI and deployed address (optional) + const { abi, address } = options; + + instance = new web3.eth.Contract(abi, address); + } else { + return reject( + new Error( + "You must pass in a contract artifact or the ABI of a deployed contract.", + ), + ); + } + + return resolve(instance); + } catch (err) { + return reject(err); + } + }); + +export { getWeb3, getContractInstance }; \ No newline at end of file