2018-05-10 16:31:10 +00:00
|
|
|
import EmbarkJS from 'Embark/EmbarkJS';
|
|
|
|
import {{contractName}} from 'Embark/contracts/{{contractName}}';
|
|
|
|
|
2018-10-05 19:57:00 +00:00
|
|
|
import React, { Component, Fragment } from 'react';
|
2018-05-10 16:16:13 +00:00
|
|
|
import ReactDOM from 'react-dom';
|
2018-05-11 18:16:27 +00:00
|
|
|
import { FormGroup, ControlLabel, FormControl, Checkbox, Button, Alert, InputGroup } from 'react-bootstrap';
|
2018-05-10 21:02:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
{{#each functions}}
|
2018-10-05 19:57:00 +00:00
|
|
|
class {{capitalize name}}Form{{@index}} extends Component {
|
2018-05-10 21:02:47 +00:00
|
|
|
constructor(props){
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
{{#if inputs.length}}
|
|
|
|
input: {
|
|
|
|
{{#each inputs}}
|
2018-05-11 13:26:27 +00:00
|
|
|
{{name}}: {{#ifeq type 'bool'}}false{{else}}''{{/ifeq}}{{#unless @last}},{{/unless}}
|
2018-05-10 21:02:47 +00:00
|
|
|
{{/each}}
|
|
|
|
},
|
|
|
|
{{/if}}
|
2018-05-11 18:16:27 +00:00
|
|
|
{{#if payable}}
|
|
|
|
value: 0,
|
|
|
|
{{/if}}
|
2018-05-10 21:02:47 +00:00
|
|
|
{{#ifview stateMutability}}
|
|
|
|
output: null,
|
|
|
|
{{/ifview}}
|
2018-05-11 16:34:16 +00:00
|
|
|
error: null,
|
2018-05-11 18:16:27 +00:00
|
|
|
mined: null
|
2018-05-10 21:02:47 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handleChange(e, name){
|
2018-05-11 13:26:27 +00:00
|
|
|
this.state.input[name] = e.target.value;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCheckbox(e, name){
|
|
|
|
this.state.input[name] = e.target.checked;
|
2018-05-10 21:02:47 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleClick(e){
|
|
|
|
e.preventDefault();
|
2018-05-11 18:16:27 +00:00
|
|
|
this.setState({output: null, error: null, receipt: null});
|
|
|
|
|
2018-05-11 14:13:42 +00:00
|
|
|
try {
|
2018-05-11 16:34:16 +00:00
|
|
|
{{#ifview stateMutability}}
|
2018-10-05 19:57:00 +00:00
|
|
|
const result = await {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}this.state.input.{{name}}{{#unless @last}}, {{/unless}}{{/each}}).call()
|
2018-05-11 14:13:42 +00:00
|
|
|
{{#iflengthgt outputs 1}}
|
2018-10-05 19:57:00 +00:00
|
|
|
this.setState({output: {
|
2018-05-11 14:13:42 +00:00
|
|
|
{{#each outputs}}
|
2018-10-05 19:57:00 +00:00
|
|
|
{{emptyname name @index}}: result[{{@index}}]{{#unless @last}},{{/unless}}
|
2018-05-11 14:13:42 +00:00
|
|
|
{{/each}}
|
2018-10-05 19:57:00 +00:00
|
|
|
}});
|
2018-05-11 14:13:42 +00:00
|
|
|
{{else}}
|
2018-10-05 19:57:00 +00:00
|
|
|
this.setState({output: result});
|
|
|
|
{{/iflengthgt}}
|
2018-05-11 16:34:16 +00:00
|
|
|
{{else}}
|
2018-10-05 19:57:00 +00:00
|
|
|
const toSend = {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}this.state.input.{{name}}{{#unless @last}}, {{/unless}}{{/each}});
|
|
|
|
|
|
|
|
const estimatedGas = await toSend.estimateGas({from: web3.eth.defaultAccount});
|
|
|
|
|
|
|
|
const receipt = await toSend.send({
|
|
|
|
{{#if payable}}
|
|
|
|
value: this.state.value,
|
|
|
|
{{/if}}
|
|
|
|
from: web3.eth.defaultAccount,
|
|
|
|
gasLimit: estimatedGas
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log(receipt);
|
|
|
|
|
|
|
|
this.setState({receipt});
|
2018-05-11 16:34:16 +00:00
|
|
|
{{/ifview}}
|
2018-05-11 14:13:42 +00:00
|
|
|
} catch(err) {
|
2018-10-05 19:57:00 +00:00
|
|
|
console.error(err);
|
2018-05-11 18:16:27 +00:00
|
|
|
this.setState({error: err.message});
|
2018-05-11 14:13:42 +00:00
|
|
|
}
|
2018-05-10 21:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render(){
|
2018-10-05 19:57:00 +00:00
|
|
|
const {input, value, error, output, receipt} = this.state;
|
|
|
|
|
2018-05-10 21:02:47 +00:00
|
|
|
return <div className="formSection">
|
|
|
|
<h3>{{name}}</h3>
|
|
|
|
<form>
|
|
|
|
{{#if inputs.length}}
|
|
|
|
{{#each inputs}}
|
2018-05-11 16:34:16 +00:00
|
|
|
<FormGroup>
|
|
|
|
<ControlLabel>{{name}}</ControlLabel>
|
|
|
|
{{#ifeq type 'bool'}}
|
|
|
|
<Checkbox
|
|
|
|
onClick={(e) => this.handleCheckbox(e, '{{name}}')}
|
|
|
|
/>
|
|
|
|
{{else}}
|
|
|
|
<FormControl
|
|
|
|
type="text"
|
2018-10-05 19:57:00 +00:00
|
|
|
defaultValue={ input.{{name}} }
|
2018-05-11 16:34:16 +00:00
|
|
|
placeholder="{{type}}"
|
|
|
|
onChange={(e) => this.handleChange(e, '{{name}}')}
|
|
|
|
/>
|
|
|
|
{{/ifeq}}
|
|
|
|
</FormGroup>
|
2018-05-10 21:02:47 +00:00
|
|
|
{{/each}}
|
|
|
|
{{/if}}
|
2018-05-11 18:16:27 +00:00
|
|
|
{{#if payable}}
|
|
|
|
<FormGroup>
|
|
|
|
<ControlLabel>Value</ControlLabel>
|
|
|
|
<InputGroup>
|
|
|
|
<InputGroup.Addon>Ξ</InputGroup.Addon>
|
|
|
|
<FormControl
|
|
|
|
type="text"
|
2018-10-05 19:57:00 +00:00
|
|
|
defaultValue={ value }
|
2018-05-11 18:16:27 +00:00
|
|
|
placeholder="{{type}}"
|
|
|
|
onChange={(e) => { this.setState({value: e.target.value}); }}
|
|
|
|
/>
|
|
|
|
<InputGroup.Addon>wei</InputGroup.Addon>
|
|
|
|
</InputGroup>
|
|
|
|
</FormGroup>
|
|
|
|
{{/if}}
|
2018-10-05 19:57:00 +00:00
|
|
|
|
|
|
|
{ error != null && <Alert bsStyle="danger">{error}</Alert> }
|
|
|
|
|
2018-05-10 21:02:47 +00:00
|
|
|
{{#ifview stateMutability}}
|
2018-05-11 18:16:27 +00:00
|
|
|
<Button type="submit" bsStyle="primary" onClick={(e) => this.handleClick(e)}>Call</Button>
|
2018-05-10 21:02:47 +00:00
|
|
|
{
|
2018-10-05 19:57:00 +00:00
|
|
|
output &&
|
|
|
|
<Fragment>
|
2018-05-10 21:02:47 +00:00
|
|
|
<h4>Results</h4>
|
2018-05-11 13:26:27 +00:00
|
|
|
{{#iflengthgt outputs 1}}
|
2018-05-10 21:02:47 +00:00
|
|
|
<ul>
|
|
|
|
{{#each outputs}}
|
2018-10-05 19:57:00 +00:00
|
|
|
<li>{{emptyname name @index}}: { output.{{emptyname name @index}} }</li>
|
2018-05-10 21:02:47 +00:00
|
|
|
{{/each}}
|
|
|
|
</ul>
|
|
|
|
{{else}}
|
2018-10-05 19:57:00 +00:00
|
|
|
{output.toString()}
|
2018-05-11 13:26:27 +00:00
|
|
|
{{/iflengthgt}}
|
2018-10-05 19:57:00 +00:00
|
|
|
</Fragment>
|
2018-05-10 21:02:47 +00:00
|
|
|
}
|
2018-05-11 16:34:16 +00:00
|
|
|
{{else}}
|
2018-05-11 18:16:27 +00:00
|
|
|
<Button type="submit" bsStyle="primary" onClick={(e) => this.handleClick(e)}>Send</Button>
|
2018-05-11 16:34:16 +00:00
|
|
|
{
|
2018-10-05 19:57:00 +00:00
|
|
|
receipt &&
|
|
|
|
<Fragment>
|
|
|
|
<Alert bsStyle={receipt.status == "0x1" ? 'success' : 'danger'}>{receipt.status == "0x1" ? 'Success' : 'Failure / Revert'} - Transaction Hash: {receipt.transactionHash}</Alert>
|
|
|
|
</Fragment>
|
|
|
|
|
2018-05-11 16:34:16 +00:00
|
|
|
}
|
2018-05-10 21:02:47 +00:00
|
|
|
{{/ifview}}
|
|
|
|
</form>
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{{/each}}
|
2018-05-15 13:53:05 +00:00
|
|
|
|
2018-10-05 19:57:00 +00:00
|
|
|
class {{contractName}}UI extends Component {
|
2018-05-15 13:53:05 +00:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
render(){
|
|
|
|
return (<div>
|
2018-10-05 19:57:00 +00:00
|
|
|
<h1>{{title}}</h1>
|
2018-05-15 13:53:05 +00:00
|
|
|
{{#each functions}}
|
|
|
|
<{{capitalize name}}Form{{@index}} />
|
|
|
|
{{/each}}
|
|
|
|
</div>);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-05 19:57:00 +00:00
|
|
|
ReactDOM.render(<{{contractName}}UI />, document.getElementById('app'));
|