aboutsummaryrefslogtreecommitdiffstats
path: root/packages/web3-wrapper
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2018-06-07 18:21:44 +0800
committerFabio Berger <me@fabioberger.com>2018-06-07 18:21:44 +0800
commit10478a6b2fd1f2a01597a88afde9cf582640a849 (patch)
tree3ebbb29656cf59b2547ec3c5a3c5b90ee6264483 /packages/web3-wrapper
parentcf8fdd3a701f21bfc3b2ec8397fa65948f5cdc78 (diff)
parente0bc01eea1c20e0afda296f331c6a475e062b59c (diff)
downloaddexon-sol-tools-10478a6b2fd1f2a01597a88afde9cf582640a849.tar
dexon-sol-tools-10478a6b2fd1f2a01597a88afde9cf582640a849.tar.gz
dexon-sol-tools-10478a6b2fd1f2a01597a88afde9cf582640a849.tar.bz2
dexon-sol-tools-10478a6b2fd1f2a01597a88afde9cf582640a849.tar.lz
dexon-sol-tools-10478a6b2fd1f2a01597a88afde9cf582640a849.tar.xz
dexon-sol-tools-10478a6b2fd1f2a01597a88afde9cf582640a849.tar.zst
dexon-sol-tools-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')
-rw-r--r--packages/web3-wrapper/CHANGELOG.json21
-rw-r--r--packages/web3-wrapper/src/index.ts2
-rw-r--r--packages/web3-wrapper/src/web3_wrapper.ts38
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;