diff options
author | Fabio Berger <me@fabioberger.com> | 2018-06-07 18:21:44 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2018-06-07 18:21:44 +0800 |
commit | 10478a6b2fd1f2a01597a88afde9cf582640a849 (patch) | |
tree | 3ebbb29656cf59b2547ec3c5a3c5b90ee6264483 /packages/web3-wrapper/src/web3_wrapper.ts | |
parent | cf8fdd3a701f21bfc3b2ec8397fa65948f5cdc78 (diff) | |
parent | e0bc01eea1c20e0afda296f331c6a475e062b59c (diff) | |
download | dexon-0x-contracts-10478a6b2fd1f2a01597a88afde9cf582640a849.tar.gz dexon-0x-contracts-10478a6b2fd1f2a01597a88afde9cf582640a849.tar.zst dexon-0x-contracts-10478a6b2fd1f2a01597a88afde9cf582640a849.zip |
Merge branch 'v2-prototype' into refactor/move-spawn-switch-to-utils
* v2-prototype: (66 commits)
Run prettier
Remove unused variable
Fix linting issues
Change shouldRenderHeader prop to shouldHideHeader
Get build and tests to pass
typo
Apply prettier
Update contracts tests after rebase
Apply various fixes based on PR feedback
Document debug_increaseTime method and fix typo in devnet README
Use an enum for ProviderType in contracts/src/utils/web3_wrapper
Update contracts package README
Update relevant changelogs
Remove global gas estimate buffer
Add Async suffix to relevant assertions
Fix linter errors
Update package.json and yarn.lock
Update more things to work with both Geth and Ganache
Small fixes and cleanup
Add additional gas to calls to fillOrderNoThrow
...
# Conflicts:
# packages/order-watcher/src/order_watcher/order_watcher.ts
# packages/react-docs/src/components/type.tsx
# packages/website/ts/components/ui/lifecycle_raised_button.tsx
# packages/website/ts/components/wallet/wallet.tsx
Diffstat (limited to 'packages/web3-wrapper/src/web3_wrapper.ts')
-rw-r--r-- | packages/web3-wrapper/src/web3_wrapper.ts | 38 |
1 files changed, 34 insertions, 4 deletions
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; |