diff options
author | Brandon Millman <brandon.millman@gmail.com> | 2018-01-24 08:56:16 +0800 |
---|---|---|
committer | Brandon Millman <brandon.millman@gmail.com> | 2018-01-24 09:42:35 +0800 |
commit | 3998b47d848d4b74d6db45d375420fe2cfcf1926 (patch) | |
tree | bf465c54ef4ff72e3e8853fc6c2c895eac347734 /packages/testnet-faucets/src/ts/handler.ts | |
parent | d965fdb11dfba5631c1f98ad3e309876c199999a (diff) | |
download | dexon-sol-tools-3998b47d848d4b74d6db45d375420fe2cfcf1926.tar dexon-sol-tools-3998b47d848d4b74d6db45d375420fe2cfcf1926.tar.gz dexon-sol-tools-3998b47d848d4b74d6db45d375420fe2cfcf1926.tar.bz2 dexon-sol-tools-3998b47d848d4b74d6db45d375420fe2cfcf1926.tar.lz dexon-sol-tools-3998b47d848d4b74d6db45d375420fe2cfcf1926.tar.xz dexon-sol-tools-3998b47d848d4b74d6db45d375420fe2cfcf1926.tar.zst dexon-sol-tools-3998b47d848d4b74d6db45d375420fe2cfcf1926.zip |
Implement testnet faucets for any testnet available via infura
Diffstat (limited to 'packages/testnet-faucets/src/ts/handler.ts')
-rw-r--r-- | packages/testnet-faucets/src/ts/handler.ts | 78 |
1 files changed, 37 insertions, 41 deletions
diff --git a/packages/testnet-faucets/src/ts/handler.ts b/packages/testnet-faucets/src/ts/handler.ts index 4bf776264..8c859be2e 100644 --- a/packages/testnet-faucets/src/ts/handler.ts +++ b/packages/testnet-faucets/src/ts/handler.ts @@ -1,3 +1,4 @@ +import { addressUtils } from '@0xproject/utils'; import * as express from 'express'; import * as _ from 'lodash'; import ProviderEngine = require('web3-provider-engine'); @@ -5,9 +6,10 @@ import HookedWalletSubprovider = require('web3-provider-engine/subproviders/hook 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 { RequestQueue } from './request_queue'; +import { rpcUrls } from './rpc_urls'; import { utils } from './utils'; import { ZRXRequestQueue } from './zrx_request_queue'; @@ -17,62 +19,55 @@ import { ZRXRequestQueue } from './zrx_request_queue'; // tslint:disable-next-line:ordered-imports import * as Web3 from 'web3'; +interface RequestQueueByNetworkId { + [networkId: string]: RequestQueue; +} + +const DEFAULT_NETWORK_ID = 42; // kovan + export class Handler { - private _etherRequestQueue: EtherRequestQueue; - private _zrxRequestQueue: ZRXRequestQueue; - private _web3: Web3; + private _etherRequestQueueByNetworkId: RequestQueueByNetworkId = {}; + private _zrxRequestQueueByNetworkId: RequestQueueByNetworkId = {}; 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); + _.forIn(rpcUrls, (rpcUrl: string, networkId: string) => { + const providerObj = this._createProviderEngine(rpcUrl); + const web3 = new Web3(providerObj); + this._etherRequestQueueByNetworkId[networkId] = new EtherRequestQueue(web3); + this._zrxRequestQueueByNetworkId[networkId] = new ZRXRequestQueue(web3, +networkId); + }); } 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(); + this._dispense(req, res, this._etherRequestQueueByNetworkId, 'ETH'); } public dispenseZRX(req: express.Request, res: express.Response) { + this._dispense(req, res, this._zrxRequestQueueByNetworkId, 'ZRX'); + } + private _dispense( + req: express.Request, + res: express.Response, + requestQueueByNetworkId: RequestQueueByNetworkId, + assetSymbol: string, + ) { const recipientAddress = req.params.recipient; if (_.isUndefined(recipientAddress) || !this._isValidEthereumAddress(recipientAddress)) { - res.status(400).send('INVALID_REQUEST'); + res.status(400).send('INVALID_RECIPIENT_ADDRESS'); + return; + } + const networkId = _.get(req.query, 'networkId', DEFAULT_NETWORK_ID); + const requestQueue = _.get(requestQueueByNetworkId, networkId); + if (_.isUndefined(requestQueue)) { + res.status(400).send('INVALID_NETWORK_ID'); return; } const lowerCaseRecipientAddress = recipientAddress.toLowerCase(); - const didAddToQueue = this._zrxRequestQueue.add(lowerCaseRecipientAddress); + const didAddToQueue = requestQueue.add(lowerCaseRecipientAddress); if (!didAddToQueue) { res.status(503).send('QUEUE_IS_FULL'); return; } - utils.consoleLog(`Added ${lowerCaseRecipientAddress} to the ZRX queue`); + utils.consoleLog(`Added ${lowerCaseRecipientAddress} to queue: ${assetSymbol} networkId: ${networkId}`); 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(); @@ -86,8 +81,9 @@ export class Handler { engine.start(); return engine; } + // tslint:disable-next-line:prefer-function-over-method private _isValidEthereumAddress(address: string): boolean { const lowercaseAddress = address.toLowerCase(); - return this._web3.isAddress(lowercaseAddress); + return addressUtils.isAddress(lowercaseAddress); } } |