refactor(@embark/utils): move decodeParams and sha3 into web3Utils

This commit is contained in:
Pascal Precht 2019-04-30 15:48:49 +02:00 committed by Pascal Precht
parent 6fbc1afbd1
commit 7174f55c6c
6 changed files with 28 additions and 23 deletions

View File

@ -54,9 +54,10 @@
"i18n": "0.8.3",
"merge": "1.2.1",
"multihashes": "0.4.14",
"web3": "1.0.0-beta.37",
"ora": "2.1.0",
"web3": "1.0.0-beta.37",
"web3-eth": "1.0.0-beta.37",
"web3-eth-abi": "1.0.0-beta.37",
"shelljs": "0.5.3"
},
"devDependencies": {

View File

@ -10,7 +10,12 @@ const toposortGraph = require('./toposort');
import { unitRegex } from './constants';
import * as AddressUtils from './addressUtils';
const web3 = require("web3");
import { getWeiBalanceFromString, getHexBalanceFromString } from './web3Utils';
import {
getWeiBalanceFromString,
getHexBalanceFromString,
decodeParams,
sha3
} from './web3Utils';
import AccountParser from './accountParser';
const { extendZeroAddressShorthand, replaceZeroAddressShorthand } = AddressUtils;
@ -178,6 +183,7 @@ const Utils = {
deconstructUrl,
defaultCorsHost,
defaultHost,
decodeParams,
dockerHostSwap,
exit,
isDocker,
@ -192,6 +198,7 @@ const Utils = {
getWeiBalanceFromString,
getHexBalanceFromString,
sha512,
sha3,
timer,
unitRegex,
runCmd,

View File

@ -1,5 +1,7 @@
import { __ } from "i18n";
import { balanceRegex } from "./constants";
const Web3EthAbi = require("web3-eth-abi");
const Web3 = require("web3");
export function getHexBalanceFromString(balanceString: string, web3: any) {
if (!web3) {
@ -39,3 +41,11 @@ export function getWeiBalanceFromString(balanceString: string, web3: any) {
return web3.utils.toWei(match[1], match[2]);
}
export function decodeParams(typesArray: any, hexString: string) {
return Web3EthAbi.decodeParameters(typesArray, hexString);
}
export function sha3(arg: any) {
return Web3.utils.sha3(arg);
}

View File

@ -1,4 +1,4 @@
let utils = require('../../utils/utils.js');
import { sha3 } from 'embark-utils';
class DeployTracker {
@ -87,7 +87,7 @@ class DeployTracker {
trackContract(contractName, code, args, address) {
if (!this.currentChain) return false;
this.currentChain.contracts[utils.sha3(code + contractName + args.join(','))] = {
this.currentChain.contracts[sha3(code + contractName + args.join(','))] = {
name: contractName,
address: address
};
@ -95,7 +95,7 @@ class DeployTracker {
getContract(contractName, code, args) {
if (!this.currentChain) return false;
let contract = this.currentChain.contracts[utils.sha3(code + contractName + args.join(','))];
let contract = this.currentChain.contracts[sha3(code + contractName + args.join(','))];
if (contract && contract.address === undefined) {
return false;
}

View File

@ -1,8 +1,7 @@
import { Contract } from "embark";
import { decodeParams, sha3 } from "embark-utils";
import { ABIDefinition } from "web3/eth/abi";
const utils = require("./utils");
export interface AddressToContract {
name: string;
functions: { [functionName: string]: FunctionSignature; };
@ -37,7 +36,7 @@ export function getAddressToContract(contractsList: Contract[], addressToContrac
.filter((func: ABIDefinition) => func.type === "function")
.map((func: ABIDefinition) => {
const name = `${func.name}(${func.inputs ? func.inputs.map((input) => input.type).join(",") : ""})`;
funcSignatures[utils.sha3(name).substring(0, 10)] = {
funcSignatures[sha3(name).substring(0, 10)] = {
abi: func,
functionName: func.name,
name,
@ -59,7 +58,7 @@ export function getTransactionParams(contract: AddressToContract, transactionInp
let paramString = "";
if (func && func.abi && func.abi.inputs) {
const decodedParameters = utils.decodeParams(func.abi.inputs, transactionInput.substring(10));
const decodedParameters = decodeParams(func.abi.inputs, transactionInput.substring(10));
func.abi.inputs.forEach((input) => {
const quote = input.type.indexOf("int") === -1 ? '"' : "";
paramString += quote + decodedParameters[input.name] + quote + ", ";

View File

@ -287,21 +287,11 @@ function isValidDomain(v) {
return isValid;
}
function decodeParams(typesArray, hexString) {
var Web3EthAbi = require('web3-eth-abi');
return Web3EthAbi.decodeParameters(typesArray, hexString);
}
function toChecksumAddress(address) {
const Web3 = require('web3');
return Web3.utils.toChecksumAddress(address);
}
function sha3(arg) {
const Web3 = require('web3');
return Web3.utils.sha3(arg);
}
/**
* Builds a URL
*
@ -462,7 +452,6 @@ module.exports = {
hexToNumber,
isValidDomain,
pingEndpoint,
decodeParams,
cd,
sed,
downloadFile,
@ -470,7 +459,6 @@ module.exports = {
extractZip,
getExternalContractUrl,
toChecksumAddress,
sha3,
normalizeInput,
buildUrl,
buildUrlFromConfig,