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 | |
parent | d965fdb11dfba5631c1f98ad3e309876c199999a (diff) | |
download | dexon-0x-contracts-3998b47d848d4b74d6db45d375420fe2cfcf1926.tar dexon-0x-contracts-3998b47d848d4b74d6db45d375420fe2cfcf1926.tar.gz dexon-0x-contracts-3998b47d848d4b74d6db45d375420fe2cfcf1926.tar.bz2 dexon-0x-contracts-3998b47d848d4b74d6db45d375420fe2cfcf1926.tar.lz dexon-0x-contracts-3998b47d848d4b74d6db45d375420fe2cfcf1926.tar.xz dexon-0x-contracts-3998b47d848d4b74d6db45d375420fe2cfcf1926.tar.zst dexon-0x-contracts-3998b47d848d4b74d6db45d375420fe2cfcf1926.zip |
Implement testnet faucets for any testnet available via infura
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | packages/testnet-faucets/README.md | 26 | ||||
-rw-r--r-- | packages/testnet-faucets/src/ts/configs.ts | 7 | ||||
-rw-r--r-- | packages/testnet-faucets/src/ts/handler.ts | 78 | ||||
-rw-r--r-- | packages/testnet-faucets/src/ts/id_management.ts | 2 | ||||
-rw-r--r-- | packages/testnet-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/zrx_request_queue.ts | 11 | ||||
-rw-r--r-- | yarn.lock | 2 |
9 files changed, 82 insertions, 61 deletions
@@ -40,8 +40,8 @@ This repository contains all the 0x developer tools written in TypeScript. Our h | Package | Description | | ----------------------------------------------------------- | ---------------------------------------------------------------- | | [`@0xproject/contracts`](/packages/contracts) | 0x solidity smart contracts & tests | -| [`@0xproject/kovan_faucets`](/packages/kovan-faucets) | A faucet micro-service that dispenses test ERC20 tokens or Ether | | [`@0xproject/monorepo-scripts`](/packages/monorepo-scripts) | Shared monorepo scripts | +| [`@0xproject/testnet_faucets`](/packages/testnet-faucets) | A faucet micro-service that dispenses test ERC20 tokens or Ether | | [`@0xproject/website`](/packages/website) | 0x website & Portal DApp | ## Usage diff --git a/packages/testnet-faucets/README.md b/packages/testnet-faucets/README.md index 504eacedc..e1d34fd58 100644 --- a/packages/testnet-faucets/README.md +++ b/packages/testnet-faucets/README.md @@ -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`. @@ -50,11 +55,23 @@ yarn dev `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 + +Both endpoints 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 @@ -68,6 +85,7 @@ docker run -d \ -e DISPENSER_PRIVATE_KEY=$DISPENSER_PRIVATE_KEY \ -e FAUCET_ROLLBAR_ACCESS_KEY=$FAUCET_ROLLBAR_ACCESS_KEY \ -e FAUCET_ENVIRONMENT=production \ +-e INFURA_API_KEY=$INFURA_API_KEY \ testnet-faucets ``` 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); @@ -2,7 +2,7 @@ # yarn lockfile v1 -"@0xproject/utils@^0.1.0", "@0xproject/utils@^0.1.3": +"@0xproject/utils@^0.1.3": version "0.1.3" resolved "https://registry.yarnpkg.com/@0xproject/utils/-/utils-0.1.3.tgz#58a9c7e19ab7710e0af17a0c2f1c7fc1b3140e85" dependencies: |