aboutsummaryrefslogtreecommitdiffstats
path: root/test/token_wrapper_test.ts
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2017-07-04 02:39:26 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2017-07-04 06:54:05 +0800
commit5a8eb77ff0a6b00e4df5d933426e451c8ef09f7b (patch)
tree6980d7de11f4ff45fc6d7d33c9087e7193dc102b /test/token_wrapper_test.ts
parentc9edeae6d8dad1fba6c4f5eca63ff5db3e6555e2 (diff)
downloaddexon-sol-tools-5a8eb77ff0a6b00e4df5d933426e451c8ef09f7b.tar
dexon-sol-tools-5a8eb77ff0a6b00e4df5d933426e451c8ef09f7b.tar.gz
dexon-sol-tools-5a8eb77ff0a6b00e4df5d933426e451c8ef09f7b.tar.bz2
dexon-sol-tools-5a8eb77ff0a6b00e4df5d933426e451c8ef09f7b.tar.lz
dexon-sol-tools-5a8eb77ff0a6b00e4df5d933426e451c8ef09f7b.tar.xz
dexon-sol-tools-5a8eb77ff0a6b00e4df5d933426e451c8ef09f7b.tar.zst
dexon-sol-tools-5a8eb77ff0a6b00e4df5d933426e451c8ef09f7b.zip
Add initial implementation and tests for zeroEx.token.subscribeAsync
Diffstat (limited to 'test/token_wrapper_test.ts')
-rw-r--r--test/token_wrapper_test.ts113
1 files changed, 112 insertions, 1 deletions
diff --git a/test/token_wrapper_test.ts b/test/token_wrapper_test.ts
index a1c035672..4a20141db 100644
--- a/test/token_wrapper_test.ts
+++ b/test/token_wrapper_test.ts
@@ -5,8 +5,17 @@ import * as Web3 from 'web3';
import * as BigNumber from 'bignumber.js';
import promisify = require('es6-promisify');
import {web3Factory} from './utils/web3_factory';
-import {ZeroEx, ZeroExError, Token} from '../src';
+import {
+ ZeroEx,
+ ZeroExError,
+ Token,
+ SubscriptionOpts,
+ TokenEvents,
+ ContractEvent,
+ TransferContractEventArgs,
+} from '../src';
import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
+import {DoneCallback} from '../src/types';
chaiSetup.configure();
const expect = chai.expect;
@@ -231,4 +240,106 @@ describe('TokenWrapper', () => {
return expect(allowanceAfterSet).to.be.bignumber.equal(expectedAllowanceAfterAllowanceSet);
});
});
+ describe('#subscribeAsync', () => {
+ const indexFilterValues = {};
+ const shouldCheckTransfer = false;
+ let tokenAddress: string;
+ const subscriptionOpts: SubscriptionOpts = {
+ fromBlock: 0,
+ toBlock: 'latest',
+ };
+ const transferAmount = new BigNumber(42);
+ const allowanceAmount = new BigNumber(42);
+ before(() => {
+ const token = tokens[0];
+ tokenAddress = token.address;
+ });
+ afterEach(async () => {
+ await zeroEx.token.stopWatchingAllEventsAsync();
+ });
+ // Hack: Mocha does not allow a test to be both async and have a `done` callback
+ // Since we need to await the receipt of the event in the `subscribeAsync` callback,
+ // we do need both. A hack is to make the top-level a sync fn w/ a done callback and then
+ // wrap the rest of the test in an async block
+ // Source: https://github.com/mochajs/mocha/issues/2407
+ it('Should receive the Transfer event when an order is filled', (done: DoneCallback) => {
+ (async () => {
+ const zeroExEvent = await zeroEx.token.subscribeAsync(
+ tokenAddress, TokenEvents.Transfer, subscriptionOpts, indexFilterValues);
+ zeroExEvent.watch((err: Error, event: ContractEvent) => {
+ expect(err).to.be.null();
+ expect(event).to.not.be.undefined();
+ expect(event.args as TransferContractEventArgs).to.be.deep.equal({
+ _from: coinbase,
+ _to: addressWithoutFunds,
+ _value: transferAmount,
+ });
+ done();
+ });
+ await zeroEx.token.transferAsync(tokenAddress, coinbase, addressWithoutFunds, transferAmount);
+ })();
+ });
+ it('Should receive the Approval event when an order is cancelled', (done: DoneCallback) => {
+ (async () => {
+ const zeroExEvent = await zeroEx.token.subscribeAsync(
+ tokenAddress, TokenEvents.Approval, subscriptionOpts, indexFilterValues);
+ zeroExEvent.watch((err: Error, event: ContractEvent) => {
+ expect(err).to.be.null();
+ expect(event).to.not.be.undefined();
+ expect(event.args as TransferContractEventArgs).to.be.deep.equal({
+ _owner: coinbase,
+ _spender: addressWithoutFunds,
+ _value: allowanceAmount,
+ });
+ done();
+ });
+ await zeroEx.token.setAllowanceAsync(tokenAddress, coinbase, addressWithoutFunds, allowanceAmount);
+ })();
+ });
+ it('Outstanding subscriptions are cancelled when zeroEx.setProviderAsync called', (done: DoneCallback) => {
+ (async () => {
+ const eventSubscriptionToBeCancelled = await zeroEx.token.subscribeAsync(
+ tokenAddress, TokenEvents.Transfer, subscriptionOpts, indexFilterValues);
+ eventSubscriptionToBeCancelled.watch((err: Error, event: ContractEvent) => {
+ done(new Error('Expected this subscription to have been cancelled'));
+ });
+
+ const newProvider = web3Factory.getRpcProvider();
+ await zeroEx.setProviderAsync(newProvider);
+
+ const eventSubscriptionToStay = await zeroEx.token.subscribeAsync(
+ tokenAddress, TokenEvents.Transfer, subscriptionOpts, indexFilterValues);
+ eventSubscriptionToStay.watch((err: Error, event: ContractEvent) => {
+ expect(err).to.be.null();
+ expect(event).to.not.be.undefined();
+ done();
+ });
+ await zeroEx.token.transferAsync(tokenAddress, coinbase, addressWithoutFunds, transferAmount);
+ })();
+ });
+ it('Should stop watch for events when stopWatchingAsync called on the eventEmitter', (done: DoneCallback) => {
+ (async () => {
+ const eventSubscriptionToBeStopped = await zeroEx.token.subscribeAsync(
+ tokenAddress, TokenEvents.Transfer, subscriptionOpts, indexFilterValues);
+ eventSubscriptionToBeStopped.watch((err: Error, event: ContractEvent) => {
+ done(new Error('Expected this subscription to have been stopped'));
+ });
+ await eventSubscriptionToBeStopped.stopWatchingAsync();
+ await zeroEx.token.transferAsync(tokenAddress, coinbase, addressWithoutFunds, transferAmount);
+ done();
+ })();
+ });
+ it('Should wrap all event args BigNumber instances in a newer version of BigNumber', (done: DoneCallback) => {
+ (async () => {
+ const zeroExEvent = await zeroEx.token.subscribeAsync(
+ tokenAddress, TokenEvents.Transfer, subscriptionOpts, indexFilterValues);
+ zeroExEvent.watch((err: Error, event: ContractEvent) => {
+ const args = event.args as TransferContractEventArgs;
+ expect(args._value.isBigNumber).to.be.true();
+ done();
+ });
+ await zeroEx.token.transferAsync(tokenAddress, coinbase, addressWithoutFunds, transferAmount);
+ })();
+ });
+ });
});