diff options
author | Alex Browne <stephenalexbrowne@gmail.com> | 2018-06-06 07:20:38 +0800 |
---|---|---|
committer | Alex Browne <stephenalexbrowne@gmail.com> | 2018-06-07 03:40:31 +0800 |
commit | d6d7f4e875b161aa7284467a61f67989f76ec89e (patch) | |
tree | d0287504809489cec96a9673ffac41429cf14cd7 | |
parent | 63caddea62453863de84a4b53e14fe3e61d3008f (diff) | |
download | dexon-sol-tools-d6d7f4e875b161aa7284467a61f67989f76ec89e.tar dexon-sol-tools-d6d7f4e875b161aa7284467a61f67989f76ec89e.tar.gz dexon-sol-tools-d6d7f4e875b161aa7284467a61f67989f76ec89e.tar.bz2 dexon-sol-tools-d6d7f4e875b161aa7284467a61f67989f76ec89e.tar.lz dexon-sol-tools-d6d7f4e875b161aa7284467a61f67989f76ec89e.tar.xz dexon-sol-tools-d6d7f4e875b161aa7284467a61f67989f76ec89e.tar.zst dexon-sol-tools-d6d7f4e875b161aa7284467a61f67989f76ec89e.zip |
Update more things to work with both Geth and Ganache
-rw-r--r-- | packages/contracts/src/utils/assertions.ts | 36 | ||||
-rw-r--r-- | packages/contracts/test/libraries/lib_bytes.ts | 29 | ||||
-rw-r--r-- | packages/contracts/test/unlimited_allowance_token.ts | 15 | ||||
-rw-r--r-- | packages/dev-utils/src/blockchain_lifecycle.ts | 11 | ||||
-rw-r--r-- | packages/web3-wrapper/src/index.ts | 2 | ||||
-rw-r--r-- | packages/web3-wrapper/src/web3_wrapper.ts | 21 |
6 files changed, 64 insertions, 50 deletions
diff --git a/packages/contracts/src/utils/assertions.ts b/packages/contracts/src/utils/assertions.ts index 1ea071d01..c08bc7271 100644 --- a/packages/contracts/src/utils/assertions.ts +++ b/packages/contracts/src/utils/assertions.ts @@ -5,38 +5,30 @@ import { constants } from './constants'; const expect = chai.expect; -// throws if the given promise does not reject with one of two expected error -// messages. -export function expectRevertOrAlwaysFailingTransaction<T>(p: Promise<T>): PromiseLike<void> { +function _expectEitherError<T>(p: Promise<T>, error1: string, error2: string): PromiseLike<void> { return expect(p) .to.be.rejected() .then(e => { expect(e).to.satisfy( - (err: Error) => - _.includes(err.message, constants.REVERT) || _.includes(err.message, 'always failing transaction'), + (err: Error) => _.includes(err.message, error1) || _.includes(err.message, error2), + `expected promise to reject with error message that includes "${error1}" or "${error2}", but got: ` + + `"${e.message}"\n`, ); }); } export function expectInsufficientFunds<T>(p: Promise<T>): PromiseLike<void> { - return expect(p) - .to.be.rejected() - .then(e => { - expect(e).to.satisfy( - (err: Error) => - _.includes(err.message, 'insufficient funds') || - _.includes(err.message, "sender doesn't have enough funds"), - ); - }); + return _expectEitherError(p, 'insufficient funds', "sender doesn't have enough funds"); +} + +export function expectRevertOrOtherError<T>(p: Promise<T>, otherError: string): PromiseLike<void> { + return _expectEitherError(p, constants.REVERT, otherError); +} + +export function expectRevertOrAlwaysFailingTransaction<T>(p: Promise<T>): PromiseLike<void> { + return expectRevertOrOtherError(p, 'always failing transaction'); } export function expectRevertOrContractCallFailed<T>(p: Promise<T>): PromiseLike<void> { - return expect(p) - .to.be.rejected() - .then(e => { - expect(e).to.satisfy( - (err: Error) => - _.includes(err.message, constants.REVERT) || _.includes(err.message, 'Contract call failed'), - ); - }); + return expectRevertOrOtherError<T>(p, 'Contract call failed'); } diff --git a/packages/contracts/test/libraries/lib_bytes.ts b/packages/contracts/test/libraries/lib_bytes.ts index 26802a60d..f5435a81e 100644 --- a/packages/contracts/test/libraries/lib_bytes.ts +++ b/packages/contracts/test/libraries/lib_bytes.ts @@ -10,7 +10,7 @@ import * as Web3 from 'web3'; import { TestLibBytesContract } from '../../src/contract_wrappers/generated/test_lib_bytes'; import { artifacts } from '../../src/utils/artifacts'; -import { expectRevertOrAlwaysFailingTransaction } from '../../src/utils/assertions'; +import { expectRevertOrAlwaysFailingTransaction, expectRevertOrOtherError } from '../../src/utils/assertions'; import { chaiSetup } from '../../src/utils/chai_setup'; import { constants } from '../../src/utils/constants'; import { provider, txDefaults, web3Wrapper } from '../../src/utils/web3_wrapper'; @@ -64,7 +64,8 @@ describe('LibBytes', () => { describe('popByte', () => { it('should revert if length is 0', async () => { - return expect(libBytes.publicPopByte.callAsync(constants.NULL_BYTES)).to.be.rejectedWith( + return expectRevertOrOtherError( + libBytes.publicPopByte.callAsync(constants.NULL_BYTES), constants.LIB_BYTES_GT_ZERO_LENGTH_REQUIRED, ); }); @@ -80,7 +81,8 @@ describe('LibBytes', () => { describe('popAddress', () => { it('should revert if length is less than 20', async () => { - return expect(libBytes.publicPopAddress.callAsync(byteArrayShorterThan20Bytes)).to.be.rejectedWith( + return expectRevertOrOtherError( + libBytes.publicPopAddress.callAsync(byteArrayShorterThan20Bytes), constants.LIB_BYTES_GTE_20_LENGTH_REQUIRED, ); }); @@ -165,7 +167,8 @@ describe('LibBytes', () => { it('should fail if the byte array is too short to hold an address)', async () => { const shortByteArray = '0xabcdef'; const offset = new BigNumber(0); - return expect(libBytes.publicReadAddress.callAsync(shortByteArray, offset)).to.be.rejectedWith( + return expectRevertOrOtherError( + libBytes.publicReadAddress.callAsync(shortByteArray, offset), constants.LIB_BYTES_GTE_20_LENGTH_REQUIRED, ); }); @@ -173,7 +176,8 @@ describe('LibBytes', () => { it('should fail if the length between the offset and end of the byte array is too short to hold an address)', async () => { const byteArray = ethUtil.addHexPrefix(testAddress); const badOffset = new BigNumber(ethUtil.toBuffer(byteArray).byteLength); - return expect(libBytes.publicReadAddress.callAsync(byteArray, badOffset)).to.be.rejectedWith( + return expectRevertOrOtherError( + libBytes.publicReadAddress.callAsync(byteArray, badOffset), constants.LIB_BYTES_GTE_20_LENGTH_REQUIRED, ); }); @@ -209,14 +213,16 @@ describe('LibBytes', () => { it('should fail if the byte array is too short to hold a bytes32)', async () => { const offset = new BigNumber(0); - return expect(libBytes.publicReadBytes32.callAsync(byteArrayShorterThan32Bytes, offset)).to.be.rejectedWith( + return expectRevertOrOtherError( + libBytes.publicReadBytes32.callAsync(byteArrayShorterThan32Bytes, offset), constants.LIB_BYTES_GTE_32_LENGTH_REQUIRED, ); }); it('should fail if the length between the offset and end of the byte array is too short to hold a bytes32)', async () => { const badOffset = new BigNumber(ethUtil.toBuffer(testBytes32).byteLength); - return expect(libBytes.publicReadBytes32.callAsync(testBytes32, badOffset)).to.be.rejectedWith( + return expectRevertOrOtherError( + libBytes.publicReadBytes32.callAsync(testBytes32, badOffset), constants.LIB_BYTES_GTE_32_LENGTH_REQUIRED, ); }); @@ -256,7 +262,8 @@ describe('LibBytes', () => { it('should fail if the byte array is too short to hold a uint256)', async () => { const offset = new BigNumber(0); - return expect(libBytes.publicReadUint256.callAsync(byteArrayShorterThan32Bytes, offset)).to.be.rejectedWith( + return expectRevertOrOtherError( + libBytes.publicReadUint256.callAsync(byteArrayShorterThan32Bytes, offset), constants.LIB_BYTES_GTE_32_LENGTH_REQUIRED, ); }); @@ -266,7 +273,8 @@ describe('LibBytes', () => { const testUint256AsBuffer = ethUtil.toBuffer(formattedTestUint256); const byteArray = ethUtil.bufferToHex(testUint256AsBuffer); const badOffset = new BigNumber(testUint256AsBuffer.byteLength); - return expect(libBytes.publicReadUint256.callAsync(byteArray, badOffset)).to.be.rejectedWith( + return expectRevertOrOtherError( + libBytes.publicReadUint256.callAsync(byteArray, badOffset), constants.LIB_BYTES_GTE_32_LENGTH_REQUIRED, ); }); @@ -287,7 +295,8 @@ describe('LibBytes', () => { // AssertionError: expected promise to be rejected with an error including 'revert' but it was fulfilled with '0x08c379a0' it('should revert if byte array has a length < 4', async () => { const byteArrayLessThan4Bytes = '0x010101'; - return expect(libBytes.publicReadFirst4.callAsync(byteArrayLessThan4Bytes)).to.be.rejectedWith( + return expectRevertOrOtherError( + libBytes.publicReadFirst4.callAsync(byteArrayLessThan4Bytes), constants.LIB_BYTES_GTE_4_LENGTH_REQUIRED, ); }); diff --git a/packages/contracts/test/unlimited_allowance_token.ts b/packages/contracts/test/unlimited_allowance_token.ts index ea20df040..a66d0055b 100644 --- a/packages/contracts/test/unlimited_allowance_token.ts +++ b/packages/contracts/test/unlimited_allowance_token.ts @@ -7,7 +7,7 @@ import * as Web3 from 'web3'; import { DummyERC20TokenContract } from '../src/contract_wrappers/generated/dummy_e_r_c20_token'; import { artifacts } from '../src/utils/artifacts'; -import { expectRevertOrAlwaysFailingTransaction } from '../src/utils/assertions'; +import { expectRevertOrAlwaysFailingTransaction, expectRevertOrOtherError } from '../src/utils/assertions'; import { chaiSetup } from '../src/utils/chai_setup'; import { constants } from '../src/utils/constants'; import { provider, txDefaults, web3Wrapper } from '../src/utils/web3_wrapper'; @@ -56,7 +56,8 @@ describe('UnlimitedAllowanceToken', () => { it('should throw if owner has insufficient balance', async () => { const ownerBalance = await token.balanceOf.callAsync(owner); const amountToTransfer = ownerBalance.plus(1); - return expect(token.transfer.callAsync(spender, amountToTransfer, { from: owner })).to.be.rejectedWith( + return expectRevertOrOtherError( + token.transfer.callAsync(spender, amountToTransfer, { from: owner }), constants.ERC20_INSUFFICIENT_BALANCE, ); }); @@ -94,11 +95,12 @@ describe('UnlimitedAllowanceToken', () => { await token.approve.sendTransactionAsync(spender, amountToTransfer, { from: owner }), constants.AWAIT_TRANSACTION_MINED_MS, ); - return expect( + return expectRevertOrOtherError( token.transferFrom.callAsync(owner, spender, amountToTransfer, { from: spender, }), - ).to.be.rejectedWith(constants.ERC20_INSUFFICIENT_BALANCE); + constants.ERC20_INSUFFICIENT_BALANCE, + ); }); it('should throw if spender has insufficient allowance', async () => { @@ -109,11 +111,12 @@ describe('UnlimitedAllowanceToken', () => { const isSpenderAllowanceInsufficient = spenderAllowance.cmp(amountToTransfer) < 0; expect(isSpenderAllowanceInsufficient).to.be.true(); - return expect( + return expectRevertOrOtherError( token.transferFrom.callAsync(owner, spender, amountToTransfer, { from: spender, }), - ).to.be.rejectedWith(constants.ERC20_INSUFFICIENT_ALLOWANCE); + constants.ERC20_INSUFFICIENT_ALLOWANCE, + ); }); it('should return true on a 0 value transfer', async () => { diff --git a/packages/dev-utils/src/blockchain_lifecycle.ts b/packages/dev-utils/src/blockchain_lifecycle.ts index 6e7957f10..8e4ad81c7 100644 --- a/packages/dev-utils/src/blockchain_lifecycle.ts +++ b/packages/dev-utils/src/blockchain_lifecycle.ts @@ -1,4 +1,4 @@ -import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import { uniqueVersionIds, Web3Wrapper } from '@0xproject/web3-wrapper'; import * as _ from 'lodash'; import * as Web3 from 'web3'; @@ -7,11 +7,6 @@ enum NodeType { Ganache = 'GANACHE', } -// These are unique identifiers contained in the response of the -// web3_clientVersion call. -const GETH_VERSION_ID = 'Geth'; -const GANACHE_VERSION_ID = 'EthereumJS TestRPC'; - export class BlockchainLifecycle { private _web3Wrapper: Web3Wrapper; private _snapshotIdsStack: number[]; @@ -54,9 +49,9 @@ export class BlockchainLifecycle { } private async _getNodeTypeAsync(): Promise<NodeType> { const version = await this._web3Wrapper.getNodeVersionAsync(); - if (_.includes(version, GETH_VERSION_ID)) { + if (_.includes(version, uniqueVersionIds.geth)) { return NodeType.Geth; - } else if (_.includes(version, GANACHE_VERSION_ID)) { + } else if (_.includes(version, uniqueVersionIds.ganache)) { return NodeType.Ganache; } else { throw new Error(`Unknown client version: ${version}`); 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 a19253449..6c9fa980e 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,12 +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<number> { - // TODO(albrow): Detect Geth vs. Ganache and use appropriate endpoint. - return this._sendRawPayloadAsync<number>({ method: 'debug_increaseTime', params: [timeDelta] }); + // 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 |