aboutsummaryrefslogtreecommitdiffstats
path: root/packages/testnet-faucets/src/ts/handler.ts
blob: 9abfcc4f04111cdf76b7f505d17a70ce7f928f6f (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { Order, ZeroEx } from '0x.js';
import { BigNumber, logUtils } from '@0xproject/utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import { Provider } from 'ethereum-types';
import * as express from 'express';
import * as _ from 'lodash';

import {
    NonceTrackerSubprovider,
    PrivateKeyWalletSubprovider,
    RPCSubprovider,
    Web3ProviderEngine,
} from '@0xproject/subproviders';

import { configs } from './configs';
import { constants } from './constants';
import { DispatchQueue } from './dispatch_queue';
import { dispenseAssetTasks } from './dispense_asset_tasks';
import { rpcUrls } from './rpc_urls';

interface NetworkConfig {
    dispatchQueue: DispatchQueue;
    web3Wrapper: Web3Wrapper;
    zeroEx: ZeroEx;
}

interface ItemByNetworkId<T> {
    [networkId: string]: T;
}

enum RequestedAssetType {
    ETH = 'ETH',
    WETH = 'WETH',
    ZRX = 'ZRX',
}

const FIVE_DAYS_IN_MS = 4.32e8; // TODO: make this configurable

export class Handler {
    private readonly _networkConfigByNetworkId: ItemByNetworkId<NetworkConfig> = {};
    private static _createProviderEngine(rpcUrl: string): Provider {
        if (_.isUndefined(configs.DISPENSER_PRIVATE_KEY)) {
            throw new Error('Dispenser Private key not found');
        }
        const engine = new Web3ProviderEngine();
        engine.addProvider(new NonceTrackerSubprovider());
        engine.addProvider(new PrivateKeyWalletSubprovider(configs.DISPENSER_PRIVATE_KEY));
        engine.addProvider(new RPCSubprovider(rpcUrl));
        engine.start();
        return engine;
    }
    constructor() {
        _.forIn(rpcUrls, (rpcUrl: string, networkId: string) => {
            const providerObj = Handler._createProviderEngine(rpcUrl);
            const web3Wrapper = new Web3Wrapper(providerObj);
            const zeroExConfig = {
                networkId: +networkId,
            };
            const zeroEx = new ZeroEx(providerObj, zeroExConfig);
            const dispatchQueue = new DispatchQueue();
            this._networkConfigByNetworkId[networkId] = {
                dispatchQueue,
                web3Wrapper,
                zeroEx,
            };
        });
    }
    public getQueueInfo(_req: express.Request, res: express.Response): void {
        res.setHeader('Content-Type', 'application/json');
        const queueInfo = _.mapValues(rpcUrls, (_rpcUrl: string, networkId: string) => {
            const dispatchQueue = this._networkConfigByNetworkId[networkId].dispatchQueue;
            return {
                full: dispatchQueue.isFull(),
                size: dispatchQueue.size(),
            };
        });
        const payload = JSON.stringify(queueInfo);
        res.status(constants.SUCCESS_STATUS).send(payload);
    }
    public dispenseEther(req: express.Request, res: express.Response): void {
        this._dispenseAsset(req, res, RequestedAssetType.ETH);
    }
    public dispenseZRX(req: express.Request, res: express.Response): void {
        this._dispenseAsset(req, res, RequestedAssetType.ZRX);
    }
    public async dispenseWETHOrderAsync(req: express.Request, res: express.Response): Promise<void> {
        await this._dispenseOrderAsync(req, res, RequestedAssetType.WETH);
    }
    public async dispenseZRXOrderAsync(
        req: express.Request,
        res: express.Response,
        _next: express.NextFunction,
    ): Promise<void> {
        await this._dispenseOrderAsync(req, res, RequestedAssetType.ZRX);
    }
    private _dispenseAsset(req: express.Request, res: express.Response, requestedAssetType: RequestedAssetType): void {
        const networkId = req.params.networkId;
        const recipient = req.params.recipient;
        const networkConfig = this._networkConfigByNetworkId[networkId];
        let dispenserTask;
        switch (requestedAssetType) {
            case RequestedAssetType.ETH:
                dispenserTask = dispenseAssetTasks.dispenseEtherTask(recipient, networkConfig.web3Wrapper);
                break;
            case RequestedAssetType.WETH:
            case RequestedAssetType.ZRX:
                dispenserTask = dispenseAssetTasks.dispenseTokenTask(
                    recipient,
                    requestedAssetType,
                    networkConfig.zeroEx,
                );
                break;
            default:
                throw new Error(`Unsupported asset type: ${requestedAssetType}`);
        }
        const didAddToQueue = networkConfig.dispatchQueue.add(dispenserTask);
        if (!didAddToQueue) {
            res.status(constants.SERVICE_UNAVAILABLE_STATUS).send('QUEUE_IS_FULL');
            return;
        }
        logUtils.log(`Added ${recipient} to queue: ${requestedAssetType} networkId: ${networkId}`);
        res.status(constants.SUCCESS_STATUS).end();
    }
    private async _dispenseOrderAsync(
        req: express.Request,
        res: express.Response,
        requestedAssetType: RequestedAssetType,
    ): Promise<void> {
        const networkConfig = _.get(this._networkConfigByNetworkId, req.params.networkId);
        if (_.isUndefined(networkConfig)) {
            res.status(constants.BAD_REQUEST_STATUS).send('UNSUPPORTED_NETWORK_ID');
            return;
        }
        const zeroEx = networkConfig.zeroEx;
        res.setHeader('Content-Type', 'application/json');
        const makerToken = await zeroEx.tokenRegistry.getTokenBySymbolIfExistsAsync(requestedAssetType);
        if (_.isUndefined(makerToken)) {
            throw new Error(`Unsupported asset type: ${requestedAssetType}`);
        }
        const takerTokenSymbol =
            requestedAssetType === RequestedAssetType.WETH ? RequestedAssetType.ZRX : RequestedAssetType.WETH;
        const takerToken = await zeroEx.tokenRegistry.getTokenBySymbolIfExistsAsync(takerTokenSymbol);
        if (_.isUndefined(takerToken)) {
            throw new Error(`Unsupported asset type: ${requestedAssetType}`);
        }
        const makerAssetAmount = ZeroEx.toBaseUnitAmount(new BigNumber(0.1), makerToken.decimals);
        const takerAssetAmount = ZeroEx.toBaseUnitAmount(new BigNumber(0.1), takerToken.decimals);
        const order: Order = {
            senderAddress: configs.DISPENSER_ADDRESS,
            makerAddress: configs.DISPENSER_ADDRESS,
            takerAddress: req.params.recipient,
            makerFee: new BigNumber(0),
            takerFee: new BigNumber(0),
            makerAssetAmount,
            takerAssetAmount,
            makerAssetData: makerToken.address,
            takerAssetData: takerToken.address,
            salt: ZeroEx.generatePseudoRandomSalt(),
            exchangeAddress: zeroEx.exchange.getContractAddress(),
            feeRecipientAddress: ZeroEx.NULL_ADDRESS,
            expirationTimeSeconds: new BigNumber(Date.now() + FIVE_DAYS_IN_MS),
        };
        const orderHash = ZeroEx.getOrderHashHex(order);
        const signature = await zeroEx.signOrderHashAsync(orderHash, configs.DISPENSER_ADDRESS, false);
        const signedOrder = {
            ...order,
            ecSignature: signature,
        };
        const payload = JSON.stringify(signedOrder);
        logUtils.log(`Dispensed signed order: ${payload}`);
        res.status(constants.SUCCESS_STATUS).send(payload);
    }
}