diff --git a/lib/modules/scaffolding-react/templates/dapp.js.tpl b/lib/modules/scaffolding-react/templates/dapp.js.tpl index c0827fc7..c02bba74 100644 --- a/lib/modules/scaffolding-react/templates/dapp.js.tpl +++ b/lib/modules/scaffolding-react/templates/dapp.js.tpl @@ -1,13 +1,13 @@ import EmbarkJS from 'Embark/EmbarkJS'; import {{contractName}} from 'Embark/contracts/{{contractName}}'; -import React from 'react'; +import React, { Component, Fragment } from 'react'; import ReactDOM from 'react-dom'; import { FormGroup, ControlLabel, FormControl, Checkbox, Button, Alert, InputGroup } from 'react-bootstrap'; {{#each functions}} -class {{capitalize name}}Form{{@index}} extends React.Component { +class {{capitalize name}}Form{{@index}} extends Component { constructor(props){ super(props); this.state = { @@ -45,45 +45,42 @@ class {{capitalize name}}Form{{@index}} extends React.Component { try { {{#ifview stateMutability}} - {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}this.state.input.{{name}}{{#unless @last}}, {{/unless}}{{/each}}) - .call() - .then((result) => { + const result = await {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}this.state.input.{{name}}{{#unless @last}}, {{/unless}}{{/each}}).call() {{#iflengthgt outputs 1}} - this.setState({output: { + this.setState({output: { {{#each outputs}} - {{emptyname name @index}}: result[{{@index}}]{{#unless @last}},{{/unless}} + {{emptyname name @index}}: result[{{@index}}]{{#unless @last}},{{/unless}} {{/each}} - }}); + }}); {{else}} - this.setState({output: result}); - {{/iflengthgt}} - }) - .catch((err) => { - this.setState({error: err.message}); - }); + this.setState({output: result}); + {{/iflengthgt}} {{else}} - {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}this.state.input.{{name}}{{#unless @last}}, {{/unless}}{{/each}}) - .send({ - {{#if payable}} - value: this.state.value, - {{/if}} - from: web3.eth.defaultAccount - }) - .then((_receipt) => { - console.log(_receipt); - this.setState({receipt: _receipt}) - }) - .catch((err) => { - console.log(err); - this.setState({error: err.message}); - }); + 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}); {{/ifview}} } catch(err) { + console.error(err); this.setState({error: err.message}); } } render(){ + const {input, value, error, output, receipt} = this.state; + return