Call Function API

This commit is contained in:
Anthony Laibe 2018-08-16 11:35:24 +01:00 committed by Pascal Precht
parent cdc7c107a6
commit c234a850e3
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
4 changed files with 52 additions and 10 deletions

View File

@ -18,7 +18,7 @@ const ContractFunction = ({method}) => (
{method.inputs.length > 0 && {method.inputs.length > 0 &&
<Card.Body> <Card.Body>
{method.inputs.map(input => ( {method.inputs.map(input => (
<Form.Group label={input.name}> <Form.Group key={input.name} label={input.name}>
<Form.Input placeholder={input.type}/> <Form.Input placeholder={input.type}/>
</Form.Group> </Form.Group>
))} ))}
@ -32,11 +32,15 @@ const ContractFunction = ({method}) => (
</Grid.Row> </Grid.Row>
); );
ContractFunction.propTypes = {
method: PropTypes.object
};
const ContractFunctions = ({contractProfile}) => ( const ContractFunctions = ({contractProfile}) => (
<Page.Content title={contractProfile.name + ' Functions'}> <Page.Content title={contractProfile.name + ' Functions'}>
{contractProfile.methods {contractProfile.methods
.filter(method => method.name !== 'constructor') .filter(method => method.name !== 'constructor')
.map(method => <ContractFunction method={method} />)} .map(method => <ContractFunction key={method.name} method={method} />)}
</Page.Content> </Page.Content>
); );

View File

@ -26,6 +26,7 @@ function mapStateToProps(state, props) {
} }
ContractContainer.propTypes = { ContractContainer.propTypes = {
match: PropTypes.object,
contract: PropTypes.object, contract: PropTypes.object,
error: PropTypes.string error: PropTypes.string
}; };

View File

@ -27,7 +27,7 @@ function mapStateToProps(state) {
ContractsContainer.propTypes = { ContractsContainer.propTypes = {
contracts: PropTypes.array, contracts: PropTypes.array,
fetchContracts: PropTypes.func, fetchContracts: PropTypes.func
}; };
export default connect( export default connect(

View File

@ -1,3 +1,4 @@
/*global web3*/
let toposort = require('toposort'); let toposort = require('toposort');
let async = require('async'); let async = require('async');
const cloneDeep = require('clone-deep'); const cloneDeep = require('clone-deep');
@ -91,6 +92,36 @@ class ContractsManager {
} }
); );
plugin.registerAPICall(
'get',
'/embark-api/contract/:contractName/function',
(req, res) => {
async.parallel({
contract: (callback) => {
self.events.request('contracts:contract', req.params.contractName, (contract) => callback(null, contract));
},
account: (callback) => {
self.events.request("blockchain:defaultAccount:get", (account) => callback(null, account));
}
}, function (err, result) {
if (err) {
return res.send({error: err.message});
}
const {account, contract} = result;
const contractObj = new web3.eth.Contract(contract.abiDefinition, contract.deployedAddress);
const abi = contract.abiDefinition.find(definition => definition.name === req.query.method);
const funcCall = (abi.constant === true || abi.stateMutability === 'view' || abi.stateMutability === 'pure') ? 'call' : 'send';
contractObj.methods[req.query.method].apply(this, req.query.inputs)[funcCall]({from: account}, (err, result) => {
if (err) {
return res.send({error: err.message});
}
res.send({result: result});
});
});
}
);
plugin.registerAPICall( plugin.registerAPICall(
'get', 'get',
'/embark-api/contracts', '/embark-api/contracts',
@ -215,7 +246,10 @@ class ContractsManager {
} }
if (parentContract === undefined) { if (parentContract === undefined) {
self.logger.error(__("{{className}}: couldn't find instanceOf contract {{parentContractName}}", {className: className, parentContractName: parentContractName})); self.logger.error(__("{{className}}: couldn't find instanceOf contract {{parentContractName}}", {
className: className,
parentContractName: parentContractName
}));
let suggestion = utils.proposeAlternative(parentContractName, dictionary, [className, parentContractName]); let suggestion = utils.proposeAlternative(parentContractName, dictionary, [className, parentContractName]);
if (suggestion) { if (suggestion) {
self.logger.warn(__('did you mean "%s"?', suggestion)); self.logger.warn(__('did you mean "%s"?', suggestion));
@ -228,7 +262,10 @@ class ContractsManager {
} }
if (contract.code !== undefined) { if (contract.code !== undefined) {
self.logger.error(__("{{className}} has code associated to it but it's configured as an instanceOf {{parentContractName}}", {className: className, parentContractName: parentContractName})); self.logger.error(__("{{className}} has code associated to it but it's configured as an instanceOf {{parentContractName}}", {
className: className,
parentContractName: parentContractName
}));
} }
contract.code = parentContract.code; contract.code = parentContract.code;