diff options
author | Fabio Berger <me@fabioberger.com> | 2018-07-04 05:11:49 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2018-07-04 05:11:49 +0800 |
commit | 547cf4bef3f2c18a26fb9fada96bd92e7988687f (patch) | |
tree | d62ddd79d96faf3c10da83b15e78e0dcee8e779e /packages/testnet-faucets | |
parent | d528ce757c7d07e7d2e6f2f65ad48efc85ec4e50 (diff) | |
download | dexon-sol-tools-547cf4bef3f2c18a26fb9fada96bd92e7988687f.tar dexon-sol-tools-547cf4bef3f2c18a26fb9fada96bd92e7988687f.tar.gz dexon-sol-tools-547cf4bef3f2c18a26fb9fada96bd92e7988687f.tar.bz2 dexon-sol-tools-547cf4bef3f2c18a26fb9fada96bd92e7988687f.tar.lz dexon-sol-tools-547cf4bef3f2c18a26fb9fada96bd92e7988687f.tar.xz dexon-sol-tools-547cf4bef3f2c18a26fb9fada96bd92e7988687f.tar.zst dexon-sol-tools-547cf4bef3f2c18a26fb9fada96bd92e7988687f.zip |
Refactor testnet-faucet to use web3Wrapper instead of web3 directly
Diffstat (limited to 'packages/testnet-faucets')
-rw-r--r-- | packages/testnet-faucets/package.json | 2 | ||||
-rw-r--r-- | packages/testnet-faucets/src/ts/dispense_asset_tasks.ts | 13 | ||||
-rw-r--r-- | packages/testnet-faucets/src/ts/handler.ts | 17 |
3 files changed, 13 insertions, 19 deletions
diff --git a/packages/testnet-faucets/package.json b/packages/testnet-faucets/package.json index 0ec62f097..5acd4ae35 100644 --- a/packages/testnet-faucets/package.json +++ b/packages/testnet-faucets/package.json @@ -20,6 +20,7 @@ "dependencies": { "0x.js": "^0.38.0", "@0xproject/subproviders": "^0.10.4", + "@0xproject/web3-wrapper": "^0.7.1", "@0xproject/typescript-typings": "^0.4.1", "@0xproject/utils": "^0.7.1", "body-parser": "^1.17.1", @@ -28,7 +29,6 @@ "express": "^4.15.2", "lodash": "^4.17.4", "rollbar": "^0.6.5", - "web3": "^0.20.0", "web3-provider-engine": "14.0.6" }, "devDependencies": { diff --git a/packages/testnet-faucets/src/ts/dispense_asset_tasks.ts b/packages/testnet-faucets/src/ts/dispense_asset_tasks.ts index 41b6c90cd..6e41ddae3 100644 --- a/packages/testnet-faucets/src/ts/dispense_asset_tasks.ts +++ b/packages/testnet-faucets/src/ts/dispense_asset_tasks.ts @@ -1,7 +1,7 @@ import { ZeroEx } from '0x.js'; import { BigNumber, logUtils, promisify } from '@0xproject/utils'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as _ from 'lodash'; -import * as Web3 from 'web3'; import { configs } from './configs'; @@ -13,22 +13,21 @@ const DISPENSE_MAX_AMOUNT_ETHER = 2; type AsyncTask = () => Promise<void>; export const dispenseAssetTasks = { - dispenseEtherTask(recipientAddress: string, web3: Web3): AsyncTask { + dispenseEtherTask(recipientAddress: string, web3Wrapper: Web3Wrapper): AsyncTask { return async () => { logUtils.log(`Processing ETH ${recipientAddress}`); - const userBalance = await promisify<BigNumber>(web3.eth.getBalance)(recipientAddress); - const maxAmountInWei = new BigNumber(web3.toWei(DISPENSE_MAX_AMOUNT_ETHER, 'ether')); + const userBalance = await web3Wrapper.getBalanceInWeiAsync(recipientAddress); + const maxAmountInWei = Web3Wrapper.toWei(new BigNumber(DISPENSE_MAX_AMOUNT_ETHER)); if (userBalance.greaterThanOrEqualTo(maxAmountInWei)) { logUtils.log( `User exceeded ETH balance maximum (${maxAmountInWei}) ${recipientAddress} ${userBalance} `, ); return; } - const sendTransactionAsync = promisify(web3.eth.sendTransaction); - const txHash = await sendTransactionAsync({ + const txHash = await web3Wrapper.sendTransactionAsync({ from: configs.DISPENSER_ADDRESS, to: recipientAddress, - value: web3.toWei(DISPENSE_AMOUNT_ETHER, 'ether'), + value: Web3Wrapper.toWei(new BigNumber(DISPENSE_AMOUNT_ETHER)), }); logUtils.log(`Sent ${DISPENSE_AMOUNT_ETHER} ETH to ${recipientAddress} tx: ${txHash}`); }; diff --git a/packages/testnet-faucets/src/ts/handler.ts b/packages/testnet-faucets/src/ts/handler.ts index 3a60d396c..6d26691d6 100644 --- a/packages/testnet-faucets/src/ts/handler.ts +++ b/packages/testnet-faucets/src/ts/handler.ts @@ -1,15 +1,10 @@ import { Order, ZeroEx } from '0x.js'; import { BigNumber, logUtils } from '@0xproject/utils'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { Provider } from 'ethereum-types'; import * as express from 'express'; import * as _ from 'lodash'; -import * as Web3 from 'web3'; -// 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 { NonceTrackerSubprovider, PrivateKeyWalletSubprovider } from '@0xproject/subproviders'; import ProviderEngine = require('web3-provider-engine'); import RpcSubprovider = require('web3-provider-engine/subproviders/rpc'); @@ -22,7 +17,7 @@ import { rpcUrls } from './rpc_urls'; interface NetworkConfig { dispatchQueue: DispatchQueue; - web3: Web3; + web3Wrapper: Web3Wrapper; zeroEx: ZeroEx; } @@ -58,15 +53,15 @@ export class Handler { constructor() { _.forIn(rpcUrls, (rpcUrl: string, networkId: string) => { const providerObj = Handler._createProviderEngine(rpcUrl); - const web3 = new Web3(providerObj); + const web3Wrapper = new Web3Wrapper(providerObj); const zeroExConfig = { networkId: +networkId, }; - const zeroEx = new ZeroEx(web3.currentProvider, zeroExConfig); + const zeroEx = new ZeroEx(providerObj, zeroExConfig); const dispatchQueue = new DispatchQueue(); this._networkConfigByNetworkId[networkId] = { dispatchQueue, - web3, + web3Wrapper, zeroEx, }; }); @@ -106,7 +101,7 @@ export class Handler { let dispenserTask; switch (requestedAssetType) { case RequestedAssetType.ETH: - dispenserTask = dispenseAssetTasks.dispenseEtherTask(recipient, networkConfig.web3); + dispenserTask = dispenseAssetTasks.dispenseEtherTask(recipient, networkConfig.web3Wrapper); break; case RequestedAssetType.WETH: case RequestedAssetType.ZRX: |