enable setting balance in mutliple formats

This commit is contained in:
Jonathan Rainville 2018-06-06 16:43:08 -04:00
parent 30a8263569
commit 4ffb5c401f
2 changed files with 63 additions and 4 deletions

View File

@ -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();

View File

@ -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);
});
});