From 709384456600f0750247fe393dc1f285fda3212a Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 6 Aug 2019 17:14:19 -0400 Subject: [PATCH] re-add gas estimates; get accounts through await instead of a callback --- .../ethereum-blockchain-client/index.js | 48 +++++++++++-------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/packages/embark/src/lib/modules/ethereum-blockchain-client/index.js b/packages/embark/src/lib/modules/ethereum-blockchain-client/index.js index eeb273353..dc499c95a 100644 --- a/packages/embark/src/lib/modules/ethereum-blockchain-client/index.js +++ b/packages/embark/src/lib/modules/ethereum-blockchain-client/index.js @@ -23,27 +23,35 @@ class EthereumBlockchainClient { let provider = await this.events.request2("blockchain:client:provider", "ethereum"); var web3 = new Web3(provider) // var web3 = new Web3("ws://localhost:8556") - web3.eth.getAccounts().then((accounts) => { - let account = accounts[0]; - // let contractObject = this.blockchain.ContractObject({abi: contract.abiDefinition}); - console.dir("== ethereum contract deployer") - let contractObj = new web3.eth.Contract(contract.abiDefinition, contract.address); - // let deployObject = this.blockchain.deployContractObject(contractObject, {arguments: contractParams, data: dataCode}); - let contractObject = contractObj.deploy({ arguments: (contract.args || []), data: ("0x" + contract.code) }); - // this.blockchain.deployContractFromObject(deployObject, - console.dir({ arguments: contract.args, data: ("0x" + contract.code) }); - console.dir("------- send") + // web3.eth.getAccounts().then((accounts) => { + let accounts = await web3.eth.getAccounts(); + let account = accounts[0]; + // let contractObject = this.blockchain.ContractObject({abi: contract.abiDefinition}); + console.dir("== ethereum contract deployer") + let contractObj = new web3.eth.Contract(contract.abiDefinition, contract.address); + // let deployObject = this.blockchain.deployContractObject(contractObject, {arguments: contractParams, data: dataCode}); + let contractObject = contractObj.deploy({ arguments: (contract.args || []), data: ("0x" + contract.code) }); - embarkJsUtils.secureSend(web3, contractObject, { - from: account, gas: 800000 - }, true, (err, receipt) => { - contract.deployedAddress = receipt.contractAddress; - contract.transactionHash = receipt.transactionHash; - done(); - }, (hash) => { - console.dir('hash is ' + hash); - }); - }) + if (contract.gas === 'auto' || !contract.gas) { + let gasValue = await contractObject.estimateGas(); + let increase_per = 1 + (Math.random() / 10.0); + contract.gas = Math.floor(gasValue * increase_per); + } + + // this.blockchain.deployContractFromObject(deployObject, + console.dir({ arguments: contract.args, data: ("0x" + contract.code) }); + console.dir("------- send") + + embarkJsUtils.secureSend(web3, contractObject, { + from: account, gas: 800000 + }, true, (err, receipt) => { + contract.deployedAddress = receipt.contractAddress; + contract.transactionHash = receipt.transactionHash; + done(); + }, (hash) => { + console.dir('hash is ' + hash); + }); + // }) } async doLinking(params, callback) {