From 30a82635690db968dcac8e10339c526104b39c9f Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 6 Jun 2018 15:33:48 -0400 Subject: [PATCH] enable using accounts and balances --- lib/tests/test.js | 39 +++++++++++++++---- .../test_app/test/another_storage_spec.js | 7 ++++ 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/lib/tests/test.js b/lib/tests/test.js index 2e81f943..f98c6a32 100644 --- a/lib/tests/test.js +++ b/lib/tests/test.js @@ -6,6 +6,7 @@ const utils = require('../utils/utils'); const constants = require('../constants'); const Events = require('../core/events'); const cloneDeep = require('clone-deep'); +const AccountParser = require('../contracts/accountParser'); function getSimulator() { try { @@ -36,12 +37,7 @@ class Test { this.compiledContracts = {}; this.web3 = new Web3(); - if (this.simOptions.node) { - this.web3.setProvider(new this.web3.providers.HttpProvider(this.simOptions.node)); - } else { - this.sim = getSimulator(); - this.web3.setProvider(this.sim.provider(this.simOptions)); - } + this.initWeb3Provider(); this.engine = new Engine({ env: this.options.env || 'test', @@ -55,11 +51,32 @@ class Test { }); this.versions_default = this.engine.config.contractsConfig.versions; + const deploymentConfig = this.engine.config.contractsConfig.versions; // Reset contract config to nothing to make sure we deploy only what we want - this.engine.config.contractsConfig = {contracts: {}, versions: this.versions_default}; + this.engine.config.contractsConfig = {contracts: {}, versions: this.versions_default, deployment: deploymentConfig}; this.engine.startService("libraryManager"); this.engine.startService("codeRunner"); + this.initDeployServices(); + this.engine.startService("codeGenerator"); + } + + initWeb3Provider() { + if (this.simOptions.node) { + this.web3.setProvider(new this.web3.providers.HttpProvider(this.simOptions.node)); + } else { + if (this.simOptions.accounts) { + this.simOptions.accounts = this.simOptions.accounts.map((account, index) => { + return {balance: this.options.deployment.accounts[index].balance || 0xFFFFFFFFFFFFFFFFFF, + secretKey: account.privateKey}; + }); + } + this.sim = getSimulator(); + this.web3.setProvider(this.sim.provider(this.simOptions)); + } + } + + initDeployServices() { this.engine.startService("web3", { web3: this.web3 }); @@ -67,7 +84,6 @@ class Test { trackContracts: false, ipcRole: 'client' }); - this.engine.startService("codeGenerator"); } init(callback) { @@ -100,6 +116,13 @@ class Test { this.simOptions = this.options.simulatorOptions || {}; this.ready = false; + if (this.options.deployment && this.options.deployment.accounts) { + // Account setup + this.simOptions.accounts = AccountParser.parseAccountsConfig(this.options.deployment.accounts, this.web3); + this.initWeb3Provider(); + this.initDeployServices(); + } + // Reset contracts this.engine.contractsManager.contracts = cloneDeep(this.builtContracts); this.engine.contractsManager.compiledContracts = cloneDeep(this.compiledContracts); diff --git a/test_apps/test_app/test/another_storage_spec.js b/test_apps/test_app/test/another_storage_spec.js index 82ee75fb..cce1aaa6 100644 --- a/test_apps/test_app/test/another_storage_spec.js +++ b/test_apps/test_app/test/another_storage_spec.js @@ -4,6 +4,13 @@ const AnotherStorage = embark.require('Embark/contracts/AnotherStorage'); const SimpleStorage = embark.require('Embark/contracts/SimpleStorage'); config({ + deployment: { + "accounts": [ + { + "mnemonic": "example exile argue silk regular smile grass bomb merge arm assist farm" + } + ] + }, contracts: { "SimpleStorage": { args: [100]