aboutsummaryrefslogtreecommitdiffstats
path: root/test/mempool_test.ts
blob: 0c8fb921a4de1e19d40f3e02e24015d322976e41 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import 'mocha';
import * as chai from 'chai';
import * as _ from 'lodash';
import * as Sinon from 'sinon';
import * as Web3 from 'web3';
import BigNumber from 'bignumber.js';
import {chaiSetup} from './utils/chai_setup';
import {web3Factory} from './utils/web3_factory';
import {
    ZeroEx,
    LogEvent,
    DecodedLogEvent,
} from '../src';
import {DoneCallback} from '../src/types';

chaiSetup.configure();
const expect = chai.expect;

describe('MempoolWatcher', () => {
    let web3: Web3;
    let zeroEx: ZeroEx;
    let stubs: Sinon.SinonStub[] = [];
    const logA = {
        address: '0x71d271f8b14adef568f8f28f1587ce7271ac4ca5',
        blockHash: null,
        blockNumber: null,
        data: '',
        logIndex: null,
        topics: [],
        transactionHash: '0x004881d38cd4a8f72f1a0d68c8b9b8124504706041ff37019c1d1ed6bfda8e17',
        transactionIndex: null,
    };
    const logB = {
        address: '0x8d12a197cb00d4747a1fe03395095ce2a5cc6819',
        blockHash: null,
        blockNumber: null,
        data: '',
        logIndex: null,
        topics: [ '0xf341246adaac6f497bc2a656f546ab9e182111d630394f0c57c710a59a2cb567' ],
        transactionHash: '0x01ef3c048b18d9b09ea195b4ed94cf8dd5f3d857a1905ff886b152cfb1166f25',
        transactionIndex: null,
    };
    const logC = {
        address: '0x1d271f8b174adef58f1587ce68f8f27271ac4ca5',
        blockHash: null,
        blockNumber: null,
        data: '',
        logIndex: null,
        topics: [ '0xf341246adaac6f497bc2a656f546ab9e182111d630394f0c57c710a59a2cb567' ],
        transactionHash: '0x01ef3c048b18d9b09ea195b4ed94cf8dd5f3d857a1905ff886b152cfb1166f25',
        transactionIndex: null,
    };
    before(async () => {
        web3 = web3Factory.create();
        const config = {
            mempoolPollingIntervalMs: 10,
        };
        zeroEx = new ZeroEx(web3.currentProvider, config);
    });
    afterEach(() => {
        // clean up any stubs after the test has completed
        _.each(stubs, s => s.restore());
        stubs = [];
        zeroEx.mempool.unsubscribe();
    });
    it('correctly emits initial log events', (done: DoneCallback) => {
        const logs: Web3.LogEntry[] = [logA, logB];
        const expectedLogEvents = [
            {
                removed: false,
                ...logA,
            },
            {
                removed: false,
                ...logB,
            },
        ];
        const getLogsStub = Sinon.stub((zeroEx.mempool as any)._web3Wrapper, 'getLogsAsync');
        getLogsStub.onCall(0).returns(logs);
        stubs.push(getLogsStub);
        const callback = (event: LogEvent) => {
            const expectedLogEvent = expectedLogEvents.shift();
            expect(event).to.be.deep.equal(expectedLogEvent);
            if (_.isEmpty(expectedLogEvents)) {
                done();
            }
        };
        zeroEx.mempool.subscribe(callback);
    });
    it('correctly computes the difference and emits only changes', (done: DoneCallback) => {
        const initialLogs: Web3.LogEntry[] = [logA, logB];
        const changedLogs: Web3.LogEntry[] = [logA, logC];
        const expectedLogEvents = [
            {
                removed: false,
                ...logA,
            },
            {
                removed: false,
                ...logB,
            },
            {
                removed: true,
                ...logB,
            },
            {
                removed: false,
                ...logC,
            },
        ];
        const getLogsStub = Sinon.stub((zeroEx.mempool as any)._web3Wrapper, 'getLogsAsync');
        getLogsStub.onCall(0).returns(initialLogs);
        getLogsStub.onCall(1).returns(changedLogs);
        stubs.push(getLogsStub);
        const callback = (event: LogEvent) => {
            // console.log(event);
            const expectedLogEvent = expectedLogEvents.shift();
            expect(event).to.be.deep.equal(expectedLogEvent);
            if (_.isEmpty(expectedLogEvents)) {
                done();
            }
        };
        zeroEx.mempool.subscribe(callback);
    });
});