aboutsummaryrefslogtreecommitdiffstats
path: root/packages/kovan-faucets/src/ts/handler.ts
blob: f3c6ba1f03610163aecd27019a9cb7496f834f4d (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
import * as express from 'express';
import * as _ from 'lodash';
import * as Web3 from 'web3';
import ProviderEngine = require('web3-provider-engine');
import HookedWalletSubprovider = require('web3-provider-engine/subproviders/hooked-wallet');
import NonceSubprovider = require('web3-provider-engine/subproviders/nonce-tracker');
import RpcSubprovider = require('web3-provider-engine/subproviders/rpc');

import {configs} from './configs';
import {EtherRequestQueue} from './ether_request_queue';
import {idManagement} from './id_management';
import {utils} from './utils';
import {ZRXRequestQueue} from './zrx_request_queue';

// HACK: web3 leaks XMLHttpRequest into the global scope and causes requests to hang
// because they are using the wrong XHR package.
// Issue: https://github.com/trufflesuite/truffle-contract/issues/14
delete (global as any).XMLHttpRequest;

export class Handler {
    private etherRequestQueue: EtherRequestQueue;
    private zrxRequestQueue: ZRXRequestQueue;
    private web3: Web3;
    constructor() {
        // Setup provider engine to talk with RPC node
        const providerObj = this.createProviderEngine(configs.RPC_URL);
        this.web3 = new Web3(providerObj);

        this.etherRequestQueue = new EtherRequestQueue(this.web3);
        this.zrxRequestQueue = new ZRXRequestQueue(this.web3);
    }
    public dispenseEther(req: express.Request, res: express.Response) {
        const recipientAddress = req.params.recipient;
        if (_.isUndefined(recipientAddress) || !this.isValidEthereumAddress(recipientAddress)) {
            res.status(400).send('INVALID_REQUEST');
            return;
        }
        const lowerCaseRecipientAddress = recipientAddress.toLowerCase();
        const didAddToQueue = this.etherRequestQueue.add(lowerCaseRecipientAddress);
        if (!didAddToQueue) {
            res.status(503).send('QUEUE_IS_FULL');
            return;
        }
        utils.consoleLog(`Added ${lowerCaseRecipientAddress} to the ETH queue`);
        res.status(200).end();
    }
    public dispenseZRX(req: express.Request, res: express.Response) {
        const recipientAddress = req.params.recipient;
        if (_.isUndefined(recipientAddress) || !this.isValidEthereumAddress(recipientAddress)) {
            res.status(400).send('INVALID_REQUEST');
            return;
        }
        const lowerCaseRecipientAddress = recipientAddress.toLowerCase();
        const didAddToQueue = this.zrxRequestQueue.add(lowerCaseRecipientAddress);
        if (!didAddToQueue) {
            res.status(503).send('QUEUE_IS_FULL');
            return;
        }
        utils.consoleLog(`Added ${lowerCaseRecipientAddress} to the ZRX queue`);
        res.status(200).end();
    }
    public getQueueInfo(req: express.Request, res: express.Response) {
        res.setHeader('Content-Type', 'application/json');
        const payload = JSON.stringify({
            ether: {
                full: this.etherRequestQueue.isFull(),
                size: this.etherRequestQueue.size(),
            },
            zrx: {
                full: this.zrxRequestQueue.isFull(),
                size: this.zrxRequestQueue.size(),
            },
        });
        res.status(200).send(payload);
    }
    // tslint:disable-next-line:prefer-function-over-method
    private createProviderEngine(rpcUrl: string) {
        const engine = new ProviderEngine();
        engine.addProvider(new NonceSubprovider());
        engine.addProvider(new HookedWalletSubprovider(idManagement));
        engine.addProvider(new RpcSubprovider({
            rpcUrl,
        }));
        engine.start();
        return engine;
    }
    private isValidEthereumAddress(address: string): boolean {
        const lowercaseAddress = address.toLowerCase();
        return this.web3.isAddress(lowercaseAddress);
    }
}