Move our contract templates to accept Provider instead of Web3Wrapper
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { schemas, SchemaValidator } from '@0xproject/json-schemas';
|
||||
import { ECSignature, Order, SignedOrder, TransactionReceiptWithDecodedLogs } from '@0xproject/types';
|
||||
import { ECSignature, Order, Provider, SignedOrder, TransactionReceiptWithDecodedLogs } from '@0xproject/types';
|
||||
import { AbiDecoder, BigNumber, intervalUtils } from '@0xproject/utils';
|
||||
import { Web3Wrapper } from '@0xproject/web3-wrapper';
|
||||
import * as ethUtil from 'ethereumjs-util';
|
||||
@@ -15,7 +15,7 @@ import { OrderStateWatcher } from './order_watcher/order_state_watcher';
|
||||
import { zeroExConfigSchema } from './schemas/zero_ex_config_schema';
|
||||
import { zeroExPrivateNetworkConfigSchema } from './schemas/zero_ex_private_network_config_schema';
|
||||
import { zeroExPublicNetworkConfigSchema } from './schemas/zero_ex_public_network_config_schema';
|
||||
import { OrderStateWatcherConfig, Web3Provider, ZeroExConfig, ZeroExError } from './types';
|
||||
import { OrderStateWatcherConfig, ZeroExConfig, ZeroExError } from './types';
|
||||
import { assert } from './utils/assert';
|
||||
import { constants } from './utils/constants';
|
||||
import { decorators } from './utils/decorators';
|
||||
@@ -115,10 +115,8 @@ export class ZeroEx {
|
||||
public static toUnitAmount(amount: BigNumber, decimals: number): BigNumber {
|
||||
assert.isValidBaseUnitAmount('amount', amount);
|
||||
assert.isNumber('decimals', decimals);
|
||||
|
||||
const aUnit = new BigNumber(10).pow(decimals);
|
||||
const unit = amount.div(aUnit);
|
||||
return unit;
|
||||
const unitAmount = Web3Wrapper.toUnitAmount(amount, decimals);
|
||||
return unitAmount;
|
||||
}
|
||||
/**
|
||||
* A baseUnit is defined as the smallest denomination of a token. An amount expressed in baseUnits
|
||||
@@ -131,13 +129,7 @@ export class ZeroEx {
|
||||
public static toBaseUnitAmount(amount: BigNumber, decimals: number): BigNumber {
|
||||
assert.isBigNumber('amount', amount);
|
||||
assert.isNumber('decimals', decimals);
|
||||
|
||||
const unit = new BigNumber(10).pow(decimals);
|
||||
const baseUnitAmount = amount.times(unit);
|
||||
const hasDecimals = baseUnitAmount.decimalPlaces() !== 0;
|
||||
if (hasDecimals) {
|
||||
throw new Error(`Invalid unit amount: ${amount.toString()} - Too many decimal places`);
|
||||
}
|
||||
const baseUnitAmount = Web3Wrapper.toBaseUnitAmount(amount, decimals);
|
||||
return baseUnitAmount;
|
||||
}
|
||||
/**
|
||||
@@ -153,12 +145,12 @@ export class ZeroEx {
|
||||
}
|
||||
/**
|
||||
* Instantiates a new ZeroEx instance that provides the public interface to the 0x.js library.
|
||||
* @param provider The Web3.js Provider instance you would like the 0x.js library to use for interacting with
|
||||
* @param provider The Provider instance you would like the 0x.js library to use for interacting with
|
||||
* the Ethereum network.
|
||||
* @param config The configuration object. Look up the type for the description.
|
||||
* @return An instance of the 0x.js ZeroEx class.
|
||||
*/
|
||||
constructor(provider: Web3Provider, config: ZeroExConfig) {
|
||||
constructor(provider: Provider, config: ZeroExConfig) {
|
||||
assert.isWeb3Provider('provider', provider);
|
||||
assert.doesConformToSchema('config', config, zeroExConfigSchema, [
|
||||
zeroExPrivateNetworkConfigSchema,
|
||||
@@ -199,7 +191,7 @@ export class ZeroEx {
|
||||
* @param provider The Web3Provider you would like the 0x.js library to use from now on.
|
||||
* @param networkId The id of the network your provider is connected to
|
||||
*/
|
||||
public setProvider(provider: Web3Provider, networkId: number): void {
|
||||
public setProvider(provider: Provider, networkId: number): void {
|
||||
this._web3Wrapper.setProvider(provider);
|
||||
(this.exchange as any)._invalidateContractInstances();
|
||||
(this.exchange as any)._setNetworkId(networkId);
|
||||
@@ -225,7 +217,7 @@ export class ZeroEx {
|
||||
* This method currently supports TestRPC, Geth and Parity above and below V1.6.6
|
||||
* @param orderHash Hex encoded orderHash to sign.
|
||||
* @param signerAddress The hex encoded Ethereum address you wish to sign it with. This address
|
||||
* must be available via the Web3.Provider supplied to 0x.js.
|
||||
* must be available via the Provider supplied to 0x.js.
|
||||
* @param shouldAddPersonalMessagePrefix Some signers add the personal message prefix `\x19Ethereum Signed Message`
|
||||
* themselves (e.g Parity Signer, Ledger, TestRPC) and others expect it to already be done by the client
|
||||
* (e.g Metamask). Depending on which signer this request is going to, decide on whether to add the prefix
|
||||
|
@@ -191,7 +191,12 @@ export class EtherTokenWrapper extends ContractWrapper {
|
||||
artifacts.EtherTokenArtifact,
|
||||
etherTokenAddress,
|
||||
);
|
||||
const contractInstance = new EtherTokenContract(this._web3Wrapper, abi, address);
|
||||
const contractInstance = new EtherTokenContract(
|
||||
abi,
|
||||
address,
|
||||
this._web3Wrapper.getProvider(),
|
||||
this._web3Wrapper.getContractDefaults(),
|
||||
);
|
||||
etherTokenContract = contractInstance;
|
||||
this._etherTokenContractsByAddress[etherTokenAddress] = etherTokenContract;
|
||||
return etherTokenContract;
|
||||
|
@@ -170,7 +170,7 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
* @param shouldThrowOnInsufficientBalanceOrAllowance Whether or not you wish for the contract call to throw
|
||||
* if upon execution the tokens cannot be transferred.
|
||||
* @param takerAddress The user Ethereum address who would like to fill this order.
|
||||
* Must be available via the supplied Web3.Provider
|
||||
* Must be available via the supplied Provider
|
||||
* passed to 0x.js.
|
||||
* @param orderTransactionOpts Optional arguments this method accepts.
|
||||
* @return Transaction hash.
|
||||
@@ -235,7 +235,7 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
* If set to false, the call will continue to fill subsequent
|
||||
* signedOrders even when some cannot be filled.
|
||||
* @param takerAddress The user Ethereum address who would like to fill these
|
||||
* orders. Must be available via the supplied Web3.Provider
|
||||
* orders. Must be available via the supplied Provider
|
||||
* passed to 0x.js.
|
||||
* @param orderTransactionOpts Optional arguments this method accepts.
|
||||
* @return Transaction hash.
|
||||
@@ -335,7 +335,7 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
* cannot be filled.
|
||||
* @param takerAddress The user Ethereum address who would like to fill
|
||||
* these orders. Must be available via the supplied
|
||||
* Web3.Provider passed to 0x.js.
|
||||
* Provider passed to 0x.js.
|
||||
* @param orderTransactionOpts Optional arguments this method accepts.
|
||||
* @return Transaction hash.
|
||||
*/
|
||||
@@ -416,7 +416,7 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
* signedOrder you wish to fill.
|
||||
* @param fillTakerTokenAmount The total amount of the takerTokens you would like to fill.
|
||||
* @param takerAddress The user Ethereum address who would like to fill this order.
|
||||
* Must be available via the supplied Web3.Provider passed to 0x.js.
|
||||
* Must be available via the supplied Provider passed to 0x.js.
|
||||
* @param orderTransactionOpts Optional arguments this method accepts.
|
||||
* @return Transaction hash.
|
||||
*/
|
||||
@@ -470,7 +470,7 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
* filled (each to the specified fillAmount) or aborted.
|
||||
* @param orderFillRequests An array of objects that conform to the OrderFillRequest interface.
|
||||
* @param takerAddress The user Ethereum address who would like to fill there orders.
|
||||
* Must be available via the supplied Web3.Provider passed to 0x.js.
|
||||
* Must be available via the supplied Provider passed to 0x.js.
|
||||
* @param orderTransactionOpts Optional arguments this method accepts.
|
||||
* @return Transaction hash.
|
||||
*/
|
||||
@@ -765,7 +765,7 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
* signedOrder you wish to fill.
|
||||
* @param fillTakerTokenAmount The total amount of the takerTokens you would like to fill.
|
||||
* @param takerAddress The user Ethereum address who would like to fill this order.
|
||||
* Must be available via the supplied Web3.Provider passed to 0x.js.
|
||||
* Must be available via the supplied Provider passed to 0x.js.
|
||||
*/
|
||||
public async validateFillOrderThrowIfInvalidAsync(
|
||||
signedOrder: SignedOrder,
|
||||
@@ -812,7 +812,7 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
* signedOrder you wish to fill.
|
||||
* @param fillTakerTokenAmount The total amount of the takerTokens you would like to fill.
|
||||
* @param takerAddress The user Ethereum address who would like to fill this order.
|
||||
* Must be available via the supplied Web3.Provider passed to 0x.js.
|
||||
* Must be available via the supplied Provider passed to 0x.js.
|
||||
*/
|
||||
public async validateFillOrKillOrderThrowIfInvalidAsync(
|
||||
signedOrder: SignedOrder,
|
||||
@@ -920,7 +920,12 @@ export class ExchangeWrapper extends ContractWrapper {
|
||||
artifacts.ExchangeArtifact,
|
||||
this._contractAddressIfExists,
|
||||
);
|
||||
const contractInstance = new ExchangeContract(this._web3Wrapper, abi, address);
|
||||
const contractInstance = new ExchangeContract(
|
||||
abi,
|
||||
address,
|
||||
this._web3Wrapper.getProvider(),
|
||||
this._web3Wrapper.getContractDefaults(),
|
||||
);
|
||||
this._exchangeContractIfExists = contractInstance;
|
||||
return this._exchangeContractIfExists;
|
||||
}
|
||||
|
@@ -121,7 +121,12 @@ export class TokenRegistryWrapper extends ContractWrapper {
|
||||
artifacts.TokenRegistryArtifact,
|
||||
this._contractAddressIfExists,
|
||||
);
|
||||
const contractInstance = new TokenRegistryContract(this._web3Wrapper, abi, address);
|
||||
const contractInstance = new TokenRegistryContract(
|
||||
abi,
|
||||
address,
|
||||
this._web3Wrapper.getProvider(),
|
||||
this._web3Wrapper.getContractDefaults(),
|
||||
);
|
||||
this._tokenRegistryContractIfExists = contractInstance;
|
||||
return this._tokenRegistryContractIfExists;
|
||||
}
|
||||
|
@@ -63,7 +63,12 @@ export class TokenTransferProxyWrapper extends ContractWrapper {
|
||||
artifacts.TokenTransferProxyArtifact,
|
||||
this._contractAddressIfExists,
|
||||
);
|
||||
const contractInstance = new TokenTransferProxyContract(this._web3Wrapper, abi, address);
|
||||
const contractInstance = new TokenTransferProxyContract(
|
||||
abi,
|
||||
address,
|
||||
this._web3Wrapper.getProvider(),
|
||||
this._web3Wrapper.getContractDefaults(),
|
||||
);
|
||||
this._tokenTransferProxyContractIfExists = contractInstance;
|
||||
return this._tokenTransferProxyContractIfExists;
|
||||
}
|
||||
|
@@ -421,7 +421,12 @@ export class TokenWrapper extends ContractWrapper {
|
||||
artifacts.TokenArtifact,
|
||||
normalizedTokenAddress,
|
||||
);
|
||||
const contractInstance = new TokenContract(this._web3Wrapper, abi, address);
|
||||
const contractInstance = new TokenContract(
|
||||
abi,
|
||||
address,
|
||||
this._web3Wrapper.getProvider(),
|
||||
this._web3Wrapper.getContractDefaults(),
|
||||
);
|
||||
tokenContract = contractInstance;
|
||||
this._tokenContractsByAddress[normalizedTokenAddress] = tokenContract;
|
||||
return tokenContract;
|
||||
|
7
packages/0x.js/src/globals.d.ts
vendored
7
packages/0x.js/src/globals.d.ts
vendored
@@ -1,10 +1,3 @@
|
||||
declare module 'web3_beta';
|
||||
|
||||
// semver-sort declarations
|
||||
declare module 'semver-sort' {
|
||||
const desc: (versions: string[]) => string[];
|
||||
}
|
||||
|
||||
declare module '*.json' {
|
||||
const json: any;
|
||||
/* tslint:disable */
|
||||
|
@@ -11,7 +11,6 @@ export {
|
||||
OrderCancellationRequest,
|
||||
OrderFillRequest,
|
||||
ContractEventArgs,
|
||||
Web3Provider,
|
||||
ZeroExConfig,
|
||||
MethodOpts,
|
||||
OrderTransactionOpts,
|
||||
@@ -32,6 +31,7 @@ export {
|
||||
ContractEventArg,
|
||||
LogWithDecodedArgs,
|
||||
Order,
|
||||
Provider,
|
||||
SignedOrder,
|
||||
ECSignature,
|
||||
TransactionReceipt,
|
||||
|
@@ -154,14 +154,6 @@ export interface OrderFillRequest {
|
||||
export type AsyncMethod = (...args: any[]) => Promise<any>;
|
||||
export type SyncMethod = (...args: any[]) => any;
|
||||
|
||||
/**
|
||||
* 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. To learn more
|
||||
* about providers, visit https://0xproject.com/wiki#Web3-Provider-Explained
|
||||
*/
|
||||
export type Web3Provider = Web3.Provider;
|
||||
|
||||
/*
|
||||
* orderExpirationCheckingIntervalMs: How often to check for expired orders. Default: 50
|
||||
* eventPollingIntervalMs: How often to poll the Ethereum node for new events. Default: 200
|
||||
|
Reference in New Issue
Block a user