Merge pull request #67 from 0xProject/web3-provider
Make ZeroEx constructor accept Web3Provider instead of Web3 instance
This commit is contained in:
@@ -82,7 +82,7 @@
|
|||||||
"typedoc": "^0.7.1",
|
"typedoc": "^0.7.1",
|
||||||
"typescript": "^2.3.3",
|
"typescript": "^2.3.3",
|
||||||
"web3-provider-engine": "^13.0.1",
|
"web3-provider-engine": "^13.0.1",
|
||||||
"web3-typescript-typings": "0.0.8",
|
"web3-typescript-typings": "^0.0.9",
|
||||||
"webpack": "^2.6.0"
|
"webpack": "^2.6.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
14
src/0x.ts
14
src/0x.ts
@@ -14,7 +14,7 @@ import {ExchangeWrapper} from './contract_wrappers/exchange_wrapper';
|
|||||||
import {TokenRegistryWrapper} from './contract_wrappers/token_registry_wrapper';
|
import {TokenRegistryWrapper} from './contract_wrappers/token_registry_wrapper';
|
||||||
import {ecSignatureSchema} from './schemas/ec_signature_schema';
|
import {ecSignatureSchema} from './schemas/ec_signature_schema';
|
||||||
import {TokenWrapper} from './contract_wrappers/token_wrapper';
|
import {TokenWrapper} from './contract_wrappers/token_wrapper';
|
||||||
import {ECSignature, ZeroExError, Order, SignedOrder} from './types';
|
import {ECSignature, ZeroExError, Order, SignedOrder, Web3Provider} from './types';
|
||||||
import * as ExchangeArtifacts from './artifacts/Exchange.json';
|
import * as ExchangeArtifacts from './artifacts/Exchange.json';
|
||||||
import {orderSchema} from './schemas/order_schemas';
|
import {orderSchema} from './schemas/order_schemas';
|
||||||
|
|
||||||
@@ -122,12 +122,12 @@ export class ZeroEx {
|
|||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Instantiates a new ZeroEx instance that provides the public interface to the 0x.js library.
|
* Instantiates a new ZeroEx instance that provides the public interface to the 0x.js library.
|
||||||
* @param web3 The Web3.js instance you would like the 0x.js library to use for interacting with
|
* @param provider The Web3.js Provider instance you would like the 0x.js library to use for interacting with
|
||||||
* the Ethereum network.
|
* the Ethereum network.
|
||||||
* @return An instance of the 0x.js ZeroEx class.
|
* @return An instance of the 0x.js ZeroEx class.
|
||||||
*/
|
*/
|
||||||
constructor(web3: Web3) {
|
constructor(provider: Web3Provider) {
|
||||||
this._web3Wrapper = new Web3Wrapper(web3);
|
this._web3Wrapper = new Web3Wrapper(provider);
|
||||||
this.token = new TokenWrapper(this._web3Wrapper);
|
this.token = new TokenWrapper(this._web3Wrapper);
|
||||||
this.exchange = new ExchangeWrapper(this._web3Wrapper, this.token);
|
this.exchange = new ExchangeWrapper(this._web3Wrapper, this.token);
|
||||||
this.tokenRegistry = new TokenRegistryWrapper(this._web3Wrapper);
|
this.tokenRegistry = new TokenRegistryWrapper(this._web3Wrapper);
|
||||||
@@ -135,9 +135,9 @@ export class ZeroEx {
|
|||||||
/**
|
/**
|
||||||
* Sets a new provider for the web3 instance used by 0x.js. Updating the provider will stop all
|
* Sets a new provider for the web3 instance used by 0x.js. Updating the provider will stop all
|
||||||
* subscriptions so you will need to re-subscribe to all events relevant to your app after this call.
|
* subscriptions so you will need to re-subscribe to all events relevant to your app after this call.
|
||||||
* @param provider The Web3.Provider you would like the 0x.js library to use from now on.
|
* @param provider The Web3Provider you would like the 0x.js library to use from now on.
|
||||||
*/
|
*/
|
||||||
public async setProviderAsync(provider: Web3.Provider) {
|
public async setProviderAsync(provider: Web3Provider) {
|
||||||
this._web3Wrapper.setProvider(provider);
|
this._web3Wrapper.setProvider(provider);
|
||||||
await this.exchange.invalidateContractInstanceAsync();
|
await this.exchange.invalidateContractInstanceAsync();
|
||||||
this.tokenRegistry.invalidateContractInstance();
|
this.tokenRegistry.invalidateContractInstance();
|
||||||
|
|||||||
@@ -19,4 +19,5 @@ export {
|
|||||||
OrderCancellationRequest,
|
OrderCancellationRequest,
|
||||||
OrderFillRequest,
|
OrderFillRequest,
|
||||||
ContractEventEmitter,
|
ContractEventEmitter,
|
||||||
|
Web3Provider,
|
||||||
} from './types';
|
} from './types';
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
|
import * as Web3 from 'web3';
|
||||||
|
|
||||||
// Utility function to create a K:V from a list of strings
|
// Utility function to create a K:V from a list of strings
|
||||||
// Adapted from: https://basarat.gitbooks.io/typescript/content/docs/types/literal-types.html
|
// Adapted from: https://basarat.gitbooks.io/typescript/content/docs/types/literal-types.html
|
||||||
@@ -280,3 +281,9 @@ export interface ContractEventEmitter {
|
|||||||
watch: (eventCallback: EventCallback) => void;
|
watch: (eventCallback: EventCallback) => void;
|
||||||
stopWatchingAsync: () => Promise<void>;
|
stopWatchingAsync: () => Promise<void>;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* We re-export the `Web3.Provider` type specified in the Web3 Typescript typings
|
||||||
|
* since it is the type of the `provider` argument to the `ZeroEx` constructor.
|
||||||
|
* It is however a `Web3` library type, not a native `0x.js` type.
|
||||||
|
*/
|
||||||
|
export type Web3Provider = Web3.Provider;
|
||||||
|
|||||||
@@ -2,14 +2,12 @@ import * as _ from 'lodash';
|
|||||||
import * as Web3 from 'web3';
|
import * as Web3 from 'web3';
|
||||||
import * as BigNumber from 'bignumber.js';
|
import * as BigNumber from 'bignumber.js';
|
||||||
import promisify = require('es6-promisify');
|
import promisify = require('es6-promisify');
|
||||||
import {ZeroExError} from './types';
|
|
||||||
import {assert} from './utils/assert';
|
|
||||||
|
|
||||||
export class Web3Wrapper {
|
export class Web3Wrapper {
|
||||||
private web3: Web3;
|
private web3: Web3;
|
||||||
constructor(web3: Web3) {
|
constructor(provider: Web3.Provider) {
|
||||||
this.web3 = new Web3();
|
this.web3 = new Web3();
|
||||||
this.web3.setProvider(web3.currentProvider);
|
this.web3.setProvider(provider);
|
||||||
}
|
}
|
||||||
public setProvider(provider: Web3.Provider) {
|
public setProvider(provider: Web3.Provider) {
|
||||||
this.web3.setProvider(provider);
|
this.web3.setProvider(provider);
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ describe('ZeroEx library', () => {
|
|||||||
describe('#setProvider', () => {
|
describe('#setProvider', () => {
|
||||||
it('overrides provider in nested web3s and invalidates contractInstances', async () => {
|
it('overrides provider in nested web3s and invalidates contractInstances', async () => {
|
||||||
const web3 = web3Factory.create();
|
const web3 = web3Factory.create();
|
||||||
const zeroEx = new ZeroEx(web3);
|
const zeroEx = new ZeroEx(web3.currentProvider);
|
||||||
// Instantiate the contract instances with the current provider
|
// Instantiate the contract instances with the current provider
|
||||||
await (zeroEx.exchange as any)._getExchangeContractAsync();
|
await (zeroEx.exchange as any)._getExchangeContractAsync();
|
||||||
await (zeroEx.tokenRegistry as any)._getTokenRegistryContractAsync();
|
await (zeroEx.tokenRegistry as any)._getTokenRegistryContractAsync();
|
||||||
@@ -51,7 +51,7 @@ describe('ZeroEx library', () => {
|
|||||||
};
|
};
|
||||||
const address = '0x5409ed021d9299bf6814279a6a1411a7e866a631';
|
const address = '0x5409ed021d9299bf6814279a6a1411a7e866a631';
|
||||||
const web3 = web3Factory.create();
|
const web3 = web3Factory.create();
|
||||||
const zeroEx = new ZeroEx(web3);
|
const zeroEx = new ZeroEx(web3.currentProvider);
|
||||||
it('should return false if the data doesn\'t pertain to the signature & address', async () => {
|
it('should return false if the data doesn\'t pertain to the signature & address', async () => {
|
||||||
expect(ZeroEx.isValidSignature('0x0', signature, address)).to.be.false();
|
expect(ZeroEx.isValidSignature('0x0', signature, address)).to.be.false();
|
||||||
return expect(
|
return expect(
|
||||||
@@ -149,7 +149,7 @@ describe('ZeroEx library', () => {
|
|||||||
});
|
});
|
||||||
it('calculates the order hash', async () => {
|
it('calculates the order hash', async () => {
|
||||||
const web3 = web3Factory.create();
|
const web3 = web3Factory.create();
|
||||||
const zeroEx = new ZeroEx(web3);
|
const zeroEx = new ZeroEx(web3.currentProvider);
|
||||||
|
|
||||||
stubs = [
|
stubs = [
|
||||||
Sinon.stub((zeroEx as any), '_getExchangeAddressAsync')
|
Sinon.stub((zeroEx as any), '_getExchangeAddressAsync')
|
||||||
@@ -164,7 +164,7 @@ describe('ZeroEx library', () => {
|
|||||||
let stubs: Sinon.SinonStub[] = [];
|
let stubs: Sinon.SinonStub[] = [];
|
||||||
let makerAddress: string;
|
let makerAddress: string;
|
||||||
const web3 = web3Factory.create();
|
const web3 = web3Factory.create();
|
||||||
const zeroEx = new ZeroEx(web3);
|
const zeroEx = new ZeroEx(web3.currentProvider);
|
||||||
before(async () => {
|
before(async () => {
|
||||||
const availableAddreses = await zeroEx.getAvailableAddressesAsync();
|
const availableAddreses = await zeroEx.getAvailableAddressesAsync();
|
||||||
makerAddress = availableAddreses[0];
|
makerAddress = availableAddreses[0];
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ const expect = chai.expect;
|
|||||||
|
|
||||||
describe('Assertion library', () => {
|
describe('Assertion library', () => {
|
||||||
const web3 = web3Factory.create();
|
const web3 = web3Factory.create();
|
||||||
const zeroEx = new ZeroEx(web3);
|
const zeroEx = new ZeroEx(web3.currentProvider);
|
||||||
describe('#isSenderAddressHexAsync', () => {
|
describe('#isSenderAddressHexAsync', () => {
|
||||||
it('throws when address is invalid', async () => {
|
it('throws when address is invalid', async () => {
|
||||||
const address = '0xdeadbeef';
|
const address = '0xdeadbeef';
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ describe('ExchangeWrapper', () => {
|
|||||||
let fillScenarios: FillScenarios;
|
let fillScenarios: FillScenarios;
|
||||||
before(async () => {
|
before(async () => {
|
||||||
web3 = web3Factory.create();
|
web3 = web3Factory.create();
|
||||||
zeroEx = new ZeroEx(web3);
|
zeroEx = new ZeroEx(web3.currentProvider);
|
||||||
userAddresses = await promisify(web3.eth.getAccounts)();
|
userAddresses = await promisify(web3.eth.getAccounts)();
|
||||||
tokens = await zeroEx.tokenRegistry.getTokensAsync();
|
tokens = await zeroEx.tokenRegistry.getTokensAsync();
|
||||||
tokenUtils = new TokenUtils(tokens);
|
tokenUtils = new TokenUtils(tokens);
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ describe('TokenRegistryWrapper', () => {
|
|||||||
let zeroEx: ZeroEx;
|
let zeroEx: ZeroEx;
|
||||||
before(async () => {
|
before(async () => {
|
||||||
const web3 = web3Factory.create();
|
const web3 = web3Factory.create();
|
||||||
zeroEx = new ZeroEx(web3);
|
zeroEx = new ZeroEx(web3.currentProvider);
|
||||||
});
|
});
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await blockchainLifecycle.startAsync();
|
await blockchainLifecycle.startAsync();
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ describe('TokenWrapper', () => {
|
|||||||
let addressWithoutFunds: string;
|
let addressWithoutFunds: string;
|
||||||
before(async () => {
|
before(async () => {
|
||||||
web3 = web3Factory.create();
|
web3 = web3Factory.create();
|
||||||
zeroEx = new ZeroEx(web3);
|
zeroEx = new ZeroEx(web3.currentProvider);
|
||||||
userAddresses = await promisify(web3.eth.getAccounts)();
|
userAddresses = await promisify(web3.eth.getAccounts)();
|
||||||
tokens = await zeroEx.tokenRegistry.getTokensAsync();
|
tokens = await zeroEx.tokenRegistry.getTokensAsync();
|
||||||
coinbase = userAddresses[0];
|
coinbase = userAddresses[0];
|
||||||
|
|||||||
@@ -4467,9 +4467,9 @@ web3-provider-engine@~8.1.0:
|
|||||||
xhr "^2.2.0"
|
xhr "^2.2.0"
|
||||||
xtend "^4.0.1"
|
xtend "^4.0.1"
|
||||||
|
|
||||||
web3-typescript-typings@0.0.8:
|
web3-typescript-typings@^0.0.9:
|
||||||
version "0.0.8"
|
version "0.0.9"
|
||||||
resolved "https://registry.yarnpkg.com/web3-typescript-typings/-/web3-typescript-typings-0.0.8.tgz#96997eeaf670fbaaf28d8814a1c27dce1a07df66"
|
resolved "https://registry.yarnpkg.com/web3-typescript-typings/-/web3-typescript-typings-0.0.9.tgz#f0c9e9bfcf0effaf16f3498b3d3883686451428b"
|
||||||
dependencies:
|
dependencies:
|
||||||
bignumber.js "^4.0.2"
|
bignumber.js "^4.0.2"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user