mirror of
https://github.com/embarklabs/embark.git
synced 2025-02-18 16:46:38 +00:00
refactor(@embark/utils): move getHexBalanceFromString to utils package
This commit is contained in:
parent
e3aed6a423
commit
5c3f39442b
@ -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){
|
function getWeiBalanceFromString(balanceString, web3){
|
||||||
if(!web3){
|
if(!web3){
|
||||||
throw new Error(__('[getWeiBalanceFromString]: Missing parameter \'web3\''));
|
throw new Error(__('[getWeiBalanceFromString]: Missing parameter \'web3\''));
|
||||||
@ -206,6 +227,7 @@ const Utils = {
|
|||||||
recursiveMerge,
|
recursiveMerge,
|
||||||
prepareContractsConfig,
|
prepareContractsConfig,
|
||||||
getWeiBalanceFromString,
|
getWeiBalanceFromString,
|
||||||
|
getHexBalanceFromString,
|
||||||
sha512,
|
sha512,
|
||||||
timer,
|
timer,
|
||||||
unitRegex,
|
unitRegex,
|
||||||
|
@ -2,7 +2,7 @@ const bip39 = require("bip39");
|
|||||||
const hdkey = require('ethereumjs-wallet/hdkey');
|
const hdkey = require('ethereumjs-wallet/hdkey');
|
||||||
const ethereumjsWallet = require('ethereumjs-wallet');
|
const ethereumjsWallet = require('ethereumjs-wallet');
|
||||||
const fs = require('../core/fs');
|
const fs = require('../core/fs');
|
||||||
const {getHexBalanceFromString} = require('../utils/utils');
|
import {getHexBalanceFromString} from 'embark-utils';
|
||||||
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
|
@ -2,8 +2,6 @@ let http = require('follow-redirects').http;
|
|||||||
let https = require('follow-redirects').https;
|
let https = require('follow-redirects').https;
|
||||||
import {canonicalHost, normalizeInput} from 'embark-utils';
|
import {canonicalHost, normalizeInput} from 'embark-utils';
|
||||||
|
|
||||||
const balanceRegex = /([0-9]+) ?([a-zA-Z]*)/;
|
|
||||||
|
|
||||||
function dirname() {
|
function dirname() {
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
return path.dirname.apply(path.dirname, arguments);
|
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);
|
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) {
|
function compact(array) {
|
||||||
return array.filter(n => n);
|
return array.filter(n => n);
|
||||||
}
|
}
|
||||||
@ -497,7 +474,6 @@ module.exports = {
|
|||||||
normalizeInput,
|
normalizeInput,
|
||||||
buildUrl,
|
buildUrl,
|
||||||
buildUrlFromConfig,
|
buildUrlFromConfig,
|
||||||
getHexBalanceFromString,
|
|
||||||
compact,
|
compact,
|
||||||
groupBy,
|
groupBy,
|
||||||
interceptLogs,
|
interceptLogs,
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
/*global __dirname, describe, it, before, after, require*/
|
/*global __dirname, describe, it, before, after, require*/
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const sinon = require('sinon');
|
const sinon = require('sinon');
|
||||||
const utils = require('../lib/utils/utils');
|
|
||||||
const AccountParser = require('../lib/utils/accountParser');
|
const AccountParser = require('../lib/utils/accountParser');
|
||||||
let TestLogger = require('../lib/utils/test_logger');
|
let TestLogger = require('../lib/utils/test_logger');
|
||||||
const Web3 = require('web3');
|
const Web3 = require('web3');
|
||||||
const i18n = require('../lib/core/i18n/i18n');
|
const i18n = require('../lib/core/i18n/i18n');
|
||||||
import { getWeiBalanceFromString } from 'embark-utils';
|
import { getWeiBalanceFromString, getHexBalanceFromString } from 'embark-utils';
|
||||||
i18n.setOrDetectLocale('en');
|
i18n.setOrDetectLocale('en');
|
||||||
|
|
||||||
describe('embark.AccountParser', function () {
|
describe('embark.AccountParser', function () {
|
||||||
@ -112,44 +111,44 @@ describe('embark.AccountParser', function () {
|
|||||||
|
|
||||||
describe('getHexBalance', () => {
|
describe('getHexBalance', () => {
|
||||||
it('should return default if no balance', () => {
|
it('should return default if no balance', () => {
|
||||||
const hexBalance = utils.getHexBalanceFromString(null, Web3);
|
const hexBalance = getHexBalanceFromString(null, Web3);
|
||||||
|
|
||||||
assert.strictEqual(hexBalance, 0xFFFFFFFFFFFFFFFFFF);
|
assert.strictEqual(hexBalance, 0xFFFFFFFFFFFFFFFFFF);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return the balance string if already hexadecimal', () => {
|
it('should return the balance string if already hexadecimal', () => {
|
||||||
const hexBalance = utils.getHexBalanceFromString('0xFFF', Web3);
|
const hexBalance = getHexBalanceFromString('0xFFF', Web3);
|
||||||
|
|
||||||
assert.strictEqual(hexBalance, '0xFFF');
|
assert.strictEqual(hexBalance, '0xFFF');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should convert to hex when decimal', () => {
|
it('should convert to hex when decimal', () => {
|
||||||
const hexBalance = utils.getHexBalanceFromString('500', Web3);
|
const hexBalance = getHexBalanceFromString('500', Web3);
|
||||||
|
|
||||||
assert.strictEqual(hexBalance, '0x1f4');
|
assert.strictEqual(hexBalance, '0x1f4');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should convert to hex with eth string', () => {
|
it('should convert to hex with eth string', () => {
|
||||||
const hexBalance = utils.getHexBalanceFromString('4ether', Web3);
|
const hexBalance = getHexBalanceFromString('4ether', Web3);
|
||||||
|
|
||||||
assert.strictEqual(hexBalance, '0x3782dace9d900000');
|
assert.strictEqual(hexBalance, '0x3782dace9d900000');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should convert to hex with eth string with space', () => {
|
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');
|
assert.strictEqual(hexBalance, '0x9cb1ed0a00');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should convert to hex with large ether values', () => {
|
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');
|
assert.strictEqual(hexBalance, '0x152d02c7e14af6800000');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fail when string is not good', () => {
|
it('should fail when string is not good', () => {
|
||||||
try {
|
try {
|
||||||
utils.getHexBalanceFromString('nogood', Web3);
|
getHexBalanceFromString('nogood', Web3);
|
||||||
assert.fail('Should have failed at getHexBalance');
|
assert.fail('Should have failed at getHexBalance');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Ok
|
// Ok
|
||||||
|
Loading…
x
Reference in New Issue
Block a user