aboutsummaryrefslogtreecommitdiffstats
path: root/packages/subproviders/test/unit/nonce_tracker_subprovider_test.ts
blob: d799bce9e30a4267097c6a3176288adedc5f18dc (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import * as chai from 'chai';
import 'make-promises-safe';
import Web3ProviderEngine = require('web3-provider-engine');
import FixtureSubprovider = require('web3-provider-engine/subproviders/fixture');

import { promisify } from '@0xproject/utils';
import EthereumTx = require('ethereumjs-tx');

import { NonceTrackerSubprovider } from '../../src';
import { chaiSetup } from '../chai_setup';

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

describe('NonceTrackerSubprovider', () => {
    let provider: Web3ProviderEngine;
    const getTransactionCountPayload = {
        jsonrpc: '2.0',
        method: 'eth_getTransactionCount',
        params: ['0x0', 'pending'],
        id: 1,
    };
    const sendTransactionPayload = {
        jsonrpc: '2.0',
        method: 'eth_sendRawTransaction',
        params: [],
        id: 1,
    };
    const txParams = [
        '0x',
        '0x09184e72a000',
        '0x2710',
        '0x0000000000000000000000000000000000000000',
        '0x',
        '0x7f7465737432000000000000000000000000000000000000000000000000000000600057',
        '0x1c',
        '0x5e1d3a76fbf824220eafc8c79ad578ad2b67d01b0c2425eb1f1347e8f50882ab',
        '0x5bd428537f05f9830e93792f90ea6a3e2d1ee84952dd96edbae9f658f831ab13',
    ];
    function createFixtureSubprovider(): FixtureSubprovider {
        let isFirstGetTransactionCount = true;
        const fixedBlockNumberAndTransactionCountProvider = new FixtureSubprovider({
            eth_getBlockByNumber: '0x01',
            eth_getTransactionCount: (_data: any, _next: any, end: any) => {
                // For testing caching we return different results on the second call
                if (isFirstGetTransactionCount) {
                    isFirstGetTransactionCount = false;
                    end(null, '0x00');
                } else {
                    end(null, '0x99');
                }
            },
        });
        return fixedBlockNumberAndTransactionCountProvider;
    }
    it('successfully caches the transaction count', async () => {
        provider = new Web3ProviderEngine();
        const nonceTrackerSubprovider = new NonceTrackerSubprovider();
        provider.addProvider(nonceTrackerSubprovider);
        provider.addProvider(createFixtureSubprovider());
        provider.start();

        const payload = { ...getTransactionCountPayload, params: ['0x0', 'pending'] };

        const response = await promisify<any>(provider.sendAsync, provider)(payload);
        expect(response.result).to.be.eq('0x00');
        const secondResponse = await promisify<any>(provider.sendAsync, provider)(payload);
        expect(secondResponse.result).to.be.eq('0x00');
    });
    it('does not cache the result for latest transaction count', async () => {
        provider = new Web3ProviderEngine();
        const nonceTrackerSubprovider = new NonceTrackerSubprovider();
        provider.addProvider(nonceTrackerSubprovider);
        provider.addProvider(createFixtureSubprovider());
        provider.start();

        const payload = { ...getTransactionCountPayload, params: ['0x0', 'latest'] };

        const response = await promisify<any>(provider.sendAsync, provider)(payload);
        expect(response.result).to.be.eq('0x00');
        const secondResponse = await promisify<any>(provider.sendAsync, provider)(payload);
        expect(secondResponse.result).to.be.eq('0x99');
    });
    it('clears the cache on a Nonce Too Low Error', async () => {
        provider = new Web3ProviderEngine();
        const nonceTrackerSubprovider = new NonceTrackerSubprovider();
        provider.addProvider(nonceTrackerSubprovider);
        provider.addProvider(createFixtureSubprovider());
        provider.addProvider(
            new FixtureSubprovider({
                eth_sendRawTransaction: (_data: any, _next: any, end: any) => {
                    end(new Error('Transaction nonce is too low'));
                },
            }),
        );
        provider.start();

        const noncePayload = {
            ...getTransactionCountPayload,
            params: ['0x1f36f546477cda21bf2296c50976f2740247906f', 'pending'],
        };
        const transaction = new EthereumTx(txParams);
        const txPayload = {
            ...sendTransactionPayload,
            params: [transaction.serialize()],
        };

        const response = await promisify<any>(provider.sendAsync, provider)(noncePayload);
        expect(response.result).to.be.eq('0x00');
        const secondResponse = await promisify<any>(provider.sendAsync, provider)(noncePayload);
        expect(secondResponse.result).to.be.eq('0x00');
        try {
            await promisify(provider.sendAsync, provider)(txPayload);
        } catch (err) {
            const thirdResponse = await promisify<any>(provider.sendAsync, provider)(noncePayload);
            expect(thirdResponse.result).to.be.eq('0x99');
        }
    });
    it('increments the used nonce when a transaction successfully submits', async () => {
        provider = new Web3ProviderEngine();
        const nonceTrackerSubprovider = new NonceTrackerSubprovider();
        provider.addProvider(nonceTrackerSubprovider);
        provider.addProvider(createFixtureSubprovider());
        provider.addProvider(
            new FixtureSubprovider({
                eth_sendRawTransaction: (_data: any, _next: any, end: any) => {
                    end(null);
                },
            }),
        );
        provider.start();

        const noncePayload = {
            ...getTransactionCountPayload,
            params: ['0x1f36f546477cda21bf2296c50976f2740247906f', 'pending'],
        };
        const transaction = new EthereumTx(txParams);
        const txPayload = {
            ...sendTransactionPayload,
            params: [transaction.serialize()],
        };

        const response = await promisify<any>(provider.sendAsync, provider)(noncePayload);
        expect(response.result).to.be.eq('0x00');
        const secondResponse = await promisify<any>(provider.sendAsync, provider)(noncePayload);
        expect(secondResponse.result).to.be.eq('0x00');
        await promisify(provider.sendAsync, provider)(txPayload);
        const thirdResponse = await promisify<any>(provider.sendAsync, provider)(noncePayload);
        expect(thirdResponse.result).to.be.eq('0x01');
    });
});