liquid-funding/app/dapp.js

107 lines
3.7 KiB
JavaScript
Raw Normal View History

2018-11-28 16:12:50 +00:00
import React from 'react';
import EmbarkJS from 'Embark/EmbarkJS';
2018-11-29 16:45:57 +00:00
import LPVault from 'Embark/contracts/LPVault';
import LiquidPledgingMock from 'Embark/contracts/LiquidPledgingMock';
2018-11-28 16:12:50 +00:00
import web3 from "Embark/web3";
2018-12-01 14:31:04 +00:00
import Divider from '@material-ui/core/Divider';
2018-12-01 22:16:58 +00:00
import Button from '@material-ui/core/Button';
2018-11-30 11:02:57 +00:00
import AddFunder from './components/AddFunder';
2018-11-30 18:36:09 +00:00
import CreateFunding from './components/CreateFunding';
2018-12-03 15:29:14 +00:00
import FunderProfilesTable from './components/FunderProfilesTable'
import PledgesTable from './components/PledgesTable'
2018-12-01 22:16:58 +00:00
import { initVaultAndLP, vaultPledgingNeedsInit, standardTokenApproval, getLpAllowance } from './utils/initialize'
import { getProfileEvents, formatFundProfileEvent, getAuthorizedPayments } from './utils/events';
import { getAllPledges, appendToExistingPledges, transferBetweenPledges } from './utils/pledges';
import { FundingContext } from './context'
2018-12-07 14:47:00 +00:00
import { cancelProfile } from './utils/fundProfiles'
2018-12-10 19:44:57 +00:00
import SetMockedTime from './components/SetMockedTime'
2018-11-28 16:12:50 +00:00
const { getNetworkType } = web3.eth.net;
class App extends React.Component {
constructor(props) {
super(props)
}
state = {
lpAllowance: 0,
fundProfiles: [],
allPledges: [],
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) {
2018-12-10 19:44:57 +00:00
console.log('mock_time:', await LiquidPledgingMock.mock_time.call())
const lpAllowance = await getLpAllowance()
const fundProfiles = await getProfileEvents()
const allPledges = await getAllPledges()
const authorizedPayments = await getAuthorizedPayments()
this.setState({
network,
environment,
needsInit: false,
lpAllowance,
fundProfiles,
allPledges,
authorizedPayments
})
}
2018-11-28 16:12:50 +00:00
});
});
}
2018-12-02 14:46:20 +00:00
appendFundProfile = async event => {
const formattedEvent = await formatFundProfileEvent(event)
this.setState((state) => {
const { fundProfiles } = state;
return {
...state,
fundProfiles: [ ...fundProfiles, formattedEvent ]
}
})
}
2018-12-03 17:15:16 +00:00
appendPledges = () => {
const { allPledges } = this.state
appendToExistingPledges(allPledges, this.setState)
}
transferPledgeAmounts = tx => {
transferBetweenPledges(this.setState.bind(this), tx)
}
2018-12-07 14:47:00 +00:00
cancelFundProfile = id => {
this.setState((state) => cancelProfile(state, id))
}
2018-11-28 16:12:50 +00:00
render() {
const { needsInit, lpAllowance, fundProfiles, allPledges, authorizedPayments } = this.state;
2018-12-07 14:47:00 +00:00
const { appendFundProfile, appendPledges, transferPledgeAmounts, cancelFundProfile } = this
const fundingContext = { transferPledgeAmounts, authorizedPayments }
2018-11-28 16:12:50 +00:00
return (
<FundingContext.Provider value={fundingContext}>
<div>
2018-12-10 15:22:52 +00:00
{!!allPledges.length && <PledgesTable data={allPledges} transferPledgeAmounts={transferPledgeAmounts} fundProfiles={fundProfiles} />}
{!!fundProfiles.length && <FunderProfilesTable data={fundProfiles} cancelFundProfile={cancelFundProfile}/>}
<AddFunder appendFundProfile={appendFundProfile} />
<Divider variant="middle" />
<CreateFunding refreshTable={appendPledges} />
{needsInit && <Button variant="outlined" color="secondary" onClick={initVaultAndLP}>
Initialize Contracts
</Button>}
<Button variant="outlined" color="primary" onClick={standardTokenApproval}>
GIVE VAULT TOKEN APPROVAL
</Button>
2018-12-10 19:44:57 +00:00
<SetMockedTime />
</div>
</FundingContext.Provider>
2018-11-28 16:12:50 +00:00
)
}
}
export default App;