aboutsummaryrefslogtreecommitdiffstats
path: root/packages
diff options
context:
space:
mode:
Diffstat (limited to 'packages')
-rw-r--r--packages/assert/src/index.ts4
-rw-r--r--packages/base-contract/src/index.ts2
-rw-r--r--packages/web3-wrapper/package.json6
-rw-r--r--packages/web3-wrapper/src/globals.d.ts8
-rw-r--r--packages/web3-wrapper/src/marshaller.ts177
-rw-r--r--packages/web3-wrapper/src/types.ts56
-rw-r--r--packages/web3-wrapper/src/utils.ts8
-rw-r--r--packages/web3-wrapper/src/web3_wrapper.ts224
-rw-r--r--packages/web3-wrapper/test/web3_wrapper_test.ts95
9 files changed, 501 insertions, 79 deletions
diff --git a/packages/assert/src/index.ts b/packages/assert/src/index.ts
index 95c7b658b..8e18416c5 100644
--- a/packages/assert/src/index.ts
+++ b/packages/assert/src/index.ts
@@ -41,8 +41,8 @@ export const assert = {
value: string,
stringEnum: any /* There is no base type for every string enum */,
): void {
- const doesBelongToStringEnum = !_.isUndefined(stringEnum[value]);
- const enumValues = _.keys(stringEnum);
+ const enumValues = _.values(stringEnum);
+ const doesBelongToStringEnum = _.includes(enumValues, value);
const enumValuesAsStrings = _.map(enumValues, enumValue => `'${enumValue}'`);
const enumValuesAsString = enumValuesAsStrings.join(', ');
assert.assert(
diff --git a/packages/base-contract/src/index.ts b/packages/base-contract/src/index.ts
index cb9042415..6b970ec27 100644
--- a/packages/base-contract/src/index.ts
+++ b/packages/base-contract/src/index.ts
@@ -80,7 +80,7 @@ export class BaseContract {
// Awaiting https://github.com/Microsoft/TypeScript/pull/13288 to be merged
} as any;
if (_.isUndefined(txDataWithDefaults.gas) && !_.isUndefined(estimateGasAsync)) {
- txDataWithDefaults.gas = await estimateGasAsync(txData);
+ txDataWithDefaults.gas = await estimateGasAsync(txDataWithDefaults as any);
}
return txDataWithDefaults;
}
diff --git a/packages/web3-wrapper/package.json b/packages/web3-wrapper/package.json
index 05d195bb9..2084cce73 100644
--- a/packages/web3-wrapper/package.json
+++ b/packages/web3-wrapper/package.json
@@ -64,12 +64,16 @@
"typescript": "2.7.1"
},
"dependencies": {
+ "@0xproject/assert": "^0.2.12",
"@0xproject/typescript-typings": "^0.4.1",
"@0xproject/utils": "^0.7.1",
+ "bn.js": "^4.11.8",
"ethereum-types": "^0.0.2",
+ "ethereumjs-util": "^5.1.1",
"ethers": "3.0.22",
"lodash": "^4.17.4",
- "web3": "^0.20.0"
+ "web3": "^0.20.0",
+ "web3-utils": "^1.0.0-beta.34"
},
"publishConfig": {
"access": "public"
diff --git a/packages/web3-wrapper/src/globals.d.ts b/packages/web3-wrapper/src/globals.d.ts
index 94e63a32d..09c9fd819 100644
--- a/packages/web3-wrapper/src/globals.d.ts
+++ b/packages/web3-wrapper/src/globals.d.ts
@@ -1,3 +1,11 @@
+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
new file mode 100644
index 000000000..06556ce90
--- /dev/null
+++ b/packages/web3-wrapper/src/marshaller.ts
@@ -0,0 +1,177 @@
+import { addressUtils, BigNumber } from '@0xproject/utils';
+import {
+ BlockParam,
+ BlockParamLiteral,
+ BlockWithoutTransactionData,
+ BlockWithTransactionData,
+ CallData,
+ CallTxDataBase,
+ LogEntry,
+ RawLogEntry,
+ Transaction,
+ TxData,
+} from 'ethereum-types';
+import ethUtil = require('ethereumjs-util');
+import * as _ from 'lodash';
+import web3Utils = require('web3-utils');
+
+import { utils } from './utils';
+
+import {
+ AbstractBlockRPC,
+ BlockWithoutTransactionDataRPC,
+ BlockWithTransactionDataRPC,
+ CallDataRPC,
+ CallTxDataBaseRPC,
+ TransactionRPC,
+ TxDataRPC,
+} from './types';
+
+export const marshaller = {
+ unmarshalIntoBlockWithoutTransactionData(
+ blockWithHexValues: BlockWithoutTransactionDataRPC,
+ ): 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),
+ };
+ 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),
+ transactions: [] as Transaction[],
+ };
+ block.transactions = _.map(blockWithHexValues.transactions, (tx: TransactionRPC) => {
+ const transaction = this.unmarshalTransaction(tx);
+ return transaction;
+ });
+ return block;
+ },
+ 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),
+ };
+ return tx;
+ },
+ marshalTxData(txData: Partial<TxData>): Partial<TxDataRPC> {
+ if (_.isUndefined(txData.from)) {
+ throw new Error(`txData is missing required "from" address.`);
+ }
+ const callTxDataBase = {
+ ...txData,
+ };
+ delete callTxDataBase.from;
+ const callTxDataBaseRPC = this._marshalCallTxDataBase(callTxDataBase);
+ const txDataRPC = {
+ ...callTxDataBaseRPC,
+ from: this.marshalAddress(txData.from),
+ };
+ const prunableIfUndefined = ['gasPrice', 'gas', 'value', 'nonce'];
+ _.each(txDataRPC, (value: any, key: string) => {
+ if (_.isUndefined(value) && _.includes(prunableIfUndefined, key)) {
+ delete (txDataRPC as any)[key];
+ }
+ });
+ return txDataRPC;
+ },
+ marshalCallData(callData: Partial<CallData>): Partial<CallDataRPC> {
+ const callTxDataBase = {
+ ...callData,
+ };
+ delete callTxDataBase.from;
+ const callTxDataBaseRPC = this._marshalCallTxDataBase(callTxDataBase);
+ const callDataRPC = {
+ ...callTxDataBaseRPC,
+ from: _.isUndefined(callData.from) ? undefined : this.marshalAddress(callData.from),
+ };
+ return callDataRPC;
+ },
+ marshalAddress(address: string): string {
+ if (addressUtils.isAddress(address)) {
+ return ethUtil.addHexPrefix(address);
+ }
+ throw new Error(`Invalid address encountered: ${address}`);
+ },
+ marshalBlockParam(blockParam: BlockParam | string | number | undefined): string | undefined {
+ if (_.isUndefined(blockParam)) {
+ return BlockParamLiteral.Latest;
+ }
+ const encodedBlockParam = _.isNumber(blockParam) ? web3Utils.toHex(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),
+ };
+ return formattedLog;
+ },
+ _marshalCallTxDataBase(callTxDataBase: Partial<CallTxDataBase>): Partial<CallTxDataBaseRPC> {
+ const callTxDataBaseRPC = {
+ ...callTxDataBase,
+ 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),
+ value: _.isUndefined(callTxDataBase.value)
+ ? undefined
+ : this._encodeAmountAsHexString(callTxDataBase.value),
+ nonce: _.isUndefined(callTxDataBase.nonce)
+ ? undefined
+ : this._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 79542da10..54a5f3f0e 100644
--- a/packages/web3-wrapper/src/types.ts
+++ b/packages/web3-wrapper/src/types.ts
@@ -1,3 +1,59 @@
export enum Web3WrapperErrors {
TransactionMiningTimeout = 'TRANSACTION_MINING_TIMEOUT',
}
+
+export interface AbstractBlockRPC {
+ number: string | null;
+ hash: string | null;
+ parentHash: string;
+ nonce: string | null;
+ sha3Uncles: string;
+ logsBloom: string | null;
+ transactionsRoot: string;
+ stateRoot: string;
+ miner: string;
+ difficulty: string;
+ totalDifficulty: string;
+ extraData: string;
+ size: string;
+ gasLimit: string;
+ gasUsed: string;
+ timestamp: string;
+ uncles: string[];
+}
+export interface BlockWithoutTransactionDataRPC extends AbstractBlockRPC {
+ transactions: string[];
+}
+export interface BlockWithTransactionDataRPC extends AbstractBlockRPC {
+ transactions: TransactionRPC[];
+}
+export interface TransactionRPC {
+ hash: string;
+ nonce: number;
+ blockHash: string | null;
+ blockNumber: string | null;
+ transactionIndex: string | null;
+ from: string;
+ to: string | null;
+ value: string;
+ gasPrice: string;
+ gas: string;
+ input: string;
+}
+
+export interface CallTxDataBaseRPC {
+ to?: string;
+ value?: string;
+ gas?: string;
+ gasPrice?: string;
+ data?: string;
+ nonce?: string;
+}
+
+export interface TxDataRPC extends CallTxDataBaseRPC {
+ from: string;
+}
+
+export interface CallDataRPC extends CallTxDataBaseRPC {
+ from?: string;
+}
diff --git a/packages/web3-wrapper/src/utils.ts b/packages/web3-wrapper/src/utils.ts
new file mode 100644
index 000000000..376f7e89b
--- /dev/null
+++ b/packages/web3-wrapper/src/utils.ts
@@ -0,0 +1,8 @@
+import * as _ from 'lodash';
+
+export const utils = {
+ isBigNumber(value: any): boolean {
+ const isBigNumber = _.isObject(value) && (value as any).isBigNumber;
+ return isBigNumber;
+ },
+};
diff --git a/packages/web3-wrapper/src/web3_wrapper.ts b/packages/web3-wrapper/src/web3_wrapper.ts
index b79ade278..e56b99366 100644
--- a/packages/web3-wrapper/src/web3_wrapper.ts
+++ b/packages/web3-wrapper/src/web3_wrapper.ts
@@ -1,6 +1,8 @@
+import { assert } from '@0xproject/assert';
import { AbiDecoder, addressUtils, BigNumber, intervalUtils, promisify } from '@0xproject/utils';
import {
BlockParam,
+ BlockParamLiteral,
BlockWithoutTransactionData,
BlockWithTransactionData,
CallData,
@@ -18,9 +20,15 @@ import {
TxData,
} from 'ethereum-types';
import * as _ from 'lodash';
-import * as Web3 from 'web3';
+import * as web3Utils from 'web3-utils';
-import { Web3WrapperErrors } from './types';
+import { marshaller } from './marshaller';
+import {
+ BlockWithoutTransactionDataRPC,
+ BlockWithTransactionDataRPC,
+ TransactionRPC,
+ Web3WrapperErrors,
+} from './types';
const BASE_TEN = 10;
@@ -38,7 +46,7 @@ export enum NodeType {
}
/**
- * A wrapper around the Web3.js 0.x library that provides a consistent, clean promise-based interface.
+ * An alternative to the Web3.js library that provides a consistent, clean, promise-based interface.
*/
export class Web3Wrapper {
/**
@@ -46,7 +54,7 @@ export class Web3Wrapper {
*/
public isZeroExWeb3Wrapper = true;
public abiDecoder: AbiDecoder;
- private _web3: Web3;
+ private _provider: Provider;
private _txDefaults: Partial<TxData>;
private _jsonRpcRequestId: number;
/**
@@ -66,6 +74,8 @@ export class Web3Wrapper {
* @return The amount in units.
*/
public static toUnitAmount(amount: BigNumber, decimals: number): BigNumber {
+ assert.isValidBaseUnitAmount('amount', amount);
+ assert.isNumber('decimals', decimals);
const aUnit = new BigNumber(BASE_TEN).pow(decimals);
const unit = amount.div(aUnit);
return unit;
@@ -79,6 +89,8 @@ export class Web3Wrapper {
* @return The amount in baseUnits.
*/
public static toBaseUnitAmount(amount: BigNumber, decimals: number): BigNumber {
+ assert.isBigNumber('amount', amount);
+ assert.isNumber('decimals', decimals);
const unit = new BigNumber(BASE_TEN).pow(decimals);
const baseUnitAmount = amount.times(unit);
const hasDecimals = baseUnitAmount.decimalPlaces() !== 0;
@@ -93,10 +105,44 @@ export class Web3Wrapper {
* @returns Amount in wei
*/
public static toWei(ethAmount: BigNumber): BigNumber {
+ assert.isBigNumber('ethAmount', ethAmount);
const ETH_DECIMALS = 18;
const balanceWei = Web3Wrapper.toBaseUnitAmount(ethAmount, ETH_DECIMALS);
return balanceWei;
}
+ private static _assertBlockParam(blockParam: string | BlockParam): void {
+ if (_.isNumber(blockParam)) {
+ return;
+ } else if (_.isString(blockParam)) {
+ assert.doesBelongToStringEnum('blockParam', blockParam, BlockParamLiteral);
+ }
+ }
+ private static _assertBlockParamOrString(blockParam: string | BlockParam): void {
+ try {
+ Web3Wrapper._assertBlockParam(blockParam);
+ } catch (err) {
+ try {
+ assert.isHexString('blockParam', blockParam as string);
+ return;
+ } catch (err) {
+ throw new Error(`Expected blockParam to be of type "string | BlockParam", encountered ${blockParam}`);
+ }
+ }
+ }
+ private static _normalizeTxReceiptStatus(status: undefined | null | string | 0 | 1): null | 0 | 1 {
+ // Transaction status might have four values
+ // undefined - Testrpc and other old clients
+ // null - New clients on old transactions
+ // number - Parity
+ // hex - Geth
+ if (_.isString(status)) {
+ return web3Utils.toDecimal(status) as 0 | 1;
+ } else if (_.isUndefined(status)) {
+ return null;
+ } else {
+ return status;
+ }
+ }
/**
* Instantiates a new Web3Wrapper.
* @param provider The Web3 provider instance you would like the Web3Wrapper to use for interacting with
@@ -105,6 +151,7 @@ export class Web3Wrapper {
* @return An instance of the Web3Wrapper class.
*/
constructor(provider: Provider, txDefaults?: Partial<TxData>) {
+ assert.isWeb3Provider('provider', provider);
if (_.isUndefined((provider as any).sendAsync)) {
// Web3@1.0 provider doesn't support synchronous http requests,
// so it only has an async `send` method, instead of a `send` and `sendAsync` in web3@0.x.x`
@@ -112,8 +159,7 @@ export class Web3Wrapper {
(provider as any).sendAsync = (provider as any).send;
}
this.abiDecoder = new AbiDecoder([]);
- this._web3 = new Web3();
- this._web3.setProvider(provider);
+ this._provider = provider;
this._txDefaults = txDefaults || {};
this._jsonRpcRequestId = 0;
}
@@ -129,14 +175,15 @@ export class Web3Wrapper {
* @return Web3 provider instance
*/
public getProvider(): Provider {
- return this._web3.currentProvider;
+ return this._provider;
}
/**
* Update the used Web3 provider
* @param provider The new Web3 provider to be set
*/
public setProvider(provider: Provider): void {
- this._web3.setProvider(provider);
+ assert.isWeb3Provider('provider', provider);
+ this._provider = provider;
}
/**
* Check whether an address is available through the backing provider. This can be
@@ -146,6 +193,7 @@ export class Web3Wrapper {
* @returns Whether the address is available through the provider.
*/
public async isSenderAddressAvailableAsync(senderAddress: string): Promise<boolean> {
+ assert.isETHAddressHex('senderAddress', senderAddress);
const addresses = await this.getAvailableAddressesAsync();
const normalizedAddress = senderAddress.toLowerCase();
return _.includes(addresses, normalizedAddress);
@@ -173,9 +221,13 @@ export class Web3Wrapper {
* @returns The transaction receipt, including it's status (0: failed, 1: succeeded or undefined: not found)
*/
public async getTransactionReceiptAsync(txHash: string): Promise<TransactionReceipt> {
- const transactionReceipt = await promisify<TransactionReceipt>(this._web3.eth.getTransactionReceipt)(txHash);
+ assert.isHexString('txHash', txHash);
+ const transactionReceipt = await this._sendRawPayloadAsync<TransactionReceipt>({
+ method: 'eth_getTransactionReceipt',
+ params: [txHash],
+ });
if (!_.isNull(transactionReceipt)) {
- transactionReceipt.status = this._normalizeTxReceiptStatus(transactionReceipt.status);
+ transactionReceipt.status = Web3Wrapper._normalizeTxReceiptStatus(transactionReceipt.status);
}
return transactionReceipt;
}
@@ -185,10 +237,13 @@ export class Web3Wrapper {
* @returns Balance in wei
*/
public async getBalanceInWeiAsync(owner: string): Promise<BigNumber> {
- let balanceInWei = await promisify<BigNumber>(this._web3.eth.getBalance)(owner);
+ assert.isETHAddressHex('owner', owner);
+ const balanceInWei = await this._sendRawPayloadAsync<string>({
+ method: 'eth_getBalance',
+ params: [owner],
+ });
// Rewrap in a new BigNumber
- balanceInWei = new BigNumber(balanceInWei);
- return balanceInWei;
+ return new BigNumber(balanceInWei);
}
/**
* Check if a contract exists at a given address
@@ -196,6 +251,7 @@ export class Web3Wrapper {
* @returns Whether or not contract code was found at the supplied address
*/
public async doesContractExistAtAddressAsync(address: string): Promise<boolean> {
+ assert.isETHAddressHex('address', address);
const code = await this.getContractCodeAsync(address);
// Regex matches 0x0, 0x00, 0x in order to accommodate poorly implemented clients
const isCodeEmpty = /^0x0{0,40}$/i.test(code);
@@ -206,8 +262,17 @@ export class Web3Wrapper {
* @param address Address of the contract
* @return Code of the contract
*/
- public async getContractCodeAsync(address: string): Promise<string> {
- const code = await promisify<string>(this._web3.eth.getCode)(address);
+ public async getContractCodeAsync(address: string, defaultBlock?: BlockParam): Promise<string> {
+ assert.isETHAddressHex('address', address);
+ if (!_.isUndefined(defaultBlock)) {
+ Web3Wrapper._assertBlockParam(defaultBlock);
+ }
+ const marshalledDefaultBlock = marshaller.marshalBlockParam(defaultBlock);
+ const encodedAddress = marshaller.marshalAddress(address);
+ const code = await this._sendRawPayloadAsync<string>({
+ method: 'eth_getCode',
+ params: [encodedAddress, marshalledDefaultBlock],
+ });
return code;
}
/**
@@ -217,6 +282,7 @@ export class Web3Wrapper {
* @return Transaction trace
*/
public async getTransactionTraceAsync(txHash: string, traceParams: TraceParams): Promise<TransactionTrace> {
+ assert.isHexString('txHash', txHash);
const trace = await this._sendRawPayloadAsync<TransactionTrace>({
method: 'debug_traceTransaction',
params: [txHash, traceParams],
@@ -230,7 +296,13 @@ export class Web3Wrapper {
* @returns Signature string (might be VRS or RSV depending on the Signer)
*/
public async signMessageAsync(address: string, message: string): Promise<string> {
- const signData = await promisify<string>(this._web3.eth.sign)(address, message);
+ assert.isETHAddressHex('address', address);
+ assert.isETHAddressHex('address', address);
+ assert.isString('message', message); // TODO: Should this be stricter? Hex string?
+ const signData = await this._sendRawPayloadAsync<string>({
+ method: 'eth_sign',
+ params: [address, message],
+ });
return signData;
}
/**
@@ -238,8 +310,12 @@ export class Web3Wrapper {
* @returns Block number
*/
public async getBlockNumberAsync(): Promise<number> {
- const blockNumber = await promisify<number>(this._web3.eth.getBlockNumber)();
- return blockNumber;
+ const blockNumberHex = await this._sendRawPayloadAsync<string>({
+ method: 'eth_blockNumber',
+ params: [],
+ });
+ const blockNumber = marshaller.convertHexToNumberOrNull(blockNumberHex);
+ return blockNumber as number;
}
/**
* Fetch a specific Ethereum block without transaction data
@@ -247,10 +323,18 @@ export class Web3Wrapper {
* @returns The requested block without transaction data
*/
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 shouldIncludeTransactionData = false;
- const blockWithoutTransactionData = await promisify<BlockWithoutTransactionData>(this._web3.eth.getBlock)(
- blockParam,
- shouldIncludeTransactionData,
+ const blockWithoutTransactionDataWithHexValues = await this._sendRawPayloadAsync<
+ BlockWithoutTransactionDataRPC
+ >({
+ method,
+ params: [encodedBlockParam, shouldIncludeTransactionData],
+ });
+ const blockWithoutTransactionData = marshaller.unmarshalIntoBlockWithoutTransactionData(
+ blockWithoutTransactionDataWithHexValues,
);
return blockWithoutTransactionData;
}
@@ -260,12 +344,21 @@ export class Web3Wrapper {
* @returns The requested block with transaction data
*/
public async getBlockWithTransactionDataAsync(blockParam: string | BlockParam): Promise<BlockWithTransactionData> {
+ Web3Wrapper._assertBlockParamOrString(blockParam);
+ let encodedBlockParam = blockParam;
+ if (_.isNumber(blockParam)) {
+ encodedBlockParam = web3Utils.toHex(blockParam);
+ }
+ const method = web3Utils.isHexStrict(blockParam) ? 'eth_getBlockByHash' : 'eth_getBlockByNumber';
const shouldIncludeTransactionData = true;
- const blockWithTransactionData = await promisify<BlockWithTransactionData>(this._web3.eth.getBlock)(
- blockParam,
- shouldIncludeTransactionData,
+ const blockWithTransactionDataWithHexValues = await this._sendRawPayloadAsync<BlockWithTransactionDataRPC>({
+ method,
+ params: [encodedBlockParam, shouldIncludeTransactionData],
+ });
+ const blockWithoutTransactionData = marshaller.unmarshalIntoBlockWithTransactionData(
+ blockWithTransactionDataWithHexValues,
);
- return blockWithTransactionData;
+ return blockWithoutTransactionData;
}
/**
* Fetch a block's timestamp
@@ -273,6 +366,7 @@ export class Web3Wrapper {
* @returns The block's timestamp
*/
public async getBlockTimestampAsync(blockParam: string | BlockParam): Promise<number> {
+ Web3Wrapper._assertBlockParamOrString(blockParam);
const { timestamp } = await this.getBlockAsync(blockParam);
return timestamp;
}
@@ -281,7 +375,10 @@ export class Web3Wrapper {
* @returns Available user addresses
*/
public async getAvailableAddressesAsync(): Promise<string[]> {
- const addresses = await promisify<string[]>(this._web3.eth.getAccounts)();
+ const addresses = await this._sendRawPayloadAsync<string>({
+ method: 'eth_accounts',
+ params: [],
+ });
const normalizedAddresses = _.map(addresses, address => address.toLowerCase());
return normalizedAddresses;
}
@@ -299,6 +396,7 @@ export class Web3Wrapper {
* @returns Whether the revert was successful
*/
public async revertSnapshotAsync(snapshotId: number): Promise<boolean> {
+ assert.isNumber('snapshotId', snapshotId);
const didRevert = await this._sendRawPayloadAsync<boolean>({ method: 'evm_revert', params: [snapshotId] });
return didRevert;
}
@@ -314,6 +412,7 @@ export class Web3Wrapper {
* @param timeDelta Amount of time to add in seconds
*/
public async increaseTimeAsync(timeDelta: number): Promise<number> {
+ assert.isNumber('timeDelta', timeDelta);
// Detect Geth vs. Ganache and use appropriate endpoint.
const version = await this.getNodeVersionAsync();
if (_.includes(version, uniqueVersionIds.geth)) {
@@ -332,11 +431,11 @@ export class Web3Wrapper {
public async getLogsAsync(filter: FilterObject): Promise<LogEntry[]> {
let fromBlock = filter.fromBlock;
if (_.isNumber(fromBlock)) {
- fromBlock = this._web3.toHex(fromBlock);
+ fromBlock = web3Utils.toHex(fromBlock);
}
let toBlock = filter.toBlock;
if (_.isNumber(toBlock)) {
- toBlock = this._web3.toHex(toBlock);
+ toBlock = web3Utils.toHex(toBlock);
}
const serializedFilter = {
...filter,
@@ -344,30 +443,22 @@ export class Web3Wrapper {
toBlock,
};
const payload = {
- jsonrpc: '2.0',
method: 'eth_getLogs',
params: [serializedFilter],
};
const rawLogs = await this._sendRawPayloadAsync<RawLogEntry[]>(payload);
- const formattedLogs = _.map(rawLogs, this._formatLog.bind(this));
+ const formattedLogs = _.map(rawLogs, marshaller.unmarshalLog.bind(marshaller));
return formattedLogs;
}
/**
- * Get a Web3 contract factory instance for a given ABI
- * @param abi Smart contract ABI
- * @returns Web3 contract factory which can create Web3 Contract instances from the supplied ABI
- */
- public getContractFromAbi(abi: ContractAbi): Web3.Contract<any> {
- const web3Contract = this._web3.eth.contract(abi);
- return web3Contract;
- }
- /**
* Calculate the estimated gas cost for a given transaction
* @param txData Transaction data
* @returns Estimated gas cost
*/
public async estimateGasAsync(txData: Partial<TxData>): Promise<number> {
- const gas = await promisify<number>(this._web3.eth.estimateGas)(txData);
+ const txDataHex = marshaller.marshalTxData(txData);
+ const gasHex = await this._sendRawPayloadAsync<string>({ method: 'eth_estimateGas', params: [txDataHex] });
+ const gas = web3Utils.toDecimal(gasHex);
return gas;
}
/**
@@ -377,7 +468,15 @@ export class Web3Wrapper {
* @returns The raw call result
*/
public async callAsync(callData: CallData, defaultBlock?: BlockParam): Promise<string> {
- const rawCallResult = await promisify<string>(this._web3.eth.call)(callData, defaultBlock);
+ if (!_.isUndefined(defaultBlock)) {
+ Web3Wrapper._assertBlockParam(defaultBlock);
+ }
+ const marshalledDefaultBlock = marshaller.marshalBlockParam(defaultBlock);
+ const callDataHex = marshaller.marshalCallData(callData);
+ const rawCallResult = await this._sendRawPayloadAsync<string>({
+ method: 'eth_call',
+ params: [callDataHex, marshalledDefaultBlock],
+ });
if (rawCallResult === '0x') {
throw new Error('Contract call failed (returned null)');
}
@@ -389,7 +488,8 @@ export class Web3Wrapper {
* @returns Transaction hash
*/
public async sendTransactionAsync(txData: TxData): Promise<string> {
- const txHash = await promisify<string>(this._web3.eth.sendTransaction)(txData);
+ const txDataHex = marshaller.marshalTxData(txData);
+ const txHash = await this._sendRawPayloadAsync<string>({ method: 'eth_sendTransaction', params: [txDataHex] });
return txHash;
}
/**
@@ -408,6 +508,11 @@ export class Web3Wrapper {
pollingIntervalMs: number = 1000,
timeoutMs?: number,
): Promise<TransactionReceiptWithDecodedLogs> {
+ assert.isHexString('txHash', txHash);
+ assert.isNumber('pollingIntervalMs', pollingIntervalMs);
+ if (!_.isUndefined(timeoutMs)) {
+ assert.isNumber('timeoutMs', timeoutMs);
+ }
// Immediately check if the transaction has already been mined.
let transactionReceipt = await this.getTransactionReceiptAsync(txHash);
if (!_.isNull(transactionReceipt)) {
@@ -493,7 +598,8 @@ export class Web3Wrapper {
* @param blockNumber The block number to reset to.
*/
public async setHeadAsync(blockNumber: number): Promise<void> {
- await this._sendRawPayloadAsync<void>({ method: 'debug_setHead', params: [this._web3.toHex(blockNumber)] });
+ assert.isNumber('blockNumber', blockNumber);
+ await this._sendRawPayloadAsync<void>({ method: 'debug_setHead', params: [web3Utils.toHex(blockNumber)] });
}
/**
* Returns either NodeType.Geth or NodeType.Ganache depending on the type of
@@ -510,7 +616,7 @@ export class Web3Wrapper {
}
}
private async _sendRawPayloadAsync<A>(payload: Partial<JSONRPCRequestPayload>): Promise<A> {
- const sendAsync = this._web3.currentProvider.sendAsync.bind(this._web3.currentProvider);
+ const sendAsync = this._provider.sendAsync.bind(this._provider);
const payloadWithDefaults = {
id: this._jsonRpcRequestId++,
params: [],
@@ -521,34 +627,4 @@ export class Web3Wrapper {
const result = response.result;
return result;
}
- private _normalizeTxReceiptStatus(status: undefined | null | string | 0 | 1): null | 0 | 1 {
- // Transaction status might have four values
- // undefined - Testrpc and other old clients
- // null - New clients on old transactions
- // number - Parity
- // hex - Geth
- if (_.isString(status)) {
- return this._web3.toDecimal(status) as 0 | 1;
- } else if (_.isUndefined(status)) {
- return null;
- } else {
- return status;
- }
- }
- private _formatLog(rawLog: RawLogEntry): LogEntry {
- const formattedLog = {
- ...rawLog,
- logIndex: this._hexToDecimal(rawLog.logIndex),
- blockNumber: this._hexToDecimal(rawLog.blockNumber),
- transactionIndex: this._hexToDecimal(rawLog.transactionIndex),
- };
- return formattedLog;
- }
- private _hexToDecimal(hex: string | null): number | null {
- if (_.isNull(hex)) {
- return null;
- }
- const decimal = this._web3.toDecimal(hex);
- return decimal;
- }
} // tslint:disable-line:max-file-line-count
diff --git a/packages/web3-wrapper/test/web3_wrapper_test.ts b/packages/web3-wrapper/test/web3_wrapper_test.ts
index 35eab3aa2..b4fd8bb44 100644
--- a/packages/web3-wrapper/test/web3_wrapper_test.ts
+++ b/packages/web3-wrapper/test/web3_wrapper_test.ts
@@ -1,18 +1,27 @@
import * as chai from 'chai';
+import { BlockParamLiteral } from 'ethereum-types';
import * as Ganache from 'ganache-core';
+import * as _ from 'lodash';
import 'mocha';
-import { Web3Wrapper } from '../src';
+import { utils } from '../src/utils';
+import { Web3Wrapper } from '../src/web3_wrapper';
import { chaiSetup } from './utils/chai_setup';
chaiSetup.configure();
const { expect } = chai;
+const NUM_GANACHE_ADDRESSES = 10;
+
describe('Web3Wrapper tests', () => {
const NETWORK_ID = 50;
const provider = Ganache.provider({ network_id: NETWORK_ID });
const web3Wrapper = new Web3Wrapper(provider);
+ let addresses: string[];
+ before(async () => {
+ addresses = await web3Wrapper.getAvailableAddressesAsync();
+ });
describe('#isAddress', () => {
it('correctly checks if a string is a valid ethereum address', () => {
expect(Web3Wrapper.isAddress('0x0')).to.be.false();
@@ -36,4 +45,88 @@ describe('Web3Wrapper tests', () => {
expect(networkId).to.be.equal(NETWORK_ID);
});
});
+ describe('#getNetworkIdAsync', () => {
+ it('gets the network id', async () => {
+ const networkId = await web3Wrapper.getNetworkIdAsync();
+ expect(networkId).to.be.equal(NETWORK_ID);
+ });
+ });
+ describe('#getAvailableAddressesAsync', () => {
+ it('gets the available addresses', async () => {
+ const availableAddresses = await web3Wrapper.getAvailableAddressesAsync();
+ expect(availableAddresses.length).to.be.equal(NUM_GANACHE_ADDRESSES);
+ expect(Web3Wrapper.isAddress(availableAddresses[0])).to.equal(true);
+ });
+ });
+ describe('#getBalanceInWeiAsync', () => {
+ it('gets the users balance in wei', async () => {
+ const secondAccount = addresses[1];
+ const balanceInWei = await web3Wrapper.getBalanceInWeiAsync(secondAccount);
+ const tenEthInWei = 100000000000000000000;
+ expect(balanceInWei).to.be.bignumber.equal(tenEthInWei);
+ });
+ it('should throw if supplied owner not an Ethereum address hex string', async () => {
+ const invalidEthAddress = 'deadbeef';
+ expect(web3Wrapper.getBalanceInWeiAsync(invalidEthAddress)).to.eventually.to.be.rejected();
+ });
+ });
+ describe('#signMessageAsync', () => {
+ it('should sign message', async () => {
+ const message = '0xdeadbeef';
+ const signer = addresses[1];
+ const signature = await web3Wrapper.signMessageAsync(signer, message);
+ const signatureLength = 132;
+ expect(signature.length).to.be.equal(signatureLength);
+ });
+ });
+ describe('#getBlockNumberAsync', () => {
+ it('get block number', async () => {
+ const blockNumber = await web3Wrapper.getBlockNumberAsync();
+ expect(typeof blockNumber).to.be.equal('number');
+ });
+ });
+ describe('#getBlockAsync', () => {
+ it('gets block when supplied a valid BlockParamLiteral value', async () => {
+ const blockParamLiteral = BlockParamLiteral.Earliest;
+ const block = await web3Wrapper.getBlockAsync(blockParamLiteral);
+ expect(block.number).to.be.equal(0);
+ expect(utils.isBigNumber(block.difficulty)).to.equal(true);
+ expect(_.isNumber(block.gasLimit)).to.equal(true);
+ });
+ it('gets block when supplied a block number', async () => {
+ const blockParamLiteral = 0;
+ const block = await web3Wrapper.getBlockAsync(blockParamLiteral);
+ expect(block.number).to.be.equal(0);
+ });
+ it('gets block when supplied a block hash', async () => {
+ const blockParamLiteral = 0;
+ const block = await web3Wrapper.getBlockAsync(blockParamLiteral);
+ const sameBlock = await web3Wrapper.getBlockAsync(block.hash as string);
+ expect(sameBlock.number).to.be.equal(0);
+ });
+ it('should throw if supplied invalid blockParam value', async () => {
+ const invalidBlockParam = 'deadbeef';
+ expect(web3Wrapper.getBlockAsync(invalidBlockParam)).to.eventually.to.be.rejected();
+ });
+ });
+ describe('#getBlockWithTransactionDataAsync', () => {
+ it('gets block when supplied a valid BlockParamLiteral value', async () => {
+ const blockParamLiteral = BlockParamLiteral.Earliest;
+ const block = await web3Wrapper.getBlockWithTransactionDataAsync(blockParamLiteral);
+ expect(block.number).to.be.equal(0);
+ expect(utils.isBigNumber(block.difficulty)).to.equal(true);
+ expect(_.isNumber(block.gasLimit)).to.equal(true);
+ });
+ it('should throw if supplied invalid blockParam value', async () => {
+ const invalidBlockParam = 'deadbeef';
+ expect(web3Wrapper.getBlockWithTransactionDataAsync(invalidBlockParam)).to.eventually.to.be.rejected();
+ });
+ });
+ describe('#getBlockTimestampAsync', () => {
+ it('gets block timestamp', async () => {
+ const blockParamLiteral = BlockParamLiteral.Earliest;
+ const timestamp = await web3Wrapper.getBlockTimestampAsync(blockParamLiteral);
+ expect(_.isNumber(timestamp)).to.be.equal(true);
+ });
+ });
});