@0x/contracts-exchange: Allow custom ExchangeWrapper.matchOrders() call in MatchOrdersTester.

This commit is contained in:
Lawrence Forman 2019-05-16 10:50:02 -04:00 committed by Amir Bandeali
parent c326ec9d1e
commit d2f10d5834

View File

@ -55,25 +55,34 @@ export interface MatchedOrders {
rightOrderTakerAssetFilledAmount?: BigNumber; rightOrderTakerAssetFilledAmount?: BigNumber;
} }
export type MatchOrdersAsyncCall =
(leftOrder: SignedOrder, rightOrder: SignedOrder, takerAddress: string)
=> Promise<TransactionReceiptWithDecodedLogs>;
export class MatchOrderTester { export class MatchOrderTester {
private readonly _exchangeWrapper: ExchangeWrapper; public exchangeWrapper: ExchangeWrapper;
private readonly _erc20Wrapper: ERC20Wrapper; public erc20Wrapper: ERC20Wrapper;
private readonly _erc721Wrapper: ERC721Wrapper; public erc721Wrapper: ERC721Wrapper;
public matchOrdersCallAsync?: MatchOrdersAsyncCall;
/** /**
* @dev Constructs new MatchOrderTester. * @dev Constructs new MatchOrderTester.
* @param exchangeWrapper Used to call to the Exchange. * @param exchangeWrapper Used to call to the Exchange.
* @param erc20Wrapper Used to fetch ERC20 balances. * @param erc20Wrapper Used to fetch ERC20 balances.
* @param erc721Wrapper Used to fetch ERC721 token owners. * @param erc721Wrapper Used to fetch ERC721 token owners.
* @param matchOrdersCallAsync Optional, custom caller for
* `ExchangeWrapper.matchOrdersAsync()`.
*/ */
constructor( constructor(
exchangeWrapper: ExchangeWrapper, exchangeWrapper: ExchangeWrapper,
erc20Wrapper: ERC20Wrapper, erc20Wrapper: ERC20Wrapper,
erc721Wrapper: ERC721Wrapper, erc721Wrapper: ERC721Wrapper,
matchOrdersCallAsync?: MatchOrdersAsyncCall,
) { ) {
this._exchangeWrapper = exchangeWrapper; this.exchangeWrapper = exchangeWrapper;
this._erc20Wrapper = erc20Wrapper; this.erc20Wrapper = erc20Wrapper;
this._erc721Wrapper = erc721Wrapper; this.erc721Wrapper = erc721Wrapper;
this.matchOrdersCallAsync = matchOrdersCallAsync;
} }
/** /**
@ -88,11 +97,11 @@ export class MatchOrderTester {
takerAddress: string, takerAddress: string,
expectedTransferAmounts: Partial<MatchTransferAmounts>, expectedTransferAmounts: Partial<MatchTransferAmounts>,
): Promise<MatchResults> { ): Promise<MatchResults> {
await assertInitialOrderStatesAsync(orders, this._exchangeWrapper); await assertInitialOrderStatesAsync(orders, this.exchangeWrapper);
// Get the token balances before executing `matchOrders()`. // Get the token balances before executing `matchOrders()`.
const initialTokenBalances = await this._getBalancesAsync(); const initialTokenBalances = await this._getBalancesAsync();
// Execute `matchOrders()` // Execute `matchOrders()`
const transactionReceipt = await this._exchangeWrapper.matchOrdersAsync( const transactionReceipt = await this._executeMatchOrdersAsync(
orders.leftOrder, orders.leftOrder,
orders.rightOrder, orders.rightOrder,
takerAddress, takerAddress,
@ -109,7 +118,7 @@ export class MatchOrderTester {
matchResults, matchResults,
transactionReceipt, transactionReceipt,
await this._getBalancesAsync(), await this._getBalancesAsync(),
this._exchangeWrapper, this.exchangeWrapper,
); );
return matchResults; return matchResults;
} }
@ -119,14 +128,30 @@ export class MatchOrderTester {
*/ */
private async _getBalancesAsync(): Promise<TokenBalancesByOwner> { private async _getBalancesAsync(): Promise<TokenBalancesByOwner> {
const [ erc20, erc721 ] = await Promise.all([ const [ erc20, erc721 ] = await Promise.all([
this._erc20Wrapper.getBalancesAsync(), this.erc20Wrapper.getBalancesAsync(),
this._erc721Wrapper.getBalancesAsync(), this.erc721Wrapper.getBalancesAsync(),
]); ]);
return { return {
erc20, erc20,
erc721, erc721,
}; };
} }
private async _executeMatchOrdersAsync(
leftOrder: SignedOrder,
rightOrder: SignedOrder,
takerAddress: string,
): Promise<TransactionReceiptWithDecodedLogs> {
const caller = this.matchOrdersCallAsync ||
((_leftOrder: SignedOrder, _rightOrder: SignedOrder, _takerAddress: string) =>
this.exchangeWrapper.matchOrdersAsync(
_leftOrder,
_rightOrder,
_takerAddress,
)
);
return caller(leftOrder, rightOrder, takerAddress);
}
} }
/** /**