104 lines
3.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'
import Snackbar from '@material-ui/core/Snackbar'
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'
2019-02-05 13:41:17 -05:00
import { updateStalePledges, getAndAddPledges } from './actions/pledges'
import { updateDelegates } from './actions/delegates'
import { MySnackbarContentWrapper } from './components/base/SnackBars'
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 account = await web3.eth.getCoinbase()
this.setState({ account })
const lpAllowance = await getLpAllowance()
2019-01-25 15:16:55 -05:00
//TODO add block based sync
const authorizedPayments = await getAuthorizedPayments()
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()
// Profiles must be loaded before pledges to set profile on pledge model
2019-01-25 15:16:55 -05:00
await addFormattedProfiles()
await getAndAddPledges()
2019-02-05 13:41:17 -05:00
await updateStalePledges()
await updateDelegates()
2019-01-25 15:16:55 -05:00
this.setState({ loading: false })
2018-12-07 09:47:00 -05:00
}
openSnackBar = (variant, message) => {
this.setState({ snackbar: { variant, message } })
}
closeSnackBar = () => {
this.setState({ snackbar: null })
}
2018-11-28 11:12:50 -05:00
render() {
const { account, needsInit, lpAllowance, loading, authorizedPayments, snackbar } = this.state
const { appendFundProfile, appendPledges, transferPledgeAmounts, openSnackBar, closeSnackBar } = this
const fundingContext = { appendPledges, appendFundProfile, account, transferPledgeAmounts, authorizedPayments, needsInit, initVaultAndLP, standardTokenApproval, openSnackBar, closeSnackBar }
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>
{snackbar && <Snackbar
anchorOrigin={{
vertical: 'bottom',
horizontal: 'center',
}}
open={snackbar}
autoHideDuration={6000}
onClose={closeSnackBar}
>
<MySnackbarContentWrapper
variant={snackbar && snackbar.variant}
message={snackbar && snackbar.message}
/>
</Snackbar>}
</FundingContext.Provider>
2018-11-28 11:12:50 -05:00
)
}
}
2018-12-15 07:01:40 -05:00
export default App