mirror of https://github.com/embarklabs/embark.git
refactor(@embark/utils): move web3 helper functions into web3Utils file
This commit is contained in:
parent
5c3f39442b
commit
e8408ece75
|
@ -49,6 +49,7 @@
|
||||||
"clipboardy": "1.2.3",
|
"clipboardy": "1.2.3",
|
||||||
"colors": "1.3.2",
|
"colors": "1.3.2",
|
||||||
"follow-redirects": "1.5.7",
|
"follow-redirects": "1.5.7",
|
||||||
|
"i18n": "0.8.3",
|
||||||
"merge": "1.2.1",
|
"merge": "1.2.1",
|
||||||
"multihashes": "0.4.14",
|
"multihashes": "0.4.14",
|
||||||
"web3": "1.0.0-beta.37",
|
"web3": "1.0.0-beta.37",
|
||||||
|
|
|
@ -7,9 +7,10 @@ const {canonicalHost, defaultCorsHost, defaultHost, dockerHostSwap, isDocker} =
|
||||||
const {findNextPort} = require('./network');
|
const {findNextPort} = require('./network');
|
||||||
const logUtils = require('./log-utils');
|
const logUtils = require('./log-utils');
|
||||||
const toposortGraph = require('./toposort');
|
const toposortGraph = require('./toposort');
|
||||||
import { unitRegex, balanceRegex } from './constants';
|
import { unitRegex } from './constants';
|
||||||
import * as AddressUtils from './addressUtils';
|
import * as AddressUtils from './addressUtils';
|
||||||
const web3 = require("web3");
|
const web3 = require("web3");
|
||||||
|
import { getWeiBalanceFromString, getHexBalanceFromString } from './web3Utils';
|
||||||
|
|
||||||
const { extendZeroAddressShorthand, replaceZeroAddressShorthand } = AddressUtils;
|
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) {
|
function prepareContractsConfig(config) {
|
||||||
Object.keys(config.contracts).forEach((contractName) => {
|
Object.keys(config.contracts).forEach((contractName) => {
|
||||||
const gas = config.contracts[contractName].gas;
|
const gas = config.contracts[contractName].gas;
|
||||||
|
|
|
@ -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]);
|
||||||
|
}
|
Loading…
Reference in New Issue