aboutsummaryrefslogtreecommitdiffstats
path: root/packages/testnet-faucets/src/ts
diff options
context:
space:
mode:
authorBrandon Millman <brandon.millman@gmail.com>2018-01-27 01:40:42 +0800
committerBrandon Millman <brandon.millman@gmail.com>2018-01-31 03:36:39 +0800
commit44162811bd57afb89e5d052a2d9db25007cc1463 (patch)
tree44a02e5ec79a097ce2105eef04ab6accddd5d1f4 /packages/testnet-faucets/src/ts
parent2bcb7d56394c43acd68d1823092de2ad047abc6d (diff)
downloaddexon-sol-tools-44162811bd57afb89e5d052a2d9db25007cc1463.tar
dexon-sol-tools-44162811bd57afb89e5d052a2d9db25007cc1463.tar.gz
dexon-sol-tools-44162811bd57afb89e5d052a2d9db25007cc1463.tar.bz2
dexon-sol-tools-44162811bd57afb89e5d052a2d9db25007cc1463.tar.lz
dexon-sol-tools-44162811bd57afb89e5d052a2d9db25007cc1463.tar.xz
dexon-sol-tools-44162811bd57afb89e5d052a2d9db25007cc1463.tar.zst
dexon-sol-tools-44162811bd57afb89e5d052a2d9db25007cc1463.zip
Add order signing and dispensing ability to faucet
Diffstat (limited to 'packages/testnet-faucets/src/ts')
-rw-r--r--packages/testnet-faucets/src/ts/global.d.ts8
-rw-r--r--packages/testnet-faucets/src/ts/handler.ts116
-rw-r--r--packages/testnet-faucets/src/ts/id_management.ts16
-rw-r--r--packages/testnet-faucets/src/ts/parameter_extractor.ts34
-rw-r--r--packages/testnet-faucets/src/ts/server.ts7
-rw-r--r--packages/testnet-faucets/src/ts/zrx_request_queue.ts7
6 files changed, 150 insertions, 38 deletions
diff --git a/packages/testnet-faucets/src/ts/global.d.ts b/packages/testnet-faucets/src/ts/global.d.ts
index 97cd35680..af9534377 100644
--- a/packages/testnet-faucets/src/ts/global.d.ts
+++ b/packages/testnet-faucets/src/ts/global.d.ts
@@ -24,3 +24,11 @@ declare module 'ethereumjs-tx' {
}
export = EthereumTx;
}
+
+// Define extra params on Request for parameter extraction in middleware
+declare namespace Express {
+ export interface Request {
+ recipientAddress: string;
+ networkId: string;
+ }
+}
diff --git a/packages/testnet-faucets/src/ts/handler.ts b/packages/testnet-faucets/src/ts/handler.ts
index bf5b3e81e..7883cefd8 100644
--- a/packages/testnet-faucets/src/ts/handler.ts
+++ b/packages/testnet-faucets/src/ts/handler.ts
@@ -1,11 +1,20 @@
-import { addressUtils } from '@0xproject/utils';
+import { Order, SignedOrder, ZeroEx } from '0x.js';
+import { BigNumber } from '@0xproject/utils';
import * as express from 'express';
import * as _ from 'lodash';
+
+// HACK: web3 injects XMLHttpRequest into the global scope and ProviderEngine checks XMLHttpRequest
+// to know whether it is running in a browser or node environment. We need it to be undefined since
+// we are not running in a browser env.
+// Filed issue: https://github.com/ethereum/web3.js/issues/844
+(global as any).XMLHttpRequest = undefined;
+
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 { RequestQueue } from './request_queue';
@@ -19,32 +28,38 @@ import { ZRXRequestQueue } from './zrx_request_queue';
// tslint:disable-next-line:ordered-imports
import * as Web3 from 'web3';
-interface RequestQueueByNetworkId {
- [networkId: string]: RequestQueue;
+interface ItemByNetworkId<T> {
+ [networkId: string]: T;
}
-enum QueueType {
+enum RequestedAssetType {
ETH = 'ETH',
+ WETH = 'WETH',
ZRX = 'ZRX',
}
-const DEFAULT_NETWORK_ID = 42; // kovan
+const TWO_DAYS_IN_MS = 1.728e8; // TODO: make this configurable
export class Handler {
- private _etherRequestQueueByNetworkId: RequestQueueByNetworkId = {};
- private _zrxRequestQueueByNetworkId: RequestQueueByNetworkId = {};
+ private _zeroExByNetworkId: ItemByNetworkId<ZeroEx> = {};
+ private _etherRequestQueueByNetworkId: ItemByNetworkId<RequestQueue> = {};
+ private _zrxRequestQueueByNetworkId: ItemByNetworkId<RequestQueue> = {};
constructor() {
_.forIn(rpcUrls, (rpcUrl: string, networkId: string) => {
const providerObj = this._createProviderEngine(rpcUrl);
const web3 = new Web3(providerObj);
+ const zeroExConfig = {
+ networkId: +networkId,
+ };
+ const zeroEx = new ZeroEx(web3.currentProvider, zeroExConfig);
+ this._zeroExByNetworkId[networkId] = zeroEx;
this._etherRequestQueueByNetworkId[networkId] = new EtherRequestQueue(web3);
- this._zrxRequestQueueByNetworkId[networkId] = new ZRXRequestQueue(web3, +networkId);
+ this._zrxRequestQueueByNetworkId[networkId] = new ZRXRequestQueue(web3, zeroEx);
});
}
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 {
@@ -62,37 +77,83 @@ export class Handler {
res.status(200).send(payload);
}
public dispenseEther(req: express.Request, res: express.Response) {
- this._dispense(req, res, this._etherRequestQueueByNetworkId, QueueType.ETH);
+ this._dispenseAsset(req, res, this._etherRequestQueueByNetworkId, RequestedAssetType.ETH);
}
public dispenseZRX(req: express.Request, res: express.Response) {
- this._dispense(req, res, this._zrxRequestQueueByNetworkId, QueueType.ZRX);
+ this._dispenseAsset(req, res, this._zrxRequestQueueByNetworkId, RequestedAssetType.ZRX);
+ }
+ public async dispenseWETHOrder(req: express.Request, res: express.Response) {
+ await this._dispenseOrder(req, res, RequestedAssetType.WETH);
}
- private _dispense(
+ public async dispenseZRXOrder(req: express.Request, res: express.Response, next: express.NextFunction) {
+ await this._dispenseOrder(req, res, RequestedAssetType.ZRX);
+ }
+ // tslint:disable-next-line:prefer-function-over-method
+ private _dispenseAsset(
req: express.Request,
res: express.Response,
- requestQueueByNetworkId: RequestQueueByNetworkId,
- queueType: QueueType,
+ requestQueueByNetworkId: ItemByNetworkId<RequestQueue>,
+ requestedAssetType: RequestedAssetType,
) {
- 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);
+ const requestQueue = _.get(requestQueueByNetworkId, req.networkId);
if (_.isUndefined(requestQueue)) {
- res.status(400).send('INVALID_NETWORK_ID');
+ res.status(400).send('UNSUPPORTED_NETWORK_ID');
return;
}
- const lowerCaseRecipientAddress = recipientAddress.toLowerCase();
- const didAddToQueue = requestQueue.add(lowerCaseRecipientAddress);
+ const didAddToQueue = requestQueue.add(req.recipientAddress);
if (!didAddToQueue) {
res.status(503).send('QUEUE_IS_FULL');
return;
}
- utils.consoleLog(`Added ${lowerCaseRecipientAddress} to queue: ${queueType} networkId: ${networkId}`);
+ utils.consoleLog(`Added ${req.recipientAddress} to queue: ${requestedAssetType} networkId: ${req.networkId}`);
res.status(200).end();
}
+ private async _dispenseOrder(req: express.Request, res: express.Response, requestedAssetType: RequestedAssetType) {
+ const zeroEx = _.get(this._zeroExByNetworkId, req.networkId);
+ if (_.isUndefined(zeroEx)) {
+ res.status(400).send('UNSUPPORTED_NETWORK_ID');
+ return;
+ }
+ res.setHeader('Content-Type', 'application/json');
+ const makerTokenAddress = await zeroEx.tokenRegistry.getTokenAddressBySymbolIfExistsAsync(requestedAssetType);
+ if (_.isUndefined(makerTokenAddress)) {
+ res.status(400).send('INVALID_TOKEN');
+ return;
+ }
+ const takerTokenSymbol =
+ requestedAssetType === RequestedAssetType.WETH ? RequestedAssetType.ZRX : RequestedAssetType.WETH;
+ const takerTokenAddress = await zeroEx.tokenRegistry.getTokenAddressBySymbolIfExistsAsync(takerTokenSymbol);
+ if (_.isUndefined(takerTokenAddress)) {
+ res.status(400).send('INVALID_TOKEN');
+ return;
+ }
+ const makerTokenAmount = new BigNumber(0.1);
+ const takerTokenAmount = new BigNumber(0.1);
+ const order: Order = {
+ maker: configs.DISPENSER_ADDRESS,
+ taker: req.recipientAddress,
+ makerFee: new BigNumber(0),
+ takerFee: new BigNumber(0),
+ makerTokenAmount,
+ takerTokenAmount,
+ makerTokenAddress,
+ takerTokenAddress,
+ salt: ZeroEx.generatePseudoRandomSalt(),
+ exchangeContractAddress: zeroEx.exchange.getContractAddress(),
+ feeRecipient: ZeroEx.NULL_ADDRESS,
+ expirationUnixTimestampSec: new BigNumber(Date.now() + TWO_DAYS_IN_MS),
+ };
+ const orderHash = ZeroEx.getOrderHashHex(order);
+ const signature = await zeroEx.signOrderHashAsync(orderHash, configs.DISPENSER_ADDRESS, false);
+ const signedOrder = {
+ ...order,
+ signature,
+ };
+ const signedOrderHash = ZeroEx.getOrderHashHex(signedOrder);
+ const payload = JSON.stringify(signedOrder);
+ utils.consoleLog(`Dispensed signed order: ${payload}`);
+ res.status(200).send(payload);
+ }
// tslint:disable-next-line:prefer-function-over-method
private _createProviderEngine(rpcUrl: string) {
const engine = new ProviderEngine();
@@ -106,9 +167,4 @@ 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 addressUtils.isAddress(lowercaseAddress);
- }
}
diff --git a/packages/testnet-faucets/src/ts/id_management.ts b/packages/testnet-faucets/src/ts/id_management.ts
index db9b610a3..b088e6cf8 100644
--- a/packages/testnet-faucets/src/ts/id_management.ts
+++ b/packages/testnet-faucets/src/ts/id_management.ts
@@ -1,8 +1,10 @@
import EthereumTx = require('ethereumjs-tx');
+import * as ethUtil from 'ethereumjs-util';
+import * as _ from 'lodash';
import { configs } from './configs';
-type Callback = (err: Error | null, accounts: any) => void;
+type Callback = (err: Error | null, result: any) => void;
export const idManagement = {
getAccounts(callback: Callback) {
@@ -18,4 +20,16 @@ export const idManagement = {
const rawTx = `0x${tx.serialize().toString('hex')}`;
callback(null, rawTx);
},
+ signMessage(message: object, callback: Callback) {
+ const data = _.get(message, 'data');
+ if (_.isUndefined(data)) {
+ callback(new Error('No data to sign'), null);
+ }
+ const privateKeyBuffer = new Buffer(configs.DISPENSER_PRIVATE_KEY as string, 'hex');
+ const dataBuff = ethUtil.toBuffer(data);
+ const msgHashBuff = ethUtil.hashPersonalMessage(dataBuff);
+ const sig = ethUtil.ecsign(msgHashBuff, privateKeyBuffer);
+ const rpcSig = ethUtil.toRpcSig(sig.v, sig.r, sig.s);
+ callback(null, rpcSig);
+ },
};
diff --git a/packages/testnet-faucets/src/ts/parameter_extractor.ts b/packages/testnet-faucets/src/ts/parameter_extractor.ts
new file mode 100644
index 000000000..f3a26ae01
--- /dev/null
+++ b/packages/testnet-faucets/src/ts/parameter_extractor.ts
@@ -0,0 +1,34 @@
+import { addressUtils } from '@0xproject/utils';
+import { NextFunction, Request, Response } from 'express';
+import * as _ from 'lodash';
+
+import { configs } from './configs';
+import { rpcUrls } from './rpc_urls';
+import { utils } from './utils';
+
+const DEFAULT_NETWORK_ID = 42; // kovan
+
+export const parameterExtractor = {
+ extract(req: Request, res: Response, next: NextFunction) {
+ const recipientAddress = req.params.recipient;
+ if (_.isUndefined(recipientAddress) || !_isValidEthereumAddress(recipientAddress)) {
+ res.status(400).send('INVALID_RECIPIENT_ADDRESS');
+ return;
+ }
+ const lowerCaseRecipientAddress = recipientAddress.toLowerCase();
+ req.recipientAddress = lowerCaseRecipientAddress;
+ const networkId = _.get(req.query, 'networkId', DEFAULT_NETWORK_ID);
+ const rpcUrl = _.get(rpcUrls, networkId);
+ if (_.isUndefined(rpcUrl)) {
+ res.status(400).send('UNSUPPORTED_NETWORK_ID');
+ return;
+ }
+ req.networkId = networkId;
+ next();
+ },
+};
+
+function _isValidEthereumAddress(address: string): boolean {
+ const lowercaseAddress = address.toLowerCase();
+ return addressUtils.isAddress(lowercaseAddress);
+}
diff --git a/packages/testnet-faucets/src/ts/server.ts b/packages/testnet-faucets/src/ts/server.ts
index 26edfff5a..38c2f1428 100644
--- a/packages/testnet-faucets/src/ts/server.ts
+++ b/packages/testnet-faucets/src/ts/server.ts
@@ -3,6 +3,7 @@ import * as express from 'express';
import { errorReporter } from './error_reporter';
import { Handler } from './handler';
+import { parameterExtractor } from './parameter_extractor';
// Setup the errorReporter to catch uncaught exceptions and unhandled rejections
errorReporter.setup();
@@ -20,8 +21,10 @@ 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));
+app.get('/ether/:recipient', parameterExtractor.extract, handler.dispenseEther.bind(handler));
+app.get('/zrx/:recipient', parameterExtractor.extract, handler.dispenseZRX.bind(handler));
+app.get('/order/weth/:recipient', parameterExtractor.extract, handler.dispenseWETHOrder.bind(handler));
+app.get('/order/zrx/:recipient', parameterExtractor.extract, handler.dispenseZRXOrder.bind(handler));
// Log to rollbar any errors unhandled by handlers
app.use(errorReporter.errorHandler());
diff --git a/packages/testnet-faucets/src/ts/zrx_request_queue.ts b/packages/testnet-faucets/src/ts/zrx_request_queue.ts
index 3d73f9dd2..1a8bfe1d8 100644
--- a/packages/testnet-faucets/src/ts/zrx_request_queue.ts
+++ b/packages/testnet-faucets/src/ts/zrx_request_queue.ts
@@ -18,13 +18,10 @@ const QUEUE_INTERVAL_MS = 5000;
export class ZRXRequestQueue extends RequestQueue {
private _zeroEx: ZeroEx;
- constructor(web3: Web3, networkId: number) {
+ constructor(web3: Web3, zeroEx: ZeroEx) {
super(web3);
this.queueIntervalMs = QUEUE_INTERVAL_MS;
- const zeroExConfig = {
- networkId,
- };
- this._zeroEx = new ZeroEx(web3.currentProvider, zeroExConfig);
+ this._zeroEx = zeroEx;
}
protected async processNextRequestFireAndForgetAsync(recipientAddress: string) {
utils.consoleLog(`Processing ZRX ${recipientAddress}`);