Refactor networkId out of web3Wrapper

This commit is contained in:
Leonid Logvinov 2017-12-08 12:51:46 +03:00
parent 5401c69163
commit b362e2c28e
No known key found for this signature in database
GPG Key ID: 0DD294BFDE8C95D4
12 changed files with 39 additions and 31 deletions

View File

@ -179,24 +179,31 @@ export class ZeroEx {
const defaults = {
gasPrice: config.gasPrice,
};
this._web3Wrapper = new Web3Wrapper(provider, config.networkId, defaults);
this._web3Wrapper = new Web3Wrapper(provider, defaults);
this.proxy = new TokenTransferProxyWrapper(
this._web3Wrapper,
config.networkId,
config.tokenTransferProxyContractAddress,
);
this.token = new TokenWrapper(
this._web3Wrapper,
config.networkId,
this._abiDecoder,
this.proxy,
);
this.exchange = new ExchangeWrapper(
this._web3Wrapper,
config.networkId,
this._abiDecoder,
this.token,
config.exchangeContractAddress,
);
this.tokenRegistry = new TokenRegistryWrapper(this._web3Wrapper, config.tokenRegistryContractAddress);
this.etherToken = new EtherTokenWrapper(this._web3Wrapper, this.token, config.etherTokenContractAddress);
this.tokenRegistry = new TokenRegistryWrapper(
this._web3Wrapper, config.networkId, config.tokenRegistryContractAddress,
);
this.etherToken = new EtherTokenWrapper(
this._web3Wrapper, config.networkId, this.token, config.etherTokenContractAddress,
);
this.orderStateWatcher = new OrderStateWatcher(
this._web3Wrapper, this._abiDecoder, this.token, this.exchange, config.orderWatcherConfig,
);

View File

@ -32,6 +32,7 @@ const CONTRACT_NAME_TO_NOT_FOUND_ERROR: {[contractName: string]: ZeroExError} =
export class ContractWrapper {
protected _web3Wrapper: Web3Wrapper;
private _networkId: number;
private _abiDecoder?: AbiDecoder;
private _blockAndLogStreamer: BlockAndLogStreamer|undefined;
private _blockAndLogStreamInterval: NodeJS.Timer;
@ -39,8 +40,9 @@ export class ContractWrapper {
private _filterCallbacks: {[filterToken: string]: EventCallback<ContractEventArgs>};
private _onLogAddedSubscriptionToken: string|undefined;
private _onLogRemovedSubscriptionToken: string|undefined;
constructor(web3Wrapper: Web3Wrapper, abiDecoder?: AbiDecoder) {
constructor(web3Wrapper: Web3Wrapper, networkId: number, abiDecoder?: AbiDecoder) {
this._web3Wrapper = web3Wrapper;
this._networkId = networkId;
this._abiDecoder = abiDecoder;
this._filters = {};
this._filterCallbacks = {};
@ -104,11 +106,10 @@ export class ContractWrapper {
): Promise<Web3.ContractInstance> {
let contractAddress: string;
if (_.isUndefined(addressIfExists)) {
const networkId = this._web3Wrapper.getNetworkId();
if (_.isUndefined(artifact.networks[networkId])) {
if (_.isUndefined(artifact.networks[this._networkId])) {
throw new Error(ZeroExError.ContractNotDeployedOnNetwork);
}
contractAddress = artifact.networks[networkId].address.toLowerCase();
contractAddress = artifact.networks[this._networkId].address.toLowerCase();
} else {
contractAddress = addressIfExists;
}
@ -123,8 +124,7 @@ export class ContractWrapper {
}
protected _getContractAddress(artifact: Artifact, addressIfExists?: string): string {
if (_.isUndefined(addressIfExists)) {
const networkId = this._web3Wrapper.getNetworkId();
const contractAddress = artifact.networks[networkId].address;
const contractAddress = artifact.networks[this._networkId].address;
if (_.isUndefined(contractAddress)) {
throw new Error(ZeroExError.ExchangeContractDoesNotExist);
}

View File

@ -18,8 +18,9 @@ export class EtherTokenWrapper extends ContractWrapper {
private _etherTokenContractIfExists?: EtherTokenContract;
private _tokenWrapper: TokenWrapper;
private _contractAddressIfExists?: string;
constructor(web3Wrapper: Web3Wrapper, tokenWrapper: TokenWrapper, contractAddressIfExists?: string) {
super(web3Wrapper);
constructor(web3Wrapper: Web3Wrapper, networkId: number, tokenWrapper: TokenWrapper,
contractAddressIfExists?: string) {
super(web3Wrapper, networkId);
this._tokenWrapper = tokenWrapper;
this._contractAddressIfExists = contractAddressIfExists;
}

View File

@ -84,9 +84,9 @@ export class ExchangeWrapper extends ContractWrapper {
];
return [orderAddresses, orderValues];
}
constructor(web3Wrapper: Web3Wrapper, abiDecoder: AbiDecoder,
constructor(web3Wrapper: Web3Wrapper, networkId: number, abiDecoder: AbiDecoder,
tokenWrapper: TokenWrapper, contractAddressIfExists?: string) {
super(web3Wrapper, abiDecoder);
super(web3Wrapper, networkId, abiDecoder);
this._tokenWrapper = tokenWrapper;
this._orderValidationUtils = new OrderValidationUtils(tokenWrapper, this);
this._contractAddressIfExists = contractAddressIfExists;

View File

@ -27,8 +27,8 @@ export class TokenRegistryWrapper extends ContractWrapper {
};
return token;
}
constructor(web3Wrapper: Web3Wrapper, contractAddressIfExists?: string) {
super(web3Wrapper);
constructor(web3Wrapper: Web3Wrapper, networkId: number, contractAddressIfExists?: string) {
super(web3Wrapper, networkId);
this._contractAddressIfExists = contractAddressIfExists;
}
/**

View File

@ -13,8 +13,8 @@ import {TokenTransferProxyContract} from './generated/token_transfer_proxy';
export class TokenTransferProxyWrapper extends ContractWrapper {
private _tokenTransferProxyContractIfExists?: TokenTransferProxyContract;
private _contractAddressIfExists?: string;
constructor(web3Wrapper: Web3Wrapper, contractAddressIfExists?: string) {
super(web3Wrapper);
constructor(web3Wrapper: Web3Wrapper, networkId: number, contractAddressIfExists?: string) {
super(web3Wrapper, networkId);
this._contractAddressIfExists = contractAddressIfExists;
}
/**

View File

@ -34,9 +34,9 @@ export class TokenWrapper extends ContractWrapper {
public UNLIMITED_ALLOWANCE_IN_BASE_UNITS = constants.UNLIMITED_ALLOWANCE_IN_BASE_UNITS;
private _tokenContractsByAddress: {[address: string]: TokenContract};
private _tokenTransferProxyWrapper: TokenTransferProxyWrapper;
constructor(web3Wrapper: Web3Wrapper, abiDecoder: AbiDecoder,
constructor(web3Wrapper: Web3Wrapper, networkId: number, abiDecoder: AbiDecoder,
tokenTransferProxyWrapper: TokenTransferProxyWrapper) {
super(web3Wrapper, abiDecoder);
super(web3Wrapper, networkId, abiDecoder);
this._tokenContractsByAddress = {};
this._tokenTransferProxyWrapper = tokenTransferProxyWrapper;
}

View File

@ -60,7 +60,7 @@ describe('EventWatcher', () => {
before(async () => {
web3 = web3Factory.create();
const pollingIntervalMs = 10;
web3Wrapper = new Web3Wrapper(web3.currentProvider, constants.TESTRPC_NETWORK_ID);
web3Wrapper = new Web3Wrapper(web3.currentProvider);
eventWatcher = new EventWatcher(web3Wrapper, pollingIntervalMs);
});
afterEach(() => {

View File

@ -46,7 +46,7 @@ describe('TokenWrapper', () => {
before(async () => {
web3 = web3Factory.create();
zeroEx = new ZeroEx(web3.currentProvider, config);
web3Wrapper = new Web3Wrapper(web3.currentProvider, config.networkId);
web3Wrapper = new Web3Wrapper(web3.currentProvider);
userAddresses = await zeroEx.getAvailableAddressesAsync();
tokens = await zeroEx.tokenRegistry.getTokensAsync();
tokenUtils = new TokenUtils(tokens);

View File

@ -31,7 +31,7 @@ export class Deployer {
const jsonrpcUrl = `http://localhost:${this.jsonrpcPort}`;
const web3Provider = new Web3.providers.HttpProvider(jsonrpcUrl);
this.defaults = opts.defaults;
this.web3Wrapper = new Web3Wrapper(web3Provider, this.networkId, this.defaults);
this.web3Wrapper = new Web3Wrapper(web3Provider, this.defaults);
}
/**
* Loads contract artifact and deploys contract with given arguments.

View File

@ -1,4 +1,5 @@
import {promisify} from '@0xproject/utils';
import {Web3Wrapper} from '@0xproject/web3-wrapper';
import * as _ from 'lodash';
import * as Web3 from 'web3';
@ -6,8 +7,8 @@ export const network = {
async getNetworkIdIfExistsAsync(port: number): Promise<number> {
const url = `http://localhost:${port}`;
const web3Provider = new Web3.providers.HttpProvider(url);
const web3 = new Web3(web3Provider);
const networkId = _.parseInt(await promisify<string>(web3.version.getNetwork)());
const web3Wrapper = new Web3Wrapper(web3Provider);
const networkId = web3Wrapper.getNetworkIdAsync();
return networkId;
},
};

View File

@ -17,10 +17,9 @@ interface RawLogEntry {
export class Web3Wrapper {
private web3: Web3;
private networkId: number;
private defaults: Partial<TxData>;
private jsonRpcRequestId: number;
constructor(provider: Web3.Provider, networkId: number, defaults?: Partial<TxData>) {
constructor(provider: Web3.Provider, defaults?: Partial<TxData>) {
if (_.isUndefined((provider as any).sendAsync)) {
// Web3@1.0 provider doesn't support synchronous http requests,
// so it only has an async `send` method, instead of a `send` and `sendAsync` in web3@0.x.x`
@ -28,7 +27,6 @@ export class Web3Wrapper {
(provider as any).sendAsync = (provider as any).send;
}
this.web3 = new Web3();
this.networkId = networkId;
this.web3.setProvider(provider);
this.defaults = defaults || {};
this.jsonRpcRequestId = 0;
@ -37,7 +35,6 @@ export class Web3Wrapper {
return this.defaults;
}
public setProvider(provider: Web3.Provider, networkId: number) {
this.networkId = networkId;
this.web3.setProvider(provider);
}
public isAddress(address: string): boolean {
@ -51,6 +48,11 @@ export class Web3Wrapper {
const nodeVersion = await promisify<string>(this.web3.version.getNode)();
return nodeVersion;
}
public async getNetworkIdAsync(): Promise<number> {
const networkIdStr = await promisify<string>(this.web3.version.getNetwork)();
const networkId = _.parseInt(networkIdStr);
return networkId;
}
public async getTransactionReceiptAsync(txHash: string): Promise<TransactionReceipt> {
const transactionReceipt = await promisify<TransactionReceipt>(this.web3.eth.getTransactionReceipt)(txHash);
if (!_.isNull(transactionReceipt)) {
@ -61,9 +63,6 @@ export class Web3Wrapper {
public getCurrentProvider(): Web3.Provider {
return this.web3.currentProvider;
}
public getNetworkId(): number {
return this.networkId;
}
public toWei(ethAmount: BigNumber): BigNumber {
const balanceWei = this.web3.toWei(ethAmount, 'ether');
return balanceWei;