Add tests for zeroEx.exchange.getLogsAsync

This commit is contained in:
Leonid Logvinov
2017-10-03 19:05:44 +03:00
parent 837618c7a0
commit 451ded4963
6 changed files with 56 additions and 8 deletions

View File

@@ -206,7 +206,11 @@ export class ZeroEx {
this._getTokenTransferProxyAddressAsync.bind(this),
);
const exchageContractAddressIfExists = _.isUndefined(config) ? undefined : config.exchangeContractAddress;
this.exchange = new ExchangeWrapper(this._web3Wrapper, this.token, exchageContractAddressIfExists);
this.exchange = new ExchangeWrapper(
this._web3Wrapper,
this._abiDecoder,
this.token,
exchageContractAddressIfExists);
this.proxy = new TokenTransferProxyWrapper(
this._web3Wrapper,
this._getTokenTransferProxyAddressAsync.bind(this),

View File

@@ -22,7 +22,7 @@ export class ContractWrapper {
}
protected async _getLogsAsync(address: string, eventName: ContractEvents, subscriptionOpts: SubscriptionOpts,
indexFilterValues: IndexedFilterValues,
abi: Web3.ContractAbi): Promise<Array<LogWithDecodedArgs|RawLog>> {
abi: Web3.ContractAbi): Promise<LogWithDecodedArgs[]> {
// TODO include indexFilterValues in topics
const eventSignature = this._getEventSignatureFromAbiByName(abi, eventName);
const filter = {

View File

@@ -40,6 +40,7 @@ import {ContractWrapper} from './contract_wrapper';
import {constants} from '../utils/constants';
import {TokenWrapper} from './token_wrapper';
import {decorators} from '../utils/decorators';
import {AbiDecoder} from '../utils/abi_decoder';
import {artifacts} from '../artifacts';
const SHOULD_VALIDATE_BY_DEFAULT = true;
@@ -80,8 +81,9 @@ export class ExchangeWrapper extends ContractWrapper {
];
return [orderAddresses, orderValues];
}
constructor(web3Wrapper: Web3Wrapper, tokenWrapper: TokenWrapper, contractAddressIfExists?: string) {
super(web3Wrapper);
constructor(web3Wrapper: Web3Wrapper, abiDecoder: AbiDecoder,
tokenWrapper: TokenWrapper, contractAddressIfExists?: string) {
super(web3Wrapper, abiDecoder);
this._tokenWrapper = tokenWrapper;
this._orderValidationUtils = new OrderValidationUtils(tokenWrapper, this);
this._exchangeLogEventEmitters = [];
@@ -665,7 +667,7 @@ export class ExchangeWrapper extends ContractWrapper {
* @return Array of logs that match the parameters
*/
public async getLogsAsync(eventName: ExchangeEvents, subscriptionOpts: SubscriptionOpts,
indexFilterValues: IndexedFilterValues): Promise<Array<LogWithDecodedArgs|RawLog>> {
indexFilterValues: IndexedFilterValues): Promise<LogWithDecodedArgs[]> {
const exchangeContractAddress = await this.getContractAddressAsync();
const logs = await this._getLogsAsync(
exchangeContractAddress, eventName, subscriptionOpts, indexFilterValues, artifacts.ExchangeArtifact.abi,

View File

@@ -290,7 +290,7 @@ export class TokenWrapper extends ContractWrapper {
* @return Array of logs that match the parameters
*/
public async getLogsAsync(tokenAddress: string, eventName: TokenEvents, subscriptionOpts: SubscriptionOpts,
indexFilterValues: IndexedFilterValues): Promise<Array<LogWithDecodedArgs|RawLog>> {
indexFilterValues: IndexedFilterValues): Promise<LogWithDecodedArgs[]> {
const logs = await this._getLogsAsync(
tokenAddress, eventName, subscriptionOpts, indexFilterValues, artifacts.TokenArtifact.abi,
);

View File

@@ -771,4 +771,46 @@ describe('ExchangeWrapper', () => {
expect(zrxAddress).to.equal(zrxToken.address);
});
});
describe('#getLogsAsync', () => {
let makerTokenAddress: string;
let takerTokenAddress: string;
let makerAddress: string;
let takerAddress: string;
const fillableAmount = new BigNumber(5);
const shouldThrowOnInsufficientBalanceOrAllowance = true;
const subscriptionOpts: SubscriptionOpts = {
fromBlock: 0,
toBlock: 'latest',
};
const indexFilterValues = {};
before(async () => {
[, makerAddress, takerAddress] = userAddresses;
const [makerToken, takerToken] = tokenUtils.getNonProtocolTokens();
makerTokenAddress = makerToken.address;
takerTokenAddress = takerToken.address;
});
it('should get logs with decoded args emitted by LogFill', async () => {
const signedOrder = await fillScenarios.createFillableSignedOrderAsync(
makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount,
);
await zeroEx.exchange.fillOrderAsync(
signedOrder, fillableAmount, shouldThrowOnInsufficientBalanceOrAllowance, takerAddress,
);
const eventName = ExchangeEvents.LogFill;
const logs = await zeroEx.exchange.getLogsAsync(eventName, subscriptionOpts, indexFilterValues);
expect(logs).to.have.length(1);
expect(logs[0].event).to.be.equal(eventName);
});
it('should only get the logs with the correct event name', async () => {
const signedOrder = await fillScenarios.createFillableSignedOrderAsync(
makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount,
);
await zeroEx.exchange.fillOrderAsync(
signedOrder, fillableAmount, shouldThrowOnInsufficientBalanceOrAllowance, takerAddress,
);
const differentEventName = ExchangeEvents.LogCancel;
const logs = await zeroEx.exchange.getLogsAsync(differentEventName, subscriptionOpts, indexFilterValues);
expect(logs).to.have.length(0);
});
});
});

View File

@@ -455,7 +455,7 @@ describe('TokenWrapper', () => {
const eventName = TokenEvents.Approval;
const logs = await zeroEx.token.getLogsAsync(
tokenAddress, eventName, subscriptionOpts, indexFilterValues,
) as LogWithDecodedArgs[];
);
expect(logs).to.have.length(1);
expect(logs[0].event).to.be.equal(eventName);
expect(logs[0].args._owner).to.be.equal(coinbase);
@@ -468,7 +468,7 @@ describe('TokenWrapper', () => {
const differentEventName = TokenEvents.Transfer;
const logs = await zeroEx.token.getLogsAsync(
tokenAddress, differentEventName, subscriptionOpts, indexFilterValues,
) as LogWithDecodedArgs[];
);
expect(logs).to.have.length(0);
});
});