Refactor contracts tests to not use injected web3 instance

This commit is contained in:
Leonid Logvinov
2018-01-19 15:34:28 +01:00
parent f2b2b86786
commit 709026bf1a
37 changed files with 89 additions and 126 deletions

View File

@@ -1,9 +1,8 @@
import { ZeroEx, ZeroExError } from '0x.js';
import { BlockchainLifecycle } from '@0xproject/dev-utils';
import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils';
import { BigNumber, promisify } from '@0xproject/utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as chai from 'chai';
import * as Web3 from 'web3';
import { Artifacts } from '../util/artifacts';
import { constants } from '../util/constants';
@@ -15,10 +14,8 @@ const { EtherToken } = new Artifacts(artifacts);
chaiSetup.configure();
const expect = chai.expect;
// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle
// with type `any` to a variable of type `Web3`.
const web3: Web3 = (global as any).web3;
const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL);
const web3 = web3Factory.create();
const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL);
describe('EtherToken', () => {
const web3Wrapper = new Web3Wrapper(web3.currentProvider);

View File

@@ -1,10 +1,9 @@
import { ZeroEx } from '0x.js';
import { BlockchainLifecycle } from '@0xproject/dev-utils';
import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils';
import { BigNumber } from '@0xproject/utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as chai from 'chai';
import ethUtil = require('ethereumjs-util');
import * as Web3 from 'web3';
import { Artifacts } from '../../util/artifacts';
import { Balances } from '../../util/balances';
@@ -19,11 +18,8 @@ import { chaiSetup } from '../utils/chai_setup';
chaiSetup.configure();
const expect = chai.expect;
const { Exchange, TokenTransferProxy, DummyToken, TokenRegistry, MaliciousToken } = new Artifacts(artifacts);
// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle
// with type `any` to a variable of type `Web3`.
const web3: Web3 = (global as any).web3;
const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL);
const web3 = web3Factory.create();
const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL);
describe('Exchange', () => {
const web3Wrapper = new Web3Wrapper(web3.currentProvider);

View File

@@ -1,13 +1,11 @@
import { ZeroEx } from '0x.js';
import { BlockchainLifecycle } from '@0xproject/dev-utils';
import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils';
import { BigNumber } from '@0xproject/utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as chai from 'chai';
import ethUtil = require('ethereumjs-util');
import * as Web3 from 'web3';
import { Artifacts } from '../../util/artifacts';
import { constants } from '../../util/constants';
import { ExchangeWrapper } from '../../util/exchange_wrapper';
import { Order } from '../../util/order';
import { OrderFactory } from '../../util/order_factory';
@@ -17,10 +15,8 @@ chaiSetup.configure();
const expect = chai.expect;
const { Exchange, TokenRegistry } = new Artifacts(artifacts);
// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle
// with type `any` to a variable of type `Web3`.
const web3: Web3 = (global as any).web3;
const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL);
const web3 = web3Factory.create();
const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL);
describe('Exchange', () => {
const web3Wrapper = new Web3Wrapper(web3.currentProvider);

View File

@@ -1,10 +1,9 @@
import { ZeroEx } from '0x.js';
import { BlockchainLifecycle } from '@0xproject/dev-utils';
import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils';
import { BigNumber } from '@0xproject/utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as chai from 'chai';
import * as _ from 'lodash';
import * as Web3 from 'web3';
import { Artifacts } from '../../util/artifacts';
import { Balances } from '../../util/balances';
@@ -18,10 +17,8 @@ import { chaiSetup } from '../utils/chai_setup';
chaiSetup.configure();
const expect = chai.expect;
const { Exchange, TokenTransferProxy, DummyToken, TokenRegistry } = new Artifacts(artifacts);
// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle
// with type `any` to a variable of type `Web3`.
const web3: Web3 = (global as any).web3;
const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL);
const web3 = web3Factory.create();
const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL);
describe('Exchange', () => {
const web3Wrapper = new Web3Wrapper(web3.currentProvider);

View File

@@ -1,8 +1,7 @@
import { BlockchainLifecycle, RPC } from '@0xproject/dev-utils';
import { BigNumber, promisify } from '@0xproject/utils';
import { BlockchainLifecycle, devConstants, RPC, web3Factory } from '@0xproject/dev-utils';
import { BigNumber } from '@0xproject/utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as chai from 'chai';
import * as Web3 from 'web3';
import * as multiSigWalletJSON from '../../build/contracts/MultiSigWalletWithTimeLock.json';
import * as truffleConf from '../truffle.js';
@@ -19,10 +18,8 @@ const MULTI_SIG_ABI = (multiSigWalletJSON as any).abi;
chaiSetup.configure();
const expect = chai.expect;
// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle
// with type `any` to a variable of type `Web3`.
const web3: Web3 = (global as any).web3;
const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL);
const web3 = web3Factory.create();
const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL);
describe('MultiSigWalletWithTimeLock', () => {
const web3Wrapper = new Web3Wrapper(web3.currentProvider);
@@ -79,8 +76,9 @@ describe('MultiSigWalletWithTimeLock', () => {
it('should set confirmation time with enough confirmations', async () => {
const res = await multiSig.confirmTransaction(txId, { from: owners[1] });
expect(res.logs).to.have.length(2);
const blockNum = await promisify<number>(web3.eth.getBlockNumber)();
const blockInfo = await promisify<Web3.BlockWithoutTransactionData>(web3.eth.getBlock)(blockNum);
const blockNum = await web3Wrapper.getBlockNumberAsync();
const blockInfo = await web3Wrapper.getBlockAsync(blockNum);
const timestamp = new BigNumber(blockInfo.timestamp);
const confirmationTimeBigNum = new BigNumber(await multiSig.confirmationTimes.call(txId));

View File

@@ -1,7 +1,6 @@
import { BlockchainLifecycle } from '@0xproject/dev-utils';
import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as chai from 'chai';
import * as Web3 from 'web3';
import * as tokenTransferProxyJSON from '../../build/contracts/TokenTransferProxy.json';
import { Artifacts } from '../util/artifacts';
@@ -16,10 +15,8 @@ const PROXY_ABI = (tokenTransferProxyJSON as any).abi;
chaiSetup.configure();
const expect = chai.expect;
// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle
// with type `any` to a variable of type `Web3`.
const web3: Web3 = (global as any).web3;
const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL);
const web3 = web3Factory.create();
const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL);
describe('MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress', () => {
const web3Wrapper = new Web3Wrapper(web3.currentProvider);

View File

@@ -1,10 +1,9 @@
import { ZeroEx } from '0x.js';
import { BlockchainLifecycle } from '@0xproject/dev-utils';
import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as chai from 'chai';
import ethUtil = require('ethereumjs-util');
import * as _ from 'lodash';
import * as Web3 from 'web3';
import { Artifacts } from '../util/artifacts';
import { constants } from '../util/constants';
@@ -16,10 +15,8 @@ import { chaiSetup } from './utils/chai_setup';
const { TokenRegistry } = new Artifacts(artifacts);
chaiSetup.configure();
const expect = chai.expect;
// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle
// with type `any` to a variable of type `Web3`.
const web3: Web3 = (global as any).web3;
const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL);
const web3 = web3Factory.create();
const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL);
describe('TokenRegistry', () => {
const web3Wrapper = new Web3Wrapper(web3.currentProvider);

View File

@@ -1,7 +1,6 @@
import { BlockchainLifecycle } from '@0xproject/dev-utils';
import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as chai from 'chai';
import * as Web3 from 'web3';
import { constants } from '../../util/constants';
import { ContractInstance } from '../../util/types';
@@ -10,10 +9,8 @@ import { chaiSetup } from '../utils/chai_setup';
chaiSetup.configure();
const expect = chai.expect;
const TokenTransferProxy = artifacts.require('./db/TokenTransferProxy.sol');
// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle
// with type `any` to a variable of type `Web3`.
const web3: Web3 = (global as any).web3;
const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL);
const web3 = web3Factory.create();
const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL);
describe('TokenTransferProxy', () => {
const web3Wrapper = new Web3Wrapper(web3.currentProvider);

View File

@@ -1,7 +1,6 @@
import { BlockchainLifecycle } from '@0xproject/dev-utils';
import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as chai from 'chai';
import * as Web3 from 'web3';
import { Artifacts } from '../../util/artifacts';
import { Balances } from '../../util/balances';
@@ -12,10 +11,8 @@ import { chaiSetup } from '../utils/chai_setup';
chaiSetup.configure();
const expect = chai.expect;
const { TokenTransferProxy, DummyToken, TokenRegistry } = new Artifacts(artifacts);
// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle
// with type `any` to a variable of type `Web3`.
const web3: Web3 = (global as any).web3;
const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL);
const web3 = web3Factory.create();
const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL);
describe('TokenTransferProxy', () => {
const web3Wrapper = new Web3Wrapper(web3.currentProvider);

View File

@@ -1,9 +1,8 @@
import { ZeroEx } from '0x.js';
import { BlockchainLifecycle } from '@0xproject/dev-utils';
import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils';
import { BigNumber } from '@0xproject/utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as chai from 'chai';
import * as Web3 from 'web3';
import { Artifacts } from '../util/artifacts';
import { constants } from '../util/constants';
@@ -12,12 +11,10 @@ import { ContractInstance } from '../util/types';
import { chaiSetup } from './utils/chai_setup';
const { DummyToken } = new Artifacts(artifacts);
// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle
// with type `any` to a variable of type `Web3`.
const web3: Web3 = (global as any).web3;
const web3 = web3Factory.create();
chaiSetup.configure();
const expect = chai.expect;
const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL);
const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL);
describe('UnlimitedAllowanceToken', () => {
const web3Wrapper = new Web3Wrapper(web3.currentProvider);

View File

@@ -1,9 +1,8 @@
import { ZeroEx } from '0x.js';
import { BlockchainLifecycle } from '@0xproject/dev-utils';
import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils';
import { BigNumber } from '@0xproject/utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as chai from 'chai';
import * as Web3 from 'web3';
import { Artifacts } from '../util/artifacts';
import { constants } from '../util/constants';
@@ -14,8 +13,8 @@ import { chaiSetup } from './utils/chai_setup';
const { DummyTokenV2 } = new Artifacts(artifacts);
chaiSetup.configure();
const expect = chai.expect;
const web3: Web3 = (global as any).web3;
const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL);
const web3 = web3Factory.create();
const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL);
describe('UnlimitedAllowanceTokenV2', () => {
const web3Wrapper = new Web3Wrapper(web3.currentProvider);

View File

@@ -1,9 +1,8 @@
import { ZeroEx } from '0x.js';
import { BlockchainLifecycle } from '@0xproject/dev-utils';
import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils';
import { BigNumber } from '@0xproject/utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as chai from 'chai';
import Web3 = require('web3');
import { Artifacts } from '../util/artifacts';
import { constants } from '../util/constants';
@@ -14,10 +13,8 @@ import { chaiSetup } from './utils/chai_setup';
chaiSetup.configure();
const expect = chai.expect;
const { Exchange, ZRXToken } = new Artifacts(artifacts);
// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle
// with type `any` to a variable of type `Web3`.
const web3: Web3 = (global as any).web3;
const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL);
const web3 = web3Factory.create();
const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL);
describe('ZRXToken', () => {
const web3Wrapper = new Web3Wrapper(web3.currentProvider);
@@ -140,10 +137,10 @@ describe('ZRXToken', () => {
const initOwnerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner);
const amountToTransfer = initOwnerBalance;
const initSpenderAllowance = MAX_UINT;
let txHash = await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, initSpenderAllowance, {
await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, initSpenderAllowance, {
gasLimit: constants.MAX_TOKEN_APPROVE_GAS,
});
txHash = await zeroEx.token.transferFromAsync(zrxAddress, owner, spender, spender, amountToTransfer, {
await zeroEx.token.transferFromAsync(zrxAddress, owner, spender, spender, amountToTransfer, {
gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS,
});
@@ -156,8 +153,8 @@ describe('ZRXToken', () => {
const initSpenderBalance = await zeroEx.token.getBalanceAsync(zrxAddress, spender);
const amountToTransfer = initOwnerBalance;
const initSpenderAllowance = initOwnerBalance;
let txHash = await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, initSpenderAllowance);
txHash = await zeroEx.token.transferFromAsync(zrxAddress, owner, spender, spender, amountToTransfer, {
await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, initSpenderAllowance);
await zeroEx.token.transferFromAsync(zrxAddress, owner, spender, spender, amountToTransfer, {
gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS,
});
@@ -171,8 +168,8 @@ describe('ZRXToken', () => {
it('should modify allowance if spender has sufficient allowance less than 2^256 - 1', async () => {
const initOwnerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner);
const amountToTransfer = initOwnerBalance;
let txHash = await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, amountToTransfer);
txHash = await zeroEx.token.transferFromAsync(zrxAddress, owner, spender, spender, amountToTransfer, {
await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, amountToTransfer);
await zeroEx.token.transferFromAsync(zrxAddress, owner, spender, spender, amountToTransfer, {
gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS,
});