From 3998b47d848d4b74d6db45d375420fe2cfcf1926 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Tue, 23 Jan 2018 16:56:16 -0800 Subject: Implement testnet faucets for any testnet available via infura --- packages/testnet-faucets/src/ts/configs.ts | 7 +- packages/testnet-faucets/src/ts/handler.ts | 78 ++++++++++------------ packages/testnet-faucets/src/ts/id_management.ts | 2 - packages/testnet-faucets/src/ts/request_queue.ts | 2 +- packages/testnet-faucets/src/ts/rpc_urls.ts | 13 ++++ .../testnet-faucets/src/ts/zrx_request_queue.ts | 11 +-- 6 files changed, 58 insertions(+), 55 deletions(-) create mode 100644 packages/testnet-faucets/src/ts/rpc_urls.ts (limited to 'packages/testnet-faucets/src') diff --git a/packages/testnet-faucets/src/ts/configs.ts b/packages/testnet-faucets/src/ts/configs.ts index 2e5a7f64d..038c8e22a 100644 --- a/packages/testnet-faucets/src/ts/configs.ts +++ b/packages/testnet-faucets/src/ts/configs.ts @@ -2,11 +2,6 @@ export const configs = { DISPENSER_ADDRESS: (process.env.DISPENSER_ADDRESS as string).toLowerCase(), DISPENSER_PRIVATE_KEY: process.env.DISPENSER_PRIVATE_KEY, ENVIRONMENT: process.env.FAUCET_ENVIRONMENT, + INFURA_API_KEY: process.env.INFURA_API_KEY, ROLLBAR_ACCESS_KEY: process.env.FAUCET_ROLLBAR_ACCESS_KEY, - RPC_URL: - process.env.FAUCET_ENVIRONMENT === 'development' - ? 'http://127.0.0.1:8545' - : `https://kovan.infura.io/${process.env.INFURA_API_KEY}`, - ZRX_TOKEN_ADDRESS: '0x6ff6c0ff1d68b964901f986d4c9fa3ac68346570', - KOVAN_NETWORK_ID: 42, }; 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); } } diff --git a/packages/testnet-faucets/src/ts/id_management.ts b/packages/testnet-faucets/src/ts/id_management.ts index 930821172..db9b610a3 100644 --- a/packages/testnet-faucets/src/ts/id_management.ts +++ b/packages/testnet-faucets/src/ts/id_management.ts @@ -1,13 +1,11 @@ import EthereumTx = require('ethereumjs-tx'); import { configs } from './configs'; -import { utils } from './utils'; type Callback = (err: Error | null, accounts: any) => void; export const idManagement = { getAccounts(callback: Callback) { - utils.consoleLog(`configs.DISPENSER_ADDRESS: ${configs.DISPENSER_ADDRESS}`); callback(null, [configs.DISPENSER_ADDRESS]); }, approveTransaction(txData: object, callback: Callback) { diff --git a/packages/testnet-faucets/src/ts/request_queue.ts b/packages/testnet-faucets/src/ts/request_queue.ts index 2b42ca4bf..20f2833a1 100644 --- a/packages/testnet-faucets/src/ts/request_queue.ts +++ b/packages/testnet-faucets/src/ts/request_queue.ts @@ -51,6 +51,6 @@ export class RequestQueue { } // tslint:disable-next-line:prefer-function-over-method protected async processNextRequestFireAndForgetAsync(recipientAddress: string) { - throw new Error('Expected processNextRequestFireAndForgetAsync to be implemented by a superclass'); + throw new Error('Expected processNextRequestFireAndForgetAsync to be implemented by a subclass'); } } diff --git a/packages/testnet-faucets/src/ts/rpc_urls.ts b/packages/testnet-faucets/src/ts/rpc_urls.ts new file mode 100644 index 000000000..25a3b938f --- /dev/null +++ b/packages/testnet-faucets/src/ts/rpc_urls.ts @@ -0,0 +1,13 @@ +import { configs } from './configs'; + +const productionRpcUrls = { + '2': `https://ropsten.infura.io/${configs.INFURA_API_KEY}`, + '3': `https://rinkeby.infura.io/${configs.INFURA_API_KEY}`, + '42': `https://kovan.infura.io/${configs.INFURA_API_KEY}`, +}; + +const developmentRpcUrls = { + '50': 'http://127.0.0.1:8545', +}; + +export const rpcUrls = configs.ENVIRONMENT === 'development' ? developmentRpcUrls : productionRpcUrls; diff --git a/packages/testnet-faucets/src/ts/zrx_request_queue.ts b/packages/testnet-faucets/src/ts/zrx_request_queue.ts index bbc06f1de..3d73f9dd2 100644 --- a/packages/testnet-faucets/src/ts/zrx_request_queue.ts +++ b/packages/testnet-faucets/src/ts/zrx_request_queue.ts @@ -18,11 +18,11 @@ const QUEUE_INTERVAL_MS = 5000; export class ZRXRequestQueue extends RequestQueue { private _zeroEx: ZeroEx; - constructor(web3: Web3) { + constructor(web3: Web3, networkId: number) { super(web3); this.queueIntervalMs = QUEUE_INTERVAL_MS; const zeroExConfig = { - networkId: configs.KOVAN_NETWORK_ID, + networkId, }; this._zeroEx = new ZeroEx(web3.currentProvider, zeroExConfig); } @@ -30,13 +30,14 @@ export class ZRXRequestQueue extends RequestQueue { utils.consoleLog(`Processing ZRX ${recipientAddress}`); const baseUnitAmount = ZeroEx.toBaseUnitAmount(DISPENSE_AMOUNT_ZRX, 18); try { - await this._zeroEx.token.transferAsync( - configs.ZRX_TOKEN_ADDRESS, + const zrxTokenAddress = this._zeroEx.exchange.getZRXTokenAddress(); + const txHash = await this._zeroEx.token.transferAsync( + zrxTokenAddress, configs.DISPENSER_ADDRESS, recipientAddress, baseUnitAmount, ); - utils.consoleLog(`Sent ${DISPENSE_AMOUNT_ZRX} ZRX to ${recipientAddress}`); + utils.consoleLog(`Sent ${DISPENSE_AMOUNT_ZRX} ZRX to ${recipientAddress} tx: ${txHash}`); } catch (err) { utils.consoleLog(`Unexpected err: ${err} - ${JSON.stringify(err)}`); await errorReporter.reportAsync(err); -- cgit v1.2.3