2018-05-13 04:31:01 +00:00
|
|
|
import EmbarkJS from 'Embark/EmbarkJS';
|
|
|
|
import ERC20Token from 'Embark/contracts/ERC20Token';
|
|
|
|
import React from 'react';
|
2018-06-05 17:19:21 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-05-13 04:31:01 +00:00
|
|
|
import { Form, FormGroup, FormControl, HelpBlock, Button } from 'react-bootstrap';
|
2018-06-05 17:19:21 +00:00
|
|
|
import { getCurrentAccount, accountsIsLoading } from '../reducers/accounts';
|
2018-05-13 04:31:01 +00:00
|
|
|
|
|
|
|
class ERC20TokenUI extends React.Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2018-05-31 23:38:21 +00:00
|
|
|
ERC20Token.options.address = props.address;
|
2018-05-13 04:31:01 +00:00
|
|
|
this.state = {
|
|
|
|
balanceOf: 0,
|
|
|
|
transferTo: "",
|
|
|
|
transferAmount: 0,
|
2018-05-31 23:38:21 +00:00
|
|
|
accountBalance: 0,
|
|
|
|
accountB: web3.eth.defaultAccount,
|
2018-05-13 04:31:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
update_transferTo(e){
|
|
|
|
this.setState({transferTo: e.target.value});
|
|
|
|
}
|
|
|
|
|
|
|
|
update_transferAmount(e){
|
|
|
|
this.setState({transferAmount: e.target.value});
|
|
|
|
}
|
|
|
|
|
|
|
|
transfer(e){
|
|
|
|
var to = this.state.transferTo;
|
|
|
|
var amount = this.state.transferAmount;
|
|
|
|
var tx = ERC20Token.methods.transfer(to, amount).send({from: web3.eth.defaultAccount});
|
|
|
|
this._addToLog(ERC20Token.options.address+".transfer(" + to + ", "+amount+")");
|
|
|
|
}
|
|
|
|
|
|
|
|
approve(e){
|
|
|
|
var to = this.state.transferTo;
|
|
|
|
var amount = this.state.transferAmount;
|
|
|
|
var tx = ERC20Token.methods.approve(to, amount).send({from: web3.eth.defaultAccount});
|
|
|
|
this._addToLog(ERC20Token.options.address+".approve(" + to + ", "+amount+")");
|
|
|
|
}
|
|
|
|
|
|
|
|
balanceOf(e){
|
|
|
|
e.preventDefault();
|
|
|
|
var who = e.target.value;
|
|
|
|
if (EmbarkJS.isNewWeb3()) {
|
|
|
|
ERC20Token.methods.balanceOf(who).call()
|
|
|
|
.then(_value => this.setState({balanceOf: _value}))
|
|
|
|
} else {
|
|
|
|
ERC20Token.balanceOf(who)
|
|
|
|
.then(_value => this.x({balanceOf: _value}));
|
|
|
|
}
|
|
|
|
this._addToLog(ERC20Token.options.address+".balanceOf(" + who + ")");
|
|
|
|
}
|
|
|
|
|
2018-05-31 23:38:21 +00:00
|
|
|
getDefaultAccountBalance(){
|
|
|
|
if (EmbarkJS.isNewWeb3()) {
|
|
|
|
ERC20Token.methods.balanceOf(web3.eth.defaultAccount).call()
|
|
|
|
.then(_value => this.setState({accountBalance: _value}))
|
|
|
|
} else {
|
|
|
|
ERC20Token.balanceOf(web3.eth.defaultAccount)
|
|
|
|
.then(_value => this.x({valueGet: _value}))
|
|
|
|
}
|
|
|
|
this._addToLog(ERC20Token.options.address + ".balanceOf(" + web3.eth.defaultAccount + ")");
|
|
|
|
}
|
2018-05-13 04:31:01 +00:00
|
|
|
|
|
|
|
_addToLog(txt){
|
2018-05-31 23:38:21 +00:00
|
|
|
console.log(txt);
|
2018-05-13 04:31:01 +00:00
|
|
|
}
|
|
|
|
|
2018-06-05 17:19:21 +00:00
|
|
|
render() {
|
|
|
|
const { account, isLoading } = this.props;
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
|
|
|
<h3> Read your account token balance </h3>
|
|
|
|
<Form inline>
|
|
|
|
<FormGroup>
|
2018-06-13 16:58:19 +00:00
|
|
|
{!isLoading && <HelpBlock>Your test token balance is <span className="accountBalance">{account.SNTBalance}</span></HelpBlock>}
|
2018-06-05 17:19:21 +00:00
|
|
|
</FormGroup>
|
|
|
|
</Form>
|
|
|
|
|
|
|
|
<h3> Read account token balance</h3>
|
|
|
|
<Form inline>
|
|
|
|
<FormGroup>
|
|
|
|
<label>
|
|
|
|
Of:
|
|
|
|
<FormControl
|
|
|
|
type="text"
|
|
|
|
defaultValue={this.state.accountB}
|
|
|
|
onChange={(e) => this.balanceOf(e)} />
|
|
|
|
</label>
|
|
|
|
<label>
|
|
|
|
<HelpBlock><span className="balanceOf">{this.state.balanceOf}</span></HelpBlock>
|
|
|
|
</label>
|
|
|
|
|
|
|
|
</FormGroup>
|
|
|
|
</Form>
|
2018-05-13 04:31:01 +00:00
|
|
|
|
2018-06-05 17:19:21 +00:00
|
|
|
<h3> Transfer/Approve token balance</h3>
|
|
|
|
<Form inline>
|
|
|
|
<FormGroup>
|
|
|
|
<label>
|
|
|
|
To:
|
|
|
|
<FormControl
|
|
|
|
type="text"
|
|
|
|
defaultValue={this.state.transferTo}
|
|
|
|
onChange={(e) => this.update_transferTo(e) } />
|
|
|
|
</label>
|
|
|
|
<label>
|
|
|
|
Amount:
|
|
|
|
<FormControl
|
|
|
|
type="text"
|
|
|
|
defaultValue={this.state.transferAmount}
|
|
|
|
onChange={(e) => this.update_transferAmount(e) } />
|
|
|
|
</label>
|
|
|
|
<Button bsStyle="primary" onClick={(e) => this.transfer(e)}>Transfer</Button>
|
|
|
|
<Button bsStyle="primary" onClick={(e) => this.approve(e)}>Approve</Button>
|
|
|
|
</FormGroup>
|
|
|
|
</Form>
|
|
|
|
|
2018-05-13 04:31:01 +00:00
|
|
|
</React.Fragment>
|
2018-06-05 17:19:21 +00:00
|
|
|
);
|
2018-05-13 04:31:01 +00:00
|
|
|
}
|
2018-06-05 17:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
|
|
|
account: getCurrentAccount(state),
|
|
|
|
isLoading: accountsIsLoading(state),
|
|
|
|
});
|
2018-05-13 04:31:01 +00:00
|
|
|
|
2018-06-05 17:19:21 +00:00
|
|
|
export default connect(mapStateToProps)(ERC20TokenUI);
|