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

View File

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

View File

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

View File

@ -1,3 +1,4 @@
/*global web3*/
let toposort = require('toposort');
let async = require('async');
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(
'get',
'/embark-api/contracts',
@ -215,7 +246,10 @@ class ContractsManager {
}
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]);
if (suggestion) {
self.logger.warn(__('did you mean "%s"?', suggestion));
@ -228,7 +262,10 @@ class ContractsManager {
}
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;
@ -274,8 +311,8 @@ class ContractsManager {
// look in code for dependencies
let libMatches = (contract.code.match(/:(.*?)(?=_)/g) || []);
for (let match of libMatches) {
self.contractDependencies[className] = self.contractDependencies[className] || [];
self.contractDependencies[className].push(match.substr(1));
self.contractDependencies[className] = self.contractDependencies[className] || [];
self.contractDependencies[className].push(match.substr(1));
}
// look in arguments for dependencies
@ -375,7 +412,7 @@ class ContractsManager {
self.events.emit("status", __("Compile/Build error"));
self.events.emit("outputError", __("Error building Dapp, please check console"));
self.logger.error(__("Error Compiling/Building contracts: ") + err);
}else{
} else {
self.compileError = false;
}
self.logger.trace("finished".underline);
@ -415,8 +452,8 @@ class ContractsManager {
let orderedDependencies;
try {
orderedDependencies = toposort(converted_dependencies.filter((x) => x[0] !== x[1])).reverse();
} catch(e) {
orderedDependencies = toposort(converted_dependencies.filter((x) => x[0] !== x[1])).reverse();
} catch (e) {
this.logger.error((__("Error: ") + e.message).red);
this.logger.error(__("there are two or more contracts that depend on each other in a cyclic manner").bold.red);
this.logger.error(__("Embark couldn't determine which one to deploy first").red);