From 4ffb5c401fae64e24927fa2838a576fc20e9caf4 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 6 Jun 2018 16:43:08 -0400 Subject: [PATCH] enable setting balance in mutliple formats --- lib/tests/test.js | 52 ++++++++++++++++++- .../test_app/test/another_storage_spec.js | 15 +++++- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/lib/tests/test.js b/lib/tests/test.js index f98c6a32..9dbc400e 100644 --- a/lib/tests/test.js +++ b/lib/tests/test.js @@ -61,14 +61,62 @@ class Test { this.engine.startService("codeGenerator"); } + // eslint-disable-next-line complexity + getMutliplicator(keyword) { + switch (keyword.toLowerCase()) { + case 'wei': return 1; + case 'ether': + case 'eth': return 1000000000000000000; + case 'finney': + case 'milli': + case 'milliether': return 1000000000000000; + case 'szabo': + case 'microether,': + case 'micro': return 1000000000000; + case 'gwei': + case 'nanoether': + case 'shannon': + case 'nano': return 1000000000; + case 'mwei': + case 'babbage': + case 'picoether': return 1000000; + case 'kwei': + case 'ada': + case 'femtoether': return 1000; + case 'kether': + case 'grand': + case 'einstein': return 1000000000000000000000; + case 'mether': return 1000000000000000000000000; + case 'gether': return 1000000000000000000000000000; + case 'tether': return 1000000000000000000000000000000; + default: console.warn('\n' + __(`Unrecognised keyword ${keyword} in balance. Will assume Wei`).yellow); + return 1; + } + } + + getBalance(balanceString) { + if (!balanceString) { + return 0xFFFFFFFFFFFFFFFFFF; + } + if (this.web3.utils.isHexStrict(balanceString)) { + return balanceString; + } + const match = balanceString.match(/([0-9]+) ?([a-zA-Z]*)/); + if (!match[2]) { + return this.web3.utils.toHex(parseInt(match[1], 10)); + } + + return this.web3.utils.toHex(parseInt(match[1], 10) * this.getMutliplicator(match[2])); + } + 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}; + const balance = this.getBalance(this.options.deployment.accounts[index].balance); + return {balance, secretKey: account.privateKey}; }); } this.sim = getSimulator(); diff --git a/test_apps/test_app/test/another_storage_spec.js b/test_apps/test_app/test/another_storage_spec.js index cce1aaa6..10ee0b35 100644 --- a/test_apps/test_app/test/another_storage_spec.js +++ b/test_apps/test_app/test/another_storage_spec.js @@ -1,13 +1,16 @@ -/*global contract, config, it, embark*/ +/*global contract, config, it, embark, web3*/ const assert = require('assert'); const AnotherStorage = embark.require('Embark/contracts/AnotherStorage'); const SimpleStorage = embark.require('Embark/contracts/SimpleStorage'); +let accounts; + config({ deployment: { "accounts": [ { - "mnemonic": "example exile argue silk regular smile grass bomb merge arm assist farm" + "mnemonic": "example exile argue silk regular smile grass bomb merge arm assist farm", + balance: "5ether" } ] }, @@ -19,6 +22,8 @@ config({ args: ["$SimpleStorage"] } } +}, (err, theAccounts) => { + accounts = theAccounts; }); contract("AnotherStorage", function() { @@ -28,4 +33,10 @@ contract("AnotherStorage", function() { let result = await AnotherStorage.methods.simpleStorageAddress().call(); assert.equal(result.toString(), SimpleStorage.options.address); }); + + it('should set the balance correctly', async function () { + const balance = await web3.eth.getBalance(accounts[0]); + assert.ok(balance < 5000000000000000000); + assert.ok(balance > 4000000000000000000); + }); });