static tests

This commit is contained in:
Michael Zhu
2019-11-20 14:32:30 -08:00
parent bb5885e2bb
commit 51f5e60224
9 changed files with 79 additions and 85 deletions

View File

@@ -2,6 +2,8 @@ export {
DummyERC721ReceiverContract,
DummyERC721TokenContract,
ERC721TokenContract,
ERC721TokenEvents,
ERC721TokenTransferEventArgs,
IERC721ReceiverContract,
} from './wrappers';
export { artifacts } from './artifacts';

View File

@@ -5,6 +5,9 @@ import { artifacts } from '../artifacts';
import { DeploymentManager } from '../framework/deployment_manager';
import { TestEth2DaiBridgeContract, TestEth2DaiContract } from '../wrappers';
/**
* Deploys test Eth2Dai exchange and bridge contracts configured to work alongside the provided `deployment`.
*/
export async function deployEth2DaiBridgeAsync(
deployment: DeploymentManager,
environment: BlockchainTestsEnvironment,

View File

@@ -9,6 +9,10 @@ import {
TestUniswapExchangeFactoryContract,
} from '../wrappers';
/**
* Deploys test Uniswap exchanges for the given tokens, a test UniswapExchangeFactory, and a test
* bridge contract configured to work alongside the provided `deployment`.
*/
export async function deployUniswapBridgeAsync(
deployment: DeploymentManager,
environment: BlockchainTestsEnvironment,

View File

@@ -1,5 +1,4 @@
import { CoordinatorContract, CoordinatorRevertErrors, SignedCoordinatorApproval } from '@0x/contracts-coordinator';
import { DevUtilsContract } from '@0x/contracts-dev-utils';
import {
ExchangeCancelEventArgs,
ExchangeCancelUpToEventArgs,
@@ -15,7 +14,6 @@ import {
hexConcat,
hexSlice,
orderHashUtils,
provider,
transactionHashUtils,
verifyEvents,
} from '@0x/contracts-test-utils';
@@ -38,7 +36,6 @@ blockchainTests.resets('Coordinator integration tests', env => {
let deployment: DeploymentManager;
let coordinator: CoordinatorContract;
let balanceStore: BlockchainBalanceStore;
let devUtils: DevUtilsContract;
let maker: Maker;
let taker: Actor;
@@ -51,7 +48,6 @@ blockchainTests.resets('Coordinator integration tests', env => {
numErc1155TokensToDeploy: 0,
});
coordinator = await deployCoordinatorAsync(deployment, env);
devUtils = new DevUtilsContract(constants.NULL_ADDRESS, provider);
const [makerToken, takerToken, makerFeeToken, takerFeeToken] = deployment.tokens.erc20;

View File

@@ -221,7 +221,6 @@ blockchainTests.resets('Exchange core', () => {
};
fillOrderWrapper = new FillOrderWrapper(
exchange,
devUtils,
{ makerAddress, takerAddress, feeRecipientAddress },
tokenContracts,
tokenIds,

View File

@@ -1,4 +1,3 @@
import { DevUtilsContract } from '@0x/contracts-dev-utils';
import { ExchangeContract } from '@0x/contracts-exchange';
import { ReferenceFunctions as LibReferenceFunctions } from '@0x/contracts-exchange-libs';
import {
@@ -56,65 +55,6 @@ export class FillOrderWrapper {
return events.map(event => _.pick(event, fieldsOfInterest)) as FillEventArgs[];
}
/**
* Locally simulates filling an order.
* @param txReceipt Transaction receipt from the actual fill, needed to update eth balance
* @param signedOrder The order being filled.
* @param takerAddress Address of taker (the address who matched the two orders)
* @param opts Optionally specifies the amount to fill.
* @param initBalanceStore Account balances prior to the fill.
* @return The expected account balances, fill results, and fill events.
*/
public async simulateFillOrderAsync(
txReceipt: TransactionReceiptWithDecodedLogs,
signedOrder: SignedOrder,
takerAddress: string,
initBalanceStore: BalanceStore,
opts: { takerAssetFillAmount?: BigNumber } = {},
): Promise<[FillResults, FillEventArgs, BalanceStore]> {
const balanceStore = LocalBalanceStore.create(initBalanceStore);
const takerAssetFillAmount =
opts.takerAssetFillAmount !== undefined ? opts.takerAssetFillAmount : signedOrder.takerAssetAmount;
// TODO(jalextowle): Change this if the integration tests take protocol fees into account.
const fillResults = LibReferenceFunctions.calculateFillResults(
signedOrder,
takerAssetFillAmount,
constants.ZERO_AMOUNT,
constants.ZERO_AMOUNT,
);
const fillEvent = FillOrderWrapper.simulateFillEvent(signedOrder, takerAddress, fillResults);
// Taker -> Maker
await balanceStore.transferAssetAsync(
takerAddress,
signedOrder.makerAddress,
fillResults.takerAssetFilledAmount,
signedOrder.takerAssetData,
);
// Maker -> Taker
await balanceStore.transferAssetAsync(
signedOrder.makerAddress,
takerAddress,
fillResults.makerAssetFilledAmount,
signedOrder.makerAssetData,
);
// Taker -> Fee Recipient
await balanceStore.transferAssetAsync(
takerAddress,
signedOrder.feeRecipientAddress,
fillResults.takerFeePaid,
signedOrder.takerFeeAssetData,
);
// Maker -> Fee Recipient
await balanceStore.transferAssetAsync(
signedOrder.makerAddress,
signedOrder.feeRecipientAddress,
fillResults.makerFeePaid,
signedOrder.makerFeeAssetData,
);
balanceStore.burnGas(txReceipt.from, constants.DEFAULT_GAS_PRICE * txReceipt.gasUsed);
return [fillResults, fillEvent, balanceStore];
}
/**
* Constructor.
* @param exchangeContract Instance of the deployed exchange contract.
@@ -124,7 +64,6 @@ export class FillOrderWrapper {
*/
public constructor(
private readonly _exchange: ExchangeContract,
private readonly _devUtils: DevUtilsContract,
tokenOwnersByName: TokenOwnersByName,
tokenContractsByName: Partial<TokenContractsByName>,
tokenIds: Partial<TokenIds>,
@@ -160,11 +99,13 @@ export class FillOrderWrapper {
await this._assertOrderStateAsync(signedOrder, initTakerAssetFilledAmount);
// Simulate and execute fill then assert outputs
const [fillResults, fillEvent, txReceipt] = await this._fillOrderAsync(signedOrder, from, opts);
const [
simulatedFillResults,
simulatedFillEvent,
simulatedFinalBalanceStore,
] = await this.simulateFillOrderAsync(txReceipt, signedOrder, from, this._blockchainBalanceStore, opts);
const [simulatedFillResults, simulatedFillEvent, simulatedFinalBalanceStore] = await simulateFillOrderAsync(
txReceipt,
signedOrder,
from,
this._blockchainBalanceStore,
opts,
);
// Assert state transition
expect(simulatedFillResults, 'Fill Results').to.be.deep.equal(fillResults);
expect(simulatedFillEvent, 'Fill Events').to.be.deep.equal(fillEvent);
@@ -218,3 +159,62 @@ export class FillOrderWrapper {
expect(actualStatus, 'order status').to.equal(expectedStatus);
}
}
/**
* Locally simulates filling an order.
* @param txReceipt Transaction receipt from the actual fill, needed to update eth balance
* @param signedOrder The order being filled.
* @param takerAddress Address of taker (the address who matched the two orders)
* @param opts Optionally specifies the amount to fill.
* @param initBalanceStore Account balances prior to the fill.
* @return The expected account balances, fill results, and fill events.
*/
async function simulateFillOrderAsync(
txReceipt: TransactionReceiptWithDecodedLogs,
signedOrder: SignedOrder,
takerAddress: string,
initBalanceStore: BalanceStore,
opts: { takerAssetFillAmount?: BigNumber } = {},
): Promise<[FillResults, FillEventArgs, BalanceStore]> {
const balanceStore = LocalBalanceStore.create(initBalanceStore);
const takerAssetFillAmount =
opts.takerAssetFillAmount !== undefined ? opts.takerAssetFillAmount : signedOrder.takerAssetAmount;
// TODO(jalextowle): Change this if the integration tests take protocol fees into account.
const fillResults = LibReferenceFunctions.calculateFillResults(
signedOrder,
takerAssetFillAmount,
constants.ZERO_AMOUNT,
constants.ZERO_AMOUNT,
);
const fillEvent = FillOrderWrapper.simulateFillEvent(signedOrder, takerAddress, fillResults);
// Taker -> Maker
await balanceStore.transferAssetAsync(
takerAddress,
signedOrder.makerAddress,
fillResults.takerAssetFilledAmount,
signedOrder.takerAssetData,
);
// Maker -> Taker
await balanceStore.transferAssetAsync(
signedOrder.makerAddress,
takerAddress,
fillResults.makerAssetFilledAmount,
signedOrder.makerAssetData,
);
// Taker -> Fee Recipient
await balanceStore.transferAssetAsync(
takerAddress,
signedOrder.feeRecipientAddress,
fillResults.takerFeePaid,
signedOrder.takerFeeAssetData,
);
// Maker -> Fee Recipient
await balanceStore.transferAssetAsync(
signedOrder.makerAddress,
signedOrder.feeRecipientAddress,
fillResults.makerFeePaid,
signedOrder.makerFeeAssetData,
);
balanceStore.burnGas(txReceipt.from, constants.DEFAULT_GAS_PRICE * txReceipt.gasUsed);
return [fillResults, fillEvent, balanceStore];
}

View File

@@ -168,7 +168,7 @@ export class MatchOrderTester {
// Update the blockchain balance store and create a new local balance store
// with the same initial balances.
await this._blockchainBalanceStore.updateBalancesAsync();
const localBalanceStore = LocalBalanceStore.create(this._deployment.devUtils, this._blockchainBalanceStore);
const localBalanceStore = LocalBalanceStore.create(this._blockchainBalanceStore);
// Execute `batchMatchOrders()`
let actualBatchMatchResults;
@@ -253,7 +253,7 @@ export class MatchOrderTester {
// Update the blockchain balance store and create a new local balance store
// with the same initial balances.
await this._blockchainBalanceStore.updateBalancesAsync();
const localBalanceStore = LocalBalanceStore.create(this._deployment.devUtils, this._blockchainBalanceStore);
const localBalanceStore = LocalBalanceStore.create(this._blockchainBalanceStore);
// Execute `matchOrders()`
let actualMatchResults;

View File

@@ -1,4 +1,3 @@
import { DevUtilsContract } from '@0x/contracts-dev-utils';
import { DummyERC20TokenContract } from '@0x/contracts-erc20';
import { DummyERC721TokenContract } from '@0x/contracts-erc721';
import { artifacts as exchangeArtifacts, ExchangeContract } from '@0x/contracts-exchange';
@@ -9,7 +8,6 @@ import {
expect,
getLatestBlockTimestampAsync,
getPercentageOfValue,
provider,
toBaseUnitAmount,
} from '@0x/contracts-test-utils';
import { BigNumber } from '@0x/utils';
@@ -26,8 +24,6 @@ import { DeploymentManager } from '../framework/deployment_manager';
import { deployForwarderAsync } from './deploy_forwarder';
import { ForwarderTestFactory } from './forwarder_test_factory';
const devUtils = new DevUtilsContract(constants.NULL_ADDRESS, provider);
blockchainTests('Forwarder integration tests', env => {
let deployment: DeploymentManager;
let forwarder: ForwarderContract;
@@ -106,13 +102,7 @@ blockchainTests('Forwarder integration tests', env => {
const tokenIds = { erc721: { [erc721Token.address]: [nftId] } };
balanceStore = new BlockchainBalanceStore(tokenOwners, tokenContracts, tokenIds);
testFactory = new ForwarderTestFactory(
forwarder,
deployment,
balanceStore,
taker,
forwarderFeeRecipient,
);
testFactory = new ForwarderTestFactory(forwarder, deployment, balanceStore, taker, forwarderFeeRecipient);
});
after(async () => {

View File

@@ -8,7 +8,7 @@ import { BalanceStore } from './balance_store';
import { TokenContractsByName, TokenOwnersByName } from './types';
export class LocalBalanceStore extends BalanceStore {
private _assetDataDecoder: IAssetDataContract;
private readonly _assetDataDecoder: IAssetDataContract;
/**
* Creates a new balance store based on an existing one.