aboutsummaryrefslogtreecommitdiffstats
path: root/packages/kovan-faucets/src/ts
diff options
context:
space:
mode:
authorBrandon Millman <brandon.millman@gmail.com>2017-11-21 05:12:15 +0800
committerBrandon Millman <brandon.millman@gmail.com>2017-12-13 07:45:22 +0800
commitbbb768c5cfa8bf713670608f859debae42b42898 (patch)
tree40ae6f11995fa44a1ee58092a8308faac63abec4 /packages/kovan-faucets/src/ts
parent5678196706a63d27fc5b00b5224c8213510e76f0 (diff)
downloaddexon-sol-tools-bbb768c5cfa8bf713670608f859debae42b42898.tar
dexon-sol-tools-bbb768c5cfa8bf713670608f859debae42b42898.tar.gz
dexon-sol-tools-bbb768c5cfa8bf713670608f859debae42b42898.tar.bz2
dexon-sol-tools-bbb768c5cfa8bf713670608f859debae42b42898.tar.lz
dexon-sol-tools-bbb768c5cfa8bf713670608f859debae42b42898.tar.xz
dexon-sol-tools-bbb768c5cfa8bf713670608f859debae42b42898.tar.zst
dexon-sol-tools-bbb768c5cfa8bf713670608f859debae42b42898.zip
Add kovan faucet project into the mono repo
Diffstat (limited to 'packages/kovan-faucets/src/ts')
-rw-r--r--packages/kovan-faucets/src/ts/configs.ts10
-rw-r--r--packages/kovan-faucets/src/ts/error_reporter.ts40
-rw-r--r--packages/kovan-faucets/src/ts/ether_request_queue.ts26
-rw-r--r--packages/kovan-faucets/src/ts/global.d.ts15
-rw-r--r--packages/kovan-faucets/src/ts/handler.ts88
-rw-r--r--packages/kovan-faucets/src/ts/id_management.ts27
-rw-r--r--packages/kovan-faucets/src/ts/request_queue.ts53
-rw-r--r--packages/kovan-faucets/src/ts/server.ts27
-rw-r--r--packages/kovan-faucets/src/ts/utils.ts10
-rw-r--r--packages/kovan-faucets/src/ts/zrx_request_queue.ts38
10 files changed, 334 insertions, 0 deletions
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<any> {
+ 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);
+ }
+ }
+}