aboutsummaryrefslogtreecommitdiffstats
path: root/packages
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2018-07-05 07:38:14 +0800
committerFabio Berger <me@fabioberger.com>2018-07-05 07:38:14 +0800
commitfd242a9cba7516f08579f0e97923a15740ef4d7a (patch)
treeda5e1aa87d2f69df22f5f7a396070cea00eed7a7 /packages
parent7efa17ef7d855e45cb874adedeee8dfc935fe40e (diff)
downloaddexon-0x-contracts-fd242a9cba7516f08579f0e97923a15740ef4d7a.tar
dexon-0x-contracts-fd242a9cba7516f08579f0e97923a15740ef4d7a.tar.gz
dexon-0x-contracts-fd242a9cba7516f08579f0e97923a15740ef4d7a.tar.bz2
dexon-0x-contracts-fd242a9cba7516f08579f0e97923a15740ef4d7a.tar.lz
dexon-0x-contracts-fd242a9cba7516f08579f0e97923a15740ef4d7a.tar.xz
dexon-0x-contracts-fd242a9cba7516f08579f0e97923a15740ef4d7a.tar.zst
dexon-0x-contracts-fd242a9cba7516f08579f0e97923a15740ef4d7a.zip
Remove web3Utils dep and write necessary utility methods
Diffstat (limited to 'packages')
-rw-r--r--packages/web3-wrapper/package.json4
-rw-r--r--packages/web3-wrapper/src/globals.d.ts8
-rw-r--r--packages/web3-wrapper/src/marshaller.ts87
-rw-r--r--packages/web3-wrapper/src/types.ts2
-rw-r--r--packages/web3-wrapper/src/utils.ts51
-rw-r--r--packages/web3-wrapper/src/web3_wrapper.ts20
6 files changed, 93 insertions, 79 deletions
diff --git a/packages/web3-wrapper/package.json b/packages/web3-wrapper/package.json
index 2084cce73..2b7c4555e 100644
--- a/packages/web3-wrapper/package.json
+++ b/packages/web3-wrapper/package.json
@@ -71,9 +71,7 @@
"ethereum-types": "^0.0.2",
"ethereumjs-util": "^5.1.1",
"ethers": "3.0.22",
- "lodash": "^4.17.4",
- "web3": "^0.20.0",
- "web3-utils": "^1.0.0-beta.34"
+ "lodash": "^4.17.4"
},
"publishConfig": {
"access": "public"
diff --git a/packages/web3-wrapper/src/globals.d.ts b/packages/web3-wrapper/src/globals.d.ts
index 09c9fd819..94e63a32d 100644
--- a/packages/web3-wrapper/src/globals.d.ts
+++ b/packages/web3-wrapper/src/globals.d.ts
@@ -1,11 +1,3 @@
-declare module 'web3-utils' {
- import * as BigNumber from 'bignumber.js';
-
- const toHex: (val: any) => string;
- const isHexStrict: (val: any) => boolean;
- const toDecimal: (val: any) => number;
-}
-
declare module '*.json' {
const json: any;
/* tslint:disable */
diff --git a/packages/web3-wrapper/src/marshaller.ts b/packages/web3-wrapper/src/marshaller.ts
index 06556ce90..d0cf6ff2d 100644
--- a/packages/web3-wrapper/src/marshaller.ts
+++ b/packages/web3-wrapper/src/marshaller.ts
@@ -13,7 +13,6 @@ import {
} from 'ethereum-types';
import ethUtil = require('ethereumjs-util');
import * as _ from 'lodash';
-import web3Utils = require('web3-utils');
import { utils } from './utils';
@@ -33,26 +32,26 @@ export const marshaller = {
): BlockWithoutTransactionData {
const block = {
...blockWithHexValues,
- gasLimit: web3Utils.toDecimal(blockWithHexValues.gasLimit),
- gasUsed: web3Utils.toDecimal(blockWithHexValues.gasUsed),
- size: web3Utils.toDecimal(blockWithHexValues.size),
- timestamp: web3Utils.toDecimal(blockWithHexValues.timestamp),
- number: _.isNull(blockWithHexValues.number) ? null : web3Utils.toDecimal(blockWithHexValues.number),
- difficulty: this._convertAmountToBigNumber(blockWithHexValues.difficulty),
- totalDifficulty: this._convertAmountToBigNumber(blockWithHexValues.totalDifficulty),
+ gasLimit: utils.convertHexToNumber(blockWithHexValues.gasLimit),
+ gasUsed: utils.convertHexToNumber(blockWithHexValues.gasUsed),
+ size: utils.convertHexToNumber(blockWithHexValues.size),
+ timestamp: utils.convertHexToNumber(blockWithHexValues.timestamp),
+ number: _.isNull(blockWithHexValues.number) ? null : utils.convertHexToNumber(blockWithHexValues.number),
+ difficulty: utils.convertAmountToBigNumber(blockWithHexValues.difficulty),
+ totalDifficulty: utils.convertAmountToBigNumber(blockWithHexValues.totalDifficulty),
};
return block;
},
unmarshalIntoBlockWithTransactionData(blockWithHexValues: BlockWithTransactionDataRPC): BlockWithTransactionData {
const block = {
...blockWithHexValues,
- gasLimit: web3Utils.toDecimal(blockWithHexValues.gasLimit),
- gasUsed: web3Utils.toDecimal(blockWithHexValues.gasUsed),
- size: web3Utils.toDecimal(blockWithHexValues.size),
- timestamp: web3Utils.toDecimal(blockWithHexValues.timestamp),
- number: _.isNull(blockWithHexValues.number) ? null : web3Utils.toDecimal(blockWithHexValues.number),
- difficulty: this._convertAmountToBigNumber(blockWithHexValues.difficulty),
- totalDifficulty: this._convertAmountToBigNumber(blockWithHexValues.totalDifficulty),
+ gasLimit: utils.convertHexToNumber(blockWithHexValues.gasLimit),
+ gasUsed: utils.convertHexToNumber(blockWithHexValues.gasUsed),
+ size: utils.convertHexToNumber(blockWithHexValues.size),
+ timestamp: utils.convertHexToNumber(blockWithHexValues.timestamp),
+ number: _.isNull(blockWithHexValues.number) ? null : utils.convertHexToNumber(blockWithHexValues.number),
+ difficulty: utils.convertAmountToBigNumber(blockWithHexValues.difficulty),
+ totalDifficulty: utils.convertAmountToBigNumber(blockWithHexValues.totalDifficulty),
transactions: [] as Transaction[],
};
block.transactions = _.map(blockWithHexValues.transactions, (tx: TransactionRPC) => {
@@ -64,12 +63,14 @@ export const marshaller = {
unmarshalTransaction(txRpc: TransactionRPC): Transaction {
const tx = {
...txRpc,
- blockNumber: !_.isNull(txRpc.blockNumber) ? web3Utils.toDecimal(txRpc.blockNumber) : null,
- transactionIndex: !_.isNull(txRpc.transactionIndex) ? web3Utils.toDecimal(txRpc.transactionIndex) : null,
- nonce: web3Utils.toDecimal(txRpc.nonce),
- gas: web3Utils.toDecimal(txRpc.gas),
- gasPrice: this._convertAmountToBigNumber(txRpc.gasPrice),
- value: this._convertAmountToBigNumber(txRpc.value),
+ blockNumber: !_.isNull(txRpc.blockNumber) ? utils.convertHexToNumber(txRpc.blockNumber) : null,
+ transactionIndex: !_.isNull(txRpc.transactionIndex)
+ ? utils.convertHexToNumber(txRpc.transactionIndex)
+ : null,
+ nonce: utils.convertHexToNumber(txRpc.nonce),
+ gas: utils.convertHexToNumber(txRpc.gas),
+ gasPrice: utils.convertAmountToBigNumber(txRpc.gasPrice),
+ value: utils.convertAmountToBigNumber(txRpc.value),
};
return tx;
},
@@ -116,15 +117,15 @@ export const marshaller = {
if (_.isUndefined(blockParam)) {
return BlockParamLiteral.Latest;
}
- const encodedBlockParam = _.isNumber(blockParam) ? web3Utils.toHex(blockParam) : blockParam;
+ const encodedBlockParam = _.isNumber(blockParam) ? utils.numberToHex(blockParam) : blockParam;
return encodedBlockParam;
},
unmarshalLog(rawLog: RawLogEntry): LogEntry {
const formattedLog = {
...rawLog,
- logIndex: this.convertHexToNumberOrNull(rawLog.logIndex),
- blockNumber: this.convertHexToNumberOrNull(rawLog.blockNumber),
- transactionIndex: this.convertHexToNumberOrNull(rawLog.transactionIndex),
+ logIndex: utils.convertHexToNumberOrNull(rawLog.logIndex),
+ blockNumber: utils.convertHexToNumberOrNull(rawLog.blockNumber),
+ transactionIndex: utils.convertHexToNumberOrNull(rawLog.transactionIndex),
};
return formattedLog;
},
@@ -134,44 +135,16 @@ export const marshaller = {
to: _.isUndefined(callTxDataBase.to) ? undefined : this.marshalAddress(callTxDataBase.to),
gasPrice: _.isUndefined(callTxDataBase.gasPrice)
? undefined
- : this._encodeAmountAsHexString(callTxDataBase.gasPrice),
- gas: _.isUndefined(callTxDataBase.gas) ? undefined : this._encodeAmountAsHexString(callTxDataBase.gas),
+ : utils.encodeAmountAsHexString(callTxDataBase.gasPrice),
+ gas: _.isUndefined(callTxDataBase.gas) ? undefined : utils.encodeAmountAsHexString(callTxDataBase.gas),
value: _.isUndefined(callTxDataBase.value)
? undefined
- : this._encodeAmountAsHexString(callTxDataBase.value),
+ : utils.encodeAmountAsHexString(callTxDataBase.value),
nonce: _.isUndefined(callTxDataBase.nonce)
? undefined
- : this._encodeAmountAsHexString(callTxDataBase.nonce),
+ : utils.encodeAmountAsHexString(callTxDataBase.nonce),
};
return callTxDataBaseRPC;
},
- convertHexToNumberOrNull(hex: string | null): number | null {
- if (_.isNull(hex)) {
- return null;
- }
- const decimal = web3Utils.toDecimal(hex);
- return decimal;
- },
- _convertAmountToBigNumber(value: string | number | BigNumber): BigNumber {
- const num = value || 0;
- const isBigNumber = utils.isBigNumber(num);
- if (isBigNumber) {
- return num as BigNumber;
- }
-
- if (_.isString(num) && (num.indexOf('0x') === 0 || num.indexOf('-0x') === 0)) {
- return new BigNumber(num.replace('0x', ''), 16);
- }
-
- const baseTen = 10;
- return new BigNumber((num as number).toString(baseTen), baseTen);
- },
- _encodeAmountAsHexString(value: string | number | BigNumber): string {
- const valueBigNumber = this._convertAmountToBigNumber(value);
- const hexBase = 16;
- const valueHex = valueBigNumber.toString(hexBase);
-
- return valueBigNumber.lessThan(0) ? '-0x' + valueHex.substr(1) : '0x' + valueHex;
- },
};
diff --git a/packages/web3-wrapper/src/types.ts b/packages/web3-wrapper/src/types.ts
index 54a5f3f0e..b7b6bd68a 100644
--- a/packages/web3-wrapper/src/types.ts
+++ b/packages/web3-wrapper/src/types.ts
@@ -29,7 +29,7 @@ export interface BlockWithTransactionDataRPC extends AbstractBlockRPC {
}
export interface TransactionRPC {
hash: string;
- nonce: number;
+ nonce: string;
blockHash: string | null;
blockNumber: string | null;
transactionIndex: string | null;
diff --git a/packages/web3-wrapper/src/utils.ts b/packages/web3-wrapper/src/utils.ts
index 376f7e89b..43ab73238 100644
--- a/packages/web3-wrapper/src/utils.ts
+++ b/packages/web3-wrapper/src/utils.ts
@@ -1,3 +1,4 @@
+import { BigNumber } from '@0xproject/utils';
import * as _ from 'lodash';
export const utils = {
@@ -5,4 +6,54 @@ export const utils = {
const isBigNumber = _.isObject(value) && (value as any).isBigNumber;
return isBigNumber;
},
+ convertHexToNumber(value: string): number {
+ console.log('value', value);
+ const valueBigNumber = new BigNumber(value);
+ const valueNumber = valueBigNumber.toNumber();
+ return valueNumber;
+ },
+ convertHexToNumberOrNull(hex: string | null): number | null {
+ if (_.isNull(hex)) {
+ return null;
+ }
+ const decimal = this.convertHexToNumber(hex);
+ return decimal;
+ },
+ convertAmountToBigNumber(value: string | number | BigNumber): BigNumber {
+ const num = value || 0;
+ const isBigNumber = utils.isBigNumber(num);
+ if (isBigNumber) {
+ return num as BigNumber;
+ }
+
+ if (_.isString(num) && (num.indexOf('0x') === 0 || num.indexOf('-0x') === 0)) {
+ return new BigNumber(num.replace('0x', ''), 16);
+ }
+
+ const baseTen = 10;
+ return new BigNumber((num as number).toString(baseTen), baseTen);
+ },
+ encodeAmountAsHexString(value: string | number | BigNumber): string {
+ const valueBigNumber = utils.convertAmountToBigNumber(value);
+ const hexBase = 16;
+ const valueHex = valueBigNumber.toString(hexBase);
+
+ return valueBigNumber.lessThan(0) ? '-0x' + valueHex.substr(1) : '0x' + valueHex;
+ },
+ numberToHex(value: number): string {
+ if (!isFinite(value) && !this.isHexStrict(value)) {
+ throw new Error('Given input "' + value + '" is not a number.');
+ }
+
+ const valueBigNumber = new BigNumber(value);
+ const hexBase = 16;
+ const result = valueBigNumber.toString(hexBase);
+
+ return valueBigNumber.lt(0) ? '-0x' + result.substr(1) : '0x' + result;
+ },
+ isHexStrict(hex: string | number): boolean {
+ return (
+ (_.isString(hex) || _.isNumber(hex)) && /^(-)?0x[0-9a-f]*$/i.test(_.isNumber(hex) ? hex.toString() : hex)
+ );
+ },
};
diff --git a/packages/web3-wrapper/src/web3_wrapper.ts b/packages/web3-wrapper/src/web3_wrapper.ts
index b1d5a59d9..35ecc5244 100644
--- a/packages/web3-wrapper/src/web3_wrapper.ts
+++ b/packages/web3-wrapper/src/web3_wrapper.ts
@@ -20,7 +20,6 @@ import {
TxData,
} from 'ethereum-types';
import * as _ from 'lodash';
-import * as web3Utils from 'web3-utils';
import { marshaller } from './marshaller';
import {
@@ -29,6 +28,7 @@ import {
TransactionRPC,
Web3WrapperErrors,
} from './types';
+import { utils } from './utils';
const BASE_TEN = 10;
@@ -136,7 +136,7 @@ export class Web3Wrapper {
// number - Parity
// hex - Geth
if (_.isString(status)) {
- return web3Utils.toDecimal(status) as 0 | 1;
+ return utils.convertHexToNumber(status) as 0 | 1;
} else if (_.isUndefined(status)) {
return null;
} else {
@@ -315,7 +315,7 @@ export class Web3Wrapper {
method: 'eth_blockNumber',
params: [],
});
- const blockNumber = marshaller.convertHexToNumberOrNull(blockNumberHex);
+ const blockNumber = utils.convertHexToNumberOrNull(blockNumberHex);
return blockNumber as number;
}
/**
@@ -326,7 +326,7 @@ export class Web3Wrapper {
public async getBlockAsync(blockParam: string | BlockParam): Promise<BlockWithoutTransactionData> {
Web3Wrapper._assertBlockParamOrString(blockParam);
const encodedBlockParam = marshaller.marshalBlockParam(blockParam);
- const method = web3Utils.isHexStrict(blockParam) ? 'eth_getBlockByHash' : 'eth_getBlockByNumber';
+ const method = utils.isHexStrict(blockParam) ? 'eth_getBlockByHash' : 'eth_getBlockByNumber';
const shouldIncludeTransactionData = false;
const blockWithoutTransactionDataWithHexValues = await this._sendRawPayloadAsync<
BlockWithoutTransactionDataRPC
@@ -348,9 +348,9 @@ export class Web3Wrapper {
Web3Wrapper._assertBlockParamOrString(blockParam);
let encodedBlockParam = blockParam;
if (_.isNumber(blockParam)) {
- encodedBlockParam = web3Utils.toHex(blockParam);
+ encodedBlockParam = utils.numberToHex(blockParam);
}
- const method = web3Utils.isHexStrict(blockParam) ? 'eth_getBlockByHash' : 'eth_getBlockByNumber';
+ const method = utils.isHexStrict(blockParam) ? 'eth_getBlockByHash' : 'eth_getBlockByNumber';
const shouldIncludeTransactionData = true;
const blockWithTransactionDataWithHexValues = await this._sendRawPayloadAsync<BlockWithTransactionDataRPC>({
method,
@@ -432,11 +432,11 @@ export class Web3Wrapper {
public async getLogsAsync(filter: FilterObject): Promise<LogEntry[]> {
let fromBlock = filter.fromBlock;
if (_.isNumber(fromBlock)) {
- fromBlock = web3Utils.toHex(fromBlock);
+ fromBlock = utils.numberToHex(fromBlock);
}
let toBlock = filter.toBlock;
if (_.isNumber(toBlock)) {
- toBlock = web3Utils.toHex(toBlock);
+ toBlock = utils.numberToHex(toBlock);
}
const serializedFilter = {
...filter,
@@ -459,7 +459,7 @@ export class Web3Wrapper {
public async estimateGasAsync(txData: Partial<TxData>): Promise<number> {
const txDataHex = marshaller.marshalTxData(txData);
const gasHex = await this._sendRawPayloadAsync<string>({ method: 'eth_estimateGas', params: [txDataHex] });
- const gas = web3Utils.toDecimal(gasHex);
+ const gas = utils.convertHexToNumber(gasHex);
return gas;
}
/**
@@ -600,7 +600,7 @@ export class Web3Wrapper {
*/
public async setHeadAsync(blockNumber: number): Promise<void> {
assert.isNumber('blockNumber', blockNumber);
- await this._sendRawPayloadAsync<void>({ method: 'debug_setHead', params: [web3Utils.toHex(blockNumber)] });
+ await this._sendRawPayloadAsync<void>({ method: 'debug_setHead', params: [utils.numberToHex(blockNumber)] });
}
/**
* Returns either NodeType.Geth or NodeType.Ganache depending on the type of