Ran prettier and fixed linter errors

This commit is contained in:
Lawrence Forman
2019-05-16 16:09:27 -04:00
committed by Amir Bandeali
parent 79f40b4ce0
commit 9d257497bd
6 changed files with 179 additions and 255 deletions

View File

@@ -73,6 +73,7 @@ contract MixinWrapperFunctions is
{
// ABI encode calldata for `fillOrder`
bytes memory fillOrderCalldata = abi.encodeWithSelector(
// solhint-disable-next-line max-line-length
// bytes4(keccak256("fillOrder((address,address,address,address,uint256,uint256,uint256,uint256,uint256,uint256,bytes,bytes,bytes,bytes),uint256,bytes)"))
// = 0x9b44d556
0x9b44d556,

View File

@@ -29,7 +29,7 @@ contract MExchangeCore is
{
// Fill event signature.
// keccak("Fill(address,address,address,address,uint256,uint256,uint256,uint256,bytes32,bytes,bytes,bytes,bytes)")
bytes32 constant FILL_EVENT_SIGNATURE = 0xcb32b586b1d019abfd3dfc2d45e7275f145185e9d53359e9b99521ca88cea0e8;
bytes32 internal constant FILL_EVENT_SIGNATURE = 0xcb32b586b1d019abfd3dfc2d45e7275f145185e9d53359e9b99521ca88cea0e8;
// Fill event is emitted whenever an order is filled.
event Fill(

View File

@@ -1,14 +1,7 @@
import { ERC20ProxyContract, ERC20Wrapper, ERC721ProxyContract, ERC721Wrapper } from '@0x/contracts-asset-proxy';
import { DummyERC20TokenContract } from '@0x/contracts-erc20';
import { DummyERC721TokenContract } from '@0x/contracts-erc721';
import {
chaiSetup,
constants,
OrderFactory,
provider,
txDefaults,
web3Wrapper,
} from '@0x/contracts-test-utils';
import { chaiSetup, constants, OrderFactory, provider, txDefaults, web3Wrapper } from '@0x/contracts-test-utils';
import { BlockchainLifecycle } from '@0x/dev-utils';
import { assetDataUtils, ExchangeRevertErrors, orderHashUtils } from '@0x/order-utils';
import { OrderStatus, RevertReason } from '@0x/types';
@@ -78,14 +71,14 @@ describe('matchOrders', () => {
chainId = await providerUtils.getChainIdAsync(provider);
// Create accounts
const accounts = await web3Wrapper.getAvailableAddressesAsync();
const usedAddresses = [
const usedAddresses = ([
owner,
makerAddressLeft,
makerAddressRight,
takerAddress,
feeRecipientAddressLeft,
feeRecipientAddressRight,
] = accounts;
] = accounts);
// Create wrappers
erc20Wrapper = new ERC20Wrapper(provider, usedAddresses, owner);
erc721Wrapper = new ERC721Wrapper(provider, usedAddresses, owner);
@@ -226,12 +219,18 @@ describe('matchOrders', () => {
const expectedTransferAmounts = {
// Left Maker
leftMakerAssetSoldByLeftMakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(13), 0),
leftMakerFeeAssetPaidByLeftMakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber('76.4705882352941176'), 16), // 76.47%
leftMakerFeeAssetPaidByLeftMakerAmount: Web3Wrapper.toBaseUnitAmount(
new BigNumber('76.4705882352941176'),
16,
), // 76.47%
// Right Maker
rightMakerAssetSoldByRightMakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(75), 0),
rightMakerFeeAssetPaidByRightMakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(100), 16), // 100%
// Taker
leftTakerFeeAssetPaidByTakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber('76.5306122448979591'), 16), // 76.53%
leftTakerFeeAssetPaidByTakerAmount: Web3Wrapper.toBaseUnitAmount(
new BigNumber('76.5306122448979591'),
16,
), // 76.53%
rightTakerFeeAssetPaidByTakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(100), 16), // 100%
};
await matchOrderTester.matchOrdersAndAssertEffectsAsync(
@@ -289,11 +288,17 @@ describe('matchOrders', () => {
// Right Maker
leftMakerAssetBoughtByRightMakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(13), 0),
rightMakerAssetSoldByRightMakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(90), 0),
rightMakerFeeAssetPaidByRightMakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber('92.7835051546391752'), 16), // 92.78%
rightMakerFeeAssetPaidByRightMakerAmount: Web3Wrapper.toBaseUnitAmount(
new BigNumber('92.7835051546391752'),
16,
), // 92.78%
// Taker
leftMakerAssetReceivedByTakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(2), 0),
leftTakerFeeAssetPaidByTakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(100), 16), // 100%
rightTakerFeeAssetPaidByTakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber('92.8571428571428571'), 16), // 92.85%
rightTakerFeeAssetPaidByTakerAmount: Web3Wrapper.toBaseUnitAmount(
new BigNumber('92.8571428571428571'),
16,
), // 92.85%
};
await matchOrderTester.matchOrdersAndAssertEffectsAsync(
{
@@ -335,11 +340,17 @@ describe('matchOrders', () => {
// Right Maker
rightMakerAssetSoldByRightMakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(22), 0),
leftMakerAssetBoughtByRightMakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(13), 0),
rightMakerFeeAssetPaidByRightMakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber('26.5060240963855421'), 16), // 26.506%
rightMakerFeeAssetPaidByRightMakerAmount: Web3Wrapper.toBaseUnitAmount(
new BigNumber('26.5060240963855421'),
16,
), // 26.506%
// Taker
leftMakerAssetReceivedByTakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(3), 0),
leftTakerFeeAssetPaidByTakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(100), 16), // 100%
rightTakerFeeAssetPaidByTakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber('26.5306122448979591'), 16), // 26.531%
rightTakerFeeAssetPaidByTakerAmount: Web3Wrapper.toBaseUnitAmount(
new BigNumber('26.5306122448979591'),
16,
), // 26.531%
};
// Match signedOrderLeft with signedOrderRight
await matchOrderTester.matchOrdersAndAssertEffectsAsync(
@@ -375,14 +386,20 @@ describe('matchOrders', () => {
const expectedTransferAmounts = {
// Left Maker
leftMakerAssetSoldByLeftMakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(11), 0),
leftMakerFeeAssetPaidByLeftMakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber('91.6666666666666666'), 16), // 91.6%
leftMakerFeeAssetPaidByLeftMakerAmount: Web3Wrapper.toBaseUnitAmount(
new BigNumber('91.6666666666666666'),
16,
), // 91.6%
// Right Maker
rightMakerAssetSoldByRightMakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(89), 0),
leftMakerAssetBoughtByRightMakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(1), 0),
rightMakerFeeAssetPaidByRightMakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(100), 16), // 100%
// Taker
leftMakerAssetReceivedByTakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(10), 0),
leftTakerFeeAssetPaidByTakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber('91.7525773195876288'), 16), // 91.75%
leftTakerFeeAssetPaidByTakerAmount: Web3Wrapper.toBaseUnitAmount(
new BigNumber('91.7525773195876288'),
16,
), // 91.75%
rightTakerFeeAssetPaidByTakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(100), 16), // 100%
};
// Match signedOrderLeft with signedOrderRight
@@ -525,11 +542,17 @@ describe('matchOrders', () => {
// The right maker always receives the better exchange and fee price.
rightMakerAssetSoldByRightMakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(1005), 0),
leftMakerAssetBoughtByRightMakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(503), 0),
rightMakerFeeAssetPaidByRightMakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber('47.2718720602069614'), 16), // 47.27%
rightMakerFeeAssetPaidByRightMakerAmount: Web3Wrapper.toBaseUnitAmount(
new BigNumber('47.2718720602069614'),
16,
), // 47.27%
// Taker
leftMakerAssetReceivedByTakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(497), 0),
leftTakerFeeAssetPaidByTakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber(100), 16), // 100%
rightTakerFeeAssetPaidByTakerAmount: Web3Wrapper.toBaseUnitAmount(new BigNumber('47.3189087488240827'), 16), // 47.31%
rightTakerFeeAssetPaidByTakerAmount: Web3Wrapper.toBaseUnitAmount(
new BigNumber('47.3189087488240827'),
16,
), // 47.31%
};
// Match signedOrderLeft with signedOrderRight
await matchOrderTester.matchOrdersAndAssertEffectsAsync(

View File

@@ -76,17 +76,11 @@ export async function fillOrderCombinatorialUtilsFactoryAsync(
const erc20FiveDecimalTokenCount = 2;
const fiveDecimals = new BigNumber(5);
const erc20FiveDecimalTokens = await erc20Wrapper.deployDummyTokensAsync(
erc20FiveDecimalTokenCount,
fiveDecimals,
);
const erc20FiveDecimalTokens = await erc20Wrapper.deployDummyTokensAsync(erc20FiveDecimalTokenCount, fiveDecimals);
const zeroDecimals = new BigNumber(0);
const erc20ZeroDecimalTokenCount = 2;
const erc20ZeroDecimalTokens = await erc20Wrapper.deployDummyTokensAsync(
erc20ZeroDecimalTokenCount,
zeroDecimals,
);
const erc20ZeroDecimalTokens = await erc20Wrapper.deployDummyTokensAsync(erc20ZeroDecimalTokenCount, zeroDecimals);
const erc20Proxy = await erc20Wrapper.deployProxyAsync();
await erc20Wrapper.setBalancesAndAllowancesAsync();
@@ -108,17 +102,11 @@ export async function fillOrderCombinatorialUtilsFactoryAsync(
await exchangeWrapper.registerAssetProxyAsync(erc721Proxy.address, ownerAddress);
await web3Wrapper.awaitTransactionSuccessAsync(
await erc20Proxy.addAuthorizedAddress.sendTransactionAsync(
exchangeContract.address,
{ from: ownerAddress },
),
await erc20Proxy.addAuthorizedAddress.sendTransactionAsync(exchangeContract.address, { from: ownerAddress }),
constants.AWAIT_TRANSACTION_MINED_MS,
);
await web3Wrapper.awaitTransactionSuccessAsync(
await erc721Proxy.addAuthorizedAddress.sendTransactionAsync(
exchangeContract.address,
{ from: ownerAddress }
),
await erc721Proxy.addAuthorizedAddress.sendTransactionAsync(exchangeContract.address, { from: ownerAddress }),
constants.AWAIT_TRANSACTION_MINED_MS,
);
@@ -140,7 +128,7 @@ export async function fillOrderCombinatorialUtilsFactoryAsync(
makerPrivateKey,
takerAddress,
exchangeWrapper,
assetWrapper
assetWrapper,
);
return fillOrderCombinatorialUtils;
}
@@ -378,9 +366,7 @@ export class FillOrderCombinatorialUtils {
};
const balanceAndProxyAllowanceFetcher = new SimpleAssetBalanceAndProxyAllowanceFetcher(this.assetWrapper);
const orderFilledCancelledFetcher = new SimpleOrderFilledCancelledFetcher(
this.exchangeWrapper,
);
const orderFilledCancelledFetcher = new SimpleOrderFilledCancelledFetcher(this.exchangeWrapper);
// 3. Figure out fill amount
const takerAssetFillAmount = await this._getTakerAssetFillAmountAsync(
@@ -453,13 +439,19 @@ export class FillOrderCombinatorialUtils {
const expTakerAssetBalanceOfMaker = await lazyStore.getBalanceAsync(takerAssetData, makerAddress);
const expMakerFeeAssetBalanceOfMaker = await lazyStore.getBalanceAsync(makerFeeAssetData, makerAddress);
const expTakerFeeAssetBalanceOfMaker = await lazyStore.getBalanceAsync(takerFeeAssetData, makerAddress);
const expMakerFeeAssetAllowanceOfMaker = await lazyStore.getProxyAllowanceAsync(makerFeeAssetData, makerAddress);
const expMakerFeeAssetAllowanceOfMaker = await lazyStore.getProxyAllowanceAsync(
makerFeeAssetData,
makerAddress,
);
const expTakerAssetBalanceOfTaker = await lazyStore.getBalanceAsync(takerAssetData, this.takerAddress);
const expTakerAssetAllowanceOfTaker = await lazyStore.getProxyAllowanceAsync(takerAssetData, this.takerAddress);
const expMakerAssetBalanceOfTaker = await lazyStore.getBalanceAsync(makerAssetData, this.takerAddress);
const expMakerFeeAssetBalanceOfTaker = await lazyStore.getBalanceAsync(makerFeeAssetData, this.takerAddress);
const expTakerFeeAssetBalanceOfTaker = await lazyStore.getBalanceAsync(takerFeeAssetData, this.takerAddress);
const expTakerFeeAssetAllowanceOfTaker = await lazyStore.getProxyAllowanceAsync(takerFeeAssetData, this.takerAddress);
const expTakerFeeAssetAllowanceOfTaker = await lazyStore.getProxyAllowanceAsync(
takerFeeAssetData,
this.takerAddress,
);
const expMakerFeeAssetBalanceOfFeeRecipient = await lazyStore.getBalanceAsync(makerFeeAssetData, feeRecipient);
const expTakerFeeAssetBalanceOfFeeRecipient = await lazyStore.getBalanceAsync(takerFeeAssetData, feeRecipient);
@@ -485,11 +477,9 @@ export class FillOrderCombinatorialUtils {
signedOrder.takerAssetAmount,
signedOrder.takerFee,
);
const fillResults = await this.exchangeWrapper.getFillOrderResultsAsync(
signedOrder,
this.takerAddress,
{ takerAssetFillAmount }
);
const fillResults = await this.exchangeWrapper.getFillOrderResultsAsync(signedOrder, this.takerAddress, {
takerAssetFillAmount,
});
expect(fillResults.takerAssetFilledAmount).to.be.bignumber.equal(
expFilledTakerAmount,
'takerAssetFilledAmount',
@@ -533,10 +523,7 @@ export class FillOrderCombinatorialUtils {
expect(log.args.makerAssetData).to.be.equal(makerAssetData, 'log.args.makerAssetData');
expect(log.args.takerAssetData).to.be.equal(takerAssetData, 'log.args.takerAssetData');
const actMakerAssetBalanceOfMaker = await this.assetWrapper.getBalanceAsync(
makerAddress,
makerAssetData,
);
const actMakerAssetBalanceOfMaker = await this.assetWrapper.getBalanceAsync(makerAddress, makerAssetData);
expect(actMakerAssetBalanceOfMaker).to.be.bignumber.equal(
expMakerAssetBalanceOfMaker,
'makerAssetBalanceOfMaker',
@@ -551,19 +538,13 @@ export class FillOrderCombinatorialUtils {
'makerAssetAllowanceOfMaker',
);
const actTakerAssetBalanceOfMaker = await this.assetWrapper.getBalanceAsync(
makerAddress,
takerAssetData,
);
const actTakerAssetBalanceOfMaker = await this.assetWrapper.getBalanceAsync(makerAddress, takerAssetData);
expect(actTakerAssetBalanceOfMaker).to.be.bignumber.equal(
expTakerAssetBalanceOfMaker,
'takerAssetBalanceOfMaker',
);
const actMakerFeeAssetBalanceOfMaker = await this.assetWrapper.getBalanceAsync(
makerAddress,
makerFeeAssetData,
);
const actMakerFeeAssetBalanceOfMaker = await this.assetWrapper.getBalanceAsync(makerAddress, makerFeeAssetData);
expect(actMakerFeeAssetBalanceOfMaker).to.be.bignumber.equal(
expMakerFeeAssetBalanceOfMaker,
'makerFeeAssetBalanceOfMaker',
@@ -578,19 +559,13 @@ export class FillOrderCombinatorialUtils {
'makerFeeAssetAllowanceOfMaker',
);
const actTakerFeeAssetBalanceOfMaker = await this.assetWrapper.getBalanceAsync(
makerAddress,
takerFeeAssetData,
);
const actTakerFeeAssetBalanceOfMaker = await this.assetWrapper.getBalanceAsync(makerAddress, takerFeeAssetData);
expect(actTakerFeeAssetBalanceOfMaker).to.be.bignumber.equal(
expTakerFeeAssetBalanceOfMaker,
'takerFeeAssetBalanceOfMaker',
);
const actTakerAssetBalanceOfTaker = await this.assetWrapper.getBalanceAsync(
this.takerAddress,
takerAssetData
);
const actTakerAssetBalanceOfTaker = await this.assetWrapper.getBalanceAsync(this.takerAddress, takerAssetData);
expect(actTakerAssetBalanceOfTaker).to.be.bignumber.equal(
expTakerAssetBalanceOfTaker,
'TakerAssetBalanceOfTaker',
@@ -606,10 +581,7 @@ export class FillOrderCombinatorialUtils {
'takerAssetAllowanceOfTaker',
);
const actMakerAssetBalanceOfTaker = await this.assetWrapper.getBalanceAsync(
this.takerAddress,
makerAssetData,
);
const actMakerAssetBalanceOfTaker = await this.assetWrapper.getBalanceAsync(this.takerAddress, makerAssetData);
expect(actMakerAssetBalanceOfTaker).to.be.bignumber.equal(
expMakerAssetBalanceOfTaker,
'makerAssetBalanceOfTaker',
@@ -779,10 +751,7 @@ export class FillOrderCombinatorialUtils {
break;
default:
throw errorUtils.spawnSwitchErr(
'makerStateScenario.feeBalance',
makerStateScenario.feeBalance,
);
throw errorUtils.spawnSwitchErr('makerStateScenario.feeBalance', makerStateScenario.feeBalance);
}
switch (makerStateScenario.traderAssetAllowance) {
@@ -853,10 +822,7 @@ export class FillOrderCombinatorialUtils {
break;
default:
throw errorUtils.spawnSwitchErr(
'makerStateScenario.feeAllowance',
makerStateScenario.feeAllowance,
);
throw errorUtils.spawnSwitchErr('makerStateScenario.feeAllowance', makerStateScenario.feeAllowance);
}
switch (takerStateScenario.traderAssetBalance) {
@@ -868,20 +834,12 @@ export class FillOrderCombinatorialUtils {
throw new Error(`Cannot set takerAssetBalanceOfTaker TooLow if takerAssetFillAmount is 0`);
}
const tooLowBalance = takerAssetFillAmount.minus(1);
await this.assetWrapper.setBalanceAsync(
this.takerAddress,
signedOrder.takerAssetData,
tooLowBalance,
);
await this.assetWrapper.setBalanceAsync(this.takerAddress, signedOrder.takerAssetData, tooLowBalance);
break;
case BalanceAmountScenario.Exact:
const exactBalance = takerAssetFillAmount;
await this.assetWrapper.setBalanceAsync(
this.takerAddress,
signedOrder.takerAssetData,
exactBalance,
);
await this.assetWrapper.setBalanceAsync(this.takerAddress, signedOrder.takerAssetData, exactBalance);
break;
default:
@@ -914,18 +872,11 @@ export class FillOrderCombinatorialUtils {
case BalanceAmountScenario.Exact:
const exactBalance = takerFee;
await this.assetWrapper.setBalanceAsync(
this.takerAddress,
signedOrder.takerFeeAssetData,
exactBalance,
);
await this.assetWrapper.setBalanceAsync(this.takerAddress, signedOrder.takerFeeAssetData, exactBalance);
break;
default:
throw errorUtils.spawnSwitchErr(
'takerStateScenario.feeBalance',
takerStateScenario.feeBalance,
);
throw errorUtils.spawnSwitchErr('takerStateScenario.feeBalance', takerStateScenario.feeBalance);
}
switch (takerStateScenario.traderAssetAllowance) {
@@ -996,10 +947,7 @@ export class FillOrderCombinatorialUtils {
break;
default:
throw errorUtils.spawnSwitchErr(
'takerStateScenario.feeAllowance',
takerStateScenario.feeAllowance,
);
throw errorUtils.spawnSwitchErr('takerStateScenario.feeAllowance', takerStateScenario.feeAllowance);
}
}
}

View File

@@ -1,17 +1,10 @@
import { ERC20Wrapper, ERC721Wrapper } from '@0x/contracts-asset-proxy';
import {
chaiSetup,
OrderStatus,
TokenBalancesByOwner,
} from '@0x/contracts-test-utils';
import { chaiSetup, OrderStatus, TokenBalancesByOwner } from '@0x/contracts-test-utils';
import { assetDataUtils, orderHashUtils } from '@0x/order-utils';
import { AssetProxyId, SignedOrder } from '@0x/types';
import { BigNumber } from '@0x/utils';
import * as chai from 'chai';
import {
LogWithDecodedArgs,
TransactionReceiptWithDecodedLogs,
} from 'ethereum-types';
import { LogWithDecodedArgs, TransactionReceiptWithDecodedLogs } from 'ethereum-types';
import * as _ from 'lodash';
import { ExchangeWrapper } from './exchange_wrapper';
@@ -63,9 +56,11 @@ export interface MatchedOrders {
rightOrderTakerAssetFilledAmount?: BigNumber;
}
export type MatchOrdersAsyncCall =
(leftOrder: SignedOrder, rightOrder: SignedOrder, takerAddress: string)
=> Promise<TransactionReceiptWithDecodedLogs>;
export type MatchOrdersAsyncCall = (
leftOrder: SignedOrder,
rightOrder: SignedOrder,
takerAddress: string,
) => Promise<TransactionReceiptWithDecodedLogs>;
export class MatchOrderTester {
public exchangeWrapper: ExchangeWrapper;
@@ -75,7 +70,7 @@ export class MatchOrderTester {
private readonly _initialTokenBalancesPromise: Promise<TokenBalancesByOwner>;
/**
* @dev Constructs new MatchOrderTester.
* Constructs new MatchOrderTester.
* @param exchangeWrapper Used to call to the Exchange.
* @param erc20Wrapper Used to fetch ERC20 balances.
* @param erc721Wrapper Used to fetch ERC721 token owners.
@@ -96,7 +91,7 @@ export class MatchOrderTester {
}
/**
* @dev Matches two complementary orders and asserts results.
* Matches two complementary orders and asserts results.
* @param orders The matched orders and filled states.
* @param takerAddress Address of taker (the address who matched the two orders)
* @param expectedTransferAmounts Expected amounts transferred as a result of order matching.
@@ -112,8 +107,9 @@ export class MatchOrderTester {
): Promise<MatchResults> {
await assertInitialOrderStatesAsync(orders, this.exchangeWrapper);
// Get the token balances before executing `matchOrders()`.
const _initialTokenBalances = initialTokenBalances ?
initialTokenBalances : await this._initialTokenBalancesPromise;
const _initialTokenBalances = initialTokenBalances
? initialTokenBalances
: await this._initialTokenBalancesPromise;
// Execute `matchOrders()`
const transactionReceipt = await this._executeMatchOrdersAsync(
orders.leftOrder,
@@ -138,7 +134,7 @@ export class MatchOrderTester {
}
/**
* @dev Fetch the current token balances of all known accounts.
* Fetch the current token balances of all known accounts.
*/
public async getBalancesAsync(): Promise<TokenBalancesByOwner> {
return getTokenBalancesAsync(this.erc20Wrapper, this.erc721Wrapper);
@@ -149,25 +145,19 @@ export class MatchOrderTester {
rightOrder: SignedOrder,
takerAddress: string,
): Promise<TransactionReceiptWithDecodedLogs> {
const caller = this.matchOrdersCallAsync ||
((_leftOrder: SignedOrder, _rightOrder: SignedOrder, _takerAddress: string) =>
this.exchangeWrapper.matchOrdersAsync(
_leftOrder,
_rightOrder,
_takerAddress,
)
);
const caller =
this.matchOrdersCallAsync ||
(async (_leftOrder: SignedOrder, _rightOrder: SignedOrder, _takerAddress: string) =>
this.exchangeWrapper.matchOrdersAsync(_leftOrder, _rightOrder, _takerAddress));
return caller(leftOrder, rightOrder, takerAddress);
}
}
/**
* @dev Converts a `Partial<MatchTransferAmounts>` to a `MatchTransferAmounts` by
* Converts a `Partial<MatchTransferAmounts>` to a `MatchTransferAmounts` by
* filling in missing fields with zero.
*/
function toFullMatchTransferAmounts(
partial: Partial<MatchTransferAmounts>,
): MatchTransferAmounts {
function toFullMatchTransferAmounts(partial: Partial<MatchTransferAmounts>): MatchTransferAmounts {
// prettier-ignore
return {
leftMakerAssetSoldByLeftMakerAmount:
@@ -199,11 +189,11 @@ function toFullMatchTransferAmounts(
rightTakerFeeAssetPaidByTakerAmount:
partial.rightTakerFeeAssetPaidByTakerAmount || ZERO,
};
}
}
/**
* @dev Simulates matching two orders by transferring amounts defined in
* `transferAmounts` and returns the results.
* Simulates matching two orders by transferring amounts defined in
* `transferAmounts` and returns the results.
* @param orders The orders being matched and their filled states.
* @param takerAddress Address of taker (the address who matched the two orders)
* @param tokenBalances Current token balances.
@@ -282,13 +272,14 @@ function simulateMatchOrders(
matchResults,
);
// Taker fees.
if (orders.leftOrder.feeRecipientAddress === orders.rightOrder.feeRecipientAddress &&
orders.leftOrder.takerFeeAssetData === orders.rightOrder.takerFeeAssetData) {
if (
orders.leftOrder.feeRecipientAddress === orders.rightOrder.feeRecipientAddress &&
orders.leftOrder.takerFeeAssetData === orders.rightOrder.takerFeeAssetData
) {
// Same asset data and recipients, so combine into a single transfer.
const totalTakerFeeAssetPaidByTakerAmount =
transferAmounts.leftTakerFeeAssetPaidByTakerAmount.plus(
transferAmounts.rightTakerFeeAssetPaidByTakerAmount,
);
const totalTakerFeeAssetPaidByTakerAmount = transferAmounts.leftTakerFeeAssetPaidByTakerAmount.plus(
transferAmounts.rightTakerFeeAssetPaidByTakerAmount,
);
transferAsset(
takerAddress,
orders.leftOrder.feeRecipientAddress,
@@ -318,7 +309,7 @@ function simulateMatchOrders(
}
/**
* @dev Simulates a transfer of assets from `fromAddress` to `toAddress`
* Simulates a transfer of assets from `fromAddress` to `toAddress`
* by updating `matchResults`.
*/
function transferAsset(
@@ -355,7 +346,7 @@ function transferAsset(
}
/**
* @dev Checks that the results of `simulateMatchOrders()` agrees with reality.
* Checks that the results of `simulateMatchOrders()` agrees with reality.
* @param matchResults The results of a `simulateMatchOrders()`.
* @param transactionReceipt The transaction receipt of a call to `matchOrders()`.
* @param actualTokenBalances The actual, on-chain token balances of known addresses.
@@ -376,17 +367,14 @@ async function assertMatchResultsAsync(
}
/**
* @dev Checks values from the logs produced by Exchange.matchOrders against
* Checks values from the logs produced by Exchange.matchOrders against
* the expected transfer amounts.
* @param orders The matched orders.
* @param takerAddress Address of taker (account that called Exchange.matchOrders)
* @param transactionReceipt Transaction receipt and logs produced by Exchange.matchOrders.
* @param expectedTransferAmounts Expected amounts transferred as a result of order matching.
*/
function assertFillEvents(
expectedFills: FillEventArgs[],
transactionReceipt: TransactionReceiptWithDecodedLogs,
): void {
function assertFillEvents(expectedFills: FillEventArgs[], transactionReceipt: TransactionReceiptWithDecodedLogs): void {
// Extract the actual `Fill` events.
const actualFills = extractFillEventsfromReceipt(transactionReceipt);
expect(actualFills.length, 'wrong number of Fill events').to.be.equal(expectedFills.length);
@@ -397,15 +385,19 @@ function assertFillEvents(
expect(actual.orderHash, `${side} order Fill event orderHash`).to.equal(expected.orderHash);
expect(actual.makerAddress, `${side} order Fill event makerAddress`).to.equal(expected.makerAddress);
expect(actual.takerAddress, `${side} order Fill event takerAddress`).to.equal(expected.takerAddress);
expect(actual.makerAssetFilledAmount, `${side} order Fill event makerAssetFilledAmount`).to.bignumber.equal(expected.makerAssetFilledAmount);
expect(actual.takerAssetFilledAmount, `${side} order Fill event takerAssetFilledAmount`).to.bignumber.equal(expected.takerAssetFilledAmount);
expect(actual.makerAssetFilledAmount, `${side} order Fill event makerAssetFilledAmount`).to.bignumber.equal(
expected.makerAssetFilledAmount,
);
expect(actual.takerAssetFilledAmount, `${side} order Fill event takerAssetFilledAmount`).to.bignumber.equal(
expected.takerAssetFilledAmount,
);
expect(actual.makerFeePaid, `${side} order Fill event makerFeePaid`).to.bignumber.equal(expected.makerFeePaid);
expect(actual.takerFeePaid, `${side} order Fill event takerFeePaid`).to.bignumber.equal(expected.takerFeePaid);
}
}
/**
* @dev Create a pair of `Fill` events for a simulated `matchOrder()`.
* Create a pair of `Fill` events for a simulated `matchOrder()`.
*/
function simulateFillEvents(
orders: MatchedOrders,
@@ -438,11 +430,9 @@ function simulateFillEvents(
}
/**
* @dev Extract `Fill` events from a transaction receipt.
* Extract `Fill` events from a transaction receipt.
*/
function extractFillEventsfromReceipt(
receipt: TransactionReceiptWithDecodedLogs,
): FillEventArgs[] {
function extractFillEventsfromReceipt(receipt: TransactionReceiptWithDecodedLogs): FillEventArgs[] {
interface RawFillEventArgs {
orderHash: string;
makerAddress: string;
@@ -452,8 +442,9 @@ function extractFillEventsfromReceipt(
makerFeePaid: string;
takerFeePaid: string;
}
const actualFills =
_.filter(receipt.logs, ['event', 'Fill']) as any as Array<LogWithDecodedArgs<RawFillEventArgs>>;
const actualFills = (_.filter(receipt.logs, ['event', 'Fill']) as any) as Array<
LogWithDecodedArgs<RawFillEventArgs>
>;
// Convert RawFillEventArgs to FillEventArgs.
return actualFills.map(fill => ({
orderHash: fill.args.orderHash,
@@ -467,62 +458,49 @@ function extractFillEventsfromReceipt(
}
/**
* @dev Asserts that all expected token holdings match the actual holdings.
* Asserts that all expected token holdings match the actual holdings.
* @param expectedBalances Expected balances.
* @param actualBalances Actual balances.
*/
function assertBalances(
expectedBalances: TokenBalancesByOwner,
actualBalances: TokenBalancesByOwner,
): void {
function assertBalances(expectedBalances: TokenBalancesByOwner, actualBalances: TokenBalancesByOwner): void {
// ERC20 Balances
expect(actualBalances.erc20, 'ERC20 balances').to.deep.equal(expectedBalances.erc20);
// ERC721 Token Ids
const sortedExpectedERC721Balances = _.mapValues(
expectedBalances.erc721,
tokenIdsByOwner => {
_.mapValues(tokenIdsByOwner, tokenIds => {
_.sortBy(tokenIds);
});
},
);
const sortedActualERC721Balances = _.mapValues(
actualBalances.erc721,
tokenIdsByOwner => {
_.mapValues(tokenIdsByOwner, tokenIds => {
_.sortBy(tokenIds);
});
},
);
const sortedExpectedERC721Balances = _.mapValues(expectedBalances.erc721, tokenIdsByOwner => {
_.mapValues(tokenIdsByOwner, tokenIds => {
_.sortBy(tokenIds);
});
});
const sortedActualERC721Balances = _.mapValues(actualBalances.erc721, tokenIdsByOwner => {
_.mapValues(tokenIdsByOwner, tokenIds => {
_.sortBy(tokenIds);
});
});
expect(sortedExpectedERC721Balances, 'ERC721 balances').to.deep.equal(sortedActualERC721Balances);
}
/**
* @dev Asserts initial exchange state for matched orders.
* Asserts initial exchange state for matched orders.
* @param orders Matched orders with intial filled amounts.
* @param exchangeWrapper ExchangeWrapper isntance.
*/
async function assertInitialOrderStatesAsync(
orders: MatchedOrders,
exchangeWrapper: ExchangeWrapper,
): Promise<void> {
async function assertInitialOrderStatesAsync(orders: MatchedOrders, exchangeWrapper: ExchangeWrapper): Promise<void> {
const pairs = [
[ orders.leftOrder, orders.leftOrderTakerAssetFilledAmount || ZERO ],
[ orders.rightOrder, orders.rightOrderTakerAssetFilledAmount || ZERO ],
[orders.leftOrder, orders.leftOrderTakerAssetFilledAmount || ZERO],
[orders.rightOrder, orders.rightOrderTakerAssetFilledAmount || ZERO],
] as Array<[SignedOrder, BigNumber]>;
await Promise.all(pairs.map(async ([ order, expectedFilledAmount ]) => {
const side = order === orders.leftOrder ? 'left' : 'right';
const orderHash = orderHashUtils.getOrderHashHex(order);
const actualFilledAmount = await exchangeWrapper.getTakerAssetFilledAmountAsync(
orderHash,
);
expect(actualFilledAmount, `${side} order initial filled amount`)
.to.bignumber.equal(expectedFilledAmount);
}));
await Promise.all(
pairs.map(async ([order, expectedFilledAmount]) => {
const side = order === orders.leftOrder ? 'left' : 'right';
const orderHash = orderHashUtils.getOrderHashHex(order);
const actualFilledAmount = await exchangeWrapper.getTakerAssetFilledAmountAsync(orderHash);
expect(actualFilledAmount, `${side} order initial filled amount`).to.bignumber.equal(expectedFilledAmount);
}),
);
}
/**
* @dev Asserts the exchange state after a call to `matchOrders()`.
* Asserts the exchange state after a call to `matchOrders()`.
* @param matchResults Results from a call to `simulateMatchOrders()`.
* @param exchangeWrapper The ExchangeWrapper instance.
*/
@@ -531,36 +509,37 @@ async function assertPostExchangeStateAsync(
exchangeWrapper: ExchangeWrapper,
): Promise<void> {
const pairs = [
[ matchResults.orders.leftOrder, matchResults.orders.leftOrderTakerAssetFilledAmount ],
[ matchResults.orders.rightOrder, matchResults.orders.rightOrderTakerAssetFilledAmount ],
[matchResults.orders.leftOrder, matchResults.orders.leftOrderTakerAssetFilledAmount],
[matchResults.orders.rightOrder, matchResults.orders.rightOrderTakerAssetFilledAmount],
] as Array<[SignedOrder, BigNumber]>;
await Promise.all(pairs.map(async ([ order, expectedFilledAmount ]) => {
const side = order === matchResults.orders.leftOrder ? 'left' : 'right';
const orderInfo = await exchangeWrapper.getOrderInfoAsync(order);
// Check filled amount of order.
const actualFilledAmount = orderInfo.orderTakerAssetFilledAmount;
expect(actualFilledAmount, `${side} order final filled amount`)
.to.be.bignumber.equal(expectedFilledAmount);
// Check status of order.
const expectedStatus =
expectedFilledAmount.isGreaterThanOrEqualTo(order.takerAssetAmount) ?
OrderStatus.FullyFilled : OrderStatus.Fillable;
const actualStatus = orderInfo.orderStatus;
expect(actualStatus, `${side} order final status`).to.equal(expectedStatus);
}));
await Promise.all(
pairs.map(async ([order, expectedFilledAmount]) => {
const side = order === matchResults.orders.leftOrder ? 'left' : 'right';
const orderInfo = await exchangeWrapper.getOrderInfoAsync(order);
// Check filled amount of order.
const actualFilledAmount = orderInfo.orderTakerAssetFilledAmount;
expect(actualFilledAmount, `${side} order final filled amount`).to.be.bignumber.equal(expectedFilledAmount);
// Check status of order.
const expectedStatus = expectedFilledAmount.isGreaterThanOrEqualTo(order.takerAssetAmount)
? OrderStatus.FullyFilled
: OrderStatus.Fillable;
const actualStatus = orderInfo.orderStatus;
expect(actualStatus, `${side} order final status`).to.equal(expectedStatus);
}),
);
}
/**
* @dev Retrive the current token balances of all known addresses.
* Retrive the current token balances of all known addresses.
* @param erc20Wrapper The ERC20Wrapper instance.
* @param erc721Wrapper The ERC721Wrapper instance.
* @return A promise that resolves to a `TokenBalancesByOwner`.
*/
export async function getTokenBalancesAsync(
erc20Wrapper: ERC20Wrapper,
erc721Wrapper: ERC721Wrapper,
): Promise<TokenBalancesByOwner> {
const [ erc20, erc721 ] = await Promise.all([
erc20Wrapper.getBalancesAsync(),
erc721Wrapper.getBalancesAsync(),
]);
const [erc20, erc721] = await Promise.all([erc20Wrapper.getBalancesAsync(), erc721Wrapper.getBalancesAsync()]);
return {
erc20,
erc721,

View File

@@ -219,45 +219,26 @@ export class OrderFactoryFromScenario {
erc20ZeroDecimalTokenAddress: string,
erc721AssetId: BigNumber,
): [BigNumber, string] => {
const feeAmount = getFeeAmountFromScenario(
orderScenario,
feeAssetDataScenario,
feeAmountScenario,
);
const feeAmount = getFeeAmountFromScenario(orderScenario, feeAssetDataScenario, feeAmountScenario);
switch (feeAssetDataScenario) {
case FeeAssetDataScenario.MakerToken:
return [ feeAmount, makerAssetData ];
return [feeAmount, makerAssetData];
case FeeAssetDataScenario.TakerToken:
return [ feeAmount, takerAssetData ];
return [feeAmount, takerAssetData];
case FeeAssetDataScenario.ERC20EighteenDecimals:
return [
feeAmount,
assetDataUtils.encodeERC20AssetData(erc20EighteenDecimalTokenAddress),
];
return [feeAmount, assetDataUtils.encodeERC20AssetData(erc20EighteenDecimalTokenAddress)];
case FeeAssetDataScenario.ERC20FiveDecimals:
return [
feeAmount,
assetDataUtils.encodeERC20AssetData(erc20FiveDecimalTokenAddress),
];
return [feeAmount, assetDataUtils.encodeERC20AssetData(erc20FiveDecimalTokenAddress)];
case FeeAssetDataScenario.ERC20ZeroDecimals:
return [
feeAmount,
assetDataUtils.encodeERC20AssetData(erc20ZeroDecimalTokenAddress),
];
return [feeAmount, assetDataUtils.encodeERC20AssetData(erc20ZeroDecimalTokenAddress)];
case FeeAssetDataScenario.ERC721:
return [
feeAmount,
assetDataUtils.encodeERC721AssetData(
this._erc721Token.address,
erc721AssetId,
),
];
return [feeAmount, assetDataUtils.encodeERC721AssetData(this._erc721Token.address, erc721AssetId)];
default:
throw errorUtils.spawnSwitchErr('FeeAssetDataScenario', feeAssetDataScenario);
}
}
};
[ makerFee, makerFeeAssetData ] = feeFromScenario(
[makerFee, makerFeeAssetData] = feeFromScenario(
orderScenario.makerFeeScenario,
orderScenario.makerFeeAssetDataScenario,
this._erc20EighteenDecimalTokenAddresses[2],
@@ -265,7 +246,7 @@ export class OrderFactoryFromScenario {
this._erc20ZeroDecimalTokenAddresses[2],
erc721MakerAssetIds[1],
);
[ takerFee, takerFeeAssetData ] = feeFromScenario(
[takerFee, takerFeeAssetData] = feeFromScenario(
orderScenario.takerFeeScenario,
orderScenario.takerFeeAssetDataScenario,
this._erc20EighteenDecimalTokenAddresses[3],
@@ -379,17 +360,9 @@ function getFeeAmountFromScenario(
throw errorUtils.spawnSwitchErr('OrderAssetAmountScenario', feeAmountScenario);
}
case FeeAssetDataScenario.MakerToken:
return getFeeAmountFromScenario(
orderScenario,
orderScenario.makerAssetDataScenario,
feeAmountScenario
);
return getFeeAmountFromScenario(orderScenario, orderScenario.makerAssetDataScenario, feeAmountScenario);
case FeeAssetDataScenario.TakerToken:
return getFeeAmountFromScenario(
orderScenario,
orderScenario.takerAssetDataScenario,
feeAmountScenario
);
return getFeeAmountFromScenario(orderScenario, orderScenario.takerAssetDataScenario, feeAmountScenario);
default:
throw errorUtils.spawnSwitchErr('FeeAssetDataScenario', feeAssetDataScenario);
}