(Refactor) Store networks' information in constant object
This commit is contained in:
parent
5225243055
commit
130397ab40
|
@ -42,7 +42,7 @@ class App extends Component {
|
||||||
await this.keysManager.init({
|
await this.keysManager.init({
|
||||||
web3: web3Config.web3Instance,
|
web3: web3Config.web3Instance,
|
||||||
netId: web3Config.netId,
|
netId: web3Config.netId,
|
||||||
addresses,
|
addresses
|
||||||
});
|
});
|
||||||
this.setState({
|
this.setState({
|
||||||
isDisabledBtn: false,
|
isDisabledBtn: false,
|
||||||
|
@ -99,7 +99,7 @@ class App extends Component {
|
||||||
}
|
}
|
||||||
async onClick() {
|
async onClick() {
|
||||||
this.setState({loading:true});
|
this.setState({loading:true});
|
||||||
const initialKey = window.web3.eth.defaultAccount;
|
const initialKey = this.state.web3Config.defaultAccount;
|
||||||
let isValid
|
let isValid
|
||||||
try {
|
try {
|
||||||
isValid = await this.keysManager.isInitialKeyValid(initialKey);
|
isValid = await this.keysManager.isInitialKeyValid(initialKey);
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import moment from "moment";
|
import moment from "moment";
|
||||||
import helpers from "./helpers";
|
import { constants } from "./constants";
|
||||||
|
|
||||||
const Footer = ({netId}) => {
|
const Footer = ({netId}) => {
|
||||||
const footerClassName = helpers.isTestnet(netId) ? "sokol" : "";
|
const footerClassName = netId in constants.NETWORKS && constants.NETWORKS[netId].TESTNET ? "sokol" : "";
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<footer className={`footer ${footerClassName}`}>
|
<footer className={`footer ${footerClassName}`}>
|
||||||
<div className="container">
|
<div className="container">
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import helpers from "./helpers";
|
import { constants } from "./constants";
|
||||||
|
|
||||||
let Header = ({netId}) => {
|
let Header = ({netId}) => {
|
||||||
const thisIsTestnet = helpers.isTestnet(netId);
|
const thisIsTestnet = netId in constants.NETWORKS && constants.NETWORKS[netId].TESTNET;
|
||||||
const headerClassName = thisIsTestnet ? "sokol" : "";
|
const headerClassName = thisIsTestnet ? "sokol" : "";
|
||||||
const logoClassName = thisIsTestnet ? "header-logo-sokol" : "header-logo";
|
const logoClassName = thisIsTestnet ? "header-logo-sokol" : "header-logo";
|
||||||
return (
|
return (
|
||||||
|
|
|
@ -8,16 +8,12 @@ const styles = (netId) => {
|
||||||
const sokol = {
|
const sokol = {
|
||||||
backgroundColor: 'rgba(47, 109, 99, 0.8)'
|
backgroundColor: 'rgba(47, 109, 99, 0.8)'
|
||||||
}
|
}
|
||||||
switch(netId) {
|
|
||||||
case constants.NETID_SOKOL:
|
if (netId in constants.NETWORKS) {
|
||||||
case constants.NETID_DAI_TEST:
|
return constants.NETWORKS[netId].TESTNET ? sokol : core
|
||||||
return sokol;
|
|
||||||
case constants.NETID_CORE:
|
|
||||||
case constants.NETID_DAI:
|
|
||||||
return core;
|
|
||||||
default:
|
|
||||||
return {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return core
|
||||||
}
|
}
|
||||||
const Loading = ({netId}) => (
|
const Loading = ({netId}) => (
|
||||||
<div className="loading-container" style={styles(netId)}>
|
<div className="loading-container" style={styles(netId)}>
|
||||||
|
|
|
@ -5,23 +5,7 @@ import helpers from "./helpers";
|
||||||
//}
|
//}
|
||||||
|
|
||||||
export default (web3Config) => {
|
export default (web3Config) => {
|
||||||
let branch;
|
const branch = constants.NETWORKS[web3Config.netId].BRANCH;
|
||||||
|
|
||||||
switch (web3Config.netId) {
|
|
||||||
case constants.NETID_DAI_TEST:
|
|
||||||
branch = "dai-test";
|
|
||||||
break;
|
|
||||||
case constants.NETID_SOKOL:
|
|
||||||
branch = "sokol";
|
|
||||||
break;
|
|
||||||
case constants.NETID_DAI:
|
|
||||||
branch = "dai";
|
|
||||||
break;
|
|
||||||
case constants.NETID_CORE:
|
|
||||||
default:
|
|
||||||
branch = "core";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
fetch(helpers.addressesURL(branch)).then((response) => {
|
fetch(helpers.addressesURL(branch)).then((response) => {
|
||||||
response.json().then((json) => {
|
response.json().then((json) => {
|
||||||
|
|
|
@ -7,10 +7,32 @@ constants.ABIsSources = {
|
||||||
};
|
};
|
||||||
constants.userDeniedTransactionPattern = 'User denied transaction';
|
constants.userDeniedTransactionPattern = 'User denied transaction';
|
||||||
|
|
||||||
constants.NETID_SOKOL = "77";
|
constants.NETWORKS = {
|
||||||
constants.NETID_CORE = "99";
|
'77': {
|
||||||
constants.NETID_DAI_TEST = "79";
|
NAME: 'Sokol',
|
||||||
constants.NETID_DAI = "100";
|
RPC: 'https://sokol.poa.network',
|
||||||
|
BRANCH: 'sokol',
|
||||||
|
TESTNET: true
|
||||||
|
},
|
||||||
|
'99': {
|
||||||
|
NAME: 'Core',
|
||||||
|
RPC: 'https://core.poa.network',
|
||||||
|
BRANCH: 'core',
|
||||||
|
TESTNET: false
|
||||||
|
},
|
||||||
|
'79': {
|
||||||
|
NAME: 'Dai-Test',
|
||||||
|
RPC: 'http://40.112.48.125',
|
||||||
|
BRANCH: 'dai-test',
|
||||||
|
TESTNET: true
|
||||||
|
},
|
||||||
|
'100': {
|
||||||
|
NAME: 'Dai',
|
||||||
|
RPC: 'https://dai.poa.network',
|
||||||
|
BRANCH: 'dai',
|
||||||
|
TESTNET: false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
constants
|
constants
|
||||||
|
|
103
src/getWeb3.js
103
src/getWeb3.js
|
@ -1,9 +1,12 @@
|
||||||
|
import Web3 from "web3";
|
||||||
import { constants } from "./constants";
|
import { constants } from "./constants";
|
||||||
|
|
||||||
let errorMsgNoMetamaskAccount = `You haven't chosen any account in MetaMask.
|
const errorMsgNoMetamaskAccount = `You haven't chosen any account in MetaMask.
|
||||||
Please choose your initial key in MetaMask and reload the page.
|
Please choose your initial key in MetaMask and reload the page.
|
||||||
Check POA Network <a href='https://github.com/poanetwork/wiki' target='blank'>wiki</a> for more info.`;
|
Check POA Network <a href='https://github.com/poanetwork/wiki' target='blank'>wiki</a> for more info.`;
|
||||||
|
|
||||||
|
const errorMsgDeniedAccess = "You have denied access to your accounts";
|
||||||
|
|
||||||
function generateElement(msg){
|
function generateElement(msg){
|
||||||
let errorNode = document.createElement("div");
|
let errorNode = document.createElement("div");
|
||||||
errorNode.innerHTML = `<div style="line-height: 1.6;">
|
errorNode.innerHTML = `<div style="line-height: 1.6;">
|
||||||
|
@ -14,66 +17,66 @@ function generateElement(msg){
|
||||||
let getWeb3 = () => {
|
let getWeb3 = () => {
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
// Wait for loading completion to avoid race conditions with web3 injection timing.
|
// Wait for loading completion to avoid race conditions with web3 injection timing.
|
||||||
window.addEventListener('load', function () {
|
window.addEventListener("load", async function () {
|
||||||
var results
|
let web3;
|
||||||
var web3 = window.web3
|
|
||||||
|
|
||||||
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
|
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
|
||||||
if (typeof web3 !== 'undefined') {
|
if (window.ethereum) {
|
||||||
// Use Mist/MetaMask's provider.
|
web3 = new Web3(window.ethereum);
|
||||||
var errorMsg = null;
|
console.log("Injected web3 detected.");
|
||||||
web3 = new window.Web3(web3.currentProvider)
|
try {
|
||||||
web3.version.getNetwork((err, netId) => {
|
await window.ethereum.enable();
|
||||||
|
} catch (e) {
|
||||||
|
reject({msg: errorMsgDeniedAccess, node: generateElement(errorMsgDeniedAccess)});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else if (window.web3) {
|
||||||
|
web3 = new Web3(window.web3.currentProvider);
|
||||||
|
console.log("Injected web3 detected.");
|
||||||
|
} else {
|
||||||
|
console.error("Metamask not found");
|
||||||
|
reject({msg: errorMsgNoMetamaskAccount, node: generateElement(errorMsgNoMetamaskAccount)});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const netId = await web3.eth.net.getId();
|
||||||
|
console.log("netId", netId);
|
||||||
|
|
||||||
let netIdName;
|
let netIdName;
|
||||||
switch (netId) {
|
let errorMsg = null;
|
||||||
case constants.NETID_SOKOL:
|
|
||||||
netIdName = "Sokol";
|
if (netId in constants.NETWORKS) {
|
||||||
console.log("This is sokol");
|
netIdName = constants.NETWORKS[netId].NAME;
|
||||||
break
|
console.log(`This is ${netIdName}`);
|
||||||
case constants.NETID_DAI_TEST:
|
} else {
|
||||||
netIdName = "Dai-Test";
|
netIdName = "ERROR";
|
||||||
console.log("This is Dai-Test");
|
|
||||||
break
|
|
||||||
case constants.NETID_CORE:
|
|
||||||
netIdName = "Core";
|
|
||||||
console.log("This is Core");
|
|
||||||
break
|
|
||||||
case constants.NETID_DAI:
|
|
||||||
netIdName = "Dai";
|
|
||||||
console.log("This is Dai");
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
netIdName = "Unknown";
|
|
||||||
errorMsg = `You aren't connected to POA Network.
|
errorMsg = `You aren't connected to POA Network.
|
||||||
Please switch on Metamask and refresh the page.
|
Please switch on Metamask and refresh the page.
|
||||||
Check POA Network <a href='https://github.com/poanetwork/wiki' target='blank'>wiki</a> for more info.
|
Check POA Network <a href='https://github.com/poanetwork/wiki' target='blank'>wiki</a> for more info.
|
||||||
<b>Current Network ID</b> is <i>${netId}</i>`;
|
<b>Current Network ID</b> is <i>${netId}</i>`;
|
||||||
|
console.log("This is an unknown network.");
|
||||||
console.log("This is an unknown network.", netId);
|
|
||||||
}
|
}
|
||||||
results = {
|
|
||||||
|
document.title = `${netIdName} - DApp Keys Generation`;
|
||||||
|
|
||||||
|
if (errorMsg !== null) {
|
||||||
|
reject({msg: errorMsg, node: generateElement(errorMsg)});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const accounts = await web3.eth.getAccounts();
|
||||||
|
const defaultAccount = accounts[0] || null;
|
||||||
|
if (defaultAccount === null) {
|
||||||
|
reject({msg: errorMsgNoMetamaskAccount, node: generateElement(errorMsgNoMetamaskAccount)});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve({
|
||||||
web3Instance: web3,
|
web3Instance: web3,
|
||||||
netIdName,
|
netIdName,
|
||||||
netId,
|
netId,
|
||||||
injectedWeb3: true
|
defaultAccount
|
||||||
}
|
});
|
||||||
document.title = `${netIdName} - Dapp Keys Generation`
|
|
||||||
var defaultAccount = web3.eth.defaultAccount || null;
|
|
||||||
if(defaultAccount === null){
|
|
||||||
reject({msg: errorMsgNoMetamaskAccount, node: generateElement(errorMsgNoMetamaskAccount)})
|
|
||||||
}
|
|
||||||
if(errorMsg !== null){
|
|
||||||
reject({msg: errorMsg, node: generateElement(errorMsg)})
|
|
||||||
}
|
|
||||||
resolve(results)
|
|
||||||
})
|
|
||||||
|
|
||||||
console.log('Injected web3 detected.');
|
|
||||||
|
|
||||||
} else {
|
|
||||||
reject({msg: errorMsgNoMetamaskAccount, node: generateElement(errorMsgNoMetamaskAccount)})
|
|
||||||
console.error('Metamask not found');
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,32 +47,12 @@ function wrongRepoAlert(addr) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function getBranch(netId) {
|
|
||||||
switch (netId) {
|
|
||||||
case constants.NETID_DAI_TEST:
|
|
||||||
return "dai-test";
|
|
||||||
case constants.NETID_SOKOL:
|
|
||||||
return "sokol";
|
|
||||||
case constants.NETID_DAI:
|
|
||||||
return "dai";
|
|
||||||
case constants.NETID_CORE:
|
|
||||||
default:
|
|
||||||
return "core";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function isTestnet(netId) {
|
|
||||||
return netId === constants.NETID_SOKOL || netId === constants.NETID_DAI_TEST;
|
|
||||||
}
|
|
||||||
|
|
||||||
let helpers = {
|
let helpers = {
|
||||||
toAscii,
|
toAscii,
|
||||||
addressesURL,
|
addressesURL,
|
||||||
ABIURL,
|
ABIURL,
|
||||||
getABI,
|
getABI,
|
||||||
wrongRepoAlert,
|
wrongRepoAlert
|
||||||
getBranch,
|
|
||||||
isTestnet
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default helpers
|
export default helpers
|
||||||
|
|
|
@ -1,24 +1,27 @@
|
||||||
import Web3 from 'web3';
|
|
||||||
import addressGenerator from './addressGenerator';
|
import addressGenerator from './addressGenerator';
|
||||||
import helpers from "./helpers";
|
import helpers from "./helpers";
|
||||||
import { constants } from "./constants";
|
import { constants } from "./constants";
|
||||||
|
|
||||||
export default class KeysManager {
|
export default class KeysManager {
|
||||||
async init({web3, netId, addresses}){
|
async init({web3, netId, addresses}){
|
||||||
this.web3_10 = new Web3(web3.currentProvider);
|
|
||||||
const {KEYS_MANAGER_ADDRESS} = addresses;
|
const {KEYS_MANAGER_ADDRESS} = addresses;
|
||||||
console.log('Keys Manager ', KEYS_MANAGER_ADDRESS);
|
console.log('Keys Manager ', KEYS_MANAGER_ADDRESS);
|
||||||
const branch = helpers.getBranch(netId);
|
|
||||||
|
|
||||||
const KeysManagerAbi = await helpers.getABI(branch, 'KeysManager')
|
const KeysManagerAbi = await helpers.getABI(constants.NETWORKS[netId].BRANCH, 'KeysManager')
|
||||||
|
|
||||||
this.keysInstance = new this.web3_10.eth.Contract(KeysManagerAbi, KEYS_MANAGER_ADDRESS);
|
this.instance = new web3.eth.Contract(KeysManagerAbi, KEYS_MANAGER_ADDRESS);
|
||||||
this.netId = netId;
|
|
||||||
|
const networkName = constants.NETWORKS[netId].NAME.toLowerCase();
|
||||||
|
if (networkName === 'dai-test' || networkName === 'dai') {
|
||||||
|
this.gasPrice = web3.utils.toWei('0', 'gwei');
|
||||||
|
} else {
|
||||||
|
this.gasPrice = web3.utils.toWei('2', 'gwei');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async isInitialKeyValid(initialKey) {
|
async isInitialKeyValid(initialKey) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const methods = this.keysInstance.methods
|
const methods = this.instance.methods
|
||||||
let getInitialKeyStatus
|
let getInitialKeyStatus
|
||||||
if (methods.getInitialKeyStatus) {
|
if (methods.getInitialKeyStatus) {
|
||||||
getInitialKeyStatus = methods.getInitialKeyStatus
|
getInitialKeyStatus = methods.getInitialKeyStatus
|
||||||
|
@ -37,14 +40,10 @@ export default class KeysManager {
|
||||||
return await addressGenerator();
|
return await addressGenerator();
|
||||||
}
|
}
|
||||||
|
|
||||||
createKeys({mining, voting, payout, sender}){
|
createKeys({mining, voting, payout, sender}) {
|
||||||
let gasPrice = '2';
|
return this.instance.methods.createKeys(mining, voting, payout).send({
|
||||||
if (this.netId === constants.NETID_DAI_TEST || this.netId === constants.NETID_DAI) {
|
|
||||||
gasPrice = '0';
|
|
||||||
}
|
|
||||||
return this.keysInstance.methods.createKeys(mining, voting, payout).send({
|
|
||||||
from: sender,
|
from: sender,
|
||||||
gasPrice: this.web3_10.utils.toWei(gasPrice, 'gwei')
|
gasPrice: this.gasPrice
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue