74 lines
2.5 KiB
JavaScript
Raw Normal View History

2018-12-15 12:03:19 -05:00
import React from 'react'
2019-01-25 15:16:55 -05:00
import { HashRouter as Router } from 'react-router-dom'
import EmbarkJS from 'Embark/EmbarkJS'
import LiquidPledging from 'Embark/contracts/LiquidPledging'
2018-12-15 15:15:17 -05:00
import web3 from 'Embark/web3'
2018-12-01 17:16:58 -05:00
import { initVaultAndLP, vaultPledgingNeedsInit, standardTokenApproval, getLpAllowance } from './utils/initialize'
2019-01-25 15:16:55 -05:00
import { getAuthorizedPayments } from './utils/events'
import { FundingContext } from './context'
2018-12-15 07:01:40 -05:00
import MainCointainer from './components/MainCointainer'
2019-01-25 15:16:55 -05:00
import { getAndAddLpEvents } from './actions/lpEvents'
import { getAndAddVaultEvents } from './actions/vaultEvents'
import { addFormattedProfiles } from './actions/profiles'
import { getAndAddPledges } from './actions/pledges'
2018-11-28 11:12:50 -05:00
2018-12-14 14:10:08 -05:00
const { getNetworkType } = web3.eth.net
2018-11-28 11:12:50 -05:00
class App extends React.Component {
state = {
2019-01-10 15:44:45 -05:00
loading: true,
lpAllowance: 0,
needsInit: true,
};
2018-11-28 11:12:50 -05:00
componentDidMount(){
2018-11-29 11:45:57 -05:00
EmbarkJS.onReady(async (err) => {
2018-12-01 09:31:04 -05:00
getNetworkType().then(async network => {
2018-11-28 11:12:50 -05:00
const { environment } = EmbarkJS
const isInitialized = await vaultPledgingNeedsInit()
if (!!isInitialized) {
2019-01-02 15:32:28 -05:00
if (environment === 'development') console.log('mock_time:', await LiquidPledging.mock_time.call())
2019-01-25 15:16:55 -05:00
const lpAllowance = await getLpAllowance()
2019-01-25 15:16:55 -05:00
//TODO add block based sync
const authorizedPayments = await getAuthorizedPayments()
const account = await web3.eth.getCoinbase()
2019-01-25 15:16:55 -05:00
this.syncWithRemote()
this.setState({
account,
network,
environment,
lpAllowance,
2018-12-14 14:10:08 -05:00
authorizedPayments,
2019-01-25 15:16:55 -05:00
needsInit: false
})
}
2018-12-16 12:59:33 -05:00
})
})
2018-11-28 11:12:50 -05:00
}
2019-01-25 15:16:55 -05:00
async syncWithRemote() {
// not running in parallel due to possible metamask / infura limitation
await getAndAddLpEvents()
await getAndAddVaultEvents()
await getAndAddPledges()
await addFormattedProfiles()
this.setState({ loading: false })
2018-12-07 09:47:00 -05:00
}
2018-11-28 11:12:50 -05:00
render() {
2019-01-25 15:16:55 -05:00
const { account, needsInit, lpAllowance, loading, authorizedPayments } = this.state
const { appendFundProfile, appendPledges, transferPledgeAmounts } = this
const fundingContext = { appendPledges, appendFundProfile, account, transferPledgeAmounts, authorizedPayments, needsInit, initVaultAndLP, standardTokenApproval }
2018-11-28 11:12:50 -05:00
return (
<FundingContext.Provider value={fundingContext}>
2018-12-15 12:03:19 -05:00
<Router>
2019-01-10 15:44:45 -05:00
<MainCointainer loading={loading} />
2018-12-15 12:03:19 -05:00
</Router>
</FundingContext.Provider>
2018-11-28 11:12:50 -05:00
)
}
}
2018-12-15 07:01:40 -05:00
export default App