liquid-funding/app/dapp.js

104 lines
3.5 KiB
JavaScript
Raw Normal View History

2019-04-16 19:09:07 +00:00
/*global web3*/
2018-12-15 17:03:19 +00:00
import React from 'react'
2019-01-25 20:16:55 +00:00
import { HashRouter as Router } from 'react-router-dom'
import EmbarkJS from 'Embark/EmbarkJS'
import LiquidPledging from 'Embark/contracts/LiquidPledging'
import Snackbar from '@material-ui/core/Snackbar'
2018-12-01 22:16:58 +00:00
import { initVaultAndLP, vaultPledgingNeedsInit, standardTokenApproval, getLpAllowance } from './utils/initialize'
2019-01-25 20:16:55 +00:00
import { getAuthorizedPayments } from './utils/events'
import { FundingContext } from './context'
2018-12-15 12:01:40 +00:00
import MainCointainer from './components/MainCointainer'
2019-01-25 20:16:55 +00:00
import { getAndAddLpEvents } from './actions/lpEvents'
import { getAndAddVaultEvents } from './actions/vaultEvents'
import { addFormattedProfiles } from './actions/profiles'
2019-02-05 18:41:17 +00:00
import { updateStalePledges, getAndAddPledges } from './actions/pledges'
import { updateDelegates } from './actions/delegates'
import { MySnackbarContentWrapper } from './components/base/SnackBars'
2018-11-28 16:12:50 +00:00
2018-12-14 19:10:08 +00:00
const { getNetworkType } = web3.eth.net
2018-11-28 16:12:50 +00:00
class App extends React.Component {
state = {
2019-01-10 20:44:45 +00:00
loading: true,
lpAllowance: 0,
needsInit: true,
};
2018-11-28 16:12:50 +00:00
componentDidMount(){
2018-11-29 16:45:57 +00:00
EmbarkJS.onReady(async (err) => {
2018-12-01 14:31:04 +00:00
getNetworkType().then(async network => {
2018-11-28 16:12:50 +00:00
const { environment } = EmbarkJS
const isInitialized = await vaultPledgingNeedsInit()
if (!!isInitialized) {
2019-01-02 20:32:28 +00:00
if (environment === 'development') console.log('mock_time:', await LiquidPledging.mock_time.call())
2019-01-25 20:16:55 +00:00
const account = await web3.eth.getCoinbase()
this.setState({ account })
const lpAllowance = await getLpAllowance()
2019-01-25 20:16:55 +00:00
//TODO add block based sync
const authorizedPayments = await getAuthorizedPayments()
2019-01-25 20:16:55 +00:00
this.syncWithRemote()
this.setState({
account,
network,
environment,
lpAllowance,
2018-12-14 19:10:08 +00:00
authorizedPayments,
2019-01-25 20:16:55 +00:00
needsInit: false
})
}
2018-12-16 17:59:33 +00:00
})
})
2018-11-28 16:12:50 +00:00
}
2019-01-25 20:16:55 +00: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 20:16:55 +00:00
await addFormattedProfiles()
await getAndAddPledges()
2019-02-05 18:41:17 +00:00
await updateStalePledges()
await updateDelegates()
2019-01-25 20:16:55 +00:00
this.setState({ loading: false })
2018-12-07 14:47:00 +00:00
}
openSnackBar = (variant, message) => {
this.setState({ snackbar: { variant, message } })
}
closeSnackBar = () => {
this.setState({ snackbar: null })
}
2018-11-28 16:12:50 +00:00
render() {
const { account, needsInit, loading, authorizedPayments, snackbar, network, environment } = this.state
const { appendFundProfile, appendPledges, transferPledgeAmounts, openSnackBar, closeSnackBar } = this
const fundingContext = { appendPledges, appendFundProfile, account, transferPledgeAmounts, authorizedPayments, needsInit, initVaultAndLP, standardTokenApproval, openSnackBar, closeSnackBar, network, environment }
2018-11-28 16:12:50 +00:00
return (
<FundingContext.Provider value={fundingContext}>
2018-12-15 17:03:19 +00:00
<Router>
2019-01-10 20:44:45 +00:00
<MainCointainer loading={loading} />
2018-12-15 17:03:19 +00: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 16:12:50 +00:00
)
}
}
2018-12-15 12:01:40 +00:00
export default App