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

Commit

Permalink
static tests
Browse files Browse the repository at this point in the history
  • Loading branch information
moodlezoup committed Nov 20, 2019
1 parent db8ed91 commit e05d09f
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 83 deletions.
2 changes: 2 additions & 0 deletions contracts/erc721/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export {
DummyERC721ReceiverContract,
DummyERC721TokenContract,
ERC721TokenContract,
ERC721TokenEvents,
ERC721TokenTransferEventArgs,
IERC721ReceiverContract,
} from './wrappers';
export { artifacts } from './artifacts';
Expand Down
3 changes: 3 additions & 0 deletions contracts/integrations/test/bridges/deploy_eth2dai_bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions contracts/integrations/test/bridges/deploy_uniswap_bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 0 additions & 4 deletions contracts/integrations/test/coordinator/coordinator_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { CoordinatorContract, CoordinatorRevertErrors, SignedCoordinatorApproval } from '@0x/contracts-coordinator';
import { DevUtilsContract } from '@0x/contracts-dev-utils';
import {
ExchangeCancelEventArgs,
ExchangeCancelUpToEventArgs,
Expand All @@ -15,7 +14,6 @@ import {
hexConcat,
hexSlice,
orderHashUtils,
provider,
transactionHashUtils,
verifyEvents,
} from '@0x/contracts-test-utils';
Expand All @@ -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;
Expand All @@ -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;

Expand Down
1 change: 0 additions & 1 deletion contracts/integrations/test/exchange/core_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ blockchainTests.resets('Exchange core', () => {
};
fillOrderWrapper = new FillOrderWrapper(
exchange,
devUtils,
{ makerAddress, takerAddress, feeRecipientAddress },
tokenContracts,
tokenIds,
Expand Down
132 changes: 66 additions & 66 deletions contracts/integrations/test/exchange/fill_order_wrapper.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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.
Expand All @@ -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>,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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];
}
12 changes: 1 addition & 11 deletions contracts/integrations/test/forwarder/forwarder_test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -9,7 +8,6 @@ import {
expect,
getLatestBlockTimestampAsync,
getPercentageOfValue,
provider,
toBaseUnitAmount,
} from '@0x/contracts-test-utils';
import { BigNumber } from '@0x/utils';
Expand All @@ -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;
Expand Down Expand Up @@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit e05d09f

Please sign in to comment.