aboutsummaryrefslogtreecommitdiffstats
path: root/packages/kovan-faucets/src/ts/handler.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/handler.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/handler.ts')
-rw-r--r--packages/kovan-faucets/src/ts/handler.ts88
1 files changed, 88 insertions, 0 deletions
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);
+ }
+}