diff options
Diffstat (limited to 'packages/contracts/test/utils')
19 files changed, 135 insertions, 94 deletions
diff --git a/packages/contracts/test/utils/asset_wrapper.ts b/packages/contracts/test/utils/asset_wrapper.ts index f291170a2..e3a4800c6 100644 --- a/packages/contracts/test/utils/asset_wrapper.ts +++ b/packages/contracts/test/utils/asset_wrapper.ts @@ -1,4 +1,4 @@ -import { assetProxyUtils } from '@0xproject/order-utils'; +import { assetDataUtils } from '@0xproject/order-utils'; import { AssetProxyId } from '@0xproject/types'; import { BigNumber, errorUtils } from '@0xproject/utils'; import * as _ from 'lodash'; @@ -17,7 +17,7 @@ interface ProxyIdToAssetWrappers { * the logic that uses it does not need to care what standard a token belongs to. */ export class AssetWrapper { - private _proxyIdToAssetWrappers: ProxyIdToAssetWrappers; + private readonly _proxyIdToAssetWrappers: ProxyIdToAssetWrappers; constructor(assetWrappers: AbstractAssetWrapper[]) { this._proxyIdToAssetWrappers = {}; _.each(assetWrappers, assetWrapper => { @@ -26,7 +26,7 @@ export class AssetWrapper { }); } public async getBalanceAsync(userAddress: string, assetData: string): Promise<BigNumber> { - const proxyId = assetProxyUtils.decodeAssetDataId(assetData); + const proxyId = assetDataUtils.decodeAssetProxyId(assetData); switch (proxyId) { case AssetProxyId.ERC20: { const erc20Wrapper = this._proxyIdToAssetWrappers[proxyId] as ERC20Wrapper; @@ -35,7 +35,7 @@ export class AssetWrapper { } case AssetProxyId.ERC721: { const assetWrapper = this._proxyIdToAssetWrappers[proxyId] as ERC721Wrapper; - const assetProxyData = assetProxyUtils.decodeERC721AssetData(assetData); + const assetProxyData = assetDataUtils.decodeERC721AssetData(assetData); const isOwner = await assetWrapper.isOwnerAsync( userAddress, assetProxyData.tokenAddress, @@ -49,7 +49,7 @@ export class AssetWrapper { } } public async setBalanceAsync(userAddress: string, assetData: string, desiredBalance: BigNumber): Promise<void> { - const proxyId = assetProxyUtils.decodeAssetDataId(assetData); + const proxyId = assetDataUtils.decodeAssetProxyId(assetData); switch (proxyId) { case AssetProxyId.ERC20: { const erc20Wrapper = this._proxyIdToAssetWrappers[proxyId] as ERC20Wrapper; @@ -61,7 +61,7 @@ export class AssetWrapper { throw new Error(`Balance for ERC721 token can only be set to 0 or 1. Got: ${desiredBalance}`); } const erc721Wrapper = this._proxyIdToAssetWrappers[proxyId] as ERC721Wrapper; - const assetProxyData = assetProxyUtils.decodeERC721AssetData(assetData); + const assetProxyData = assetDataUtils.decodeERC721AssetData(assetData); const doesTokenExist = erc721Wrapper.doesTokenExistAsync( assetProxyData.tokenAddress, assetProxyData.tokenId, @@ -107,7 +107,7 @@ export class AssetWrapper { } } public async getProxyAllowanceAsync(userAddress: string, assetData: string): Promise<BigNumber> { - const proxyId = assetProxyUtils.decodeAssetDataId(assetData); + const proxyId = assetDataUtils.decodeAssetProxyId(assetData); switch (proxyId) { case AssetProxyId.ERC20: { const erc20Wrapper = this._proxyIdToAssetWrappers[proxyId] as ERC20Wrapper; @@ -116,7 +116,7 @@ export class AssetWrapper { } case AssetProxyId.ERC721: { const assetWrapper = this._proxyIdToAssetWrappers[proxyId] as ERC721Wrapper; - const erc721ProxyData = assetProxyUtils.decodeERC721AssetData(assetData); + const erc721ProxyData = assetDataUtils.decodeERC721AssetData(assetData); const isProxyApprovedForAll = await assetWrapper.isProxyApprovedForAllAsync( userAddress, erc721ProxyData.tokenAddress, @@ -141,7 +141,7 @@ export class AssetWrapper { assetData: string, desiredAllowance: BigNumber, ): Promise<void> { - const proxyId = assetProxyUtils.decodeAssetDataId(assetData); + const proxyId = assetDataUtils.decodeAssetProxyId(assetData); switch (proxyId) { case AssetProxyId.ERC20: { const erc20Wrapper = this._proxyIdToAssetWrappers[proxyId] as ERC20Wrapper; @@ -159,7 +159,7 @@ export class AssetWrapper { ); } const erc721Wrapper = this._proxyIdToAssetWrappers[proxyId] as ERC721Wrapper; - const assetProxyData = assetProxyUtils.decodeERC721AssetData(assetData); + const assetProxyData = assetDataUtils.decodeERC721AssetData(assetData); const doesTokenExist = await erc721Wrapper.doesTokenExistAsync( assetProxyData.tokenAddress, diff --git a/packages/contracts/test/utils/increase_time.ts b/packages/contracts/test/utils/block_timestamp.ts index 4565d8dbc..1159792c4 100644 --- a/packages/contracts/test/utils/increase_time.ts +++ b/packages/contracts/test/utils/block_timestamp.ts @@ -29,3 +29,12 @@ export async function increaseTimeAndMineBlockAsync(seconds: number): Promise<nu return offset; } + +/** + * Returns the timestamp of the latest block in seconds since the Unix epoch. + * @returns a new Promise which will resolve with the timestamp in seconds. + */ +export async function getLatestBlockTimestampAsync(): Promise<number> { + const currentBlock = await web3Wrapper.getBlockAsync('latest'); + return currentBlock.timestamp; +} diff --git a/packages/contracts/test/utils/constants.ts b/packages/contracts/test/utils/constants.ts index 7f3ad62e1..7dac38f56 100644 --- a/packages/contracts/test/utils/constants.ts +++ b/packages/contracts/test/utils/constants.ts @@ -17,6 +17,7 @@ const TESTRPC_PRIVATE_KEYS_STRINGS = [ ]; export const constants = { + BASE_16: 16, INVALID_OPCODE: 'invalid opcode', TESTRPC_NETWORK_ID: 50, // Note(albrow): In practice V8 and most other engines limit the minimum @@ -24,9 +25,10 @@ export const constants = { // ensure we always use the minimum interval. AWAIT_TRANSACTION_MINED_MS: 0, MAX_ETHERTOKEN_WITHDRAW_GAS: 43000, + MAX_EXECUTE_TRANSACTION_GAS: 1000000, MAX_TOKEN_TRANSFERFROM_GAS: 80000, MAX_TOKEN_APPROVE_GAS: 60000, - TRANSFER_FROM_GAS: 150000, + MAX_TRANSFER_FROM_GAS: 150000, DUMMY_TOKEN_NAME: '', DUMMY_TOKEN_SYMBOL: '', DUMMY_TOKEN_DECIMALS: new BigNumber(18), @@ -46,4 +48,5 @@ export const constants = { makerFee: Web3Wrapper.toBaseUnitAmount(new BigNumber(1), 18), takerFee: Web3Wrapper.toBaseUnitAmount(new BigNumber(1), 18), }, + WORD_LENGTH: 32, }; diff --git a/packages/contracts/test/utils/core_combinatorial_utils.ts b/packages/contracts/test/utils/core_combinatorial_utils.ts index 7c16ef201..44a5199c0 100644 --- a/packages/contracts/test/utils/core_combinatorial_utils.ts +++ b/packages/contracts/test/utils/core_combinatorial_utils.ts @@ -1,5 +1,5 @@ import { - assetProxyUtils, + assetDataUtils, BalanceAndProxyAllowanceLazyStore, ExchangeTransferSimulator, orderHashUtils, @@ -72,7 +72,7 @@ export async function coreCombinatorialUtilsFactoryAsync( erc20EighteenDecimalTokenB, zrxToken, ] = await erc20Wrapper.deployDummyTokensAsync(erc20EighteenDecimalTokenCount, eighteenDecimals); - const zrxAssetData = assetProxyUtils.encodeERC20AssetData(zrxToken.address); + const zrxAssetData = assetDataUtils.encodeERC20AssetData(zrxToken.address); const erc20FiveDecimalTokenCount = 2; const fiveDecimals = new BigNumber(5); @@ -598,8 +598,8 @@ export class CoreCombinatorialUtils { break; case TakerAssetFillAmountScenario.LessThanRemainingFillableTakerAssetAmount: - const takerAssetProxyId = assetProxyUtils.decodeAssetDataId(signedOrder.takerAssetData); - const makerAssetProxyId = assetProxyUtils.decodeAssetDataId(signedOrder.makerAssetData); + const takerAssetProxyId = assetDataUtils.decodeAssetProxyId(signedOrder.takerAssetData); + const makerAssetProxyId = assetDataUtils.decodeAssetProxyId(signedOrder.makerAssetData); const isEitherAssetERC721 = takerAssetProxyId === AssetProxyId.ERC721 || makerAssetProxyId === AssetProxyId.ERC721; if (isEitherAssetERC721) { diff --git a/packages/contracts/test/utils/erc20_wrapper.ts b/packages/contracts/test/utils/erc20_wrapper.ts index cf1433791..424aae579 100644 --- a/packages/contracts/test/utils/erc20_wrapper.ts +++ b/packages/contracts/test/utils/erc20_wrapper.ts @@ -1,4 +1,4 @@ -import { assetProxyUtils } from '@0xproject/order-utils'; +import { assetDataUtils } from '@0xproject/order-utils'; import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { Provider } from 'ethereum-types'; @@ -13,11 +13,11 @@ import { ERC20BalancesByOwner } from './types'; import { txDefaults } from './web3_wrapper'; export class ERC20Wrapper { - private _tokenOwnerAddresses: string[]; - private _contractOwnerAddress: string; - private _web3Wrapper: Web3Wrapper; - private _provider: Provider; - private _dummyTokenContracts: DummyERC20TokenContract[]; + private readonly _tokenOwnerAddresses: string[]; + private readonly _contractOwnerAddress: string; + private readonly _web3Wrapper: Web3Wrapper; + private readonly _provider: Provider; + private readonly _dummyTokenContracts: DummyERC20TokenContract[]; private _proxyContract?: ERC20ProxyContract; private _proxyIdIfExists?: string; constructor(provider: Provider, tokenOwnerAddresses: string[], contractOwnerAddress: string) { @@ -154,7 +154,7 @@ export class ERC20Wrapper { return tokenAddresses; } private _getTokenContractFromAssetData(assetData: string): DummyERC20TokenContract { - const erc20ProxyData = assetProxyUtils.decodeERC20AssetData(assetData); + const erc20ProxyData = assetDataUtils.decodeERC20AssetData(assetData); const tokenAddress = erc20ProxyData.tokenAddress; const tokenContractIfExists = _.find(this._dummyTokenContracts, c => c.address === tokenAddress); if (_.isUndefined(tokenContractIfExists)) { diff --git a/packages/contracts/test/utils/erc721_wrapper.ts b/packages/contracts/test/utils/erc721_wrapper.ts index a38dfb811..d023b4d02 100644 --- a/packages/contracts/test/utils/erc721_wrapper.ts +++ b/packages/contracts/test/utils/erc721_wrapper.ts @@ -13,11 +13,11 @@ import { ERC721TokenIdsByOwner } from './types'; import { txDefaults } from './web3_wrapper'; export class ERC721Wrapper { - private _tokenOwnerAddresses: string[]; - private _contractOwnerAddress: string; - private _web3Wrapper: Web3Wrapper; - private _provider: Provider; - private _dummyTokenContracts: DummyERC721TokenContract[]; + private readonly _tokenOwnerAddresses: string[]; + private readonly _contractOwnerAddress: string; + private readonly _web3Wrapper: Web3Wrapper; + private readonly _provider: Provider; + private readonly _dummyTokenContracts: DummyERC721TokenContract[]; private _proxyContract?: ERC721ProxyContract; private _proxyIdIfExists?: string; private _initialTokenIdsByOwner: ERC721TokenIdsByOwner = {}; diff --git a/packages/contracts/test/utils/exchange_wrapper.ts b/packages/contracts/test/utils/exchange_wrapper.ts index 155d0eeb0..490ea959b 100644 --- a/packages/contracts/test/utils/exchange_wrapper.ts +++ b/packages/contracts/test/utils/exchange_wrapper.ts @@ -11,9 +11,9 @@ import { orderUtils } from './order_utils'; import { OrderInfo, SignedTransaction } from './types'; export class ExchangeWrapper { - private _exchange: ExchangeContract; - private _web3Wrapper: Web3Wrapper; - private _logDecoder: LogDecoder; + private readonly _exchange: ExchangeContract; + private readonly _web3Wrapper: Web3Wrapper; + private readonly _logDecoder: LogDecoder; constructor(exchangeContract: ExchangeContract, provider: Provider) { this._exchange = exchangeContract; this._web3Wrapper = new Web3Wrapper(provider); @@ -223,6 +223,10 @@ export class ExchangeWrapper { const orderInfo = (await this._exchange.getOrderInfo.callAsync(signedOrder)) as OrderInfo; return orderInfo; } + public async getOrdersInfoAsync(signedOrders: SignedOrder[]): Promise<OrderInfo[]> { + const ordersInfo = (await this._exchange.getOrdersInfo.callAsync(signedOrders)) as OrderInfo[]; + return ordersInfo; + } public async matchOrdersAsync( signedOrderLeft: SignedOrder, signedOrderRight: SignedOrder, diff --git a/packages/contracts/test/utils/forwarder_wrapper.ts b/packages/contracts/test/utils/forwarder_wrapper.ts index d227420ee..e39df14b1 100644 --- a/packages/contracts/test/utils/forwarder_wrapper.ts +++ b/packages/contracts/test/utils/forwarder_wrapper.ts @@ -1,4 +1,4 @@ -import { assetProxyUtils } from '@0xproject/order-utils'; +import { assetDataUtils } from '@0xproject/order-utils'; import { AssetProxyId, SignedOrder } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; @@ -18,13 +18,13 @@ const ZERO_AMOUNT = new BigNumber(0); const INSUFFICENT_ORDERS_FOR_MAKER_AMOUNT = 'Unable to satisfy makerAssetFillAmount with provided orders'; export class ForwarderWrapper { - private _web3Wrapper: Web3Wrapper; - private _forwarderContract: ForwarderContract; - private _logDecoder: LogDecoder; - private _zrxAddress: string; + private readonly _web3Wrapper: Web3Wrapper; + private readonly _forwarderContract: ForwarderContract; + private readonly _logDecoder: LogDecoder; + private readonly _zrxAddress: string; private static _createOptimizedSellOrders(signedOrders: SignedOrder[]): MarketSellOrders { const marketSellOrders = formatters.createMarketSellOrders(signedOrders, ZERO_AMOUNT); - const assetDataId = assetProxyUtils.decodeAssetDataId(signedOrders[0].makerAssetData); + const assetDataId = assetDataUtils.decodeAssetProxyId(signedOrders[0].makerAssetData); // Contract will fill this in for us as all of the assetData is assumed to be the same for (let i = 0; i < signedOrders.length; i++) { if (i !== 0 && assetDataId === AssetProxyId.ERC20) { @@ -90,7 +90,7 @@ export class ForwarderWrapper { txData: TxDataPayable, opts: { feeProportion?: number; feeRecipient?: string } = {}, ): Promise<TransactionReceiptWithDecodedLogs> { - const assetDataId = assetProxyUtils.decodeAssetDataId(orders[0].makerAssetData); + const assetDataId = assetDataUtils.decodeAssetProxyId(orders[0].makerAssetData); if (assetDataId !== AssetProxyId.ERC20) { throw new Error('Asset type not supported by marketSellEthForERC20'); } @@ -116,7 +116,7 @@ export class ForwarderWrapper { feeProportion: number, makerAssetFillAmount: BigNumber, ): Promise<BigNumber> { - const assetProxyId = assetProxyUtils.decodeAssetDataId(orders[0].makerAssetData); + const assetProxyId = assetDataUtils.decodeAssetProxyId(orders[0].makerAssetData); switch (assetProxyId) { case AssetProxyId.ERC20: { const fillAmountWei = this._calculateMarketBuyERC20FillAmountAsync( @@ -145,7 +145,7 @@ export class ForwarderWrapper { feeProportion: number, makerAssetFillAmount: BigNumber, ): Promise<BigNumber> { - const makerAssetData = assetProxyUtils.decodeAssetData(orders[0].makerAssetData); + const makerAssetData = assetDataUtils.decodeAssetDataOrThrow(orders[0].makerAssetData); const makerAssetToken = makerAssetData.tokenAddress; const params = formatters.createMarketBuyOrders(orders, makerAssetFillAmount); diff --git a/packages/contracts/test/utils/log_decoder.ts b/packages/contracts/test/utils/log_decoder.ts index 07127ba79..5a4801319 100644 --- a/packages/contracts/test/utils/log_decoder.ts +++ b/packages/contracts/test/utils/log_decoder.ts @@ -15,9 +15,9 @@ import { artifacts } from './artifacts'; import { constants } from './constants'; export class LogDecoder { - private _web3Wrapper: Web3Wrapper; - private _contractAddress: string; - private _abiDecoder: AbiDecoder; + private readonly _web3Wrapper: Web3Wrapper; + private readonly _contractAddress: string; + private readonly _abiDecoder: AbiDecoder; public static wrapLogBigNumbers(log: any): any { const argNames = _.keys(log.args); for (const argName of argNames) { diff --git a/packages/contracts/test/utils/match_order_tester.ts b/packages/contracts/test/utils/match_order_tester.ts index 6145779b0..fa2eabc12 100644 --- a/packages/contracts/test/utils/match_order_tester.ts +++ b/packages/contracts/test/utils/match_order_tester.ts @@ -1,4 +1,4 @@ -import { assetProxyUtils, orderHashUtils } from '@0xproject/order-utils'; +import { assetDataUtils, orderHashUtils } from '@0xproject/order-utils'; import { AssetProxyId, SignedOrder } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; import * as chai from 'chai'; @@ -14,10 +14,10 @@ chaiSetup.configure(); const expect = chai.expect; export class MatchOrderTester { - private _exchangeWrapper: ExchangeWrapper; - private _erc20Wrapper: ERC20Wrapper; - private _erc721Wrapper: ERC721Wrapper; - private _feeTokenAddress: string; + private readonly _exchangeWrapper: ExchangeWrapper; + private readonly _erc20Wrapper: ERC20Wrapper; + private readonly _erc721Wrapper: ERC721Wrapper; + private readonly _feeTokenAddress: string; /// @dev Compares a pair of ERC20 balances and a pair of ERC721 token owners. /// @param expectedNewERC20BalancesByOwner Expected ERC20 balances. @@ -233,10 +233,10 @@ export class MatchOrderTester { const expectedNewERC20BalancesByOwner = _.cloneDeep(erc20BalancesByOwner); const expectedNewERC721TokenIdsByOwner = _.cloneDeep(erc721TokenIdsByOwner); // Left Maker Asset (Right Taker Asset) - const makerAssetProxyIdLeft = assetProxyUtils.decodeAssetDataId(signedOrderLeft.makerAssetData); + const makerAssetProxyIdLeft = assetDataUtils.decodeAssetProxyId(signedOrderLeft.makerAssetData); if (makerAssetProxyIdLeft === AssetProxyId.ERC20) { // Decode asset data - const erc20AssetData = assetProxyUtils.decodeERC20AssetData(signedOrderLeft.makerAssetData); + const erc20AssetData = assetDataUtils.decodeERC20AssetData(signedOrderLeft.makerAssetData); const makerAssetAddressLeft = erc20AssetData.tokenAddress; const takerAssetAddressRight = makerAssetAddressLeft; // Left Maker @@ -255,7 +255,7 @@ export class MatchOrderTester { ][makerAssetAddressLeft].add(expectedTransferAmounts.amountReceivedByTaker); } else if (makerAssetProxyIdLeft === AssetProxyId.ERC721) { // Decode asset data - const erc721AssetData = assetProxyUtils.decodeERC721AssetData(signedOrderLeft.makerAssetData); + const erc721AssetData = assetDataUtils.decodeERC721AssetData(signedOrderLeft.makerAssetData); const makerAssetAddressLeft = erc721AssetData.tokenAddress; const makerAssetIdLeft = erc721AssetData.tokenId; const takerAssetAddressRight = makerAssetAddressLeft; @@ -268,10 +268,10 @@ export class MatchOrderTester { } // Left Taker Asset (Right Maker Asset) // Note: This exchange is only between the order makers: the Taker does not receive any of the left taker asset. - const takerAssetProxyIdLeft = assetProxyUtils.decodeAssetDataId(signedOrderLeft.takerAssetData); + const takerAssetProxyIdLeft = assetDataUtils.decodeAssetProxyId(signedOrderLeft.takerAssetData); if (takerAssetProxyIdLeft === AssetProxyId.ERC20) { // Decode asset data - const erc20AssetData = assetProxyUtils.decodeERC20AssetData(signedOrderLeft.takerAssetData); + const erc20AssetData = assetDataUtils.decodeERC20AssetData(signedOrderLeft.takerAssetData); const takerAssetAddressLeft = erc20AssetData.tokenAddress; const makerAssetAddressRight = takerAssetAddressLeft; // Left Maker @@ -286,7 +286,7 @@ export class MatchOrderTester { ); } else if (takerAssetProxyIdLeft === AssetProxyId.ERC721) { // Decode asset data - const erc721AssetData = assetProxyUtils.decodeERC721AssetData(signedOrderRight.makerAssetData); + const erc721AssetData = assetDataUtils.decodeERC721AssetData(signedOrderRight.makerAssetData); const makerAssetAddressRight = erc721AssetData.tokenAddress; const makerAssetIdRight = erc721AssetData.tokenId; const takerAssetAddressLeft = makerAssetAddressRight; diff --git a/packages/contracts/test/utils/multi_sig_wrapper.ts b/packages/contracts/test/utils/multi_sig_wrapper.ts index 6e7746dfc..8c8055d4a 100644 --- a/packages/contracts/test/utils/multi_sig_wrapper.ts +++ b/packages/contracts/test/utils/multi_sig_wrapper.ts @@ -6,12 +6,13 @@ import * as _ from 'lodash'; import { AssetProxyOwnerContract } from '../../generated_contract_wrappers/asset_proxy_owner'; import { MultiSigWalletContract } from '../../generated_contract_wrappers/multi_sig_wallet'; +import { constants } from './constants'; import { LogDecoder } from './log_decoder'; export class MultiSigWrapper { - private _multiSig: MultiSigWalletContract; - private _web3Wrapper: Web3Wrapper; - private _logDecoder: LogDecoder; + private readonly _multiSig: MultiSigWalletContract; + private readonly _web3Wrapper: Web3Wrapper; + private readonly _logDecoder: LogDecoder; constructor(multiSigContract: MultiSigWalletContract, provider: Provider) { this._multiSig = multiSigContract; this._web3Wrapper = new Web3Wrapper(provider); @@ -36,7 +37,10 @@ export class MultiSigWrapper { return tx; } public async executeTransactionAsync(txId: BigNumber, from: string): Promise<TransactionReceiptWithDecodedLogs> { - const txHash = await this._multiSig.executeTransaction.sendTransactionAsync(txId, { from }); + const txHash = await this._multiSig.executeTransaction.sendTransactionAsync(txId, { + from, + gas: constants.MAX_EXECUTE_TRANSACTION_GAS, + }); const tx = await this._logDecoder.getTxWithDecodedLogsAsync(txHash); return tx; } @@ -48,6 +52,7 @@ export class MultiSigWrapper { const txHash = await (this ._multiSig as AssetProxyOwnerContract).executeRemoveAuthorizedAddressAtIndex.sendTransactionAsync(txId, { from, + gas: constants.MAX_EXECUTE_TRANSACTION_GAS, }); const tx = await this._logDecoder.getTxWithDecodedLogsAsync(txHash); return tx; diff --git a/packages/contracts/test/utils/order_factory.ts b/packages/contracts/test/utils/order_factory.ts index 009dbc396..63a893695 100644 --- a/packages/contracts/test/utils/order_factory.ts +++ b/packages/contracts/test/utils/order_factory.ts @@ -2,25 +2,26 @@ import { generatePseudoRandomSalt, orderHashUtils } from '@0xproject/order-utils import { Order, SignatureType, SignedOrder } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; +import { getLatestBlockTimestampAsync } from './block_timestamp'; import { constants } from './constants'; import { signingUtils } from './signing_utils'; export class OrderFactory { - private _defaultOrderParams: Partial<Order>; - private _privateKey: Buffer; + private readonly _defaultOrderParams: Partial<Order>; + private readonly _privateKey: Buffer; constructor(privateKey: Buffer, defaultOrderParams: Partial<Order>) { this._defaultOrderParams = defaultOrderParams; this._privateKey = privateKey; } - public newSignedOrder( + public async newSignedOrderAsync( customOrderParams: Partial<Order> = {}, signatureType: SignatureType = SignatureType.EthSign, - ): SignedOrder { - const tenMinutes = 10 * 60 * 1000; - const randomExpiration = new BigNumber(Date.now() + tenMinutes); + ): Promise<SignedOrder> { + const tenMinutesInSeconds = 10 * 60; + const currentBlockTimestamp = await getLatestBlockTimestampAsync(); const order = ({ senderAddress: constants.NULL_ADDRESS, - expirationTimeSeconds: randomExpiration, + expirationTimeSeconds: new BigNumber(currentBlockTimestamp).add(tenMinutesInSeconds), salt: generatePseudoRandomSalt(), takerAddress: constants.NULL_ADDRESS, ...this._defaultOrderParams, diff --git a/packages/contracts/test/utils/order_factory_from_scenario.ts b/packages/contracts/test/utils/order_factory_from_scenario.ts index 526505871..a908140b9 100644 --- a/packages/contracts/test/utils/order_factory_from_scenario.ts +++ b/packages/contracts/test/utils/order_factory_from_scenario.ts @@ -1,4 +1,4 @@ -import { assetProxyUtils, generatePseudoRandomSalt } from '@0xproject/order-utils'; +import { assetDataUtils, generatePseudoRandomSalt } from '@0xproject/order-utils'; import { Order } from '@0xproject/types'; import { BigNumber, errorUtils } from '@0xproject/utils'; @@ -24,13 +24,13 @@ const FIVE_UNITS_FIVE_DECIMALS = new BigNumber(500_000); const ONE_NFT_UNIT = new BigNumber(1); export class OrderFactoryFromScenario { - private _userAddresses: string[]; - private _zrxAddress: string; - private _nonZrxERC20EighteenDecimalTokenAddresses: string[]; - private _erc20FiveDecimalTokenAddresses: string[]; - private _erc721Token: DummyERC721TokenContract; - private _erc721Balances: ERC721TokenIdsByOwner; - private _exchangeAddress: string; + private readonly _userAddresses: string[]; + private readonly _zrxAddress: string; + private readonly _nonZrxERC20EighteenDecimalTokenAddresses: string[]; + private readonly _erc20FiveDecimalTokenAddresses: string[]; + private readonly _erc721Token: DummyERC721TokenContract; + private readonly _erc721Balances: ERC721TokenIdsByOwner; + private readonly _exchangeAddress: string; constructor( userAddresses: string[], zrxAddress: string, @@ -75,18 +75,16 @@ export class OrderFactoryFromScenario { switch (orderScenario.makerAssetDataScenario) { case AssetDataScenario.ZRXFeeToken: - makerAssetData = assetProxyUtils.encodeERC20AssetData(this._zrxAddress); + makerAssetData = assetDataUtils.encodeERC20AssetData(this._zrxAddress); break; case AssetDataScenario.ERC20NonZRXEighteenDecimals: - makerAssetData = assetProxyUtils.encodeERC20AssetData( - this._nonZrxERC20EighteenDecimalTokenAddresses[0], - ); + makerAssetData = assetDataUtils.encodeERC20AssetData(this._nonZrxERC20EighteenDecimalTokenAddresses[0]); break; case AssetDataScenario.ERC20FiveDecimals: - makerAssetData = assetProxyUtils.encodeERC20AssetData(this._erc20FiveDecimalTokenAddresses[0]); + makerAssetData = assetDataUtils.encodeERC20AssetData(this._erc20FiveDecimalTokenAddresses[0]); break; case AssetDataScenario.ERC721: - makerAssetData = assetProxyUtils.encodeERC721AssetData( + makerAssetData = assetDataUtils.encodeERC721AssetData( this._erc721Token.address, erc721MakerAssetIds[0], ); @@ -97,18 +95,16 @@ export class OrderFactoryFromScenario { switch (orderScenario.takerAssetDataScenario) { case AssetDataScenario.ZRXFeeToken: - takerAssetData = assetProxyUtils.encodeERC20AssetData(this._zrxAddress); + takerAssetData = assetDataUtils.encodeERC20AssetData(this._zrxAddress); break; case AssetDataScenario.ERC20NonZRXEighteenDecimals: - takerAssetData = assetProxyUtils.encodeERC20AssetData( - this._nonZrxERC20EighteenDecimalTokenAddresses[1], - ); + takerAssetData = assetDataUtils.encodeERC20AssetData(this._nonZrxERC20EighteenDecimalTokenAddresses[1]); break; case AssetDataScenario.ERC20FiveDecimals: - takerAssetData = assetProxyUtils.encodeERC20AssetData(this._erc20FiveDecimalTokenAddresses[1]); + takerAssetData = assetDataUtils.encodeERC20AssetData(this._erc20FiveDecimalTokenAddresses[1]); break; case AssetDataScenario.ERC721: - takerAssetData = assetProxyUtils.encodeERC721AssetData( + takerAssetData = assetDataUtils.encodeERC721AssetData( this._erc721Token.address, erc721TakerAssetIds[0], ); diff --git a/packages/contracts/test/utils/simple_asset_balance_and_proxy_allowance_fetcher.ts b/packages/contracts/test/utils/simple_asset_balance_and_proxy_allowance_fetcher.ts index a295a40c4..598ee6d29 100644 --- a/packages/contracts/test/utils/simple_asset_balance_and_proxy_allowance_fetcher.ts +++ b/packages/contracts/test/utils/simple_asset_balance_and_proxy_allowance_fetcher.ts @@ -4,7 +4,7 @@ import { BigNumber } from '@0xproject/utils'; import { AssetWrapper } from './asset_wrapper'; export class SimpleAssetBalanceAndProxyAllowanceFetcher implements AbstractBalanceAndProxyAllowanceFetcher { - private _assetWrapper: AssetWrapper; + private readonly _assetWrapper: AssetWrapper; constructor(assetWrapper: AssetWrapper) { this._assetWrapper = assetWrapper; } diff --git a/packages/contracts/test/utils/simple_order_filled_cancelled_fetcher.ts b/packages/contracts/test/utils/simple_order_filled_cancelled_fetcher.ts index 24afe36b7..ed69ecc63 100644 --- a/packages/contracts/test/utils/simple_order_filled_cancelled_fetcher.ts +++ b/packages/contracts/test/utils/simple_order_filled_cancelled_fetcher.ts @@ -4,8 +4,8 @@ import { BigNumber } from '@0xproject/utils'; import { ExchangeWrapper } from './exchange_wrapper'; export class SimpleOrderFilledCancelledFetcher implements AbstractOrderFilledCancelledFetcher { - private _exchangeWrapper: ExchangeWrapper; - private _zrxAssetData: string; + private readonly _exchangeWrapper: ExchangeWrapper; + private readonly _zrxAssetData: string; constructor(exchange: ExchangeWrapper, zrxAssetData: string) { this._exchangeWrapper = exchange; this._zrxAssetData = zrxAssetData; diff --git a/packages/contracts/test/utils/token_registry_wrapper.ts b/packages/contracts/test/utils/token_registry_wrapper.ts index 0abf20e03..f1c40e8ff 100644 --- a/packages/contracts/test/utils/token_registry_wrapper.ts +++ b/packages/contracts/test/utils/token_registry_wrapper.ts @@ -8,8 +8,8 @@ import { Token } from './types'; import { constants } from './constants'; export class TokenRegWrapper { - private _tokenReg: TokenRegistryContract; - private _web3Wrapper: Web3Wrapper; + private readonly _tokenReg: TokenRegistryContract; + private readonly _web3Wrapper: Web3Wrapper; constructor(tokenRegContract: TokenRegistryContract, provider: Provider) { this._tokenReg = tokenRegContract; this._web3Wrapper = new Web3Wrapper(provider); diff --git a/packages/contracts/test/utils/transaction_factory.ts b/packages/contracts/test/utils/transaction_factory.ts index 348c0715d..281c1e30d 100644 --- a/packages/contracts/test/utils/transaction_factory.ts +++ b/packages/contracts/test/utils/transaction_factory.ts @@ -15,9 +15,9 @@ const EIP712_ZEROEX_TRANSACTION_SCHEMA: EIP712Schema = { }; export class TransactionFactory { - private _signerBuff: Buffer; - private _exchangeAddress: string; - private _privateKey: Buffer; + private readonly _signerBuff: Buffer; + private readonly _exchangeAddress: string; + private readonly _privateKey: Buffer; constructor(privateKey: Buffer, exchangeAddress: string) { this._privateKey = privateKey; this._exchangeAddress = exchangeAddress; diff --git a/packages/contracts/test/utils/type_encoding_utils.ts b/packages/contracts/test/utils/type_encoding_utils.ts new file mode 100644 index 000000000..75307b9bd --- /dev/null +++ b/packages/contracts/test/utils/type_encoding_utils.ts @@ -0,0 +1,21 @@ +import { BigNumber } from '@0xproject/utils'; +import BN = require('bn.js'); +import ethUtil = require('ethereumjs-util'); + +import { constants } from './constants'; + +export const typeEncodingUtils = { + encodeUint256(value: BigNumber): Buffer { + const base = 10; + const formattedValue = new BN(value.toString(base)); + const encodedValue = ethUtil.toBuffer(formattedValue); + // tslint:disable-next-line:custom-no-magic-numbers + const paddedValue = ethUtil.setLengthLeft(encodedValue, constants.WORD_LENGTH); + return paddedValue; + }, + decodeUint256(encodedValue: Buffer): BigNumber { + const formattedValue = ethUtil.bufferToHex(encodedValue); + const value = new BigNumber(formattedValue, constants.BASE_16); + return value; + }, +}; diff --git a/packages/contracts/test/utils/web3_wrapper.ts b/packages/contracts/test/utils/web3_wrapper.ts index c9d83a02d..acb3103b7 100644 --- a/packages/contracts/test/utils/web3_wrapper.ts +++ b/packages/contracts/test/utils/web3_wrapper.ts @@ -51,8 +51,10 @@ export const provider = web3Factory.getRpcProvider(providerConfigs); const isCoverageEnabled = env.parseBoolean(EnvVars.SolidityCoverage); const isProfilerEnabled = env.parseBoolean(EnvVars.SolidityProfiler); const isRevertTraceEnabled = env.parseBoolean(EnvVars.SolidityRevertTrace); -const enabledSubproviderCount = _.filter([isCoverageEnabled, isProfilerEnabled, isRevertTraceEnabled], _.identity) - .length; +const enabledSubproviderCount = _.filter( + [isCoverageEnabled, isProfilerEnabled, isRevertTraceEnabled], + _.identity.bind(_), +).length; if (enabledSubproviderCount > 1) { throw new Error(`Only one of coverage, profiler, or revert trace subproviders can be enabled at a time`); } |