2018-08-07 19:27:57 +00:00
|
|
|
import React, {Component, Fragment} from 'react';
|
2018-08-06 17:47:56 +00:00
|
|
|
import AddIcon from '@material-ui/icons/Add';
|
|
|
|
import BalanceIcon from '@material-ui/icons/AccountBalance';
|
|
|
|
import Button from '@material-ui/core/Button';
|
2018-08-07 19:15:00 +00:00
|
|
|
import Card from '@material-ui/core/Card';
|
|
|
|
import CardContent from '@material-ui/core/CardContent';
|
2018-08-06 17:47:56 +00:00
|
|
|
import CircularProgress from '@material-ui/core/CircularProgress';
|
|
|
|
import EmbarkJS from 'Embark/EmbarkJS';
|
2018-08-07 02:30:16 +00:00
|
|
|
import Identity from 'Embark/contracts/Identity';
|
2018-08-06 17:47:56 +00:00
|
|
|
import KeyIcon from '@material-ui/icons/VpnKey';
|
|
|
|
import LinearProgress from '@material-ui/core/LinearProgress';
|
|
|
|
import List from '@material-ui/core/List';
|
|
|
|
import ListItem from '@material-ui/core/ListItem';
|
|
|
|
import ListItemIcon from '@material-ui/core/ListItemIcon';
|
|
|
|
import ListItemText from '@material-ui/core/ListItemText';
|
2018-08-07 02:30:16 +00:00
|
|
|
import NumberIcon from '@material-ui/icons/ConfirmationNumber';
|
2018-08-06 17:47:56 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import RefreshIcon from '@material-ui/icons/Refresh';
|
2018-08-06 18:10:25 +00:00
|
|
|
import STT from 'Embark/contracts/STT';
|
2018-08-06 17:47:56 +00:00
|
|
|
import Typography from '@material-ui/core/Typography';
|
|
|
|
import config from '../config';
|
|
|
|
import web3 from 'Embark/web3';
|
|
|
|
import {withStyles} from '@material-ui/core/styles';
|
2018-08-07 02:30:16 +00:00
|
|
|
window.Id = Identity;
|
2018-08-06 17:47:56 +00:00
|
|
|
const styles = theme => ({
|
|
|
|
button: {
|
2018-08-06 18:10:25 +00:00
|
|
|
marginRight: theme.spacing.unit * 2
|
|
|
|
},
|
2018-08-07 19:15:00 +00:00
|
|
|
card: {
|
|
|
|
marginBottom: theme.spacing.unit * 2
|
|
|
|
},
|
2018-08-06 18:10:25 +00:00
|
|
|
icon: {
|
|
|
|
marginRight: theme.spacing.unit
|
2018-08-06 17:47:56 +00:00
|
|
|
},
|
|
|
|
container: {
|
|
|
|
width: '100%',
|
|
|
|
position: 'relative'
|
|
|
|
},
|
|
|
|
root: {
|
|
|
|
width: '100%',
|
|
|
|
maxWidth: 420,
|
|
|
|
backgroundColor: theme.palette.background.paper
|
|
|
|
},
|
|
|
|
right: {
|
|
|
|
position: 'absolute',
|
2018-08-07 19:27:57 +00:00
|
|
|
top: theme.spacing.unit * 4,
|
2018-08-06 17:47:56 +00:00
|
|
|
right: theme.spacing.unit * 2
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
class Status extends Component {
|
|
|
|
|
|
|
|
constructor(props){
|
|
|
|
super(props);
|
2018-09-14 14:22:12 +00:00
|
|
|
|
2018-08-06 17:47:56 +00:00
|
|
|
this.state = {
|
2018-09-14 14:22:12 +00:00
|
|
|
'isDev': true,
|
2018-08-06 17:47:56 +00:00
|
|
|
'identityEthBalance': 0,
|
2018-08-06 18:10:25 +00:00
|
|
|
'identitySTTBalance': 0,
|
2018-08-06 17:47:56 +00:00
|
|
|
'relayerEthBalance': 0,
|
2018-08-06 18:10:25 +00:00
|
|
|
'relayerSTTBalance': 0,
|
2018-08-06 17:47:56 +00:00
|
|
|
'block': 0,
|
|
|
|
'submitState': {
|
|
|
|
'etherSend': false,
|
2018-08-06 18:10:25 +00:00
|
|
|
'createIdentity': false,
|
|
|
|
'generateSTT': false
|
2018-08-06 17:47:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount(){
|
|
|
|
EmbarkJS.onReady(err => {
|
|
|
|
if(err) {
|
|
|
|
console.error(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-07 19:15:00 +00:00
|
|
|
this.getBlock();
|
2018-08-07 02:30:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getBlock = () => {
|
2018-09-14 14:22:12 +00:00
|
|
|
|
|
|
|
// Default for devenv
|
|
|
|
web3.eth.net.getId().then(netId => {
|
2018-09-14 16:00:30 +00:00
|
|
|
this.setState({isDev: netId != 1});
|
2018-09-14 14:22:12 +00:00
|
|
|
});
|
|
|
|
|
2018-09-14 15:35:46 +00:00
|
|
|
this.web3BlockRead();
|
|
|
|
}
|
|
|
|
|
|
|
|
web3BlockRead = () => {
|
|
|
|
web3.eth.getBlock('latest').then(block => {
|
2018-08-07 02:30:16 +00:00
|
|
|
this.setState({block: block.number});
|
|
|
|
this.readChain();
|
2018-11-07 11:54:05 +00:00
|
|
|
setTimeout(this.web3BlockRead, 5000);
|
2018-08-07 19:15:00 +00:00
|
|
|
return true;
|
2018-08-06 17:47:56 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-07 02:30:16 +00:00
|
|
|
readChain = () => {
|
2018-08-06 17:47:56 +00:00
|
|
|
if(this.props.identityAddress){
|
|
|
|
web3.eth.getBalance(this.props.identityAddress)
|
|
|
|
.then(identityEthBalance => {
|
|
|
|
this.setState({identityEthBalance});
|
|
|
|
});
|
2018-08-06 18:10:25 +00:00
|
|
|
|
|
|
|
STT.methods.balanceOf(this.props.identityAddress)
|
|
|
|
.call()
|
|
|
|
.then(identitySTTBalance => {
|
|
|
|
this.setState({identitySTTBalance: web3.utils.fromWei(identitySTTBalance, 'ether')});
|
|
|
|
});
|
2018-08-07 02:30:16 +00:00
|
|
|
|
|
|
|
Identity.options.address = this.props.identityAddress;
|
|
|
|
Identity.methods.nonce()
|
|
|
|
.call()
|
|
|
|
.then((nonce) => {
|
|
|
|
this.props.nonceUpdateFunction(nonce);
|
2018-08-07 19:15:00 +00:00
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
console.log("Address " + this.props.identityAddress + " is not an identity");
|
2018-08-07 02:30:16 +00:00
|
|
|
});
|
2018-08-06 17:47:56 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 14:22:12 +00:00
|
|
|
web3.eth.getBalance(this.props.relayerAddress)
|
2018-08-06 17:47:56 +00:00
|
|
|
.then(relayerEthBalance => {
|
|
|
|
this.setState({relayerEthBalance});
|
|
|
|
});
|
2018-08-06 18:10:25 +00:00
|
|
|
|
2018-09-14 14:22:12 +00:00
|
|
|
STT.methods.balanceOf(this.props.relayerAddress)
|
2018-08-06 18:10:25 +00:00
|
|
|
.call()
|
|
|
|
.then(relayerSTTBalance => {
|
|
|
|
this.setState({relayerSTTBalance: web3.utils.fromWei(relayerSTTBalance, 'ether')});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
generateSTT = (event) => {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
let submitState = this.state.submitState;
|
|
|
|
submitState.generateSTT = true;
|
|
|
|
this.setState({submitState});
|
|
|
|
|
2018-08-07 02:30:16 +00:00
|
|
|
let toSend = STT.methods.generateTokens(this.props.identityAddress, web3.utils.toWei('5000', 'ether'));
|
2018-08-06 18:10:25 +00:00
|
|
|
toSend.estimateGas()
|
|
|
|
.then(estimatedGas => {
|
|
|
|
return toSend.send({gas: estimatedGas + 10000});
|
|
|
|
})
|
|
|
|
.then((receipt) => {
|
|
|
|
console.log(receipt);
|
|
|
|
|
|
|
|
submitState = this.state.submitState;
|
|
|
|
submitState.generateSTT = false;
|
|
|
|
this.setState({submitState});
|
|
|
|
});
|
2018-08-06 17:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
createIdentity = (event) => {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
let submitState = this.state.submitState;
|
2018-08-07 19:15:00 +00:00
|
|
|
submitState.createIdentity = false;
|
2018-08-06 17:47:56 +00:00
|
|
|
this.setState({submitState});
|
|
|
|
|
|
|
|
this.props.identityCreationFunction(() => {
|
|
|
|
submitState = this.state.submitState;
|
|
|
|
submitState.createIdentity = false;
|
|
|
|
this.setState({submitState});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
sendEther = (event) => {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
let submitState = this.state.submitState;
|
2018-08-07 19:15:00 +00:00
|
|
|
submitState.etherSend = false;
|
2018-08-06 17:47:56 +00:00
|
|
|
this.setState({submitState});
|
|
|
|
|
2018-09-14 14:22:12 +00:00
|
|
|
web3.eth.sendTransaction({from: web3.eth.defaultAccount, to: this.props.relayerAddress, value: web3.utils.toWei('1', "ether")})
|
2018-08-06 17:47:56 +00:00
|
|
|
.then((receipt) => {
|
|
|
|
console.log(receipt);
|
|
|
|
submitState = this.state.submitState;
|
|
|
|
submitState.etherSend = false;
|
|
|
|
this.setState({submitState});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render(){
|
2018-09-14 14:22:12 +00:00
|
|
|
const {classes, identityAddress, nonce, relayerAddress} = this.props;
|
|
|
|
const {identityEthBalance, relayerEthBalance, identitySTTBalance, relayerSTTBalance, submitState, block, isDev} = this.state;
|
2018-08-06 17:47:56 +00:00
|
|
|
|
2018-08-07 19:27:57 +00:00
|
|
|
return <Fragment>
|
2018-08-07 19:15:00 +00:00
|
|
|
<Card className={classes.card}>
|
|
|
|
<CardContent>
|
|
|
|
<Typography>
|
|
|
|
Whisper Messages:
|
|
|
|
</Typography>
|
|
|
|
<pre>{this.props.message}</pre>
|
|
|
|
</CardContent>
|
|
|
|
</Card>
|
2018-08-07 19:27:57 +00:00
|
|
|
<div className={classes.container}>
|
|
|
|
{ (submitState.createIdentity || submitState.etherSend || submitState.generateSTT) && <LinearProgress /> }
|
|
|
|
|
2018-08-06 17:47:56 +00:00
|
|
|
<List dense={true}>
|
|
|
|
<ListItem>
|
|
|
|
<Typography variant="display1">
|
|
|
|
Identity
|
|
|
|
</Typography>
|
2018-09-14 14:22:12 +00:00
|
|
|
|
|
|
|
{ isDev && <Button className={classes.button} color="primary" aria-label="New Identity" onClick={this.createIdentity} disabled={submitState.createIdentity}>
|
2018-08-06 18:10:25 +00:00
|
|
|
<RefreshIcon className={classes.icon} />
|
2018-08-06 17:47:56 +00:00
|
|
|
Create new identity
|
2018-09-14 14:22:12 +00:00
|
|
|
</Button> }
|
|
|
|
{ isDev && <Button className={classes.button} color="primary" aria-label="Generate STT" onClick={this.generateSTT} disabled={submitState.generateSTT}>
|
2018-08-06 18:10:25 +00:00
|
|
|
<AddIcon className={classes.icon} />
|
|
|
|
Generate 5K STT (only on dev)
|
2018-09-14 14:22:12 +00:00
|
|
|
</Button> }
|
2018-08-06 17:47:56 +00:00
|
|
|
</ListItem>
|
|
|
|
<ListItem className={classes.root}>
|
|
|
|
<ListItemIcon>
|
|
|
|
<KeyIcon />
|
|
|
|
</ListItemIcon>
|
|
|
|
<ListItemText
|
|
|
|
primary={identityAddress}
|
|
|
|
secondary="Address"
|
|
|
|
/>
|
|
|
|
</ListItem>
|
2018-08-07 02:30:16 +00:00
|
|
|
<ListItem className={classes.root}>
|
|
|
|
<ListItemIcon>
|
|
|
|
<NumberIcon />
|
|
|
|
</ListItemIcon>
|
|
|
|
<ListItemText
|
|
|
|
primary={nonce}
|
|
|
|
secondary="Nonce"
|
|
|
|
/>
|
|
|
|
</ListItem>
|
2018-08-06 17:47:56 +00:00
|
|
|
<ListItem className={classes.root}>
|
|
|
|
<ListItemIcon>
|
|
|
|
<BalanceIcon />
|
|
|
|
</ListItemIcon>
|
|
|
|
<ListItemText
|
|
|
|
primary={identityEthBalance}
|
|
|
|
secondary="ETH Balance (wei)"
|
|
|
|
/>
|
|
|
|
<ListItemText
|
2018-08-06 18:10:25 +00:00
|
|
|
primary={identitySTTBalance}
|
|
|
|
secondary="STT Balance"
|
2018-08-06 17:47:56 +00:00
|
|
|
/>
|
|
|
|
</ListItem>
|
|
|
|
</List>
|
|
|
|
|
|
|
|
<List dense={true}>
|
|
|
|
<ListItem>
|
|
|
|
<Typography variant="display1">
|
2018-08-07 19:15:00 +00:00
|
|
|
Relayer
|
2018-08-06 17:47:56 +00:00
|
|
|
</Typography>
|
2018-10-10 19:35:42 +00:00
|
|
|
{ false && isDev && <Button className={classes.button} color="primary" aria-label="Add ether" onClick={this.sendEther} disabled={submitState.etherSend}>
|
2018-08-06 18:10:25 +00:00
|
|
|
<AddIcon className={classes.icon} />
|
2018-08-06 17:47:56 +00:00
|
|
|
Send ether
|
2018-09-14 14:22:12 +00:00
|
|
|
</Button> }
|
2018-08-06 17:47:56 +00:00
|
|
|
</ListItem>
|
|
|
|
<ListItem className={classes.root}>
|
|
|
|
<ListItemIcon>
|
|
|
|
<KeyIcon />
|
|
|
|
</ListItemIcon>
|
|
|
|
<ListItemText
|
|
|
|
primary={relayerAddress}
|
|
|
|
secondary="Address"
|
|
|
|
/>
|
|
|
|
</ListItem>
|
|
|
|
<ListItem className={classes.root}>
|
|
|
|
<ListItemIcon>
|
|
|
|
<BalanceIcon />
|
|
|
|
</ListItemIcon>
|
|
|
|
<ListItemText
|
|
|
|
primary={relayerEthBalance}
|
|
|
|
secondary="ETH Balance (wei)"
|
|
|
|
/>
|
|
|
|
<ListItemText
|
2018-08-06 18:10:25 +00:00
|
|
|
primary={relayerSTTBalance}
|
2018-08-06 17:47:56 +00:00
|
|
|
secondary="STT Balance (wei)"
|
|
|
|
/>
|
|
|
|
</ListItem>
|
|
|
|
</List>
|
|
|
|
<div className={classes.right}>
|
|
|
|
<CircularProgress />
|
|
|
|
<Typography>
|
|
|
|
Block<br />#{block}
|
|
|
|
</Typography>
|
|
|
|
</div>
|
2018-08-07 19:27:57 +00:00
|
|
|
</div>
|
|
|
|
</Fragment>;
|
2018-08-06 17:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Status.propTypes = {
|
|
|
|
classes: PropTypes.object.isRequired,
|
|
|
|
identityAddress: PropTypes.string,
|
2018-08-07 02:30:16 +00:00
|
|
|
nonce: PropTypes.string.isRequired,
|
|
|
|
identityCreationFunction: PropTypes.func.isRequired,
|
2018-08-07 19:15:00 +00:00
|
|
|
nonceUpdateFunction: PropTypes.func.isRequired,
|
2018-09-14 14:22:12 +00:00
|
|
|
message: PropTypes.string,
|
|
|
|
relayerAddress: PropTypes.string
|
2018-08-06 17:47:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default withStyles(styles)(Status);
|