diff --git a/.env.example b/.env.example index 32e377e91..390f02957 100644 --- a/.env.example +++ b/.env.example @@ -1 +1,2 @@ REACT_APP_REGISTRY_ADDRESS='{"1":"0x445b4c58113C784640a978cfE39b0065118B91A5","3":"0xD4543Fe1ded1637768516A3Ef35614F91F715370","4":"0x16C25e1139fC96E8705A59E7a4F132d11DfFeE4E","42":"0x3d9EE1B10a002B9113e5dAeF8F19067503d6162A","12648430":"0xe78a0f7e598cc8b0bb87894b0f60dd2a88d6a8ab","77":"0x058faf0b1248a6de96ef51696564fe5d64b5e231","99":"0x41D420ED515E51A4023fBa2945a46186274181d6"}' +REACT_APP_INFURA_TOKEN='kEpzZR9fIyO3a8gTqJcI' \ No newline at end of file diff --git a/src/components/crowdsale/utils.js b/src/components/crowdsale/utils.js index c9f5850f4..9c52e2e38 100644 --- a/src/components/crowdsale/utils.js +++ b/src/components/crowdsale/utils.js @@ -314,218 +314,69 @@ function getTokenData () { if (!web3) { resolve('no MetaMask') - return } - web3.eth.getAccounts().then(accounts => { - if (accounts.length === 0) { - resolve('no accounts') - return - } - - let propsCount = 0 - let cbCount = 0 - let tokenObj = toJS(contractStore.token) - console.log('tokenObj', tokenObj) - - attachToContract(tokenObj.abi, tokenObj.addr) - .then(tokenContract => { - console.log('attach to token contract') + let account - if (!tokenContract) { - noContractAlert() - reject('no contract') - return - } - - propsCount++ - tokenContract.methods.name().call((err, name) => { - cbCount++ + web3.eth.getAccounts() + .then(accounts => { + if (accounts.length === 0) { + resolve('no accounts') + } else { + account = accounts[0] + } + }) + .then(() => { + let tokenObj = toJS(contractStore.token) + console.log('tokenObj', tokenObj) + return attachToContract(tokenObj.abi, tokenObj.addr) + }) + .then(tokenContract => { + console.log('attach to token contract') - if (err) { - return console.log(err) - } + if (!tokenContract) { + noContractAlert() + reject('no contract') + } - console.log('token name:', name) + let getTokenName = tokenContract.methods.name().call(); + let getTokenSymbol = tokenContract.methods.symbol().call(); + let getTokenDecimals = tokenContract.methods.decimals().call(); + let getTokenTotalSupply = tokenContract.methods.totalSupply().call(); + let getBalanceOf = tokenContract.methods.balanceOf(account).call(); + + return Promise.all([ + getTokenName, + getTokenSymbol, + getTokenDecimals, + getTokenTotalSupply, + getBalanceOf + ]) + .then(([ + name, + ticker, + decimals, + supply, + balanceOf + ]) => { + + console.log('token name: ' + name) tokenStore.setProperty('name', name) - - if (propsCount === cbCount) { - resolve() - } - }) - - propsCount++ - tokenContract.methods.symbol().call((err, ticker) => { - cbCount++ - - if (err) { - console.log(err) - } - console.log('token ticker: ' + ticker) tokenStore.setProperty('ticker', ticker) - - if (propsCount === cbCount) { - resolve() - } - }) - - if (tokenContract.methods.balanceOf) { - propsCount++ - tokenContract.methods.balanceOf.call(accounts[0], (err, balanceOf) => { - cbCount++ - - if (err) { - return console.log(err) - } - - const balance = toBigNumber(balanceOf) - console.log('balanceOf:', balance.toFixed()) - - const currentAmount = crowdsalePageStore.tokenAmountOf || 0 - crowdsalePageStore.setProperty('tokenAmountOf', balance.plus(currentAmount).toFixed()) - - if (propsCount === cbCount) { - resolve() - } - }) - } - - propsCount++ - tokenContract.methods.decimals().call((err, decimals) => { - cbCount++ - - if (err) { - console.log(err) - } - - console.log('token decimals:', decimals) + const balance = toBigNumber(balanceOf) + console.log('balanceOf:', balance.toFixed()) + const currentAmount = crowdsalePageStore.tokenAmountOf || 0 + crowdsalePageStore.setProperty('tokenAmountOf', balance.plus(currentAmount).toFixed()) + console.log('token decimals: ' + decimals) tokenStore.setProperty('decimals', decimals) - - if (propsCount === cbCount) { - resolve() - } - }) - - propsCount++ - tokenContract.methods.totalSupply().call((err, supply) => { - cbCount++ - - if (err) { - console.log(err) - } - - console.log('token supply:', supply) + console.log('token supply: ' + supply) tokenStore.setProperty('supply', supply) - if (propsCount === cbCount) { - resolve() - - } else { - let propsCount = 0 - let cbCount = 0 - - attachToContract(contractStore.token.abi, contractStore.token.addr) - .then(tokenContract => { - console.log('attach to token contract') - - if (!tokenContract) { - noContractAlert() - reject('no contract') - return - } - - propsCount++ - tokenContract.methods.name().call((err, name) => { - cbCount++ - - if (err) { - return console.log(err) - } - - console.log('token name:', name) - tokenStore.setProperty('name', name) - - if (propsCount === cbCount) { - resolve() - } - }) - - propsCount++ - tokenContract.methods.symbol().call((err, ticker) => { - cbCount++ - - if (err) { - console.log(err) - } - - console.log('token ticker:', ticker) - tokenStore.setProperty('ticker', ticker) - - if (propsCount === cbCount) { - resolve() - } - }) - - if (tokenContract.methods.balanceOf) { - propsCount++ - tokenContract.methods.balanceOf(accounts[0]).call((err, balanceOf) => { - cbCount++ - - if (err) { - return console.log(err) - } - - const balance = toBigNumber(balanceOf) - console.log('balanceOf:', balance.toFixed()) - - const currentAmount = crowdsalePageStore.tokenAmountOf || 0 - crowdsalePageStore.setProperty('tokenAmountOf', balance.plus(currentAmount).toFixed()) - - if (propsCount === cbCount) { - resolve() - } - }) - } - - propsCount++ - tokenContract.methods.decimals().call((err, decimals) => { - cbCount++ - - if (err) { - console.log(err) - } - - console.log('token decimals:', decimals) - tokenStore.setProperty('decimals', decimals) - - if (propsCount === cbCount) { - resolve() - } - }) - - propsCount++ - tokenContract.methods.totalSupply().call((err, supply) => { - cbCount++ - - if (err) { - console.log(err) - } - - console.log('token supply:', supply) - tokenStore.setProperty('supply', supply) - - if (propsCount === cbCount) { - resolve() - } - }) - }) - .catch(reject) - } + resolve() }) - }) - .catch(reject) - }) + .catch(reject) + }) }) } diff --git a/src/components/invest/index.js b/src/components/invest/index.js index f1f1d1e83..bf06d01bd 100644 --- a/src/components/invest/index.js +++ b/src/components/invest/index.js @@ -105,38 +105,39 @@ export class Invest extends React.Component { const crowdsaleAddrs = typeof joinedCrowdsales === 'string' ? [joinedCrowdsales] : joinedCrowdsales contractStore.setContractProperty('crowdsale', 'addr', crowdsaleAddrs) - web3.eth.getAccounts().then((accounts) => { - if (accounts.length === 0) { - this.setState({ loading: false }) - return - } + web3.eth.getAccounts() + .then((accounts) => { + if (accounts.length === 0) { + this.setState({ loading: false }) + } - this.setState({ - curAddr: accounts[0], - web3 + this.setState({ + curAddr: accounts[0], + web3 + }) }) - - if (!contractStore.crowdsale.addr) { - this.setState({ loading: false }) - return - } - - findCurrentContractRecursively(0, null, crowdsaleContract => { - if (!crowdsaleContract) { + .then(() => { + if (!contractStore.crowdsale.addr) { this.setState({ loading: false }) return } - initializeAccumulativeData() - .then(() => getCrowdsaleData(crowdsaleContract)) - .then(() => getAccumulativeCrowdsaleData()) - .then(() => getCrowdsaleTargetDates()) - .then(() => this.checkIsFinalized()) - .then(() => this.setTimers()) - .catch(err => console.log(err)) - .then(() => this.setState({ loading: false })) + findCurrentContractRecursively(0, null, crowdsaleContract => { + if (!crowdsaleContract) { + this.setState({ loading: false }) + return + } + + initializeAccumulativeData() + .then(() => getCrowdsaleData(crowdsaleContract)) + .then(() => getAccumulativeCrowdsaleData()) + .then(() => getCrowdsaleTargetDates()) + .then(() => this.checkIsFinalized()) + .then(() => this.setTimers()) + .catch(err => console.log(err)) + .then(() => this.setState({ loading: false })) + }) }) - }) }) } diff --git a/src/stores/Web3Store.js b/src/stores/Web3Store.js index d91a15a90..26f8b534f 100644 --- a/src/stores/Web3Store.js +++ b/src/stores/Web3Store.js @@ -1,5 +1,8 @@ import Web3 from 'web3' import { observable } from 'mobx'; +import { getQueryVariable } from '../utils/utils' +import { CrowdsaleConfig } from '../components/Common/config' +import { CHAINS } from '../utils/constants' class Web3Store { @@ -19,7 +22,12 @@ class Web3Store { }) } + getInfuraLink = (network) => { + return `https://${network}.infura.io/${process.env['REACT_APP_INFURA_TOKEN']}` + } + getWeb3 = cb => { + const networkID = CrowdsaleConfig.networkID ? CrowdsaleConfig.networkID : getQueryVariable('networkID') var web3 = window.web3; if (typeof web3 === 'undefined') { // no web3, use fallback @@ -27,17 +35,41 @@ class Web3Store { const devEnvironment = process.env.NODE_ENV === 'development'; if (devEnvironment) { web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545')); + } else { + let infuraLink + switch(networkID) { + case 1: + infuraLink = this.getInfuraLink(CHAINS.MAINNET) + break + case 2: + infuraLink = this.getInfuraLink(CHAINS.MORDEN) + break + case 3: + infuraLink = this.getInfuraLink(CHAINS.ROPSTEN) + break + case 4: + infuraLink = this.getInfuraLink(CHAINS.RINKEBY) + break + case 42: + infuraLink = this.getInfuraLink(CHAINS.KOVAN) + break + default: + infuraLink = this.getInfuraLink(CHAINS.MAINNET) + break + } + web3 = new Web3(new Web3.providers.HttpProvider(infuraLink)); } cb(web3, false); + return web3; } else { // window.web3 == web3 most of the time. Don't override the provided, // web3, just wrap it in your Web3. var myWeb3 = new Web3(web3.currentProvider); cb(myWeb3, false); + return myWeb3; } - return myWeb3; } }