From e8408ece75e4d9c25b200c80781e83580d56b434 Mon Sep 17 00:00:00 2001 From: Pascal Precht Date: Tue, 30 Apr 2019 13:32:10 +0200 Subject: [PATCH] refactor(@embark/utils): move web3 helper functions into web3Utils file --- packages/embark-utils/package.json | 1 + packages/embark-utils/src/index.js | 42 ++------------------------ packages/embark-utils/src/web3Utils.ts | 41 +++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 40 deletions(-) create mode 100644 packages/embark-utils/src/web3Utils.ts diff --git a/packages/embark-utils/package.json b/packages/embark-utils/package.json index 6018adefd..e067a8006 100644 --- a/packages/embark-utils/package.json +++ b/packages/embark-utils/package.json @@ -49,6 +49,7 @@ "clipboardy": "1.2.3", "colors": "1.3.2", "follow-redirects": "1.5.7", + "i18n": "0.8.3", "merge": "1.2.1", "multihashes": "0.4.14", "web3": "1.0.0-beta.37", diff --git a/packages/embark-utils/src/index.js b/packages/embark-utils/src/index.js index 551eadae1..1f21829c0 100644 --- a/packages/embark-utils/src/index.js +++ b/packages/embark-utils/src/index.js @@ -7,9 +7,10 @@ const {canonicalHost, defaultCorsHost, defaultHost, dockerHostSwap, isDocker} = const {findNextPort} = require('./network'); const logUtils = require('./log-utils'); const toposortGraph = require('./toposort'); -import { unitRegex, balanceRegex } from './constants'; +import { unitRegex } from './constants'; import * as AddressUtils from './addressUtils'; const web3 = require("web3"); +import { getWeiBalanceFromString, getHexBalanceFromString } from './web3Utils'; const { extendZeroAddressShorthand, replaceZeroAddressShorthand } = AddressUtils; @@ -128,45 +129,6 @@ 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\'')); - } - if (!balanceString) { - return 0; - } - 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.toWei(match[1], match[2]); -} - function prepareContractsConfig(config) { Object.keys(config.contracts).forEach((contractName) => { const gas = config.contracts[contractName].gas; diff --git a/packages/embark-utils/src/web3Utils.ts b/packages/embark-utils/src/web3Utils.ts new file mode 100644 index 000000000..bf2d7527e --- /dev/null +++ b/packages/embark-utils/src/web3Utils.ts @@ -0,0 +1,41 @@ +import { __ } from "i18n"; +import { balanceRegex } from "./constants"; + +export function getHexBalanceFromString(balanceString: string, web3: any) { + 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])); +} + +export function getWeiBalanceFromString(balanceString: string, web3: any) { + if (!web3) { + throw new Error(__("[getWeiBalanceFromString]: Missing parameter 'web3'")); + } + if (!balanceString) { + return 0; + } + 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.toWei(match[1], match[2]); +}