refactor(@embark/utils): move web3 helper functions into web3Utils file

This commit is contained in:
Pascal Precht 2019-04-30 13:32:10 +02:00 committed by Pascal Precht
parent 5c3f39442b
commit e8408ece75
3 changed files with 44 additions and 40 deletions

View File

@ -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",

View File

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

View File

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