Skip to content

Commit

Permalink
Merge pull request #826 from poanetwork/infura
Browse files Browse the repository at this point in the history
(Fix && Refactoring) Infura usage if no Metamask to get crowdsale data
  • Loading branch information
vbaranov committed May 7, 2018
2 parents 783fd87 + 1ae83b0 commit 9238870
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 226 deletions.
1 change: 1 addition & 0 deletions .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'
251 changes: 51 additions & 200 deletions src/components/crowdsale/utils.js
Expand Up @@ -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)
})
})
}

Expand Down
51 changes: 26 additions & 25 deletions src/components/invest/index.js
Expand Up @@ -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 }))
})
})
})
})
}

Expand Down

0 comments on commit 9238870

Please sign in to comment.