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", "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",
"ora": "2.1.0", "ora": "2.1.0",
"web3": "1.0.0-beta.37", "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" "shelljs": "0.5.3"
}, },
"devDependencies": { "devDependencies": {

View File

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

View File

@ -1,5 +1,7 @@
import { __ } from "i18n"; import { __ } from "i18n";
import { balanceRegex } from "./constants"; import { balanceRegex } from "./constants";
const Web3EthAbi = require("web3-eth-abi");
const Web3 = require("web3");
export function getHexBalanceFromString(balanceString: string, web3: any) { export function getHexBalanceFromString(balanceString: string, web3: any) {
if (!web3) { if (!web3) {
@ -39,3 +41,11 @@ export function getWeiBalanceFromString(balanceString: string, web3: any) {
return web3.utils.toWei(match[1], match[2]); 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 { class DeployTracker {
@ -87,7 +87,7 @@ class DeployTracker {
trackContract(contractName, code, args, address) { trackContract(contractName, code, args, address) {
if (!this.currentChain) return false; if (!this.currentChain) return false;
this.currentChain.contracts[utils.sha3(code + contractName + args.join(','))] = { this.currentChain.contracts[sha3(code + contractName + args.join(','))] = {
name: contractName, name: contractName,
address: address address: address
}; };
@ -95,7 +95,7 @@ class DeployTracker {
getContract(contractName, code, args) { getContract(contractName, code, args) {
if (!this.currentChain) return false; 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) { if (contract && contract.address === undefined) {
return false; return false;
} }

View File

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

View File

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