76 lines
2.1 KiB
JavaScript
Raw Normal View History

import EmbarkJS from 'Embark/EmbarkJS';
2018-05-14 10:58:48 -04:00
import React from 'react';
2018-08-06 09:18:30 -04:00
import web3 from 'Embark/web3';
2018-05-14 10:58:48 -04:00
class AccountBalance extends React.Component {
constructor(props) {
super(props);
this.state = {
eth: 0,
rnd: 0
};
}
componentDidMount(){
EmbarkJS.onReady(err => {
2018-08-06 09:18:30 -04:00
if(!err) this.updateBalances();
2018-05-14 10:58:48 -04:00
});
}
updateBalances(ev){
if(ev) ev.preventDefault();
web3.eth.getBalance(this.props.address)
.then(eth => {
2018-08-06 09:18:30 -04:00
this.setState({eth});
});
this.props.RND.methods.balanceOf(this.props.address)
.call()
.then(rnd => {
2018-08-06 09:18:30 -04:00
this.setState({rnd});
});
}
sendEther(ev){
ev.preventDefault();
web3.eth.sendTransaction({to: this.props.address, value: web3.utils.toWei('1', 'ether')})
.then(() => {
this.updateBalances();
return true;
2018-08-06 09:18:30 -04:00
});
}
generateTokens(ev){
ev.preventDefault();
this.props.RND.methods.generateTokens(this.props.address, web3.utils.toWei('500', 'ether'))
.send({gas: 1000000})
.then(() => {
this.updateBalances();
return true;
});
}
2018-05-14 10:58:48 -04:00
render(){
const rnd = web3.utils.fromWei(this.state.rnd, "ether");
const eth = web3.utils.fromWei(this.state.eth, "ether");
2018-05-14 10:58:48 -04:00
return <div>
<h3>{this.props.name}</h3>
<small>{this.props.address}</small>
<p><b>RDN</b><br /><small>{rnd}</small></p>
<p><b>ETH</b><br /><small>{eth}</small></p>
<a href="#" onClick={(ev) => this.updateBalances(ev)}>Update balances</a><br />
<a href="#" onClick={(ev) => this.generateTokens(ev)}>Generate Tokens</a><br />
<a href="#" onClick={(ev) => this.sendEther(ev)}>Send 1 Ether</a><br />
2018-05-14 10:58:48 -04:00
</div>;
}
2018-08-06 09:18:30 -04:00
}
2018-05-14 10:58:48 -04:00
2018-08-06 09:18:30 -04:00
export default AccountBalance;