Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
Merge pull request #2357 from 0xProject/refactor/integrations/transac…
Browse files Browse the repository at this point in the history
…tion-tests

`@0x/contracts-integrations`: Transaction integration tests
  • Loading branch information
moodlezoup authored Dec 3, 2019
2 parents 761d0a0 + 2232870 commit 8685cf9
Show file tree
Hide file tree
Showing 23 changed files with 1,502 additions and 1,260 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ blockchainTests('erc20-bridge-sampler', env => {
}

function getDeterministicOrderInfo(order: Order): OrderInfo {
const hash = getPackedHash(toHex(order.salt, 32));
const hash = getPackedHash(hexLeftPad(order.salt, 32));
return {
orderHash: hash,
orderStatus: new BigNumber(hash).mod(255).toNumber(),
Expand Down
996 changes: 0 additions & 996 deletions contracts/exchange/test/transactions.ts

This file was deleted.

117 changes: 33 additions & 84 deletions contracts/integrations/test/coordinator/coordinator_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
} from '@0x/contracts-test-utils';
import { SignedOrder, SignedZeroExTransaction } from '@0x/types';
import { BigNumber } from '@0x/utils';
import { TransactionReceiptWithDecodedLogs } from 'ethereum-types';

import { Actor } from '../framework/actors/base';
import { FeeRecipient } from '../framework/actors/fee_recipient';
Expand Down Expand Up @@ -96,72 +95,9 @@ blockchainTests.resets('Coordinator integration tests', env => {
});

after(async () => {
Actor.count = 0;
Actor.reset();
});

async function simulateFillsAsync(
orders: SignedOrder[],
txReceipt: TransactionReceiptWithDecodedLogs,
msgValue?: BigNumber,
): Promise<LocalBalanceStore> {
let remainingValue = msgValue || constants.ZERO_AMOUNT;
const localBalanceStore = LocalBalanceStore.create(balanceStore);
// Transaction gas cost
localBalanceStore.burnGas(txReceipt.from, DeploymentManager.gasPrice.times(txReceipt.gasUsed));

for (const order of orders) {
// Taker -> Maker
await localBalanceStore.transferAssetAsync(
taker.address,
maker.address,
order.takerAssetAmount,
order.takerAssetData,
);
// Maker -> Taker
await localBalanceStore.transferAssetAsync(
maker.address,
taker.address,
order.makerAssetAmount,
order.makerAssetData,
);
// Taker -> Fee Recipient
await localBalanceStore.transferAssetAsync(
taker.address,
feeRecipient.address,
order.takerFee,
order.takerFeeAssetData,
);
// Maker -> Fee Recipient
await localBalanceStore.transferAssetAsync(
maker.address,
feeRecipient.address,
order.makerFee,
order.makerFeeAssetData,
);

// Protocol fee
if (remainingValue.isGreaterThanOrEqualTo(DeploymentManager.protocolFee)) {
localBalanceStore.sendEth(
txReceipt.from,
deployment.staking.stakingProxy.address,
DeploymentManager.protocolFee,
);
remainingValue = remainingValue.minus(DeploymentManager.protocolFee);
} else {
await localBalanceStore.transferAssetAsync(
taker.address,
deployment.staking.stakingProxy.address,
DeploymentManager.protocolFee,
deployment.assetDataEncoder
.ERC20Token(deployment.tokens.weth.address)
.getABIEncodedTransactionData(),
);
}
}

return localBalanceStore;
}

function expectedFillEvent(order: SignedOrder): ExchangeFillEventArgs {
return {
makerAddress: order.makerAddress,
Expand Down Expand Up @@ -191,10 +127,7 @@ blockchainTests.resets('Coordinator integration tests', env => {
before(async () => {
order = await maker.signOrderAsync();
data = exchangeDataEncoder.encodeOrdersToExchangeData(fnName, [order]);
transaction = await taker.signTransactionAsync({
data,
gasPrice: DeploymentManager.gasPrice,
});
transaction = await taker.signTransactionAsync({ data });
approval = await feeRecipient.signCoordinatorApprovalAsync(transaction, taker.address);
});

Expand All @@ -204,7 +137,14 @@ blockchainTests.resets('Coordinator integration tests', env => {
.executeTransaction(transaction, taker.address, transaction.signature, [approval.signature])
.awaitTransactionSuccessAsync({ from: taker.address, value: DeploymentManager.protocolFee });

const expectedBalances = await simulateFillsAsync([order], txReceipt, DeploymentManager.protocolFee);
const expectedBalances = LocalBalanceStore.create(balanceStore);
expectedBalances.simulateFills(
[order],
taker.address,
txReceipt,
deployment,
DeploymentManager.protocolFee,
);
await balanceStore.updateBalancesAsync();
balanceStore.assertEquals(expectedBalances);
verifyEvents(txReceipt, [expectedFillEvent(order)], ExchangeEvents.Fill);
Expand All @@ -215,7 +155,14 @@ blockchainTests.resets('Coordinator integration tests', env => {
.executeTransaction(transaction, feeRecipient.address, transaction.signature, [])
.awaitTransactionSuccessAsync({ from: feeRecipient.address, value: DeploymentManager.protocolFee });

const expectedBalances = await simulateFillsAsync([order], txReceipt, DeploymentManager.protocolFee);
const expectedBalances = LocalBalanceStore.create(balanceStore);
expectedBalances.simulateFills(
[order],
taker.address,
txReceipt,
deployment,
DeploymentManager.protocolFee,
);
await balanceStore.updateBalancesAsync();
balanceStore.assertEquals(expectedBalances);
verifyEvents(txReceipt, [expectedFillEvent(order)], ExchangeEvents.Fill);
Expand All @@ -229,9 +176,12 @@ blockchainTests.resets('Coordinator integration tests', env => {
value: DeploymentManager.protocolFee.plus(1),
});

const expectedBalances = await simulateFillsAsync(
const expectedBalances = LocalBalanceStore.create(balanceStore);
expectedBalances.simulateFills(
[order],
taker.address,
txReceipt,
deployment,
DeploymentManager.protocolFee.plus(1),
);
await balanceStore.updateBalancesAsync();
Expand All @@ -244,7 +194,8 @@ blockchainTests.resets('Coordinator integration tests', env => {
.executeTransaction(transaction, feeRecipient.address, transaction.signature, [])
.awaitTransactionSuccessAsync({ from: feeRecipient.address });

const expectedBalances = await simulateFillsAsync([order], txReceipt);
const expectedBalances = LocalBalanceStore.create(balanceStore);
expectedBalances.simulateFills([order], taker.address, txReceipt, deployment);
await balanceStore.updateBalancesAsync();
balanceStore.assertEquals(expectedBalances);
verifyEvents(txReceipt, [expectedFillEvent(order)], ExchangeEvents.Fill);
Expand All @@ -255,7 +206,8 @@ blockchainTests.resets('Coordinator integration tests', env => {
.executeTransaction(transaction, feeRecipient.address, transaction.signature, [])
.awaitTransactionSuccessAsync({ from: feeRecipient.address, value: new BigNumber(1) });

const expectedBalances = await simulateFillsAsync([order], txReceipt, new BigNumber(1));
const expectedBalances = LocalBalanceStore.create(balanceStore);
expectedBalances.simulateFills([order], taker.address, txReceipt, deployment, new BigNumber(1));
await balanceStore.updateBalancesAsync();
balanceStore.assertEquals(expectedBalances);
verifyEvents(txReceipt, [expectedFillEvent(order)], ExchangeEvents.Fill);
Expand Down Expand Up @@ -309,10 +261,7 @@ blockchainTests.resets('Coordinator integration tests', env => {
before(async () => {
orders = [await maker.signOrderAsync(), await maker.signOrderAsync()];
data = exchangeDataEncoder.encodeOrdersToExchangeData(fnName, orders);
transaction = await taker.signTransactionAsync({
data,
gasPrice: DeploymentManager.gasPrice,
});
transaction = await taker.signTransactionAsync({ data });
approval = await feeRecipient.signCoordinatorApprovalAsync(transaction, taker.address);
});

Expand All @@ -323,7 +272,8 @@ blockchainTests.resets('Coordinator integration tests', env => {
.executeTransaction(transaction, taker.address, transaction.signature, [approval.signature])
.awaitTransactionSuccessAsync({ from: taker.address, value });

const expectedBalances = await simulateFillsAsync(orders, txReceipt, value);
const expectedBalances = LocalBalanceStore.create(balanceStore);
expectedBalances.simulateFills(orders, taker.address, txReceipt, deployment, value);
await balanceStore.updateBalancesAsync();
balanceStore.assertEquals(expectedBalances);
verifyEvents(txReceipt, orders.map(order => expectedFillEvent(order)), ExchangeEvents.Fill);
Expand All @@ -335,7 +285,8 @@ blockchainTests.resets('Coordinator integration tests', env => {
.executeTransaction(transaction, feeRecipient.address, transaction.signature, [])
.awaitTransactionSuccessAsync({ from: feeRecipient.address, value });

const expectedBalances = await simulateFillsAsync(orders, txReceipt, value);
const expectedBalances = LocalBalanceStore.create(balanceStore);
expectedBalances.simulateFills(orders, taker.address, txReceipt, deployment, value);
await balanceStore.updateBalancesAsync();
balanceStore.assertEquals(expectedBalances);
verifyEvents(txReceipt, orders.map(order => expectedFillEvent(order)), ExchangeEvents.Fill);
Expand All @@ -347,7 +298,8 @@ blockchainTests.resets('Coordinator integration tests', env => {
.executeTransaction(transaction, feeRecipient.address, transaction.signature, [])
.awaitTransactionSuccessAsync({ from: feeRecipient.address, value });

const expectedBalances = await simulateFillsAsync(orders, txReceipt, value);
const expectedBalances = LocalBalanceStore.create(balanceStore);
expectedBalances.simulateFills(orders, taker.address, txReceipt, deployment, value);
await balanceStore.updateBalancesAsync();
balanceStore.assertEquals(expectedBalances);
verifyEvents(txReceipt, orders.map(order => expectedFillEvent(order)), ExchangeEvents.Fill);
Expand Down Expand Up @@ -400,7 +352,6 @@ blockchainTests.resets('Coordinator integration tests', env => {
const data = exchangeDataEncoder.encodeOrdersToExchangeData(ExchangeFunctionName.CancelOrder, [order]);
const transaction = await maker.signTransactionAsync({
data,
gasPrice: DeploymentManager.gasPrice,
});
const txReceipt = await coordinator
.executeTransaction(transaction, maker.address, transaction.signature, [])
Expand All @@ -413,7 +364,6 @@ blockchainTests.resets('Coordinator integration tests', env => {
const data = exchangeDataEncoder.encodeOrdersToExchangeData(ExchangeFunctionName.BatchCancelOrders, orders);
const transaction = await maker.signTransactionAsync({
data,
gasPrice: DeploymentManager.gasPrice,
});
const txReceipt = await coordinator
.executeTransaction(transaction, maker.address, transaction.signature, [])
Expand All @@ -425,7 +375,6 @@ blockchainTests.resets('Coordinator integration tests', env => {
const data = exchangeDataEncoder.encodeOrdersToExchangeData(ExchangeFunctionName.CancelOrdersUpTo, []);
const transaction = await maker.signTransactionAsync({
data,
gasPrice: DeploymentManager.gasPrice,
});
const txReceipt = await coordinator
.executeTransaction(transaction, maker.address, transaction.signature, [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ blockchainTests.resets('matchOrders integration tests', env => {
});

after(async () => {
Actor.count = 0;
Actor.reset();
});

describe('batchMatchOrders and batchMatchOrdersWithMaximalFill rich errors', async () => {
Expand Down
30 changes: 13 additions & 17 deletions contracts/integrations/test/exchange/exchange_wrapper_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,45 +124,41 @@ blockchainTests.resets('Exchange wrappers', env => {
});

after(async () => {
Actor.count = 0;
Actor.reset();
});

interface SignedOrderWithValidity {
signedOrder: SignedOrder;
isValid: boolean;
}

async function simulateFillAsync(
signedOrder: SignedOrder,
expectedFillResults: FillResults,
shouldUseWeth: boolean,
): Promise<void> {
function simulateFill(signedOrder: SignedOrder, expectedFillResults: FillResults, shouldUseWeth: boolean): void {
// taker -> maker
await localBalances.transferAssetAsync(
localBalances.transferAsset(
taker.address,
maker.address,
expectedFillResults.takerAssetFilledAmount,
signedOrder.takerAssetData,
);

// maker -> taker
await localBalances.transferAssetAsync(
localBalances.transferAsset(
maker.address,
taker.address,
expectedFillResults.makerAssetFilledAmount,
signedOrder.makerAssetData,
);

// maker -> feeRecipient
await localBalances.transferAssetAsync(
localBalances.transferAsset(
maker.address,
feeRecipient,
expectedFillResults.makerFeePaid,
signedOrder.makerFeeAssetData,
);

// taker -> feeRecipient
await localBalances.transferAssetAsync(
localBalances.transferAsset(
taker.address,
feeRecipient,
expectedFillResults.takerFeePaid,
Expand All @@ -171,7 +167,7 @@ blockchainTests.resets('Exchange wrappers', env => {

// taker -> protocol fees
if (shouldUseWeth) {
await localBalances.transferAssetAsync(
localBalances.transferAsset(
taker.address,
deployment.staking.stakingProxy.address,
expectedFillResults.protocolFeePaid,
Expand Down Expand Up @@ -343,7 +339,7 @@ blockchainTests.resets('Exchange wrappers', env => {
const shouldPayWethFees = DeploymentManager.protocolFee.gt(value);

// Simulate filling the order
await simulateFillAsync(signedOrder, expectedFillResults, shouldPayWethFees);
simulateFill(signedOrder, expectedFillResults, shouldPayWethFees);

// Ensure that the correct logs were emitted and that the balances are accurate.
await assertResultsAsync(receipt, [{ signedOrder, expectedFillResults, shouldPayWethFees }]);
Expand Down Expand Up @@ -444,7 +440,7 @@ blockchainTests.resets('Exchange wrappers', env => {
}

fillTestInfo.push({ signedOrder, expectedFillResults, shouldPayWethFees });
await simulateFillAsync(signedOrder, expectedFillResults, shouldPayWethFees);
simulateFill(signedOrder, expectedFillResults, shouldPayWethFees);
}

const contractFn = deployment.exchange.batchFillOrders(
Expand Down Expand Up @@ -506,7 +502,7 @@ blockchainTests.resets('Exchange wrappers', env => {
}

fillTestInfo.push({ signedOrder, expectedFillResults, shouldPayWethFees });
await simulateFillAsync(signedOrder, expectedFillResults, shouldPayWethFees);
simulateFill(signedOrder, expectedFillResults, shouldPayWethFees);
}

const contractFn = deployment.exchange.batchFillOrKillOrders(
Expand Down Expand Up @@ -600,7 +596,7 @@ blockchainTests.resets('Exchange wrappers', env => {
}

fillTestInfo.push({ signedOrder, expectedFillResults, shouldPayWethFees });
await simulateFillAsync(signedOrder, expectedFillResults, shouldPayWethFees);
simulateFill(signedOrder, expectedFillResults, shouldPayWethFees);
} else {
totalFillResults.push(nullFillResults);
}
Expand Down Expand Up @@ -714,7 +710,7 @@ blockchainTests.resets('Exchange wrappers', env => {
takerFillAmount,
);

await simulateFillAsync(signedOrder, expectedFillResults, shouldPayWethFees);
simulateFill(signedOrder, expectedFillResults, shouldPayWethFees);
fillTestInfo.push({ signedOrder, expectedFillResults, shouldPayWethFees });

totalFillResults = addFillResults(totalFillResults, expectedFillResults);
Expand Down Expand Up @@ -912,7 +908,7 @@ blockchainTests.resets('Exchange wrappers', env => {
makerAssetBought,
);

await simulateFillAsync(signedOrder, expectedFillResults, shouldPayWethFees);
simulateFill(signedOrder, expectedFillResults, shouldPayWethFees);
fillTestInfo.push({ signedOrder, expectedFillResults, shouldPayWethFees });

totalFillResults = addFillResults(totalFillResults, expectedFillResults);
Expand Down
Loading

0 comments on commit 8685cf9

Please sign in to comment.