This repository has been archived by the owner on Jul 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #375 from 0xProject/feature/testnet-faucets/queue-…
…by-network Organize async task queues by network
- Loading branch information
Showing
7 changed files
with
150 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { intervalUtils } from '@0xproject/utils'; | ||
import * as _ from 'lodash'; | ||
|
||
import { errorReporter } from './error_reporter'; | ||
import { utils } from './utils'; | ||
|
||
const MAX_QUEUE_SIZE = 500; | ||
const DEFAULT_QUEUE_INTERVAL_MS = 1000; | ||
|
||
export class DispatchQueue { | ||
private _queueIntervalMs: number; | ||
private _queue: Array<() => Promise<void>>; | ||
private _queueIntervalIdIfExists?: NodeJS.Timer; | ||
constructor() { | ||
this._queueIntervalMs = DEFAULT_QUEUE_INTERVAL_MS; | ||
this._queue = []; | ||
this._start(); | ||
} | ||
public add(taskAsync: () => Promise<void>): boolean { | ||
if (this.isFull()) { | ||
return false; | ||
} | ||
this._queue.push(taskAsync); | ||
return true; | ||
} | ||
public size(): number { | ||
return this._queue.length; | ||
} | ||
public isFull(): boolean { | ||
return this.size() >= MAX_QUEUE_SIZE; | ||
} | ||
public stop() { | ||
if (!_.isUndefined(this._queueIntervalIdIfExists)) { | ||
intervalUtils.clearAsyncExcludingInterval(this._queueIntervalIdIfExists); | ||
} | ||
} | ||
private _start() { | ||
this._queueIntervalIdIfExists = intervalUtils.setAsyncExcludingInterval( | ||
async () => { | ||
const taskAsync = this._queue.shift(); | ||
if (_.isUndefined(taskAsync)) { | ||
return Promise.resolve(); | ||
} | ||
await taskAsync(); | ||
}, | ||
this._queueIntervalMs, | ||
(err: Error) => { | ||
utils.consoleLog(`Unexpected err: ${err} - ${JSON.stringify(err)}`); | ||
// tslint:disable-next-line:no-floating-promises | ||
errorReporter.reportAsync(err); | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { ZeroEx } from '0x.js'; | ||
import { BigNumber, promisify } from '@0xproject/utils'; | ||
import * as _ from 'lodash'; | ||
import * as Web3 from 'web3'; | ||
|
||
import { configs } from './configs'; | ||
import { errorReporter } from './error_reporter'; | ||
import { utils } from './utils'; | ||
|
||
const DISPENSE_AMOUNT_ETHER = 0.1; | ||
const DISPENSE_AMOUNT_TOKEN = 0.1; | ||
|
||
export const dispenseAssetTasks = { | ||
dispenseEtherTask(recipientAddress: string, web3: Web3) { | ||
return async () => { | ||
utils.consoleLog(`Processing ETH ${recipientAddress}`); | ||
const sendTransactionAsync = promisify(web3.eth.sendTransaction); | ||
const txHash = await sendTransactionAsync({ | ||
from: configs.DISPENSER_ADDRESS, | ||
to: recipientAddress, | ||
value: web3.toWei(DISPENSE_AMOUNT_ETHER, 'ether'), | ||
}); | ||
utils.consoleLog(`Sent ${DISPENSE_AMOUNT_ETHER} ETH to ${recipientAddress} tx: ${txHash}`); | ||
}; | ||
}, | ||
dispenseTokenTask(recipientAddress: string, tokenSymbol: string, zeroEx: ZeroEx) { | ||
return async () => { | ||
utils.consoleLog(`Processing ${tokenSymbol} ${recipientAddress}`); | ||
const amountToDispense = new BigNumber(DISPENSE_AMOUNT_TOKEN); | ||
const token = await zeroEx.tokenRegistry.getTokenBySymbolIfExistsAsync(tokenSymbol); | ||
if (_.isUndefined(token)) { | ||
throw new Error(`Unsupported asset type: ${tokenSymbol}`); | ||
} | ||
const baseUnitAmount = ZeroEx.toBaseUnitAmount(amountToDispense, token.decimals); | ||
const txHash = await zeroEx.token.transferAsync( | ||
token.address, | ||
configs.DISPENSER_ADDRESS, | ||
recipientAddress, | ||
baseUnitAmount, | ||
); | ||
utils.consoleLog(`Sent ${amountToDispense} ZRX to ${recipientAddress} tx: ${txHash}`); | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.