aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contract-wrappers/test
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2018-08-09 23:03:41 +0800
committerGitHub <noreply@github.com>2018-08-09 23:03:41 +0800
commit15e15f994a1b18cf2e9be151194c826d53a01601 (patch)
tree57b6db2615875c00cb16b1c94c7be113c2a18618 /packages/contract-wrappers/test
parentd44ff6a91582ed2b4dc25059d52556c4f9c6e163 (diff)
parent53713188fee57391040c24cc627fdc5ab8982d2e (diff)
downloaddexon-0x-contracts-15e15f994a1b18cf2e9be151194c826d53a01601.tar
dexon-0x-contracts-15e15f994a1b18cf2e9be151194c826d53a01601.tar.gz
dexon-0x-contracts-15e15f994a1b18cf2e9be151194c826d53a01601.tar.bz2
dexon-0x-contracts-15e15f994a1b18cf2e9be151194c826d53a01601.tar.lz
dexon-0x-contracts-15e15f994a1b18cf2e9be151194c826d53a01601.tar.xz
dexon-0x-contracts-15e15f994a1b18cf2e9be151194c826d53a01601.tar.zst
dexon-0x-contracts-15e15f994a1b18cf2e9be151194c826d53a01601.zip
Merge branch 'development' into sol-cov-fixes
Diffstat (limited to 'packages/contract-wrappers/test')
-rw-r--r--packages/contract-wrappers/test/calldata_optimization_utils_test.ts60
-rw-r--r--packages/contract-wrappers/test/exchange_wrapper_test.ts11
-rw-r--r--packages/contract-wrappers/test/forwarder_wrapper_test.ts130
3 files changed, 200 insertions, 1 deletions
diff --git a/packages/contract-wrappers/test/calldata_optimization_utils_test.ts b/packages/contract-wrappers/test/calldata_optimization_utils_test.ts
new file mode 100644
index 000000000..a4cea772f
--- /dev/null
+++ b/packages/contract-wrappers/test/calldata_optimization_utils_test.ts
@@ -0,0 +1,60 @@
+import { orderFactory } from '@0xproject/order-utils';
+import * as chai from 'chai';
+import * as _ from 'lodash';
+import 'mocha';
+
+import { assert } from '../src/utils/assert';
+import { calldataOptimizationUtils } from '../src/utils/calldata_optimization_utils';
+import { constants } from '../src/utils/constants';
+
+import { chaiSetup } from './utils/chai_setup';
+
+chaiSetup.configure();
+const expect = chai.expect;
+
+// utility for generating a set of order objects with mostly NULL values
+// except for a specified makerAssetData and takerAssetData
+const FAKE_ORDERS_COUNT = 5;
+const generateFakeOrders = (makerAssetData: string, takerAssetData: string) =>
+ _.map(_.range(FAKE_ORDERS_COUNT), index => {
+ const order = orderFactory.createOrder(
+ constants.NULL_ADDRESS,
+ constants.ZERO_AMOUNT,
+ makerAssetData,
+ constants.ZERO_AMOUNT,
+ takerAssetData,
+ constants.NULL_ADDRESS,
+ );
+ return {
+ ...order,
+ signature: 'dummy signature',
+ };
+ });
+
+describe('calldataOptimizationUtils', () => {
+ const fakeMakerAssetData = 'fakeMakerAssetData';
+ const fakeTakerAssetData = 'fakeTakerAssetData';
+ const orders = generateFakeOrders(fakeMakerAssetData, fakeTakerAssetData);
+ describe('#optimizeForwarderOrders', () => {
+ it('should make makerAssetData `0x` unless first order', () => {
+ const optimizedOrders = calldataOptimizationUtils.optimizeForwarderOrders(orders);
+ expect(optimizedOrders[0].makerAssetData).to.equal(fakeMakerAssetData);
+ const ordersWithoutHead = _.slice(optimizedOrders, 1);
+ _.forEach(ordersWithoutHead, order => expect(order.makerAssetData).to.equal(constants.NULL_BYTES));
+ });
+ it('should make all takerAssetData `0x`', () => {
+ const optimizedOrders = calldataOptimizationUtils.optimizeForwarderOrders(orders);
+ _.forEach(optimizedOrders, order => expect(order.takerAssetData).to.equal(constants.NULL_BYTES));
+ });
+ });
+ describe('#optimizeForwarderFeeOrders', () => {
+ it('should make all makerAssetData `0x`', () => {
+ const optimizedOrders = calldataOptimizationUtils.optimizeForwarderFeeOrders(orders);
+ _.forEach(optimizedOrders, order => expect(order.makerAssetData).to.equal(constants.NULL_BYTES));
+ });
+ it('should make all takerAssetData `0x`', () => {
+ const optimizedOrders = calldataOptimizationUtils.optimizeForwarderFeeOrders(orders);
+ _.forEach(optimizedOrders, order => expect(order.takerAssetData).to.equal(constants.NULL_BYTES));
+ });
+ });
+});
diff --git a/packages/contract-wrappers/test/exchange_wrapper_test.ts b/packages/contract-wrappers/test/exchange_wrapper_test.ts
index dca212f65..fa3b49eb9 100644
--- a/packages/contract-wrappers/test/exchange_wrapper_test.ts
+++ b/packages/contract-wrappers/test/exchange_wrapper_test.ts
@@ -277,6 +277,15 @@ describe('ExchangeWrapper', () => {
expect(orderInfo.orderHash).to.be.equal(orderHash);
});
});
+ describe('#getOrdersInfoAsync', () => {
+ it('should get the orders info', async () => {
+ const ordersInfo = await contractWrappers.exchange.getOrdersInfoAsync([signedOrder, anotherSignedOrder]);
+ const orderHash = orderHashUtils.getOrderHashHex(signedOrder);
+ expect(ordersInfo[0].orderHash).to.be.equal(orderHash);
+ const anotherOrderHash = orderHashUtils.getOrderHashHex(anotherSignedOrder);
+ expect(ordersInfo[1].orderHash).to.be.equal(anotherOrderHash);
+ });
+ });
describe('#isValidSignature', () => {
it('should check if the signature is valid', async () => {
const orderHash = orderHashUtils.getOrderHashHex(signedOrder);
@@ -295,7 +304,7 @@ describe('ExchangeWrapper', () => {
});
});
describe('#isAllowedValidatorAsync', () => {
- it('should check if the validator is alllowed', async () => {
+ it('should check if the validator is allowed', async () => {
const signerAddress = makerAddress;
const validatorAddress = constants.NULL_ADDRESS;
const isAllowed = await contractWrappers.exchange.isAllowedValidatorAsync(signerAddress, validatorAddress);
diff --git a/packages/contract-wrappers/test/forwarder_wrapper_test.ts b/packages/contract-wrappers/test/forwarder_wrapper_test.ts
new file mode 100644
index 000000000..3f3b40e0b
--- /dev/null
+++ b/packages/contract-wrappers/test/forwarder_wrapper_test.ts
@@ -0,0 +1,130 @@
+import { BlockchainLifecycle, callbackErrorReporter } from '@0xproject/dev-utils';
+import { FillScenarios } from '@0xproject/fill-scenarios';
+import { assetDataUtils, orderHashUtils } from '@0xproject/order-utils';
+import { DoneCallback, SignedOrder } from '@0xproject/types';
+import { BigNumber } from '@0xproject/utils';
+import * as chai from 'chai';
+import { BlockParamLiteral } from 'ethereum-types';
+import 'mocha';
+
+import {
+ ContractWrappers,
+ DecodedLogEvent,
+ ExchangeCancelEventArgs,
+ ExchangeEvents,
+ ExchangeFillEventArgs,
+ OrderStatus,
+} from '../src';
+
+import { chaiSetup } from './utils/chai_setup';
+import { constants } from './utils/constants';
+import { tokenUtils } from './utils/token_utils';
+import { provider, web3Wrapper } from './utils/web3_wrapper';
+
+chaiSetup.configure();
+const expect = chai.expect;
+const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
+
+describe('ForwarderWrapper', () => {
+ const contractWrappersConfig = {
+ networkId: constants.TESTRPC_NETWORK_ID,
+ blockPollingIntervalMs: 0,
+ };
+ const fillableAmount = new BigNumber(5);
+ const takerTokenFillAmount = new BigNumber(5);
+ let contractWrappers: ContractWrappers;
+ let fillScenarios: FillScenarios;
+ let forwarderContractAddress: string;
+ let exchangeContractAddress: string;
+ let zrxTokenAddress: string;
+ let userAddresses: string[];
+ let coinbase: string;
+ let makerAddress: string;
+ let takerAddress: string;
+ let feeRecipient: string;
+ let anotherMakerAddress: string;
+ let makerTokenAddress: string;
+ let takerTokenAddress: string;
+ let makerAssetData: string;
+ let takerAssetData: string;
+ let signedOrder: SignedOrder;
+ let anotherSignedOrder: SignedOrder;
+ before(async () => {
+ await blockchainLifecycle.startAsync();
+ contractWrappers = new ContractWrappers(provider, contractWrappersConfig);
+ forwarderContractAddress = contractWrappers.forwarder.getContractAddress();
+ exchangeContractAddress = contractWrappers.exchange.getContractAddress();
+ userAddresses = await web3Wrapper.getAvailableAddressesAsync();
+ zrxTokenAddress = tokenUtils.getProtocolTokenAddress();
+ fillScenarios = new FillScenarios(
+ provider,
+ userAddresses,
+ zrxTokenAddress,
+ exchangeContractAddress,
+ contractWrappers.erc20Proxy.getContractAddress(),
+ contractWrappers.erc721Proxy.getContractAddress(),
+ );
+ [coinbase, makerAddress, takerAddress, feeRecipient, anotherMakerAddress] = userAddresses;
+ [makerTokenAddress] = tokenUtils.getDummyERC20TokenAddresses();
+ takerTokenAddress = tokenUtils.getWethTokenAddress();
+ [makerAssetData, takerAssetData] = [
+ assetDataUtils.encodeERC20AssetData(makerTokenAddress),
+ assetDataUtils.encodeERC20AssetData(takerTokenAddress),
+ ];
+ signedOrder = await fillScenarios.createFillableSignedOrderAsync(
+ makerAssetData,
+ takerAssetData,
+ makerAddress,
+ constants.NULL_ADDRESS,
+ fillableAmount,
+ );
+ anotherSignedOrder = await fillScenarios.createFillableSignedOrderAsync(
+ makerAssetData,
+ takerAssetData,
+ makerAddress,
+ constants.NULL_ADDRESS,
+ fillableAmount,
+ );
+ });
+ after(async () => {
+ await blockchainLifecycle.revertAsync();
+ });
+ beforeEach(async () => {
+ await blockchainLifecycle.startAsync();
+ });
+ afterEach(async () => {
+ await blockchainLifecycle.revertAsync();
+ });
+ describe('#marketBuyOrdersWithEthAsync', () => {
+ it('should market buy orders with eth', async () => {
+ const signedOrders = [signedOrder, anotherSignedOrder];
+ const makerAssetFillAmount = signedOrder.makerAssetAmount.plus(anotherSignedOrder.makerAssetAmount);
+ const txHash = await contractWrappers.forwarder.marketBuyOrdersWithEthAsync(
+ signedOrders,
+ makerAssetFillAmount,
+ takerAddress,
+ makerAssetFillAmount,
+ );
+ await web3Wrapper.awaitTransactionSuccessAsync(txHash, constants.AWAIT_TRANSACTION_MINED_MS);
+ const ordersInfo = await contractWrappers.exchange.getOrdersInfoAsync([signedOrder, anotherSignedOrder]);
+ expect(ordersInfo[0].orderStatus).to.be.equal(OrderStatus.FULLY_FILLED);
+ expect(ordersInfo[1].orderStatus).to.be.equal(OrderStatus.FULLY_FILLED);
+ });
+ });
+ describe('#marketSellOrdersWithEthAsync', () => {
+ it('should market sell orders with eth', async () => {
+ const signedOrders = [signedOrder, anotherSignedOrder];
+ const makerAssetFillAmount = signedOrder.makerAssetAmount.plus(anotherSignedOrder.makerAssetAmount);
+ const txHash = await contractWrappers.forwarder.marketSellOrdersWithEthAsync(
+ signedOrders,
+ takerAddress,
+ makerAssetFillAmount,
+ );
+ await web3Wrapper.awaitTransactionSuccessAsync(txHash, constants.AWAIT_TRANSACTION_MINED_MS);
+ const ordersInfo = await contractWrappers.exchange.getOrdersInfoAsync([signedOrder, anotherSignedOrder]);
+ expect(ordersInfo[0].orderStatus).to.be.equal(OrderStatus.FULLY_FILLED);
+ expect(ordersInfo[1].orderStatus).to.be.equal(OrderStatus.FILLABLE);
+ expect(ordersInfo[1].orderTakerAssetFilledAmount).to.be.bignumber.equal(new BigNumber(4)); // only 95% of ETH is sold
+ });
+ });
+});