From 3edf7c24192cfd2eaafb4d0d7c6655d5e80c1967 Mon Sep 17 00:00:00 2001 From: hodlbank Date: Fri, 3 Aug 2018 23:08:02 +0000 Subject: [PATCH 1/7] Applying simulatorAccounts configuration for "embark simulator". --- lib/cmds/simulator.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/cmds/simulator.js b/lib/cmds/simulator.js index 8254fd3db..5cc5a6f90 100644 --- a/lib/cmds/simulator.js +++ b/lib/cmds/simulator.js @@ -32,6 +32,25 @@ class Simulator { cmds.push("--mnemonic \"" + (simulatorMnemonic) +"\""); } + // the simulatorAccounts configuration overrides a mnemonic + let simulatorAccounts = this.blockchainConfig.simulatorAccounts || options.simulatorAccounts; + if (simulatorAccounts && simulatorAccounts.length > 0) { + simulatorAccounts.forEach((account, index) => { + let hexBalance = '0x8AC7230489E80000'; // 10 ether; + if (account.hexBalance) { // TODO: ensure it is 0x-prefixed hex + if (account.hexBalance.match(/0x[a-f0-9]+/i)) { + hexBalance = account.hexBalance; + } else { + this.logger.warn('Balance provided for the account #' + index + ' is not a valid hex number. Using 10 ether as a default.'); + } + } else { + this.logger.warn('No balance has been provided for the account #' + index + '. Using 10 ether as a default.'); + } + let cmd = '--account="' + account.privateKey + ','+hexBalance + '"'; + cmds.push(cmd); + }); + } + // adding blocktime only if it is defined in the blockchainConfig or options let simulatorBlocktime = this.blockchainConfig.simulatorBlocktime || options.simulatorBlocktime; if (simulatorBlocktime) { From 960202dee9b659c4f2863139cfc1fe102e0c9eb9 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Fri, 3 Aug 2018 08:50:42 -0400 Subject: [PATCH 2/7] disable ens if it is not enabled --- lib/modules/ens/index.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/modules/ens/index.js b/lib/modules/ens/index.js index c51a86ac1..3ac8443b8 100644 --- a/lib/modules/ens/index.js +++ b/lib/modules/ens/index.js @@ -14,6 +14,13 @@ class ENS { this.registration = this.namesConfig.register || {}; this.embark = embark; + if (this.namesConfig === {} || + this.namesConfig.enabled !== true || + this.namesConfig.available_providers.indexOf('ens') < 0 || + this.namesConfig.provider !== 'ens') { + return; + } + this.addENSToEmbarkJS(); this.configureContracts(); this.registerEvents(); @@ -179,14 +186,6 @@ class ENS { addENSToEmbarkJS() { const self = this; - // TODO: make this a shouldAdd condition - if (this.namesConfig === {}) { - return; - } - - if ((this.namesConfig.available_providers.indexOf('ens') < 0) && (this.namesConfig.provider !== 'ens' || this.namesConfig.enabled !== true)) { - return; - } // get namehash, import it into file self.events.request("version:get:eth-ens-namehash", function (EnsNamehashVersion) { From 69bd34392b1b6a747222ccc573e1ae4a0a17512e Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Fri, 3 Aug 2018 15:15:01 -0400 Subject: [PATCH 3/7] remove provider!==ens --- lib/modules/ens/index.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/modules/ens/index.js b/lib/modules/ens/index.js index 3ac8443b8..2f94092e7 100644 --- a/lib/modules/ens/index.js +++ b/lib/modules/ens/index.js @@ -16,9 +16,10 @@ class ENS { if (this.namesConfig === {} || this.namesConfig.enabled !== true || - this.namesConfig.available_providers.indexOf('ens') < 0 || - this.namesConfig.provider !== 'ens') { + this.namesConfig.available_providers.indexOf('ens') < 0) { return; + } else if (this.namesConfig.provider !== 'ens') { + this.providerNotENS = true; } this.addENSToEmbarkJS(); @@ -186,6 +187,9 @@ class ENS { addENSToEmbarkJS() { const self = this; + if (self.providerNotENS) { + return; + } // get namehash, import it into file self.events.request("version:get:eth-ens-namehash", function (EnsNamehashVersion) { From 513154e42721a2f76783a48472e7f8bf368ae68a Mon Sep 17 00:00:00 2001 From: hodlbank Date: Fri, 3 Aug 2018 19:52:34 +0000 Subject: [PATCH 4/7] Enabling contract:deploy:beforeDeploy plugins to access deploymentAccount value by adding it into the contract object. --- lib/modules/deployment/contract_deployer.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/modules/deployment/contract_deployer.js b/lib/modules/deployment/contract_deployer.js index 9a21c5323..a45238adf 100644 --- a/lib/modules/deployment/contract_deployer.js +++ b/lib/modules/deployment/contract_deployer.js @@ -143,7 +143,6 @@ class ContractDeployer { let self = this; let accounts = []; let contractParams = (contract.realArgs || contract.args).slice(); - let contractCode = contract.code; let deploymentAccount = self.blockchain.defaultAccount(); let deployObject; @@ -171,10 +170,12 @@ class ContractDeployer { } deploymentAccount = deploymentAccount || accounts[0]; + contract.deploymentAccount = deploymentAccount; next(); }); }, function doLinking(next) { + let contractCode = contract.code; self.events.request('contracts:list', (_err, contracts) => { for (let contractObj of contracts) { let filename = contractObj.filename; @@ -196,7 +197,7 @@ class ContractDeployer { } contractCode = contractCode.replace(new RegExp(toReplace, "g"), deployedAddress); } - // saving code changes back to contract object + // saving code changes back to the contract object contract.code = contractCode; next(); }); @@ -205,6 +206,7 @@ class ContractDeployer { self.plugins.emitAndRunActionsForEvent('deploy:contract:beforeDeploy', {contract: contract}, next); }, function createDeployObject(next) { + let contractCode = contract.code; let contractObject = self.blockchain.ContractObject({abi: contract.abiDefinition}); try { @@ -236,7 +238,7 @@ class ContractDeployer { self.logger.info(__("deploying") + " " + contract.className.bold.cyan + " " + __("with").green + " " + contract.gas + " " + __("gas at the price of").green + " " + contract.gasPrice + " " + __("Wei, estimated cost:").green + " " + estimatedCost + " Wei".green); self.blockchain.deployContractFromObject(deployObject, { - from: deploymentAccount, + from: contract.deploymentAccount, gas: contract.gas, gasPrice: contract.gasPrice }, function(error, receipt) { From c60a2a6fed34f56b8f4da08ba4081cbe19c3835b Mon Sep 17 00:00:00 2001 From: hodlbank Date: Fri, 3 Aug 2018 19:56:19 +0000 Subject: [PATCH 5/7] Allow user to specify "random" as privateKey configuration. In such case embark generates some random privateKey for the account. --- lib/utils/accountParser.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/utils/accountParser.js b/lib/utils/accountParser.js index f407cf54b..1399108cd 100644 --- a/lib/utils/accountParser.js +++ b/lib/utils/accountParser.js @@ -47,6 +47,12 @@ class AccountParser { if (accountConfig.balance) { hexBalance = AccountParser.getHexBalance(accountConfig.balance, web3); } + + if (accountConfig.privateKey === 'random') { + let randomAccount = web3.eth.accounts.create(); + accountConfig.privateKey = randomAccount.privateKey; + } + if (accountConfig.privateKey) { if (!accountConfig.privateKey.startsWith('0x')) { accountConfig.privateKey = '0x' + accountConfig.privateKey; From ab0a3b073544675c619ed0b5aa0456923cc1aee9 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Mon, 6 Aug 2018 08:47:27 -0400 Subject: [PATCH 6/7] only set provider if provider is ENS --- lib/modules/ens/index.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/modules/ens/index.js b/lib/modules/ens/index.js index 2f94092e7..472ea3168 100644 --- a/lib/modules/ens/index.js +++ b/lib/modules/ens/index.js @@ -18,9 +18,8 @@ class ENS { this.namesConfig.enabled !== true || this.namesConfig.available_providers.indexOf('ens') < 0) { return; - } else if (this.namesConfig.provider !== 'ens') { - this.providerNotENS = true; } + this.doSetENSProvider = this.namesConfig.provider === 'ens'; this.addENSToEmbarkJS(); this.configureContracts(); @@ -63,7 +62,10 @@ class ENS { resolverAbi: results[2].abiDefinition, resolverAddress: results[2].deployedAddress }; - self.addSetProvider(config); + + if (self.doSetENSProvider) { + self.addSetProvider(config); + } if (!self.env === 'development' || !self.registration || !self.registration.subdomains || !Object.keys(self.registration.subdomains).length) { return cb(); @@ -187,9 +189,6 @@ class ENS { addENSToEmbarkJS() { const self = this; - if (self.providerNotENS) { - return; - } // get namehash, import it into file self.events.request("version:get:eth-ens-namehash", function (EnsNamehashVersion) { From 922a400b5ba78198d4f0f9772b2c1f97ef8c0b15 Mon Sep 17 00:00:00 2001 From: hodlbank Date: Mon, 6 Aug 2018 20:28:16 +0000 Subject: [PATCH 7/7] [m] Parsing simulatorAccounts using AccountParser . --- lib/cmds/simulator.js | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/lib/cmds/simulator.js b/lib/cmds/simulator.js index 5cc5a6f90..a1a4d3dd3 100644 --- a/lib/cmds/simulator.js +++ b/lib/cmds/simulator.js @@ -32,21 +32,14 @@ class Simulator { cmds.push("--mnemonic \"" + (simulatorMnemonic) +"\""); } - // the simulatorAccounts configuration overrides a mnemonic + // as ganache-cli documentation explains, the simulatorAccounts configuration overrides a mnemonic let simulatorAccounts = this.blockchainConfig.simulatorAccounts || options.simulatorAccounts; if (simulatorAccounts && simulatorAccounts.length > 0) { - simulatorAccounts.forEach((account, index) => { - let hexBalance = '0x8AC7230489E80000'; // 10 ether; - if (account.hexBalance) { // TODO: ensure it is 0x-prefixed hex - if (account.hexBalance.match(/0x[a-f0-9]+/i)) { - hexBalance = account.hexBalance; - } else { - this.logger.warn('Balance provided for the account #' + index + ' is not a valid hex number. Using 10 ether as a default.'); - } - } else { - this.logger.warn('No balance has been provided for the account #' + index + '. Using 10 ether as a default.'); - } - let cmd = '--account="' + account.privateKey + ','+hexBalance + '"'; + let web3 = new (require('web3'))(); + let AccountParser = require('../utils/accountParser.js'); + let parsedAccounts = AccountParser.parseAccountsConfig(simulatorAccounts, web3, this.logger); + parsedAccounts.forEach((account) => { + let cmd = '--account="' + account.privateKey + ','+account.hexBalance + '"'; cmds.push(cmd); }); }