2018-04-23 20:42:52 +00:00
|
|
|
import EmbarkJS from 'Embark/EmbarkJS';
|
2019-02-21 04:23:11 +00:00
|
|
|
import SimpleStorage from '../../embarkArtifacts/contracts/SimpleStorage';
|
2018-04-23 20:42:52 +00:00
|
|
|
import React from 'react';
|
2019-08-23 15:51:03 +00:00
|
|
|
import {Form, FormGroup, Input, HelpBlock, Button, FormText} from 'reactstrap';
|
2018-08-15 17:00:46 +00:00
|
|
|
|
2018-04-23 20:42:52 +00:00
|
|
|
class Blockchain extends React.Component {
|
|
|
|
|
2018-08-15 17:00:46 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
valueSet: 10,
|
|
|
|
valueGet: "",
|
|
|
|
logs: []
|
2018-11-16 09:05:42 +00:00
|
|
|
};
|
2018-08-15 17:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleChange(e) {
|
|
|
|
this.setState({ valueSet: e.target.value });
|
|
|
|
}
|
|
|
|
|
|
|
|
checkEnter(e, func) {
|
|
|
|
if (e.key !== 'Enter') {
|
|
|
|
return;
|
2018-04-23 20:42:52 +00:00
|
|
|
}
|
2018-08-15 17:00:46 +00:00
|
|
|
e.preventDefault();
|
|
|
|
func.apply(this, [e]);
|
|
|
|
}
|
|
|
|
|
2018-11-16 09:05:42 +00:00
|
|
|
async setValue(e) {
|
2018-08-15 17:00:46 +00:00
|
|
|
e.preventDefault();
|
2018-11-16 09:05:42 +00:00
|
|
|
await EmbarkJS.enableEthereum();
|
2018-08-15 17:00:46 +00:00
|
|
|
var value = parseInt(this.state.valueSet, 10);
|
|
|
|
|
2018-09-15 11:51:17 +00:00
|
|
|
SimpleStorage.methods.set(value).send();
|
|
|
|
this._addToLog("SimpleStorage.methods.set(value).send()");
|
2018-08-15 17:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getValue(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
2018-09-14 22:39:47 +00:00
|
|
|
SimpleStorage.methods.get().call().then(_value => this.setState({ valueGet: _value }));
|
|
|
|
this._addToLog("SimpleStorage.methods.get(console.log)");
|
2018-08-15 17:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_addToLog(txt) {
|
|
|
|
this.state.logs.push(txt);
|
|
|
|
this.setState({ logs: this.state.logs });
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (<React.Fragment>
|
|
|
|
<h3> 1. Set the value in the blockchain</h3>
|
2019-08-23 15:51:03 +00:00
|
|
|
<Form onKeyDown={(e) => this.checkEnter(e, this.setValue)}>
|
|
|
|
<FormGroup className="inline-input-btn">
|
|
|
|
<Input
|
2018-08-15 17:00:46 +00:00
|
|
|
type="text"
|
|
|
|
defaultValue={this.state.valueSet}
|
|
|
|
onChange={(e) => this.handleChange(e)}/>
|
2019-08-23 15:51:03 +00:00
|
|
|
<Button color="primary" onClick={(e) => this.setValue(e)}>Set Value</Button>
|
|
|
|
<FormText color="muted">Once you set the value, the transaction will need to be mined and then the value will be updated
|
|
|
|
on the blockchain.</FormText>
|
2018-08-15 17:00:46 +00:00
|
|
|
</FormGroup>
|
|
|
|
</Form>
|
|
|
|
|
|
|
|
<h3> 2. Get the current value</h3>
|
2019-08-23 15:51:03 +00:00
|
|
|
<Form>
|
2018-08-15 17:00:46 +00:00
|
|
|
<FormGroup>
|
2019-08-23 15:51:03 +00:00
|
|
|
<Button color="primary" onClick={(e) => this.getValue(e)}>Get Value</Button>
|
|
|
|
<FormText color="muted">Click the button to get the current value. The initial value is 100.</FormText>
|
|
|
|
{this.state.valueGet && this.state.valueGet !== 0 &&
|
|
|
|
<p>Current value is <span className="value font-weight-bold">{this.state.valueGet}</span></p>}
|
2018-08-15 17:00:46 +00:00
|
|
|
</FormGroup>
|
|
|
|
</Form>
|
|
|
|
|
|
|
|
<h3> 3. Contract Calls </h3>
|
|
|
|
<p>Javascript calls being made: </p>
|
|
|
|
<div className="logs">
|
2018-04-23 20:42:52 +00:00
|
|
|
{
|
|
|
|
this.state.logs.map((item, i) => <p key={i}>{item}</p>)
|
|
|
|
}
|
2018-08-15 17:00:46 +00:00
|
|
|
</div>
|
2018-04-23 20:42:52 +00:00
|
|
|
</React.Fragment>
|
2018-08-15 17:00:46 +00:00
|
|
|
);
|
2018-04-23 20:42:52 +00:00
|
|
|
}
|
2018-08-15 17:00:46 +00:00
|
|
|
}
|
2018-04-23 20:42:52 +00:00
|
|
|
|
2018-08-15 17:00:46 +00:00
|
|
|
export default Blockchain;
|