From 918f3cde948592e9cb83342c051b71378821f3c5 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 9 Mar 2018 15:08:02 +0100 Subject: Remove duplicitous methods from website's webWrapper --- packages/website/package.json | 1 + packages/website/ts/web3_wrapper.ts | 44 +++++++------------------------------ 2 files changed, 9 insertions(+), 36 deletions(-) (limited to 'packages/website') diff --git a/packages/website/package.json b/packages/website/package.json index db3035642..a7fc1fe26 100644 --- a/packages/website/package.json +++ b/packages/website/package.json @@ -22,6 +22,7 @@ "@0xproject/react-docs": "^0.0.1", "@0xproject/react-shared": "^0.0.1", "@0xproject/subproviders": "^0.7.0", + "@0xproject/web3-wrapper": "^0.2.1", "@0xproject/utils": "^0.4.1", "accounting": "^0.4.1", "basscss": "^8.0.3", diff --git a/packages/website/ts/web3_wrapper.ts b/packages/website/ts/web3_wrapper.ts index 9d8d771af..fca98ac8f 100644 --- a/packages/website/ts/web3_wrapper.ts +++ b/packages/website/ts/web3_wrapper.ts @@ -1,10 +1,11 @@ import { BigNumber, intervalUtils, promisify } from '@0xproject/utils'; +import { Web3Wrapper as Web3WrapperBase } from '@0xproject/web3-wrapper'; import * as _ from 'lodash'; import { Dispatcher } from 'ts/redux/dispatcher'; import { utils } from 'ts/utils/utils'; import * as Web3 from 'web3'; -export class Web3Wrapper { +export class Web3Wrapper extends Web3WrapperBase { private _dispatcher: Dispatcher; private _web3: Web3; private _prevNetworkId: number; @@ -18,6 +19,7 @@ export class Web3Wrapper { networkIdIfExists: number, shouldPollUserAddress: boolean, ) { + super(provider); this._dispatcher = dispatcher; this._prevNetworkId = networkIdIfExists; this._shouldPollUserAddress = shouldPollUserAddress; @@ -25,56 +27,30 @@ export class Web3Wrapper { this._web3 = new Web3(); this._web3.setProvider(provider); } - public isAddress(address: string) { - return this._web3.isAddress(address); - } - public async getAccountsAsync(): Promise { - const addresses = await promisify(this._web3.eth.getAccounts)(); - return addresses; - } public async getFirstAccountIfExistsAsync() { - const addresses = await this.getAccountsAsync(); + const addresses = await this.getAvailableAddressesAsync(); if (_.isEmpty(addresses)) { return ''; } return addresses[0]; } - public async getNodeVersionAsync(): Promise { - const nodeVersion = await promisify(this._web3.version.getNode)(); - return nodeVersion; - } public getProviderObj() { return this._web3.currentProvider; } - public async getNetworkIdIfExists() { + public async getNetworkIdIfExistsAsync(): Promise { try { - const networkId = await this._getNetworkAsync(); + const networkId = await this.getNetworkIdAsync(); return Number(networkId); } catch (err) { return undefined; } } public async getBalanceInEthAsync(owner: string): Promise { - const balanceInWei: BigNumber = await promisify(this._web3.eth.getBalance)(owner); + const balanceInWei = await this.getBalanceInWeiAsync(owner); const balanceEthOldBigNumber = this._web3.fromWei(balanceInWei, 'ether'); const balanceEth = new BigNumber(balanceEthOldBigNumber); return balanceEth; } - public async doesContractExistAtAddressAsync(address: string): Promise { - const code = await promisify(this._web3.eth.getCode)(address); - // Regex matches 0x0, 0x00, 0x in order to accomodate poorly implemented clients - const zeroHexAddressRegex = /^0[xX][0]*$/; - const didFindCode = _.isNull(code.match(zeroHexAddressRegex)); - return didFindCode; - } - public async signTransactionAsync(address: string, message: string): Promise { - const signData = await promisify(this._web3.eth.sign)(address, message); - return signData; - } - public async getBlockTimestampAsync(blockHash: string): Promise { - const { timestamp } = await promisify(this._web3.eth.getBlock)(blockHash); - return timestamp; - } public destroy() { this._stopEmittingNetworkConnectionAndUserBalanceStateAsync(); // HACK: stop() is only available on providerEngine instances @@ -98,7 +74,7 @@ export class Web3Wrapper { this._watchNetworkAndBalanceIntervalId = intervalUtils.setAsyncExcludingInterval( async () => { // Check for network state changes - const currentNetworkId = await this.getNetworkIdIfExists(); + const currentNetworkId = await this.getNetworkIdIfExistsAsync(); if (currentNetworkId !== this._prevNetworkId) { this._prevNetworkId = currentNetworkId; this._dispatcher.updateNetworkId(currentNetworkId); @@ -138,10 +114,6 @@ export class Web3Wrapper { }, ); } - private async _getNetworkAsync() { - const networkId = await promisify(this._web3.version.getNetwork)(); - return networkId; - } private async _updateUserEtherBalanceAsync(userAddress: string) { const balance = await this.getBalanceInEthAsync(userAddress); if (!balance.eq(this._prevUserEtherBalanceInEth)) { -- cgit