Implement testnet faucets for any testnet available via infura
This commit is contained in:
parent
d965fdb11d
commit
3998b47d84
@ -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
|
||||
|
@ -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
|
||||
```
|
||||
|
||||
|
@ -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,
|
||||
};
|
||||
|
@ -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';
|
||||
|
||||
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);
|
||||
interface RequestQueueByNetworkId {
|
||||
[networkId: string]: RequestQueue;
|
||||
}
|
||||
|
||||
this._etherRequestQueue = new EtherRequestQueue(this._web3);
|
||||
this._zrxRequestQueue = new ZRXRequestQueue(this._web3);
|
||||
const DEFAULT_NETWORK_ID = 42; // kovan
|
||||
|
||||
export class Handler {
|
||||
private _etherRequestQueueByNetworkId: RequestQueueByNetworkId = {};
|
||||
private _zrxRequestQueueByNetworkId: RequestQueueByNetworkId = {};
|
||||
constructor() {
|
||||
_.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);
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
13
packages/testnet-faucets/src/ts/rpc_urls.ts
Normal file
13
packages/testnet-faucets/src/ts/rpc_urls.ts
Normal file
@ -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;
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user