diff options
Diffstat (limited to 'packages')
-rw-r--r-- | packages/kovan-faucets/src/ts/handler.ts | 93 | ||||
-rw-r--r-- | packages/testnet-faucets/Dockerfile (renamed from packages/kovan-faucets/Dockerfile) | 0 | ||||
-rw-r--r-- | packages/testnet-faucets/README.md (renamed from packages/kovan-faucets/README.md) | 65 | ||||
-rw-r--r-- | packages/testnet-faucets/gulpfile.js (renamed from packages/kovan-faucets/gulpfile.js) | 0 | ||||
-rw-r--r-- | packages/testnet-faucets/package.json (renamed from packages/kovan-faucets/package.json) | 2 | ||||
-rw-r--r-- | packages/testnet-faucets/scripts/postpublish.js (renamed from packages/kovan-faucets/scripts/postpublish.js) | 0 | ||||
-rw-r--r-- | packages/testnet-faucets/src/ts/configs.ts (renamed from packages/kovan-faucets/src/ts/configs.ts) | 7 | ||||
-rw-r--r-- | packages/testnet-faucets/src/ts/error_reporter.ts (renamed from packages/kovan-faucets/src/ts/error_reporter.ts) | 0 | ||||
-rw-r--r-- | packages/testnet-faucets/src/ts/ether_request_queue.ts (renamed from packages/kovan-faucets/src/ts/ether_request_queue.ts) | 0 | ||||
-rw-r--r-- | packages/testnet-faucets/src/ts/global.d.ts (renamed from packages/kovan-faucets/src/ts/global.d.ts) | 0 | ||||
-rw-r--r-- | packages/testnet-faucets/src/ts/handler.ts | 114 | ||||
-rw-r--r-- | packages/testnet-faucets/src/ts/id_management.ts (renamed from packages/kovan-faucets/src/ts/id_management.ts) | 2 | ||||
-rw-r--r-- | packages/testnet-faucets/src/ts/request_queue.ts (renamed from packages/kovan-faucets/src/ts/request_queue.ts) | 2 | ||||
-rw-r--r-- | packages/testnet-faucets/src/ts/rpc_urls.ts | 13 | ||||
-rw-r--r-- | packages/testnet-faucets/src/ts/server.ts (renamed from packages/kovan-faucets/src/ts/server.ts) | 1 | ||||
-rw-r--r-- | packages/testnet-faucets/src/ts/utils.ts (renamed from packages/kovan-faucets/src/ts/utils.ts) | 0 | ||||
-rw-r--r-- | packages/testnet-faucets/src/ts/zrx_request_queue.ts (renamed from packages/kovan-faucets/src/ts/zrx_request_queue.ts) | 11 | ||||
-rw-r--r-- | packages/testnet-faucets/tsconfig.json (renamed from packages/kovan-faucets/tsconfig.json) | 0 | ||||
-rw-r--r-- | packages/testnet-faucets/tslint.json (renamed from packages/kovan-faucets/tslint.json) | 0 |
19 files changed, 195 insertions, 115 deletions
diff --git a/packages/kovan-faucets/src/ts/handler.ts b/packages/kovan-faucets/src/ts/handler.ts deleted file mode 100644 index 4bf776264..000000000 --- a/packages/kovan-faucets/src/ts/handler.ts +++ /dev/null @@ -1,93 +0,0 @@ -import * as express from 'express'; -import * as _ from 'lodash'; -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. -// Filed issue: https://github.com/ethereum/web3.js/issues/844 -// tslint:disable-next-line:ordered-imports -import * as Web3 from 'web3'; - -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); - } -} diff --git a/packages/kovan-faucets/Dockerfile b/packages/testnet-faucets/Dockerfile index 6d6ddc192..6d6ddc192 100644 --- a/packages/kovan-faucets/Dockerfile +++ b/packages/testnet-faucets/Dockerfile diff --git a/packages/kovan-faucets/README.md b/packages/testnet-faucets/README.md index 07bd23575..00352cc2a 100644 --- a/packages/kovan-faucets/README.md +++ b/packages/testnet-faucets/README.md @@ -1,4 +1,4 @@ -## @0xproject/kovan_faucets +## @0xproject/testnet-faucets This faucet dispenses 0.1 test ether to one recipient per second and 0.1 test ZRX every 5 seconds. It has a max queue size of 1000. @@ -31,14 +31,19 @@ yarn install Set the following environment variables: ```bash -export FAUCET_ENVIRONMENT=development export DISPENSER_ADDRESS=0x5409ed021d9299bf6814279a6a1411a7e866a631 export DISPENSER_PRIVATE_KEY=f2f48ee19680706196e2e339e5da3491186e0c4c5030670656b0e0164837257d export FAUCET_ROLLBAR_ACCESS_KEY={GET_THIS_FROM_ROLLBAR_ACCOUNT_SETTINGS} export INFURA_API_KEY={GET_THIS_FROM_INFURA} ``` -Infura API Key can be requested here: https://infura.io/register.html +If you want to talk to testrpc, set the following environment variable: + +```bash +export FAUCET_ENVIRONMENT=development +``` + +Infura API Key can be requested here: https://infura.io/signup Note: The above public/private keys exist when running `testrpc` with the following option `--mnemonic concert load couple harbor equip island argue ramp clarify fence smart topic`. @@ -48,27 +53,73 @@ yarn dev ### Endpoints +`GET /ping` + +Returns `pong` + +`GET /info` + +Returns a JSON payload describing the state of the queues for each network. For example: + +```json +{ + "3":{ + "ether":{ + "full":false, + "size":4 + }, + "zrx":{ + "full":false, + "size":6 + } + }, + "42":{ + "ether":{ + "full":false, + "size":8 + }, + "zrx":{ + "full":false, + "size":20 + } + } +} +``` + `GET /ether/:recipient` -Where recipient_address is a hex encoded Ethereum address prefixed with `0x`. +Where recipient is a hex encoded Ethereum address prefixed with `0x`. `GET /zrx/:recipient` -Where recipient_address is a hex encoded Ethereum address prefixed with `0x`. +Where recipient is a hex encoded Ethereum address prefixed with `0x`. + +#### Parameters + +The endpoints `/ether` and `/zrx` take a query parameter named `networkId` to specify the desired network where you would like to receive the ETH or ZRX. For example: + +```bash +curl -i http://localhost:3000/ether/0x14e2F1F157E7DD4057D02817436D628A37120FD1\?networkId=3 +``` + +This command will request the local server to initiate a transfer of 0.1 ETH from the dispensing address to `0x14e2F1F157E7DD4057D02817436D628A37120FD1` on the Ropsten testnet. + +If no `networkId` is provided via query parameters the faucet will default to network 42 (Kovan) ### Docker configs ``` docker run -d \ -p 80:3000 \ ---name kovan-faucets \ +--name testnet-faucets \ --log-opt max-size=100m \ --log-opt max-file=20 \ -e DISPENSER_ADDRESS=$DISPENSER_ADDRESS \ -e DISPENSER_PRIVATE_KEY=$DISPENSER_PRIVATE_KEY \ -e FAUCET_ROLLBAR_ACCESS_KEY=$FAUCET_ROLLBAR_ACCESS_KEY \ -e FAUCET_ENVIRONMENT=production \ -kovan-faucets +-e INFURA_API_KEY=$INFURA_API_KEY \ +testnet-faucets ``` ### Lint diff --git a/packages/kovan-faucets/gulpfile.js b/packages/testnet-faucets/gulpfile.js index 773faf33a..773faf33a 100644 --- a/packages/kovan-faucets/gulpfile.js +++ b/packages/testnet-faucets/gulpfile.js diff --git a/packages/kovan-faucets/package.json b/packages/testnet-faucets/package.json index 7dc6b0512..bb0484a04 100644 --- a/packages/kovan-faucets/package.json +++ b/packages/testnet-faucets/package.json @@ -1,6 +1,6 @@ { "private": true, - "name": "@0xproject/kovan_faucets", + "name": "@0xproject/testnet-faucets", "version": "1.0.6", "description": "A faucet micro-service that dispenses test ERC20 tokens or Ether", "main": "server.js", diff --git a/packages/kovan-faucets/scripts/postpublish.js b/packages/testnet-faucets/scripts/postpublish.js index 16d67e03f..16d67e03f 100644 --- a/packages/kovan-faucets/scripts/postpublish.js +++ b/packages/testnet-faucets/scripts/postpublish.js diff --git a/packages/kovan-faucets/src/ts/configs.ts b/packages/testnet-faucets/src/ts/configs.ts index 2e5a7f64d..038c8e22a 100644 --- a/packages/kovan-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/kovan-faucets/src/ts/error_reporter.ts b/packages/testnet-faucets/src/ts/error_reporter.ts index 6865d3893..6865d3893 100644 --- a/packages/kovan-faucets/src/ts/error_reporter.ts +++ b/packages/testnet-faucets/src/ts/error_reporter.ts diff --git a/packages/kovan-faucets/src/ts/ether_request_queue.ts b/packages/testnet-faucets/src/ts/ether_request_queue.ts index 1c4b19ab9..1c4b19ab9 100644 --- a/packages/kovan-faucets/src/ts/ether_request_queue.ts +++ b/packages/testnet-faucets/src/ts/ether_request_queue.ts diff --git a/packages/kovan-faucets/src/ts/global.d.ts b/packages/testnet-faucets/src/ts/global.d.ts index 97cd35680..97cd35680 100644 --- a/packages/kovan-faucets/src/ts/global.d.ts +++ b/packages/testnet-faucets/src/ts/global.d.ts diff --git a/packages/testnet-faucets/src/ts/handler.ts b/packages/testnet-faucets/src/ts/handler.ts new file mode 100644 index 000000000..bf5b3e81e --- /dev/null +++ b/packages/testnet-faucets/src/ts/handler.ts @@ -0,0 +1,114 @@ +import { addressUtils } from '@0xproject/utils'; +import * as express from 'express'; +import * as _ from 'lodash'; +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 { 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'; + +// HACK: web3 leaks XMLHttpRequest into the global scope and causes requests to hang +// because they are using the wrong XHR package. +// Filed issue: https://github.com/ethereum/web3.js/issues/844 +// tslint:disable-next-line:ordered-imports +import * as Web3 from 'web3'; + +interface RequestQueueByNetworkId { + [networkId: string]: RequestQueue; +} + +enum QueueType { + ETH = 'ETH', + ZRX = 'ZRX', +} + +const DEFAULT_NETWORK_ID = 42; // kovan + +export class Handler { + private _etherRequestQueueByNetworkId: RequestQueueByNetworkId = {}; + private _zrxRequestQueueByNetworkId: RequestQueueByNetworkId = {}; + constructor() { + _.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 getQueueInfo(req: express.Request, res: express.Response) { + res.setHeader('Content-Type', 'application/json'); + const queueInfo = _.mapValues(rpcUrls, (rpcUrl: string, networkId: string) => { + utils.consoleLog(networkId); + const etherRequestQueue = this._etherRequestQueueByNetworkId[networkId]; + const zrxRequestQueue = this._zrxRequestQueueByNetworkId[networkId]; + return { + ether: { + full: etherRequestQueue.isFull(), + size: etherRequestQueue.size(), + }, + zrx: { + full: zrxRequestQueue.isFull(), + size: zrxRequestQueue.size(), + }, + }; + }); + const payload = JSON.stringify(queueInfo); + res.status(200).send(payload); + } + public dispenseEther(req: express.Request, res: express.Response) { + this._dispense(req, res, this._etherRequestQueueByNetworkId, QueueType.ETH); + } + public dispenseZRX(req: express.Request, res: express.Response) { + this._dispense(req, res, this._zrxRequestQueueByNetworkId, QueueType.ZRX); + } + private _dispense( + req: express.Request, + res: express.Response, + requestQueueByNetworkId: RequestQueueByNetworkId, + queueType: QueueType, + ) { + const recipientAddress = req.params.recipient; + if (_.isUndefined(recipientAddress) || !this._isValidEthereumAddress(recipientAddress)) { + 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 = requestQueue.add(lowerCaseRecipientAddress); + if (!didAddToQueue) { + res.status(503).send('QUEUE_IS_FULL'); + return; + } + utils.consoleLog(`Added ${lowerCaseRecipientAddress} to queue: ${queueType} networkId: ${networkId}`); + res.status(200).end(); + } + // 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; + } + // tslint:disable-next-line:prefer-function-over-method + private _isValidEthereumAddress(address: string): boolean { + const lowercaseAddress = address.toLowerCase(); + return addressUtils.isAddress(lowercaseAddress); + } +} diff --git a/packages/kovan-faucets/src/ts/id_management.ts b/packages/testnet-faucets/src/ts/id_management.ts index 930821172..db9b610a3 100644 --- a/packages/kovan-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/kovan-faucets/src/ts/request_queue.ts b/packages/testnet-faucets/src/ts/request_queue.ts index 2b42ca4bf..20f2833a1 100644 --- a/packages/kovan-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/kovan-faucets/src/ts/server.ts b/packages/testnet-faucets/src/ts/server.ts index 23642787d..26edfff5a 100644 --- a/packages/kovan-faucets/src/ts/server.ts +++ b/packages/testnet-faucets/src/ts/server.ts @@ -19,6 +19,7 @@ const handler = new Handler(); app.get('/ping', (req: express.Request, res: express.Response) => { res.status(200).send('pong'); }); +app.get('/info', handler.getQueueInfo.bind(handler)); app.get('/ether/:recipient', handler.dispenseEther.bind(handler)); app.get('/zrx/:recipient', handler.dispenseZRX.bind(handler)); diff --git a/packages/kovan-faucets/src/ts/utils.ts b/packages/testnet-faucets/src/ts/utils.ts index 893f82ca3..893f82ca3 100644 --- a/packages/kovan-faucets/src/ts/utils.ts +++ b/packages/testnet-faucets/src/ts/utils.ts diff --git a/packages/kovan-faucets/src/ts/zrx_request_queue.ts b/packages/testnet-faucets/src/ts/zrx_request_queue.ts index bbc06f1de..3d73f9dd2 100644 --- a/packages/kovan-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); diff --git a/packages/kovan-faucets/tsconfig.json b/packages/testnet-faucets/tsconfig.json index 7f0c084ff..7f0c084ff 100644 --- a/packages/kovan-faucets/tsconfig.json +++ b/packages/testnet-faucets/tsconfig.json diff --git a/packages/kovan-faucets/tslint.json b/packages/testnet-faucets/tslint.json index ffaefe83a..ffaefe83a 100644 --- a/packages/kovan-faucets/tslint.json +++ b/packages/testnet-faucets/tslint.json |