diff options
author | Alex Browne <stephenalexbrowne@gmail.com> | 2018-06-07 04:43:29 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-07 04:43:29 +0800 |
commit | 785b9811f30869f01242ce9ff81c282bf7f5352f (patch) | |
tree | fe1615f0db90f76627bfb88874c19634321105bc /packages/web3-wrapper | |
parent | da3f783a9ff69b059b1a98f502d980660d6bacab (diff) | |
parent | 643c77ded08d3082aff7ae47063d40c9c1fdb677 (diff) | |
download | dexon-sol-tools-785b9811f30869f01242ce9ff81c282bf7f5352f.tar.gz dexon-sol-tools-785b9811f30869f01242ce9ff81c282bf7f5352f.tar.zst dexon-sol-tools-785b9811f30869f01242ce9ff81c282bf7f5352f.zip |
Merge pull request #622 from 0xProject/geth-devnet-rebase-on-v2
Run contract tests against private Geth network
Diffstat (limited to 'packages/web3-wrapper')
-rw-r--r-- | packages/web3-wrapper/CHANGELOG.json | 21 | ||||
-rw-r--r-- | packages/web3-wrapper/src/index.ts | 2 | ||||
-rw-r--r-- | packages/web3-wrapper/src/web3_wrapper.ts | 38 |
3 files changed, 56 insertions, 5 deletions
diff --git a/packages/web3-wrapper/CHANGELOG.json b/packages/web3-wrapper/CHANGELOG.json index b753ffbac..54a816e23 100644 --- a/packages/web3-wrapper/CHANGELOG.json +++ b/packages/web3-wrapper/CHANGELOG.json @@ -1,5 +1,26 @@ [ { + "version": "0.7.0", + "changes": [ + { + "note": "Add exported uniqueVersionIds object", + "pr": 622 + }, + { + "note": "Update increaseTimeAsync to work with Geth", + "pr": 622 + }, + { + "note": "Make callAsync throw if raw call result is 0x (null)", + "pr": 622 + }, + { + "note": "Add new setHeadAsync method", + "pr": 622 + } + ] + }, + { "timestamp": 1527009133, "version": "0.6.4", "changes": [ diff --git a/packages/web3-wrapper/src/index.ts b/packages/web3-wrapper/src/index.ts index 7309e09a8..b14fa7406 100644 --- a/packages/web3-wrapper/src/index.ts +++ b/packages/web3-wrapper/src/index.ts @@ -1,2 +1,2 @@ -export { Web3Wrapper } from './web3_wrapper'; +export { Web3Wrapper, uniqueVersionIds } from './web3_wrapper'; export { Web3WrapperErrors } from './types'; diff --git a/packages/web3-wrapper/src/web3_wrapper.ts b/packages/web3-wrapper/src/web3_wrapper.ts index 3de152df1..559bf3ea9 100644 --- a/packages/web3-wrapper/src/web3_wrapper.ts +++ b/packages/web3-wrapper/src/web3_wrapper.ts @@ -21,6 +21,13 @@ import { Web3WrapperErrors } from './types'; const BASE_TEN = 10; +// These are unique identifiers contained in the response of the +// web3_clientVersion call. +export const uniqueVersionIds = { + geth: 'Geth', + ganache: 'EthereumJS TestRPC', +}; + /** * A wrapper around the Web3.js 0.x library that provides a consistent, clean promise-based interface. */ @@ -254,11 +261,20 @@ export class Web3Wrapper { await this._sendRawPayloadAsync<string>({ method: 'evm_mine', params: [] }); } /** - * Increase the next blocks timestamp on TestRPC/Ganache local node + * Increase the next blocks timestamp on TestRPC/Ganache or Geth local node. + * Will throw if provider is neither TestRPC/Ganache or Geth. * @param timeDelta Amount of time to add in seconds */ - public async increaseTimeAsync(timeDelta: number): Promise<void> { - await this._sendRawPayloadAsync<string>({ method: 'evm_increaseTime', params: [timeDelta] }); + public async increaseTimeAsync(timeDelta: number): Promise<number> { + // Detect Geth vs. Ganache and use appropriate endpoint. + const version = await this.getNodeVersionAsync(); + if (_.includes(version, uniqueVersionIds.geth)) { + return this._sendRawPayloadAsync<number>({ method: 'debug_increaseTime', params: [timeDelta] }); + } else if (_.includes(version, uniqueVersionIds.ganache)) { + return this._sendRawPayloadAsync<number>({ method: 'evm_increaseTime', params: [timeDelta] }); + } else { + throw new Error(`Unknown client version: ${version}`); + } } /** * Retrieve smart contract logs for a given filter @@ -281,7 +297,6 @@ export class Web3Wrapper { }; const payload = { jsonrpc: '2.0', - id: this._jsonRpcRequestId++, method: 'eth_getLogs', params: [serializedFilter], }; @@ -315,6 +330,9 @@ export class Web3Wrapper { */ public async callAsync(callData: CallData, defaultBlock?: BlockParam): Promise<string> { const rawCallResult = await promisify<string>(this._web3.eth.call)(callData, defaultBlock); + if (rawCallResult === '0x') { + throw new Error('Contract call failed (returned null)'); + } return rawCallResult; } /** @@ -403,8 +421,20 @@ export class Web3Wrapper { } return receipt; } + /** + * Calls the 'debug_setHead' JSON RPC method, which sets the current head of + * the local chain by block number. Note, this is a destructive action and + * may severely damage your chain. Use with extreme caution. As of now, this + * is only supported by Geth. It sill throw if the 'debug_setHead' method is + * not supported. + * @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)] }); + } private async _sendRawPayloadAsync<A>(payload: Partial<JSONRPCRequestPayload>): Promise<A> { const sendAsync = this._web3.currentProvider.sendAsync.bind(this._web3.currentProvider); + payload.id = this._jsonRpcRequestId++; const response = await promisify<JSONRPCResponsePayload>(sendAsync)(payload); const result = response.result; return result; |