From bbb768c5cfa8bf713670608f859debae42b42898 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Mon, 20 Nov 2017 13:12:15 -0800 Subject: Add kovan faucet project into the mono repo --- packages/0x.js/tsconfig.json | 3 +- packages/assert/tsconfig.json | 3 +- packages/connect/tsconfig.json | 3 +- packages/json-schemas/tsconfig.json | 3 +- packages/kovan-faucets/Dockerfile | 13 +++ packages/kovan-faucets/README.md | 64 ++++++++++++++ packages/kovan-faucets/gulpfile.js | 98 ++++++++++++++++++++++ packages/kovan-faucets/package.json | 43 ++++++++++ packages/kovan-faucets/scripts/postpublish.js | 14 ++++ packages/kovan-faucets/src/ts/configs.ts | 10 +++ packages/kovan-faucets/src/ts/error_reporter.ts | 40 +++++++++ .../kovan-faucets/src/ts/ether_request_queue.ts | 26 ++++++ packages/kovan-faucets/src/ts/global.d.ts | 15 ++++ packages/kovan-faucets/src/ts/handler.ts | 88 +++++++++++++++++++ packages/kovan-faucets/src/ts/id_management.ts | 27 ++++++ packages/kovan-faucets/src/ts/request_queue.ts | 53 ++++++++++++ packages/kovan-faucets/src/ts/server.ts | 27 ++++++ packages/kovan-faucets/src/ts/utils.ts | 10 +++ packages/kovan-faucets/src/ts/zrx_request_queue.ts | 38 +++++++++ packages/kovan-faucets/tsconfig.json | 15 ++++ packages/kovan-faucets/tslint.json | 5 ++ 21 files changed, 594 insertions(+), 4 deletions(-) create mode 100644 packages/kovan-faucets/Dockerfile create mode 100644 packages/kovan-faucets/README.md create mode 100644 packages/kovan-faucets/gulpfile.js create mode 100644 packages/kovan-faucets/package.json create mode 100644 packages/kovan-faucets/scripts/postpublish.js create mode 100644 packages/kovan-faucets/src/ts/configs.ts create mode 100644 packages/kovan-faucets/src/ts/error_reporter.ts create mode 100644 packages/kovan-faucets/src/ts/ether_request_queue.ts create mode 100644 packages/kovan-faucets/src/ts/global.d.ts create mode 100644 packages/kovan-faucets/src/ts/handler.ts create mode 100644 packages/kovan-faucets/src/ts/id_management.ts create mode 100644 packages/kovan-faucets/src/ts/request_queue.ts create mode 100644 packages/kovan-faucets/src/ts/server.ts create mode 100644 packages/kovan-faucets/src/ts/utils.ts create mode 100644 packages/kovan-faucets/src/ts/zrx_request_queue.ts create mode 100644 packages/kovan-faucets/tsconfig.json create mode 100644 packages/kovan-faucets/tslint.json (limited to 'packages') diff --git a/packages/0x.js/tsconfig.json b/packages/0x.js/tsconfig.json index a5da86b88..b02dd3ac8 100644 --- a/packages/0x.js/tsconfig.json +++ b/packages/0x.js/tsconfig.json @@ -8,7 +8,8 @@ "declaration": true, "noImplicitAny": true, "experimentalDecorators": true, - "strictNullChecks": true + "strictNullChecks": true, + "types": [ "jsonschema", "lodash", "mocha", "node", "sinon", "uuid"] }, "include": [ "./src/**/*", diff --git a/packages/assert/tsconfig.json b/packages/assert/tsconfig.json index 709e20154..0de9af8f0 100644 --- a/packages/assert/tsconfig.json +++ b/packages/assert/tsconfig.json @@ -7,7 +7,8 @@ "sourceMap": true, "declaration": true, "noImplicitAny": true, - "strictNullChecks": true + "strictNullChecks": true, + "types": [ "lodash", "mocha", "valid-url" ] }, "include": [ "./src/**/*", diff --git a/packages/connect/tsconfig.json b/packages/connect/tsconfig.json index a6c8277f8..9648e5436 100644 --- a/packages/connect/tsconfig.json +++ b/packages/connect/tsconfig.json @@ -7,7 +7,8 @@ "sourceMap": true, "declaration": true, "noImplicitAny": true, - "strictNullChecks": true + "strictNullChecks": true, + "types": [ "fetch-mock", "lodash", "mocha", "query-string", "websocket" ] }, "include": [ "./src/**/*", diff --git a/packages/json-schemas/tsconfig.json b/packages/json-schemas/tsconfig.json index 40c2f0c8c..0a0513d01 100644 --- a/packages/json-schemas/tsconfig.json +++ b/packages/json-schemas/tsconfig.json @@ -7,7 +7,8 @@ "sourceMap": true, "declaration": true, "noImplicitAny": true, - "strictNullChecks": true + "strictNullChecks": true, + "types": [ "lodash.foreach", "lodash.values", "mocha"] }, "include": [ "./src/**/*", diff --git a/packages/kovan-faucets/Dockerfile b/packages/kovan-faucets/Dockerfile new file mode 100644 index 000000000..6d6ddc192 --- /dev/null +++ b/packages/kovan-faucets/Dockerfile @@ -0,0 +1,13 @@ +FROM node + +WORKDIR /src + +COPY package.json . +RUN npm i +RUN npm install forever -g + +COPY . . + +EXPOSE 3000 + +CMD ["forever", "./bin/server.js"] diff --git a/packages/kovan-faucets/README.md b/packages/kovan-faucets/README.md new file mode 100644 index 000000000..72c1368fb --- /dev/null +++ b/packages/kovan-faucets/README.md @@ -0,0 +1,64 @@ +Test Ether Faucet +---------------------- + +This faucet dispenses 0.1 test ether to one recipient per second. It has a max queue size of 1000. + + +## Install + +Install project dependencies: + +``` +npm install +``` + +## Start + +Set the following environment variables: + +``` +export FAUCET_ENVIRONMENT=development +export DISPENSER_ADDRESS=0x5409ed021d9299bf6814279a6a1411a7e866a631 +export DISPENSER_PRIVATE_KEY=f2f48ee19680706196e2e339e5da3491186e0c4c5030670656b0e0164837257d +export FAUCET_ROLLBAR_ACCESS_KEY={GET_THIS_FROM_ROLLBAR_ACCOUNT_SETTINGS} +``` + +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`. + +The real production key with kovan ETH exist in 1password + +``` +npm run dev +``` + +## Endpoints + +```GET /rain/:recipient_address``` + +Where recipient_address is a hex encoded Ethereum address prefixed with `0x`. + +```GET /queue``` + +Returns the status of the queue + +```javascript +{ + "full": false, + "size": 0 +} +``` + +## Docker configs + +``` +docker run -d \ +-p 80:3000 \ +--name kovan-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 +``` diff --git a/packages/kovan-faucets/gulpfile.js b/packages/kovan-faucets/gulpfile.js new file mode 100644 index 000000000..3e80e656e --- /dev/null +++ b/packages/kovan-faucets/gulpfile.js @@ -0,0 +1,98 @@ +const gulp = require('gulp'); +const nodemon = require('nodemon'); +const path = require('path'); +const webpack = require('webpack'); +const fs = require('fs'); + +const nodeModules = {}; +fs.readdirSync('node_modules') + .filter(function(x) { + return ['.bin'].indexOf(x) === -1; + }) + .forEach(function(mod) { + nodeModules[mod] = 'commonjs ' + mod; + }); + +const config = { + target: 'node', + entry: ['./src/ts/server.ts'], + output: { + path: path.join(__dirname, '/bin'), + filename: 'server.js', + }, + devtool: 'source-map', + resolve: { + modules: [ + path.join(__dirname, '/src/ts'), + 'node_modules', + ], + extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'], + alias: { + ts: path.join(__dirname, '/src/ts'), + contract_artifacts: path.join(__dirname, '/src/contract_artifacts'), + }, + }, + module: { + rules: [ + { + test: /\.js$/, + loader: 'source-map-loader', + }, + { + test: /\.tsx?$/, + loader: 'awesome-typescript-loader', + }, + ], + }, + plugins: [ + new webpack.BannerPlugin({ + banner: 'require("source-map-support").install();', + raw: true, + entryOnly: false, + }), + ], + externals: nodeModules, + watchOptions: { + ignored: /bin|node_modules|transpiled/ + }, +}; + +gulp.task('build', function(done) { + webpack(config).run(onBuild(done)); +}); + +gulp.task('watch', function() { + webpack(config).watch(100, function(err, stats) { + onBuild()(err, stats); + nodemon.restart(); + }); +}); + +gulp.task('run', ['watch'], function() { + nodemon({ + execMap: { + js: 'node', + }, + script: path.join(__dirname, 'bin/server'), + ignore: ['*'], + watch: ['foo/'], + ext: 'noop', + }).on('restart', function() { + console.log('Restarted!'); + }); +}); + +function onBuild(done) { + return function(err, stats) { + if(err) { + console.log('Error', err); + } + else { + console.log(stats.toString()); + } + + if(done) { + done(); + } + } +} diff --git a/packages/kovan-faucets/package.json b/packages/kovan-faucets/package.json new file mode 100644 index 000000000..db1e1675d --- /dev/null +++ b/packages/kovan-faucets/package.json @@ -0,0 +1,43 @@ +{ + "name": "@0xproject/erc20-token-faucet", + "version": "1.0.0", + "description": "A faucet micro-service that dispenses test ERC20 tokens", + "main": "server.js", + "scripts": { + "build": "node ../../node_modules/gulp/bin/gulp.js build", + "dev": "node ../../node_modules/gulp/bin/gulp.js run", + "start": "node ./bin/server.js", + "lint": "tslint src/**/*.ts", + "clean": "shx rm -rf bin" + }, + "author": "Fabio Berger", + "license": "Apache-2.0", + "dependencies": { + "0x.js": "~0.25.1", + "bignumber.js": "~4.1.0", + "body-parser": "^1.17.1", + "es6-promisify": "^5.0.0", + "ethereumjs-tx": "^1.2.5", + "express": "^4.15.2", + "lodash": "^4.17.4", + "rollbar": "^0.6.5", + "web3": "^0.20.0", + "web3-provider-engine": "^13.0.1" + }, + "devDependencies": { + "@0xproject/tslint-config": "^0.1.0", + "@types/body-parser": "^1.16.1", + "@types/es6-promise": "^0.0.33", + "@types/express": "^4.0.35", + "@types/lodash": "^4.14.57", + "awesome-typescript-loader": "^3.1.3", + "gulp": "^3.9.1", + "nodemon": "^1.11.0", + "shx": "^0.2.2", + "source-map-loader": "^0.2.0", + "tslint": "5.8.0", + "typescript": "~2.6.1", + "web3-typescript-typings": "^0.7.1", + "webpack": "^3.1.0" + } +} diff --git a/packages/kovan-faucets/scripts/postpublish.js b/packages/kovan-faucets/scripts/postpublish.js new file mode 100644 index 000000000..7fa452b08 --- /dev/null +++ b/packages/kovan-faucets/scripts/postpublish.js @@ -0,0 +1,14 @@ +const postpublish_utils = require('../../../scripts/postpublish_utils'); +const packageJSON = require('../package.json'); + +const subPackageName = packageJSON.name; + +postpublish_utils.getLatestTagAndVersionAsync(subPackageName) + .then(function(result) { + const releaseName = postpublish_utils.getReleaseName(subPackageName, result.version); + const assets = []; + return postpublish_utils.publishReleaseNotes(result.tag, releaseName, assets); + }) + .catch (function(err) { + throw err; + }); diff --git a/packages/kovan-faucets/src/ts/configs.ts b/packages/kovan-faucets/src/ts/configs.ts new file mode 100644 index 000000000..2b130446d --- /dev/null +++ b/packages/kovan-faucets/src/ts/configs.ts @@ -0,0 +1,10 @@ +export const configs = { + DISPENSER_ADDRESS: process.env.DISPENSER_ADDRESS.toLowerCase(), + DISPENSER_PRIVATE_KEY: process.env.DISPENSER_PRIVATE_KEY, + ENVIRONMENT: process.env.FAUCET_ENVIRONMENT, + 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/T5WSC8cautR4KXyYgsRs', + ZRX_TOKEN_ADDRESS: '0x6ff6c0ff1d68b964901f986d4c9fa3ac68346570', +}; diff --git a/packages/kovan-faucets/src/ts/error_reporter.ts b/packages/kovan-faucets/src/ts/error_reporter.ts new file mode 100644 index 000000000..74e99da15 --- /dev/null +++ b/packages/kovan-faucets/src/ts/error_reporter.ts @@ -0,0 +1,40 @@ +import * as fs from 'fs'; +import * as express from 'express'; +import {utils} from './utils'; +import {configs} from './configs'; +import rollbar = require('rollbar'); + +export const errorReporter = { + setup() { + rollbar.init(configs.ROLLBAR_ACCESS_KEY, { + environment: configs.ENVIRONMENT, + }); + + rollbar.handleUncaughtExceptions(configs.ROLLBAR_ACCESS_KEY); + + process.on('unhandledRejection', (err: Error) => { + utils.consoleLog(`Uncaught exception ${err}. Stack: ${err.stack}`); + this.report(err); + process.exit(1); + }); + }, + reportAsync(err: Error, req?: express.Request): Promise { + if (configs.ENVIRONMENT === 'development') { + return; // Do not log development environment errors + } + + return new Promise((resolve, reject) => { + rollbar.handleError(err, req, (rollbarErr: Error) => { + if (rollbarErr) { + utils.consoleLog(`Error reporting to rollbar, ignoring: ${rollbarErr}`); + reject(rollbarErr); + } else { + resolve(); + } + }); + }); + }, + errorHandler() { + return rollbar.errorHandler(configs.ROLLBAR_ACCESS_KEY); + }, +}; diff --git a/packages/kovan-faucets/src/ts/ether_request_queue.ts b/packages/kovan-faucets/src/ts/ether_request_queue.ts new file mode 100644 index 000000000..663f98082 --- /dev/null +++ b/packages/kovan-faucets/src/ts/ether_request_queue.ts @@ -0,0 +1,26 @@ +import * as _ from 'lodash'; +import promisify = require('es6-promisify'); +import {utils} from './utils'; +import {configs} from './configs'; +import {RequestQueue} from './request_queue'; +import {errorReporter} from './error_reporter'; + +const DISPENSE_AMOUNT_ETHER = 0.1; + +export class EtherRequestQueue extends RequestQueue { + protected async processNextRequestFireAndForgetAsync(recipientAddress: string) { + utils.consoleLog(`Processing ETH ${recipientAddress}`); + const sendTransactionAsync = promisify(this.web3.eth.sendTransaction); + try { + const txHash = await sendTransactionAsync({ + from: configs.DISPENSER_ADDRESS, + to: recipientAddress, + value: this.web3.toWei(DISPENSE_AMOUNT_ETHER, 'ether'), + }); + utils.consoleLog(`Sent ${DISPENSE_AMOUNT_ETHER} ETH to ${recipientAddress} tx: ${txHash}`); + } catch (err) { + utils.consoleLog(`Unexpected err: ${err} - ${JSON.stringify(err)}`); + await errorReporter.reportAsync(err); + } + } +} diff --git a/packages/kovan-faucets/src/ts/global.d.ts b/packages/kovan-faucets/src/ts/global.d.ts new file mode 100644 index 000000000..cdfa67378 --- /dev/null +++ b/packages/kovan-faucets/src/ts/global.d.ts @@ -0,0 +1,15 @@ +declare module 'elliptic'; +declare module 'rollbar'; +declare module 'ethereumjs-tx'; +declare module 'es6-promisify'; +declare module 'web3-provider-engine'; +declare module 'web3-provider-engine/subproviders/rpc'; +declare module 'web3-provider-engine/subproviders/nonce-tracker'; +declare module 'web3-provider-engine/subproviders/hooked-wallet'; + +declare module '*.json' { + const json: any; + /* tslint:disable */ + export default json; + /* tslint:enable */ +} diff --git a/packages/kovan-faucets/src/ts/handler.ts b/packages/kovan-faucets/src/ts/handler.ts new file mode 100644 index 000000000..460008e93 --- /dev/null +++ b/packages/kovan-faucets/src/ts/handler.ts @@ -0,0 +1,88 @@ +import * as _ from 'lodash'; +import * as express from 'express'; +import {EtherRequestQueue} from './ether_request_queue'; +import {ZRXRequestQueue} from './zrx_request_queue'; +import {configs} from './configs'; +import {utils} from './utils'; +import ProviderEngine = require('web3-provider-engine'); +import RpcSubprovider = require('web3-provider-engine/subproviders/rpc'); +import NonceSubprovider = require('web3-provider-engine/subproviders/nonce-tracker'); +import HookedWalletSubprovider = require('web3-provider-engine/subproviders/hooked-wallet'); +import {idManagement} from './id_management'; +import * as Web3 from 'web3'; +// HACK: web3 leaks XMLHttpRequest into the global scope and causes requests to hang +// because they are using the wrong XHR package. +// Issue: https://github.com/trufflesuite/truffle-contract/issues/14 +delete (global as any).XMLHttpRequest; + +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); + } + 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/src/ts/id_management.ts b/packages/kovan-faucets/src/ts/id_management.ts new file mode 100644 index 000000000..838650be0 --- /dev/null +++ b/packages/kovan-faucets/src/ts/id_management.ts @@ -0,0 +1,27 @@ +import {configs} from './configs'; +import * as EthereumTx from 'ethereumjs-tx'; + +type Callback = (err: Error, accounts: any) => void; + +export const idManagement = { + getAccounts(callback: Callback) { + /* tslint:disable */ + console.log('configs.DISPENSER_ADDRESS', configs.DISPENSER_ADDRESS); + /* tslint:enable */ + callback(null, [ + configs.DISPENSER_ADDRESS, + ]); + }, + approveTransaction(txData: object, callback: Callback) { + callback(null, true); + }, + signTransaction(txData: object, callback: Callback) { + /* tslint:disable */ + let tx = new EthereumTx(txData); + /* tslint:enable */ + const privateKeyBuffer = new Buffer(configs.DISPENSER_PRIVATE_KEY, 'hex'); + tx.sign(privateKeyBuffer); + const rawTx = `0x${tx.serialize().toString('hex')}`; + callback(null, rawTx); + }, +}; diff --git a/packages/kovan-faucets/src/ts/request_queue.ts b/packages/kovan-faucets/src/ts/request_queue.ts new file mode 100644 index 000000000..301aec8d5 --- /dev/null +++ b/packages/kovan-faucets/src/ts/request_queue.ts @@ -0,0 +1,53 @@ +import * as _ from 'lodash'; +import * as timers from 'timers'; +import * as Web3 from 'web3'; +// HACK: web3 leaks XMLHttpRequest into the global scope and causes requests to hang +// because they are using the wrong XHR package. +// Issue: https://github.com/trufflesuite/truffle-contract/issues/14 +delete (global as any).XMLHttpRequest; + +const MAX_QUEUE_SIZE = 500; +const DEFAULT_QUEUE_INTERVAL_MS = 1000; + +export class RequestQueue { + protected queueIntervalMs: number; + protected queue: string[]; + protected queueIntervalId: NodeJS.Timer; + protected web3: Web3; + constructor(web3: any) { + this.queueIntervalMs = DEFAULT_QUEUE_INTERVAL_MS; + this.queue = []; + + this.web3 = web3; + + this.start(); + } + public add(recipientAddress: string): boolean { + if (this.isFull()) { + return false; + } + this.queue.push(recipientAddress); + return true; + } + public size(): number { + return this.queue.length; + } + public isFull(): boolean { + return this.size() >= MAX_QUEUE_SIZE; + } + protected start() { + this.queueIntervalId = timers.setInterval(() => { + if (this.queue.length === 0) { + return; + } + const recipientAddress = this.queue.shift(); + this.processNextRequestFireAndForgetAsync(recipientAddress); + }, this.queueIntervalMs); + } + protected stop() { + clearInterval(this.queueIntervalId); + } + protected async processNextRequestFireAndForgetAsync(recipientAddress: string) { + throw new Error('Expected processNextRequestFireAndForgetAsync to be implemented by a superclass'); + } +} diff --git a/packages/kovan-faucets/src/ts/server.ts b/packages/kovan-faucets/src/ts/server.ts new file mode 100644 index 000000000..0ae10e556 --- /dev/null +++ b/packages/kovan-faucets/src/ts/server.ts @@ -0,0 +1,27 @@ +import * as bodyParser from 'body-parser'; +import * as express from 'express'; +import {Handler} from './handler'; +import {errorReporter} from './error_reporter'; + +// Setup the errorReporter to catch uncaught exceptions and unhandled rejections +errorReporter.setup(); + +const app = express(); +app.use(bodyParser.json()); // for parsing application/json +app.use((req, res, next) => { + res.header('Access-Control-Allow-Origin', '*'); + res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); + next(); +}); + +const handler = new Handler(); +app.get('/ping', (req: express.Request, res: express.Response) => { res.status(200).send('pong'); }); +app.get('/rain/:recipient', handler.dispenseEther.bind(handler)); // Deprecated gracefully +app.get('/ether/:recipient', handler.dispenseEther.bind(handler)); +app.get('/zrx/:recipient', handler.dispenseZRX.bind(handler)); +app.get('/queue', handler.getQueueInfo.bind(handler)); // Deprecated gracefully + +// Log to rollbar any errors unhandled by handlers +app.use(errorReporter.errorHandler()); +const port = process.env.PORT || 3000; +app.listen(port); diff --git a/packages/kovan-faucets/src/ts/utils.ts b/packages/kovan-faucets/src/ts/utils.ts new file mode 100644 index 000000000..fd9630b76 --- /dev/null +++ b/packages/kovan-faucets/src/ts/utils.ts @@ -0,0 +1,10 @@ +import * as _ from 'lodash'; +import * as express from 'express'; + +export const utils = { + consoleLog(message: string) { + /* tslint:disable */ + console.log(message); + /* tslint:enable */ + }, +}; diff --git a/packages/kovan-faucets/src/ts/zrx_request_queue.ts b/packages/kovan-faucets/src/ts/zrx_request_queue.ts new file mode 100644 index 000000000..e66a2b300 --- /dev/null +++ b/packages/kovan-faucets/src/ts/zrx_request_queue.ts @@ -0,0 +1,38 @@ +import * as _ from 'lodash'; +import {ZeroEx} from '0x.js'; +import BigNumber from 'bignumber.js'; +import promisify = require('es6-promisify'); +import {utils} from './utils'; +import {configs} from './configs'; +import {RequestQueue} from './request_queue'; +import {errorReporter} from './error_reporter'; +import * as Web3 from 'web3'; +// HACK: web3 leaks XMLHttpRequest into the global scope and causes requests to hang +// because they are using the wrong XHR package. +// Issue: https://github.com/trufflesuite/truffle-contract/issues/14 +delete (global as any).XMLHttpRequest; + +const DISPENSE_AMOUNT_ZRX = new BigNumber(0.1); +const QUEUE_INTERVAL_MS = 5000; + +export class ZRXRequestQueue extends RequestQueue { + private zeroEx: ZeroEx; + constructor(web3: Web3) { + super(web3); + this.queueIntervalMs = QUEUE_INTERVAL_MS; + this.zeroEx = new ZeroEx(web3.currentProvider); + } + protected async processNextRequestFireAndForgetAsync(recipientAddress: string) { + utils.consoleLog(`Processing ZRX ${recipientAddress}`); + const baseUnitAmount = ZeroEx.toBaseUnitAmount(DISPENSE_AMOUNT_ZRX, 18); + try { + await this.zeroEx.token.transferAsync( + configs.ZRX_TOKEN_ADDRESS, configs.DISPENSER_ADDRESS, recipientAddress, baseUnitAmount, + ); + utils.consoleLog(`Sent ${DISPENSE_AMOUNT_ZRX} ZRX to ${recipientAddress}`); + } catch (err) { + utils.consoleLog(`Unexpected err: ${err} - ${JSON.stringify(err)}`); + await errorReporter.reportAsync(err); + } + } +} diff --git a/packages/kovan-faucets/tsconfig.json b/packages/kovan-faucets/tsconfig.json new file mode 100644 index 000000000..4e995704a --- /dev/null +++ b/packages/kovan-faucets/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "outDir": "./transpiled/", + "sourceMap": true, + "noImplicitAny": true, + "module": "commonjs", + "target": "es5", + "baseUrl": "./src/", + "types": [ "body-parser", "es6-promise", "express", "lodash" ] + }, + "include": [ + "../../node_modules/web3-typescript-typings/index.d.ts", + "./src/ts/**/*" + ] +} diff --git a/packages/kovan-faucets/tslint.json b/packages/kovan-faucets/tslint.json new file mode 100644 index 000000000..a07795151 --- /dev/null +++ b/packages/kovan-faucets/tslint.json @@ -0,0 +1,5 @@ +{ + "extends": [ + "@0xproject/tslint-config" + ] +} -- cgit v1.2.3 From 1e9f23ebba5e0729b64e757d6901205278871e9f Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Tue, 12 Dec 2017 13:06:02 -0800 Subject: Fix build errors --- packages/0x.js/tsconfig.json | 3 +-- packages/assert/tsconfig.json | 3 +-- packages/connect/tsconfig.json | 3 +-- packages/json-schemas/tsconfig.json | 3 +-- packages/kovan-faucets/README.md | 2 -- packages/kovan-faucets/gulpfile.js | 16 +++++----------- packages/kovan-faucets/package.json | 21 +++++++++++---------- packages/kovan-faucets/scripts/postpublish.js | 14 -------------- packages/kovan-faucets/src/ts/error_reporter.ts | 9 +++++---- .../kovan-faucets/src/ts/ether_request_queue.ts | 7 ++++--- packages/kovan-faucets/src/ts/handler.ts | 19 +++++++++++-------- packages/kovan-faucets/src/ts/id_management.ts | 3 ++- packages/kovan-faucets/src/ts/request_queue.ts | 3 +++ packages/kovan-faucets/src/ts/server.ts | 3 ++- packages/kovan-faucets/src/ts/utils.ts | 2 +- packages/kovan-faucets/src/ts/zrx_request_queue.ts | 15 ++++++++++----- packages/kovan-faucets/tsconfig.json | 11 ++++++----- packages/website/package.json | 2 +- 18 files changed, 65 insertions(+), 74 deletions(-) delete mode 100644 packages/kovan-faucets/scripts/postpublish.js (limited to 'packages') diff --git a/packages/0x.js/tsconfig.json b/packages/0x.js/tsconfig.json index b02dd3ac8..a5da86b88 100644 --- a/packages/0x.js/tsconfig.json +++ b/packages/0x.js/tsconfig.json @@ -8,8 +8,7 @@ "declaration": true, "noImplicitAny": true, "experimentalDecorators": true, - "strictNullChecks": true, - "types": [ "jsonschema", "lodash", "mocha", "node", "sinon", "uuid"] + "strictNullChecks": true }, "include": [ "./src/**/*", diff --git a/packages/assert/tsconfig.json b/packages/assert/tsconfig.json index 0de9af8f0..709e20154 100644 --- a/packages/assert/tsconfig.json +++ b/packages/assert/tsconfig.json @@ -7,8 +7,7 @@ "sourceMap": true, "declaration": true, "noImplicitAny": true, - "strictNullChecks": true, - "types": [ "lodash", "mocha", "valid-url" ] + "strictNullChecks": true }, "include": [ "./src/**/*", diff --git a/packages/connect/tsconfig.json b/packages/connect/tsconfig.json index 9648e5436..a6c8277f8 100644 --- a/packages/connect/tsconfig.json +++ b/packages/connect/tsconfig.json @@ -7,8 +7,7 @@ "sourceMap": true, "declaration": true, "noImplicitAny": true, - "strictNullChecks": true, - "types": [ "fetch-mock", "lodash", "mocha", "query-string", "websocket" ] + "strictNullChecks": true }, "include": [ "./src/**/*", diff --git a/packages/json-schemas/tsconfig.json b/packages/json-schemas/tsconfig.json index 0a0513d01..40c2f0c8c 100644 --- a/packages/json-schemas/tsconfig.json +++ b/packages/json-schemas/tsconfig.json @@ -7,8 +7,7 @@ "sourceMap": true, "declaration": true, "noImplicitAny": true, - "strictNullChecks": true, - "types": [ "lodash.foreach", "lodash.values", "mocha"] + "strictNullChecks": true }, "include": [ "./src/**/*", diff --git a/packages/kovan-faucets/README.md b/packages/kovan-faucets/README.md index 72c1368fb..b372c8b15 100644 --- a/packages/kovan-faucets/README.md +++ b/packages/kovan-faucets/README.md @@ -25,8 +25,6 @@ export FAUCET_ROLLBAR_ACCESS_KEY={GET_THIS_FROM_ROLLBAR_ACCOUNT_SETTINGS} 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`. -The real production key with kovan ETH exist in 1password - ``` npm run dev ``` diff --git a/packages/kovan-faucets/gulpfile.js b/packages/kovan-faucets/gulpfile.js index 3e80e656e..773faf33a 100644 --- a/packages/kovan-faucets/gulpfile.js +++ b/packages/kovan-faucets/gulpfile.js @@ -3,19 +3,11 @@ const nodemon = require('nodemon'); const path = require('path'); const webpack = require('webpack'); const fs = require('fs'); - -const nodeModules = {}; -fs.readdirSync('node_modules') - .filter(function(x) { - return ['.bin'].indexOf(x) === -1; - }) - .forEach(function(mod) { - nodeModules[mod] = 'commonjs ' + mod; - }); +const nodeExternals = require('webpack-node-externals'); const config = { target: 'node', - entry: ['./src/ts/server.ts'], + entry: [path.join(__dirname, '/src/ts/server.ts')], output: { path: path.join(__dirname, '/bin'), filename: 'server.js', @@ -51,7 +43,9 @@ const config = { entryOnly: false, }), ], - externals: nodeModules, + externals: nodeExternals({ + modulesDir: path.join(__dirname, '../../node_modules') + }), watchOptions: { ignored: /bin|node_modules|transpiled/ }, diff --git a/packages/kovan-faucets/package.json b/packages/kovan-faucets/package.json index db1e1675d..b4fe84b86 100644 --- a/packages/kovan-faucets/package.json +++ b/packages/kovan-faucets/package.json @@ -1,23 +1,24 @@ { - "name": "@0xproject/erc20-token-faucet", + "private": true, + "name": "@0xproject/token-and-ether-faucet", "version": "1.0.0", - "description": "A faucet micro-service that dispenses test ERC20 tokens", + "description": "A faucet micro-service that dispenses test ERC20 tokens or Ether", "main": "server.js", "scripts": { "build": "node ../../node_modules/gulp/bin/gulp.js build", "dev": "node ../../node_modules/gulp/bin/gulp.js run", "start": "node ./bin/server.js", - "lint": "tslint src/**/*.ts", + "lint": "tslint --project src/**/*.ts", "clean": "shx rm -rf bin" }, "author": "Fabio Berger", "license": "Apache-2.0", "dependencies": { - "0x.js": "~0.25.1", + "0x.js": "~0.27.2", "bignumber.js": "~4.1.0", "body-parser": "^1.17.1", "es6-promisify": "^5.0.0", - "ethereumjs-tx": "^1.2.5", + "ethereumjs-tx": "^1.3.3", "express": "^4.15.2", "lodash": "^4.17.4", "rollbar": "^0.6.5", @@ -25,11 +26,10 @@ "web3-provider-engine": "^13.0.1" }, "devDependencies": { - "@0xproject/tslint-config": "^0.1.0", + "@0xproject/tslint-config": "^0.2.1", "@types/body-parser": "^1.16.1", - "@types/es6-promise": "^0.0.33", "@types/express": "^4.0.35", - "@types/lodash": "^4.14.57", + "@types/lodash": "^4.14.86", "awesome-typescript-loader": "^3.1.3", "gulp": "^3.9.1", "nodemon": "^1.11.0", @@ -37,7 +37,8 @@ "source-map-loader": "^0.2.0", "tslint": "5.8.0", "typescript": "~2.6.1", - "web3-typescript-typings": "^0.7.1", - "webpack": "^3.1.0" + "web3-typescript-typings": "^0.7.2", + "webpack": "^3.1.0", + "webpack-node-externals": "^1.6.0" } } diff --git a/packages/kovan-faucets/scripts/postpublish.js b/packages/kovan-faucets/scripts/postpublish.js deleted file mode 100644 index 7fa452b08..000000000 --- a/packages/kovan-faucets/scripts/postpublish.js +++ /dev/null @@ -1,14 +0,0 @@ -const postpublish_utils = require('../../../scripts/postpublish_utils'); -const packageJSON = require('../package.json'); - -const subPackageName = packageJSON.name; - -postpublish_utils.getLatestTagAndVersionAsync(subPackageName) - .then(function(result) { - const releaseName = postpublish_utils.getReleaseName(subPackageName, result.version); - const assets = []; - return postpublish_utils.publishReleaseNotes(result.tag, releaseName, assets); - }) - .catch (function(err) { - throw err; - }); diff --git a/packages/kovan-faucets/src/ts/error_reporter.ts b/packages/kovan-faucets/src/ts/error_reporter.ts index 74e99da15..db21647c0 100644 --- a/packages/kovan-faucets/src/ts/error_reporter.ts +++ b/packages/kovan-faucets/src/ts/error_reporter.ts @@ -1,9 +1,10 @@ -import * as fs from 'fs'; import * as express from 'express'; -import {utils} from './utils'; -import {configs} from './configs'; +import * as fs from 'fs'; import rollbar = require('rollbar'); +import {configs} from './configs'; +import {utils} from './utils'; + export const errorReporter = { setup() { rollbar.init(configs.ROLLBAR_ACCESS_KEY, { @@ -18,7 +19,7 @@ export const errorReporter = { process.exit(1); }); }, - reportAsync(err: Error, req?: express.Request): Promise { + async reportAsync(err: Error, req?: express.Request): Promise { if (configs.ENVIRONMENT === 'development') { return; // Do not log development environment errors } diff --git a/packages/kovan-faucets/src/ts/ether_request_queue.ts b/packages/kovan-faucets/src/ts/ether_request_queue.ts index 663f98082..b1648ca28 100644 --- a/packages/kovan-faucets/src/ts/ether_request_queue.ts +++ b/packages/kovan-faucets/src/ts/ether_request_queue.ts @@ -1,9 +1,10 @@ -import * as _ from 'lodash'; import promisify = require('es6-promisify'); -import {utils} from './utils'; +import * as _ from 'lodash'; + import {configs} from './configs'; -import {RequestQueue} from './request_queue'; import {errorReporter} from './error_reporter'; +import {RequestQueue} from './request_queue'; +import {utils} from './utils'; const DISPENSE_AMOUNT_ETHER = 0.1; diff --git a/packages/kovan-faucets/src/ts/handler.ts b/packages/kovan-faucets/src/ts/handler.ts index 460008e93..f3c6ba1f0 100644 --- a/packages/kovan-faucets/src/ts/handler.ts +++ b/packages/kovan-faucets/src/ts/handler.ts @@ -1,15 +1,17 @@ -import * as _ from 'lodash'; import * as express from 'express'; -import {EtherRequestQueue} from './ether_request_queue'; -import {ZRXRequestQueue} from './zrx_request_queue'; -import {configs} from './configs'; -import {utils} from './utils'; +import * as _ from 'lodash'; +import * as Web3 from 'web3'; import ProviderEngine = require('web3-provider-engine'); -import RpcSubprovider = require('web3-provider-engine/subproviders/rpc'); -import NonceSubprovider = require('web3-provider-engine/subproviders/nonce-tracker'); 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 * as Web3 from 'web3'; +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. // Issue: https://github.com/trufflesuite/truffle-contract/issues/14 @@ -71,6 +73,7 @@ export class Handler { }); 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()); diff --git a/packages/kovan-faucets/src/ts/id_management.ts b/packages/kovan-faucets/src/ts/id_management.ts index 838650be0..2090ce58a 100644 --- a/packages/kovan-faucets/src/ts/id_management.ts +++ b/packages/kovan-faucets/src/ts/id_management.ts @@ -1,6 +1,7 @@ -import {configs} from './configs'; import * as EthereumTx from 'ethereumjs-tx'; +import {configs} from './configs'; + type Callback = (err: Error, accounts: any) => void; export const idManagement = { diff --git a/packages/kovan-faucets/src/ts/request_queue.ts b/packages/kovan-faucets/src/ts/request_queue.ts index 301aec8d5..98e8e381a 100644 --- a/packages/kovan-faucets/src/ts/request_queue.ts +++ b/packages/kovan-faucets/src/ts/request_queue.ts @@ -41,12 +41,15 @@ export class RequestQueue { return; } const recipientAddress = this.queue.shift(); + // tslint:disable-next-line:no-floating-promises this.processNextRequestFireAndForgetAsync(recipientAddress); + }, this.queueIntervalMs); } protected stop() { clearInterval(this.queueIntervalId); } + // tslint:disable-next-line:prefer-function-over-method protected async processNextRequestFireAndForgetAsync(recipientAddress: string) { throw new Error('Expected processNextRequestFireAndForgetAsync to be implemented by a superclass'); } diff --git a/packages/kovan-faucets/src/ts/server.ts b/packages/kovan-faucets/src/ts/server.ts index 0ae10e556..d60363ceb 100644 --- a/packages/kovan-faucets/src/ts/server.ts +++ b/packages/kovan-faucets/src/ts/server.ts @@ -1,7 +1,8 @@ import * as bodyParser from 'body-parser'; import * as express from 'express'; -import {Handler} from './handler'; + import {errorReporter} from './error_reporter'; +import {Handler} from './handler'; // Setup the errorReporter to catch uncaught exceptions and unhandled rejections errorReporter.setup(); diff --git a/packages/kovan-faucets/src/ts/utils.ts b/packages/kovan-faucets/src/ts/utils.ts index fd9630b76..6a0b262e6 100644 --- a/packages/kovan-faucets/src/ts/utils.ts +++ b/packages/kovan-faucets/src/ts/utils.ts @@ -1,5 +1,5 @@ -import * as _ from 'lodash'; import * as express from 'express'; +import * as _ from 'lodash'; export const utils = { consoleLog(message: string) { diff --git a/packages/kovan-faucets/src/ts/zrx_request_queue.ts b/packages/kovan-faucets/src/ts/zrx_request_queue.ts index e66a2b300..8971f94f0 100644 --- a/packages/kovan-faucets/src/ts/zrx_request_queue.ts +++ b/packages/kovan-faucets/src/ts/zrx_request_queue.ts @@ -1,12 +1,13 @@ -import * as _ from 'lodash'; import {ZeroEx} from '0x.js'; import BigNumber from 'bignumber.js'; import promisify = require('es6-promisify'); -import {utils} from './utils'; +import * as _ from 'lodash'; +import * as Web3 from 'web3'; + import {configs} from './configs'; -import {RequestQueue} from './request_queue'; import {errorReporter} from './error_reporter'; -import * as Web3 from 'web3'; +import {RequestQueue} from './request_queue'; +import {utils} from './utils'; // HACK: web3 leaks XMLHttpRequest into the global scope and causes requests to hang // because they are using the wrong XHR package. // Issue: https://github.com/trufflesuite/truffle-contract/issues/14 @@ -14,13 +15,17 @@ delete (global as any).XMLHttpRequest; const DISPENSE_AMOUNT_ZRX = new BigNumber(0.1); const QUEUE_INTERVAL_MS = 5000; +const KOVAN_NETWORK_ID = 42; export class ZRXRequestQueue extends RequestQueue { private zeroEx: ZeroEx; constructor(web3: Web3) { super(web3); this.queueIntervalMs = QUEUE_INTERVAL_MS; - this.zeroEx = new ZeroEx(web3.currentProvider); + const config = { + networkId: KOVAN_NETWORK_ID, + }; + this.zeroEx = new ZeroEx(web3.currentProvider, config); } protected async processNextRequestFireAndForgetAsync(recipientAddress: string) { utils.consoleLog(`Processing ZRX ${recipientAddress}`); diff --git a/packages/kovan-faucets/tsconfig.json b/packages/kovan-faucets/tsconfig.json index 4e995704a..6b14a6f6b 100644 --- a/packages/kovan-faucets/tsconfig.json +++ b/packages/kovan-faucets/tsconfig.json @@ -1,12 +1,13 @@ { "compilerOptions": { - "outDir": "./transpiled/", - "sourceMap": true, - "noImplicitAny": true, "module": "commonjs", "target": "es5", - "baseUrl": "./src/", - "types": [ "body-parser", "es6-promise", "express", "lodash" ] + "lib": [ "es2015", "dom" ], + "outDir": "lib", + "sourceMap": true, + "declaration": true, + "noImplicitAny": true, + "experimentalDecorators": true, }, "include": [ "../../node_modules/web3-typescript-typings/index.d.ts", diff --git a/packages/website/package.json b/packages/website/package.json index f005668f8..d52cfc6ff 100644 --- a/packages/website/package.json +++ b/packages/website/package.json @@ -18,7 +18,7 @@ "author": "Fabio Berger", "license": "Apache-2.0", "dependencies": { - "0x.js": "0.27.1", + "0x.js": "~0.27.2", "@0xproject/subproviders": "^0.1.0", "accounting": "^0.4.1", "basscss": "^8.0.3", -- cgit v1.2.3 From 91f276d9254bba3d93fc821c4fadab2eb6fc1d65 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Tue, 12 Dec 2017 17:18:15 -0800 Subject: Cleanup kovan-faucet package --- packages/contracts/package.json | 2 +- packages/kovan-faucets/README.md | 10 +++++++--- packages/kovan-faucets/package.json | 8 ++++---- packages/kovan-faucets/src/ts/configs.ts | 3 ++- packages/kovan-faucets/src/ts/ether_request_queue.ts | 2 +- packages/kovan-faucets/src/ts/global.d.ts | 17 ++++++++++++++--- packages/kovan-faucets/src/ts/handler.ts | 5 ----- packages/kovan-faucets/src/ts/id_management.ts | 11 ++++------- packages/kovan-faucets/src/ts/request_queue.ts | 4 ---- packages/kovan-faucets/src/ts/server.ts | 2 -- packages/kovan-faucets/src/ts/zrx_request_queue.ts | 13 ++++--------- 11 files changed, 37 insertions(+), 40 deletions(-) (limited to 'packages') diff --git a/packages/contracts/package.json b/packages/contracts/package.json index f4d0b5865..93f28f990 100644 --- a/packages/contracts/package.json +++ b/packages/contracts/package.json @@ -54,7 +54,7 @@ "yargs": "^10.0.3" }, "dependencies": { - "0x.js": "^0.27.1", + "0x.js": "~0.27.2", "@0xproject/json-schemas": "^0.6.10", "@0xproject/utils": "^0.1.0", "@0xproject/web3-wrapper": "^0.1.0", diff --git a/packages/kovan-faucets/README.md b/packages/kovan-faucets/README.md index b372c8b15..8903440e3 100644 --- a/packages/kovan-faucets/README.md +++ b/packages/kovan-faucets/README.md @@ -21,8 +21,11 @@ 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 + 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`. ``` @@ -31,13 +34,14 @@ npm run dev ## Endpoints -```GET /rain/:recipient_address``` +```GET /ether/:recipient``` Where recipient_address is a hex encoded Ethereum address prefixed with `0x`. -```GET /queue``` +```GET /zrx/:recipient``` + +Where recipient_address is a hex encoded Ethereum address prefixed with `0x`. -Returns the status of the queue ```javascript { diff --git a/packages/kovan-faucets/package.json b/packages/kovan-faucets/package.json index b4fe84b86..378cd2613 100644 --- a/packages/kovan-faucets/package.json +++ b/packages/kovan-faucets/package.json @@ -1,6 +1,6 @@ { "private": true, - "name": "@0xproject/token-and-ether-faucet", + "name": "@0xproject/kovan_faucets", "version": "1.0.0", "description": "A faucet micro-service that dispenses test ERC20 tokens or Ether", "main": "server.js", @@ -8,16 +8,16 @@ "build": "node ../../node_modules/gulp/bin/gulp.js build", "dev": "node ../../node_modules/gulp/bin/gulp.js run", "start": "node ./bin/server.js", - "lint": "tslint --project src/**/*.ts", + "lint": "tslint --project . 'src/**/*.ts'", "clean": "shx rm -rf bin" }, "author": "Fabio Berger", "license": "Apache-2.0", "dependencies": { + "@0xproject/utils": "^0.1.0", "0x.js": "~0.27.2", "bignumber.js": "~4.1.0", "body-parser": "^1.17.1", - "es6-promisify": "^5.0.0", "ethereumjs-tx": "^1.3.3", "express": "^4.15.2", "lodash": "^4.17.4", @@ -34,7 +34,7 @@ "gulp": "^3.9.1", "nodemon": "^1.11.0", "shx": "^0.2.2", - "source-map-loader": "^0.2.0", + "source-map-loader": "^0.1.6", "tslint": "5.8.0", "typescript": "~2.6.1", "web3-typescript-typings": "^0.7.2", diff --git a/packages/kovan-faucets/src/ts/configs.ts b/packages/kovan-faucets/src/ts/configs.ts index 2b130446d..f432ba66f 100644 --- a/packages/kovan-faucets/src/ts/configs.ts +++ b/packages/kovan-faucets/src/ts/configs.ts @@ -5,6 +5,7 @@ export const configs = { 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/T5WSC8cautR4KXyYgsRs', + `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/ether_request_queue.ts b/packages/kovan-faucets/src/ts/ether_request_queue.ts index b1648ca28..0750e6170 100644 --- a/packages/kovan-faucets/src/ts/ether_request_queue.ts +++ b/packages/kovan-faucets/src/ts/ether_request_queue.ts @@ -1,4 +1,4 @@ -import promisify = require('es6-promisify'); +import {promisify} from '@0xproject/utils'; import * as _ from 'lodash'; import {configs} from './configs'; diff --git a/packages/kovan-faucets/src/ts/global.d.ts b/packages/kovan-faucets/src/ts/global.d.ts index cdfa67378..c698207dd 100644 --- a/packages/kovan-faucets/src/ts/global.d.ts +++ b/packages/kovan-faucets/src/ts/global.d.ts @@ -1,7 +1,4 @@ -declare module 'elliptic'; declare module 'rollbar'; -declare module 'ethereumjs-tx'; -declare module 'es6-promisify'; declare module 'web3-provider-engine'; declare module 'web3-provider-engine/subproviders/rpc'; declare module 'web3-provider-engine/subproviders/nonce-tracker'; @@ -13,3 +10,17 @@ declare module '*.json' { export default json; /* tslint:enable */ } + +// Ethereumjs-tx declarations +declare module 'ethereumjs-tx' { + class EthereumTx { + public raw: Buffer[]; + public r: Buffer; + public s: Buffer; + public v: Buffer; + public serialize(): Buffer; + public sign(buffer: Buffer): void; + constructor(txParams: any); + } + export = EthereumTx; +} diff --git a/packages/kovan-faucets/src/ts/handler.ts b/packages/kovan-faucets/src/ts/handler.ts index f3c6ba1f0..d76a29514 100644 --- a/packages/kovan-faucets/src/ts/handler.ts +++ b/packages/kovan-faucets/src/ts/handler.ts @@ -12,11 +12,6 @@ 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. -// Issue: https://github.com/trufflesuite/truffle-contract/issues/14 -delete (global as any).XMLHttpRequest; - export class Handler { private etherRequestQueue: EtherRequestQueue; private zrxRequestQueue: ZRXRequestQueue; diff --git a/packages/kovan-faucets/src/ts/id_management.ts b/packages/kovan-faucets/src/ts/id_management.ts index 2090ce58a..89d6f6fd1 100644 --- a/packages/kovan-faucets/src/ts/id_management.ts +++ b/packages/kovan-faucets/src/ts/id_management.ts @@ -1,14 +1,13 @@ -import * as EthereumTx from 'ethereumjs-tx'; +import EthereumTx = require('ethereumjs-tx'); import {configs} from './configs'; +import {utils} from './utils'; type Callback = (err: Error, accounts: any) => void; export const idManagement = { getAccounts(callback: Callback) { - /* tslint:disable */ - console.log('configs.DISPENSER_ADDRESS', configs.DISPENSER_ADDRESS); - /* tslint:enable */ + utils.consoleLog(`configs.DISPENSER_ADDRESS: ${configs.DISPENSER_ADDRESS}`); callback(null, [ configs.DISPENSER_ADDRESS, ]); @@ -17,9 +16,7 @@ export const idManagement = { callback(null, true); }, signTransaction(txData: object, callback: Callback) { - /* tslint:disable */ - let tx = new EthereumTx(txData); - /* tslint:enable */ + const tx = new EthereumTx(txData); const privateKeyBuffer = new Buffer(configs.DISPENSER_PRIVATE_KEY, 'hex'); tx.sign(privateKeyBuffer); const rawTx = `0x${tx.serialize().toString('hex')}`; diff --git a/packages/kovan-faucets/src/ts/request_queue.ts b/packages/kovan-faucets/src/ts/request_queue.ts index 98e8e381a..2a3fd4d03 100644 --- a/packages/kovan-faucets/src/ts/request_queue.ts +++ b/packages/kovan-faucets/src/ts/request_queue.ts @@ -1,10 +1,6 @@ import * as _ from 'lodash'; import * as timers from 'timers'; import * as Web3 from 'web3'; -// HACK: web3 leaks XMLHttpRequest into the global scope and causes requests to hang -// because they are using the wrong XHR package. -// Issue: https://github.com/trufflesuite/truffle-contract/issues/14 -delete (global as any).XMLHttpRequest; const MAX_QUEUE_SIZE = 500; const DEFAULT_QUEUE_INTERVAL_MS = 1000; diff --git a/packages/kovan-faucets/src/ts/server.ts b/packages/kovan-faucets/src/ts/server.ts index d60363ceb..fbb9caf1e 100644 --- a/packages/kovan-faucets/src/ts/server.ts +++ b/packages/kovan-faucets/src/ts/server.ts @@ -17,10 +17,8 @@ app.use((req, res, next) => { const handler = new Handler(); app.get('/ping', (req: express.Request, res: express.Response) => { res.status(200).send('pong'); }); -app.get('/rain/:recipient', handler.dispenseEther.bind(handler)); // Deprecated gracefully app.get('/ether/:recipient', handler.dispenseEther.bind(handler)); app.get('/zrx/:recipient', handler.dispenseZRX.bind(handler)); -app.get('/queue', handler.getQueueInfo.bind(handler)); // Deprecated gracefully // Log to rollbar any errors unhandled by handlers app.use(errorReporter.errorHandler()); diff --git a/packages/kovan-faucets/src/ts/zrx_request_queue.ts b/packages/kovan-faucets/src/ts/zrx_request_queue.ts index 8971f94f0..72500789c 100644 --- a/packages/kovan-faucets/src/ts/zrx_request_queue.ts +++ b/packages/kovan-faucets/src/ts/zrx_request_queue.ts @@ -1,6 +1,6 @@ import {ZeroEx} from '0x.js'; +import {promisify} from '@0xproject/utils'; import BigNumber from 'bignumber.js'; -import promisify = require('es6-promisify'); import * as _ from 'lodash'; import * as Web3 from 'web3'; @@ -8,24 +8,19 @@ import {configs} from './configs'; import {errorReporter} from './error_reporter'; import {RequestQueue} from './request_queue'; import {utils} from './utils'; -// HACK: web3 leaks XMLHttpRequest into the global scope and causes requests to hang -// because they are using the wrong XHR package. -// Issue: https://github.com/trufflesuite/truffle-contract/issues/14 -delete (global as any).XMLHttpRequest; const DISPENSE_AMOUNT_ZRX = new BigNumber(0.1); const QUEUE_INTERVAL_MS = 5000; -const KOVAN_NETWORK_ID = 42; export class ZRXRequestQueue extends RequestQueue { private zeroEx: ZeroEx; constructor(web3: Web3) { super(web3); this.queueIntervalMs = QUEUE_INTERVAL_MS; - const config = { - networkId: KOVAN_NETWORK_ID, + const zeroExConfig = { + networkId: configs.KOVAN_NETWORK_ID, }; - this.zeroEx = new ZeroEx(web3.currentProvider, config); + this.zeroEx = new ZeroEx(web3.currentProvider, zeroExConfig); } protected async processNextRequestFireAndForgetAsync(recipientAddress: string) { utils.consoleLog(`Processing ZRX ${recipientAddress}`); -- cgit v1.2.3 From 6ba4ed99c42b4976902046625790698b490c3e0e Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Wed, 13 Dec 2017 15:05:57 -0800 Subject: Fix hanging requests by importing web3 last --- packages/kovan-faucets/src/ts/handler.ts | 7 ++++++- packages/kovan-faucets/src/ts/request_queue.ts | 5 +++++ packages/kovan-faucets/src/ts/zrx_request_queue.ts | 7 ++++++- 3 files changed, 17 insertions(+), 2 deletions(-) (limited to 'packages') diff --git a/packages/kovan-faucets/src/ts/handler.ts b/packages/kovan-faucets/src/ts/handler.ts index d76a29514..1c6866a1c 100644 --- a/packages/kovan-faucets/src/ts/handler.ts +++ b/packages/kovan-faucets/src/ts/handler.ts @@ -1,6 +1,5 @@ import * as express from 'express'; import * as _ from 'lodash'; -import * as Web3 from 'web3'; import ProviderEngine = require('web3-provider-engine'); import HookedWalletSubprovider = require('web3-provider-engine/subproviders/hooked-wallet'); import NonceSubprovider = require('web3-provider-engine/subproviders/nonce-tracker'); @@ -12,6 +11,12 @@ 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. +// Issue: https://github.com/trufflesuite/truffle-contract/issues/14 +// tslint:disable-next-line:ordered-imports +import * as Web3 from 'web3'; + export class Handler { private etherRequestQueue: EtherRequestQueue; private zrxRequestQueue: ZRXRequestQueue; diff --git a/packages/kovan-faucets/src/ts/request_queue.ts b/packages/kovan-faucets/src/ts/request_queue.ts index 2a3fd4d03..0b5e66ae9 100644 --- a/packages/kovan-faucets/src/ts/request_queue.ts +++ b/packages/kovan-faucets/src/ts/request_queue.ts @@ -1,5 +1,10 @@ import * as _ from 'lodash'; import * as timers from 'timers'; + +// HACK: web3 leaks XMLHttpRequest into the global scope and causes requests to hang +// because they are using the wrong XHR package. +// Issue: https://github.com/trufflesuite/truffle-contract/issues/14 +// tslint:disable-next-line:ordered-imports import * as Web3 from 'web3'; const MAX_QUEUE_SIZE = 500; diff --git a/packages/kovan-faucets/src/ts/zrx_request_queue.ts b/packages/kovan-faucets/src/ts/zrx_request_queue.ts index 72500789c..6e6590e7c 100644 --- a/packages/kovan-faucets/src/ts/zrx_request_queue.ts +++ b/packages/kovan-faucets/src/ts/zrx_request_queue.ts @@ -2,13 +2,18 @@ import {ZeroEx} from '0x.js'; import {promisify} from '@0xproject/utils'; import BigNumber from 'bignumber.js'; import * as _ from 'lodash'; -import * as Web3 from 'web3'; import {configs} from './configs'; import {errorReporter} from './error_reporter'; import {RequestQueue} from './request_queue'; import {utils} from './utils'; +// HACK: web3 leaks XMLHttpRequest into the global scope and causes requests to hang +// because they are using the wrong XHR package. +// Issue: https://github.com/trufflesuite/truffle-contract/issues/14 +// tslint:disable-next-line:ordered-imports +import * as Web3 from 'web3'; + const DISPENSE_AMOUNT_ZRX = new BigNumber(0.1); const QUEUE_INTERVAL_MS = 5000; -- cgit v1.2.3