From d9ae5cbfc7f69e17229360059017460460ec0d91 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Tue, 21 Aug 2018 11:38:05 +0100 Subject: [PATCH] PR feedback: Use request --- lib/modules/contracts_manager/index.js | 45 ++++++++++++++------------ 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/lib/modules/contracts_manager/index.js b/lib/modules/contracts_manager/index.js index b19a9897..8d40b1c4 100644 --- a/lib/modules/contracts_manager/index.js +++ b/lib/modules/contracts_manager/index.js @@ -108,20 +108,22 @@ class ContractsManager { 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'; - 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}); - }); - } catch (e) { - res.send({result: e.message}); - } + self.events.request("blockchain:contract:create", {abi: contract.abiDefinition, address: contract.deployedAddress}, (contractObj) => { + 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}); + }); + } catch (e) { + res.send({result: e.message}); + } + }); }); } ); @@ -137,19 +139,22 @@ class ContractsManager { account: (callback) => { self.events.request("blockchain:defaultAccount:get", (account) => callback(null, account)); } - }, async (error, result) => { + }, (error, result) => { if (error) { return res.send({error: error.message}); } const {account, contract} = result; - const contractObj = new web3.eth.Contract(contract.abiDefinition); - try { - let gas = await contractObj.deploy({data: `0x${contract.code}`, arguments: req.body.inputs}).estimateGas(); - let newContract = await contractObj.deploy({data: `0x${contract.code}`, arguments: req.body.inputs}).send({from: account, gas}); - res.send({result: newContract._address}); - } catch (e) { - res.send({result: e.message}); - } + + self.events.request("blockchain:contract:create", {abi: contract.abiDefinition}, async (contractObj) => { + try { + const params = {data: `0x${contract.code}`, arguments: req.body.inputs}; + let gas = await contractObj.deploy(params).estimateGas(); + let newContract = await contractObj.deploy(params).send({from: account, gas}); + res.send({result: newContract._address}); + } catch (e) { + res.send({result: e.message}); + } + }); }); } );