diff --git a/embark-ui/src/components/ContractFunctions.js b/embark-ui/src/components/ContractFunctions.js index 8a561818..e52289f3 100644 --- a/embark-ui/src/components/ContractFunctions.js +++ b/embark-ui/src/components/ContractFunctions.js @@ -5,15 +5,31 @@ import { Grid, Form, Button, + List, Card } from "tabler-react"; class ContractFunction extends Component { constructor(props) { - super(props) + super(props); this.state = { inputs: {} }; } + buttonTitle() { + const { method } =this.props; + if (method.name === 'constructor') { + return 'Deploy'; + } + + return (method.mutability === 'view' || method.mutability === 'pure') ? 'Call' : 'Send'; + } + + inputsAsArray(){ + return this.props.method.inputs + .map(input => this.state.inputs[input.name]) + .filter(value => value); + } + handleChange(e, name) { let newInputs = this.state.inputs; newInputs[name] = e.target.value; @@ -22,8 +38,11 @@ class ContractFunction extends Component { handleCall(e) { e.preventDefault(); - const inputs = this.props.method.inputs.map(input => this.state.inputs[input.name]); - this.props.postContractFunction(this.props.contractProfile.name, this.props.method.name, inputs); + this.props.postContractFunction(this.props.contractProfile.name, this.props.method.name, this.inputsAsArray()); + } + + callDisabled() { + return this.inputsAsArray().length !== this.props.method.inputs.length; } render() { @@ -34,17 +53,25 @@ class ContractFunction extends Component { {this.props.method.name} - {this.props.method.inputs.length > 0 && {this.props.method.inputs.map(input => ( this.handleChange(e, input.name)}/> ))} + this.handleCall(e)}> + {this.buttonTitle()} + - } - this.handleCall(e)}>Call + + {this.props.contractFunctions.map(contractFunction => ( + + {contractFunction.inputs.length > 0 && Inputs: {contractFunction.inputs.join(', ')}} + Result: {contractFunction.result} + + ))} + @@ -53,7 +80,6 @@ class ContractFunction extends Component { } } - ContractFunction.propTypes = { contractProfile: PropTypes.object, method: PropTypes.object, @@ -61,22 +87,40 @@ ContractFunction.propTypes = { postContractFunction: PropTypes.func }; +const filterContractFunctions = (contractFunctions, contractName, method) => { + return contractFunctions.filter((contractFunction) => ( + contractFunction.contractName === contractName && contractFunction.method === method + )); +}; + const ContractFunctions = (props) => { const {contractProfile} = props; + return ( {contractProfile.methods - .filter(method => method.name !== 'constructor') - .map(method => )} + .filter((method) => { + return props.onlyConstructor ? method.name === 'constructor' : method.name !== 'constructor'; + }) + .map(method => )} - ) + ); }; ContractFunctions.propTypes = { + onlyConstructor: PropTypes.bool, contractProfile: PropTypes.object, contractFunctions: PropTypes.arrayOf(PropTypes.object), postContractFunction: PropTypes.func }; +ContractFunctions.defaultProps = { + onlyConstructor: false +}; + export default ContractFunctions; diff --git a/embark-ui/src/containers/ContractFunctionsContainer.js b/embark-ui/src/containers/ContractFunctionsContainer.js index c9f951b6..a5296193 100644 --- a/embark-ui/src/containers/ContractFunctionsContainer.js +++ b/embark-ui/src/containers/ContractFunctionsContainer.js @@ -20,7 +20,8 @@ class ContractFunctionsContainer extends Component { render={({contractProfile, contractFunctions, postContractFunction}) => ( + constructor={true} + postContractFunction={postContractFunction}/> )} /> ); } diff --git a/lib/modules/contracts_manager/index.js b/lib/modules/contracts_manager/index.js index 96254773..cc3cbcae 100644 --- a/lib/modules/contracts_manager/index.js +++ b/lib/modules/contracts_manager/index.js @@ -103,21 +103,25 @@ class ContractsManager { account: (callback) => { self.events.request("blockchain:defaultAccount:get", (account) => callback(null, account)); } - }, function (err, result) { - if (err) { - return res.send({error: err.message}); + }, function (error, result) { + if (error) { + return res.send({error: error.message}); } const {account, contract} = result; const contractObj = new web3.eth.Contract(contract.abiDefinition, contract.deployedAddress); const abi = contract.abiDefinition.find(definition => definition.name === req.body.method); const funcCall = (abi.constant === true || abi.stateMutability === 'view' || abi.stateMutability === 'pure') ? 'call' : 'send'; - contractObj.methods[req.body.method].apply(this, req.body.inputs)[funcCall]({from: account}, (err, result) => { - if (err) { - return res.send({error: err.message}); - } + try { + contractObj.methods[req.body.method].apply(this, req.body.inputs)[funcCall]({from: account}, (error, result) => { + if (error) { + return res.send({result: error.message}); + } - res.send({result: result}); - }); + return res.send({result}); + }); + } catch (e) { + return res.send({result: e.message}); + } }); } ); diff --git a/templates/boilerplate/chains.json b/templates/boilerplate/chains.json index f057cbbc..0967ef42 100644 --- a/templates/boilerplate/chains.json +++ b/templates/boilerplate/chains.json @@ -1,22 +1 @@ -{ - "0xf957b38706f783689e96d5b17c2c24a2a1b2093b8e7c5f43e748878a73456984": { - "contracts": { - "0xa72e15b4fc637528e678fdcab66f178b753f609a6d2a66564f1676fde4d51ed3": { - "name": "SimpleStorage", - "address": "0x595dE951E7458Cf6e49606C91294A764098248AD" - }, - "0x05a320b7f23891fc8c8c40312e0ae5a104d4734b3c0692eb24f9e99fca0ada3f": { - "name": "ENSRegistry", - "address": "0x07dF106F12b516a36e29e23b2acF1e47b26Cb81B" - }, - "0xa971bc944c276c3a2f98bdad24d102d00615680517267a9e48cf7091ce512c88": { - "name": "Resolver", - "address": "0x8e43bD6a0357385D5a35980baCa7A2644459038b" - }, - "0x618f78549c37a154c4931bb2f10e4b92fbf8004fc1dfa6c11b3276257d3c84f0": { - "name": "FIFSRegistrar", - "address": "0xf2086ADff73E5B6412c1ca089250b77f9B28984B" - } - } - } -} +{}
Inputs: {contractFunction.inputs.join(', ')}