diff --git a/packages/embark-utils/src/index.js b/packages/embark-utils/src/index.js index c9e7e4ee4..551eadae1 100644 --- a/packages/embark-utils/src/index.js +++ b/packages/embark-utils/src/index.js @@ -128,6 +128,27 @@ function deconstructUrl(endpoint) { }; } +function getHexBalanceFromString(balanceString, web3) { + if(!web3){ + throw new Error(__('[getHexBalanceFromString]: Missing parameter \'web3\'')); + } + if (!balanceString) { + return 0xFFFFFFFFFFFFFFFFFF; + } + if (web3.utils.isHexStrict(balanceString)) { + return balanceString; + } + const match = balanceString.match(balanceRegex); + if (!match) { + throw new Error(__('Unrecognized balance string "%s"', balanceString)); + } + if (!match[2]) { + return web3.utils.toHex(match[1]); + } + + return web3.utils.toHex(web3.utils.toWei(match[1], match[2])); +} + function getWeiBalanceFromString(balanceString, web3){ if(!web3){ throw new Error(__('[getWeiBalanceFromString]: Missing parameter \'web3\'')); @@ -206,6 +227,7 @@ const Utils = { recursiveMerge, prepareContractsConfig, getWeiBalanceFromString, + getHexBalanceFromString, sha512, timer, unitRegex, diff --git a/packages/embark/src/lib/utils/accountParser.js b/packages/embark/src/lib/utils/accountParser.js index 841825588..e8f3724de 100644 --- a/packages/embark/src/lib/utils/accountParser.js +++ b/packages/embark/src/lib/utils/accountParser.js @@ -2,7 +2,7 @@ const bip39 = require("bip39"); const hdkey = require('ethereumjs-wallet/hdkey'); const ethereumjsWallet = require('ethereumjs-wallet'); const fs = require('../core/fs'); -const {getHexBalanceFromString} = require('../utils/utils'); +import {getHexBalanceFromString} from 'embark-utils'; const path = require('path'); diff --git a/packages/embark/src/lib/utils/utils.js b/packages/embark/src/lib/utils/utils.js index f7eb0de16..a04fdf6ee 100644 --- a/packages/embark/src/lib/utils/utils.js +++ b/packages/embark/src/lib/utils/utils.js @@ -2,8 +2,6 @@ let http = require('follow-redirects').http; let https = require('follow-redirects').https; import {canonicalHost, normalizeInput} from 'embark-utils'; -const balanceRegex = /([0-9]+) ?([a-zA-Z]*)/; - function dirname() { const path = require('path'); return path.dirname.apply(path.dirname, arguments); @@ -342,27 +340,6 @@ function buildUrlFromConfig(configObj) { return this.buildUrl(configObj.protocol, canonicalHost(configObj.host), configObj.port, configObj.type); } -function getHexBalanceFromString(balanceString, web3) { - if(!web3){ - throw new Error(__('[utils.getHexBalanceFromString]: Missing parameter \'web3\'')); - } - if (!balanceString) { - return 0xFFFFFFFFFFFFFFFFFF; - } - if (web3.utils.isHexStrict(balanceString)) { - return balanceString; - } - const match = balanceString.match(balanceRegex); - if (!match) { - throw new Error(__('Unrecognized balance string "%s"', balanceString)); - } - if (!match[2]) { - return web3.utils.toHex(match[1]); - } - - return web3.utils.toHex(web3.utils.toWei(match[1], match[2])); -} - function compact(array) { return array.filter(n => n); } @@ -497,7 +474,6 @@ module.exports = { normalizeInput, buildUrl, buildUrlFromConfig, - getHexBalanceFromString, compact, groupBy, interceptLogs, diff --git a/packages/embark/src/test/accountParser.js b/packages/embark/src/test/accountParser.js index 54bca8863..ed36ca5f8 100644 --- a/packages/embark/src/test/accountParser.js +++ b/packages/embark/src/test/accountParser.js @@ -1,12 +1,11 @@ /*global __dirname, describe, it, before, after, require*/ const assert = require('assert'); const sinon = require('sinon'); -const utils = require('../lib/utils/utils'); const AccountParser = require('../lib/utils/accountParser'); let TestLogger = require('../lib/utils/test_logger'); const Web3 = require('web3'); const i18n = require('../lib/core/i18n/i18n'); -import { getWeiBalanceFromString } from 'embark-utils'; +import { getWeiBalanceFromString, getHexBalanceFromString } from 'embark-utils'; i18n.setOrDetectLocale('en'); describe('embark.AccountParser', function () { @@ -112,44 +111,44 @@ describe('embark.AccountParser', function () { describe('getHexBalance', () => { it('should return default if no balance', () => { - const hexBalance = utils.getHexBalanceFromString(null, Web3); + const hexBalance = getHexBalanceFromString(null, Web3); assert.strictEqual(hexBalance, 0xFFFFFFFFFFFFFFFFFF); }); it('should return the balance string if already hexadecimal', () => { - const hexBalance = utils.getHexBalanceFromString('0xFFF', Web3); + const hexBalance = getHexBalanceFromString('0xFFF', Web3); assert.strictEqual(hexBalance, '0xFFF'); }); it('should convert to hex when decimal', () => { - const hexBalance = utils.getHexBalanceFromString('500', Web3); + const hexBalance = getHexBalanceFromString('500', Web3); assert.strictEqual(hexBalance, '0x1f4'); }); it('should convert to hex with eth string', () => { - const hexBalance = utils.getHexBalanceFromString('4ether', Web3); + const hexBalance = getHexBalanceFromString('4ether', Web3); assert.strictEqual(hexBalance, '0x3782dace9d900000'); }); it('should convert to hex with eth string with space', () => { - const hexBalance = utils.getHexBalanceFromString('673 shannon', Web3); + const hexBalance = getHexBalanceFromString('673 shannon', Web3); assert.strictEqual(hexBalance, '0x9cb1ed0a00'); }); it('should convert to hex with large ether values', () => { - const hexBalance = utils.getHexBalanceFromString('100000 ether', Web3); + const hexBalance = getHexBalanceFromString('100000 ether', Web3); assert.strictEqual(hexBalance, '0x152d02c7e14af6800000'); }); it('should fail when string is not good', () => { try { - utils.getHexBalanceFromString('nogood', Web3); + getHexBalanceFromString('nogood', Web3); assert.fail('Should have failed at getHexBalance'); } catch (e) { // Ok