Prefix private vars with _

This commit is contained in:
Leonid Logvinov
2017-06-10 15:36:52 +02:00
parent 8b9d8ad1b6
commit d727bed6ab
7 changed files with 138 additions and 137 deletions

View File

@@ -33,7 +33,7 @@ export class ZeroEx {
public exchange: ExchangeWrapper; public exchange: ExchangeWrapper;
public tokenRegistry: TokenRegistryWrapper; public tokenRegistry: TokenRegistryWrapper;
public token: TokenWrapper; public token: TokenWrapper;
private web3Wrapper: Web3Wrapper; private _web3Wrapper: Web3Wrapper;
/** /**
* Verifies that the elliptic curve signature `signature` was generated * Verifies that the elliptic curve signature `signature` was generated
* by signing `dataHex` with the private key corresponding to the `signerAddressHex` address. * by signing `dataHex` with the private key corresponding to the `signerAddressHex` address.
@@ -128,10 +128,10 @@ export class ZeroEx {
* @return An instance of the 0x.js ZeroEx class. * @return An instance of the 0x.js ZeroEx class.
*/ */
constructor(web3: Web3) { constructor(web3: Web3) {
this.web3Wrapper = new Web3Wrapper(web3); this._web3Wrapper = new Web3Wrapper(web3);
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);
} }
/** /**
* 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
@@ -139,7 +139,7 @@ export class ZeroEx {
* @param provider The Web3.Provider you would like the 0x.js library to use from now on. * @param provider The Web3.Provider you would like the 0x.js library to use from now on.
*/ */
public async setProviderAsync(provider: Web3.Provider) { public async setProviderAsync(provider: Web3.Provider) {
this.web3Wrapper.setProvider(provider); this._web3Wrapper.setProvider(provider);
await this.exchange.invalidateContractInstanceAsync(); await this.exchange.invalidateContractInstanceAsync();
this.tokenRegistry.invalidateContractInstance(); this.tokenRegistry.invalidateContractInstance();
this.token.invalidateContractInstances(); this.token.invalidateContractInstances();
@@ -149,7 +149,7 @@ export class ZeroEx {
* @return An array of Ethereum addresses available. * @return An array of Ethereum addresses available.
*/ */
public async getAvailableAddressesAsync(): Promise<string[]> { public async getAvailableAddressesAsync(): Promise<string[]> {
const availableAddresses = await this.web3Wrapper.getAvailableAddressesAsync(); const availableAddresses = await this._web3Wrapper.getAvailableAddressesAsync();
return availableAddresses; return availableAddresses;
} }
/** /**
@@ -160,7 +160,7 @@ export class ZeroEx {
public async getOrderHashHexAsync(order: Order|SignedOrder): Promise<string> { public async getOrderHashHexAsync(order: Order|SignedOrder): Promise<string> {
assert.doesConformToSchema('order', order, orderSchema); assert.doesConformToSchema('order', order, orderSchema);
const exchangeContractAddr = await this.getExchangeAddressAsync(); const exchangeContractAddr = await this._getExchangeAddressAsync();
const orderHashHex = utils.getOrderHashHex(order, exchangeContractAddr); const orderHashHex = utils.getOrderHashHex(order, exchangeContractAddr);
return orderHashHex; return orderHashHex;
} }
@@ -174,10 +174,10 @@ export class ZeroEx {
*/ */
public async signOrderHashAsync(orderHashHex: string, signerAddress: string): Promise<ECSignature> { public async signOrderHashAsync(orderHashHex: string, signerAddress: string): Promise<ECSignature> {
assert.isHexString('orderHashHex', orderHashHex); assert.isHexString('orderHashHex', orderHashHex);
await assert.isSenderAddressAsync('signerAddress', signerAddress, this.web3Wrapper); await assert.isSenderAddressAsync('signerAddress', signerAddress, this._web3Wrapper);
let msgHashHex; let msgHashHex;
const nodeVersion = await this.web3Wrapper.getNodeVersionAsync(); const nodeVersion = await this._web3Wrapper.getNodeVersionAsync();
const isParityNode = utils.isParityNode(nodeVersion); const isParityNode = utils.isParityNode(nodeVersion);
if (isParityNode) { if (isParityNode) {
// Parity node adds the personalMessage prefix itself // Parity node adds the personalMessage prefix itself
@@ -188,7 +188,7 @@ export class ZeroEx {
msgHashHex = ethUtil.bufferToHex(msgHashBuff); msgHashHex = ethUtil.bufferToHex(msgHashBuff);
} }
const signature = await this.web3Wrapper.signTransactionAsync(signerAddress, msgHashHex); const signature = await this._web3Wrapper.signTransactionAsync(signerAddress, msgHashHex);
let signatureData; let signatureData;
const [nodeVersionNumber] = findVersions(nodeVersion); const [nodeVersionNumber] = findVersions(nodeVersion);
@@ -224,8 +224,8 @@ export class ZeroEx {
} }
return ecSignature; return ecSignature;
} }
private async getExchangeAddressAsync() { private async _getExchangeAddressAsync() {
const networkIdIfExists = await this.web3Wrapper.getNetworkIdIfExistsAsync(); const networkIdIfExists = await this._web3Wrapper.getNetworkIdIfExistsAsync();
const exchangeNetworkConfigsIfExists = _.isUndefined(networkIdIfExists) ? const exchangeNetworkConfigsIfExists = _.isUndefined(networkIdIfExists) ?
undefined : undefined :
(ExchangeArtifacts as any).networks[networkIdIfExists]; (ExchangeArtifacts as any).networks[networkIdIfExists];

View File

@@ -9,7 +9,7 @@ export class ContractWrapper {
constructor(web3Wrapper: Web3Wrapper) { constructor(web3Wrapper: Web3Wrapper) {
this.web3Wrapper = web3Wrapper; this.web3Wrapper = web3Wrapper;
} }
protected async instantiateContractIfExistsAsync(artifact: Artifact, address?: string): Promise<ContractInstance> { protected async _instantiateContractIfExistsAsync(artifact: Artifact, address?: string): Promise<ContractInstance> {
const c = await contract(artifact); const c = await contract(artifact);
const providerObj = this.web3Wrapper.getCurrentProvider(); const providerObj = this.web3Wrapper.getCurrentProvider();
c.setProvider(providerObj); c.setProvider(providerObj);

View File

@@ -37,7 +37,7 @@ import {constants} from '../utils/constants';
import {TokenWrapper} from './token_wrapper'; import {TokenWrapper} from './token_wrapper';
export class ExchangeWrapper extends ContractWrapper { export class ExchangeWrapper extends ContractWrapper {
private exchangeContractErrCodesToMsg = { private _exchangeContractErrCodesToMsg = {
[ExchangeContractErrCodes.ERROR_FILL_EXPIRED]: ExchangeContractErrs.ORDER_FILL_EXPIRED, [ExchangeContractErrCodes.ERROR_FILL_EXPIRED]: ExchangeContractErrs.ORDER_FILL_EXPIRED,
[ExchangeContractErrCodes.ERROR_CANCEL_EXPIRED]: ExchangeContractErrs.ORDER_FILL_EXPIRED, [ExchangeContractErrCodes.ERROR_CANCEL_EXPIRED]: ExchangeContractErrs.ORDER_FILL_EXPIRED,
[ExchangeContractErrCodes.ERROR_FILL_NO_VALUE]: ExchangeContractErrs.ORDER_REMAINING_FILL_AMOUNT_ZERO, [ExchangeContractErrCodes.ERROR_FILL_NO_VALUE]: ExchangeContractErrs.ORDER_REMAINING_FILL_AMOUNT_ZERO,
@@ -45,10 +45,10 @@ export class ExchangeWrapper extends ContractWrapper {
[ExchangeContractErrCodes.ERROR_FILL_TRUNCATION]: ExchangeContractErrs.ORDER_FILL_ROUNDING_ERROR, [ExchangeContractErrCodes.ERROR_FILL_TRUNCATION]: ExchangeContractErrs.ORDER_FILL_ROUNDING_ERROR,
[ExchangeContractErrCodes.ERROR_FILL_BALANCE_ALLOWANCE]: ExchangeContractErrs.FILL_BALANCE_ALLOWANCE_ERROR, [ExchangeContractErrCodes.ERROR_FILL_BALANCE_ALLOWANCE]: ExchangeContractErrs.FILL_BALANCE_ALLOWANCE_ERROR,
}; };
private exchangeContractIfExists?: ExchangeContract; private _exchangeContractIfExists?: ExchangeContract;
private exchangeLogEventObjs: ContractEventObj[]; private _exchangeLogEventObjs: ContractEventObj[];
private tokenWrapper: TokenWrapper; private _tokenWrapper: TokenWrapper;
private static getOrderAddressesAndValues(order: Order): [OrderAddresses, OrderValues] { private static _getOrderAddressesAndValues(order: Order): [OrderAddresses, OrderValues] {
const orderAddresses: OrderAddresses = [ const orderAddresses: OrderAddresses = [
order.maker, order.maker,
order.taker, order.taker,
@@ -68,12 +68,12 @@ export class ExchangeWrapper extends ContractWrapper {
} }
constructor(web3Wrapper: Web3Wrapper, tokenWrapper: TokenWrapper) { constructor(web3Wrapper: Web3Wrapper, tokenWrapper: TokenWrapper) {
super(web3Wrapper); super(web3Wrapper);
this.tokenWrapper = tokenWrapper; this._tokenWrapper = tokenWrapper;
this.exchangeLogEventObjs = []; this._exchangeLogEventObjs = [];
} }
public async invalidateContractInstanceAsync(): Promise<void> { public async invalidateContractInstanceAsync(): Promise<void> {
await this.stopWatchingExchangeLogEventsAsync(); await this._stopWatchingExchangeLogEventsAsync();
delete this.exchangeContractIfExists; delete this._exchangeContractIfExists;
} }
/** /**
* Returns the unavailable takerAmount of an order. Unavailable amount is defined as the total * Returns the unavailable takerAmount of an order. Unavailable amount is defined as the total
@@ -86,7 +86,7 @@ export class ExchangeWrapper extends ContractWrapper {
public async getUnavailableTakerAmountAsync(orderHashHex: string): Promise<BigNumber.BigNumber> { public async getUnavailableTakerAmountAsync(orderHashHex: string): Promise<BigNumber.BigNumber> {
assert.isValidOrderHash('orderHashHex', orderHashHex); assert.isValidOrderHash('orderHashHex', orderHashHex);
const exchangeContract = await this.getExchangeContractAsync(); const exchangeContract = await this._getExchangeContractAsync();
let unavailableAmountInBaseUnits = await exchangeContract.getUnavailableValueT.call(orderHashHex); let unavailableAmountInBaseUnits = await exchangeContract.getUnavailableValueT.call(orderHashHex);
// Wrap BigNumbers returned from web3 with our own (later) version of BigNumber // Wrap BigNumbers returned from web3 with our own (later) version of BigNumber
unavailableAmountInBaseUnits = new BigNumber(unavailableAmountInBaseUnits); unavailableAmountInBaseUnits = new BigNumber(unavailableAmountInBaseUnits);
@@ -100,7 +100,7 @@ export class ExchangeWrapper extends ContractWrapper {
public async getFilledTakerAmountAsync(orderHashHex: string): Promise<BigNumber.BigNumber> { public async getFilledTakerAmountAsync(orderHashHex: string): Promise<BigNumber.BigNumber> {
assert.isValidOrderHash('orderHashHex', orderHashHex); assert.isValidOrderHash('orderHashHex', orderHashHex);
const exchangeContract = await this.getExchangeContractAsync(); const exchangeContract = await this._getExchangeContractAsync();
let fillAmountInBaseUnits = await exchangeContract.filled.call(orderHashHex); let fillAmountInBaseUnits = await exchangeContract.filled.call(orderHashHex);
// Wrap BigNumbers returned from web3 with our own (later) version of BigNumber // Wrap BigNumbers returned from web3 with our own (later) version of BigNumber
fillAmountInBaseUnits = new BigNumber(fillAmountInBaseUnits); fillAmountInBaseUnits = new BigNumber(fillAmountInBaseUnits);
@@ -115,7 +115,7 @@ export class ExchangeWrapper extends ContractWrapper {
public async getCanceledTakerAmountAsync(orderHashHex: string): Promise<BigNumber.BigNumber> { public async getCanceledTakerAmountAsync(orderHashHex: string): Promise<BigNumber.BigNumber> {
assert.isValidOrderHash('orderHashHex', orderHashHex); assert.isValidOrderHash('orderHashHex', orderHashHex);
const exchangeContract = await this.getExchangeContractAsync(); const exchangeContract = await this._getExchangeContractAsync();
let cancelledAmountInBaseUnits = await exchangeContract.cancelled.call(orderHashHex); let cancelledAmountInBaseUnits = await exchangeContract.cancelled.call(orderHashHex);
// Wrap BigNumbers returned from web3 with our own (later) version of BigNumber // Wrap BigNumbers returned from web3 with our own (later) version of BigNumber
cancelledAmountInBaseUnits = new BigNumber(cancelledAmountInBaseUnits); cancelledAmountInBaseUnits = new BigNumber(cancelledAmountInBaseUnits);
@@ -142,10 +142,10 @@ export class ExchangeWrapper extends ContractWrapper {
assert.isBoolean('shouldCheckTransfer', shouldCheckTransfer); assert.isBoolean('shouldCheckTransfer', shouldCheckTransfer);
await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper); await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper);
const exchangeInstance = await this.getExchangeContractAsync(); const exchangeInstance = await this._getExchangeContractAsync();
await this.validateFillOrderAndThrowIfInvalidAsync(signedOrder, takerTokenFillAmount, takerAddress); await this._validateFillOrderAndThrowIfInvalidAsync(signedOrder, takerTokenFillAmount, takerAddress);
const [orderAddresses, orderValues] = ExchangeWrapper.getOrderAddressesAndValues(signedOrder); const [orderAddresses, orderValues] = ExchangeWrapper._getOrderAddressesAndValues(signedOrder);
const gas = await exchangeInstance.fill.estimateGas( const gas = await exchangeInstance.fill.estimateGas(
orderAddresses, orderAddresses,
@@ -172,7 +172,7 @@ export class ExchangeWrapper extends ContractWrapper {
gas, gas,
}, },
); );
this.throwErrorLogsAsErrors(response.logs); this._throwErrorLogsAsErrors(response.logs);
} }
/** /**
* Sequentially and atomically fills signedOrders up to the specified takerTokenFillAmount. * Sequentially and atomically fills signedOrders up to the specified takerTokenFillAmount.
@@ -198,7 +198,7 @@ export class ExchangeWrapper extends ContractWrapper {
assert.doesConformToSchema('signedOrders', signedOrders, signedOrdersSchema); assert.doesConformToSchema('signedOrders', signedOrders, signedOrdersSchema);
await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper); await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper);
for (const signedOrder of signedOrders) { for (const signedOrder of signedOrders) {
await this.validateFillOrderAndThrowIfInvalidAsync( await this._validateFillOrderAndThrowIfInvalidAsync(
signedOrder, takerTokenFillAmount, takerAddress); signedOrder, takerTokenFillAmount, takerAddress);
} }
if (_.isEmpty(signedOrders)) { if (_.isEmpty(signedOrders)) {
@@ -207,7 +207,7 @@ export class ExchangeWrapper extends ContractWrapper {
const orderAddressesValuesAndSignatureArray = _.map(signedOrders, signedOrder => { const orderAddressesValuesAndSignatureArray = _.map(signedOrders, signedOrder => {
return [ return [
...ExchangeWrapper.getOrderAddressesAndValues(signedOrder), ...ExchangeWrapper._getOrderAddressesAndValues(signedOrder),
signedOrder.ecSignature.v, signedOrder.ecSignature.v,
signedOrder.ecSignature.r, signedOrder.ecSignature.r,
signedOrder.ecSignature.s, signedOrder.ecSignature.s,
@@ -218,7 +218,7 @@ export class ExchangeWrapper extends ContractWrapper {
orderAddressesValuesAndSignatureArray, orderAddressesValuesAndSignatureArray,
); );
const exchangeInstance = await this.getExchangeContractAsync(); const exchangeInstance = await this._getExchangeContractAsync();
const gas = await exchangeInstance.fillUpTo.estimateGas( const gas = await exchangeInstance.fillUpTo.estimateGas(
orderAddressesArray, orderAddressesArray,
orderValuesArray, orderValuesArray,
@@ -244,7 +244,7 @@ export class ExchangeWrapper extends ContractWrapper {
gas, gas,
}, },
); );
this.throwErrorLogsAsErrors(response.logs); this._throwErrorLogsAsErrors(response.logs);
} }
/** /**
* Batch version of fillOrderAsync. * Batch version of fillOrderAsync.
@@ -265,7 +265,7 @@ export class ExchangeWrapper extends ContractWrapper {
await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper); await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper);
assert.doesConformToSchema('orderFillRequests', orderFillRequests, orderFillRequestsSchema); assert.doesConformToSchema('orderFillRequests', orderFillRequests, orderFillRequestsSchema);
for (const orderFillRequest of orderFillRequests) { for (const orderFillRequest of orderFillRequests) {
await this.validateFillOrderAndThrowIfInvalidAsync( await this._validateFillOrderAndThrowIfInvalidAsync(
orderFillRequest.signedOrder, orderFillRequest.takerTokenFillAmount, takerAddress); orderFillRequest.signedOrder, orderFillRequest.takerTokenFillAmount, takerAddress);
} }
if (_.isEmpty(orderFillRequests)) { if (_.isEmpty(orderFillRequests)) {
@@ -274,7 +274,7 @@ export class ExchangeWrapper extends ContractWrapper {
const orderAddressesValuesAmountsAndSignatureArray = _.map(orderFillRequests, orderFillRequest => { const orderAddressesValuesAmountsAndSignatureArray = _.map(orderFillRequests, orderFillRequest => {
return [ return [
...ExchangeWrapper.getOrderAddressesAndValues(orderFillRequest.signedOrder), ...ExchangeWrapper._getOrderAddressesAndValues(orderFillRequest.signedOrder),
orderFillRequest.takerTokenFillAmount, orderFillRequest.takerTokenFillAmount,
orderFillRequest.signedOrder.ecSignature.v, orderFillRequest.signedOrder.ecSignature.v,
orderFillRequest.signedOrder.ecSignature.r, orderFillRequest.signedOrder.ecSignature.r,
@@ -286,7 +286,7 @@ export class ExchangeWrapper extends ContractWrapper {
orderAddressesValuesAmountsAndSignatureArray, orderAddressesValuesAmountsAndSignatureArray,
); );
const exchangeInstance = await this.getExchangeContractAsync(); const exchangeInstance = await this._getExchangeContractAsync();
const gas = await exchangeInstance.batchFill.estimateGas( const gas = await exchangeInstance.batchFill.estimateGas(
orderAddressesArray, orderAddressesArray,
orderValuesArray, orderValuesArray,
@@ -312,7 +312,7 @@ export class ExchangeWrapper extends ContractWrapper {
gas, gas,
}, },
); );
this.throwErrorLogsAsErrors(response.logs); this._throwErrorLogsAsErrors(response.logs);
} }
/** /**
* Attempts to fill a specific amount of an order. If the entire amount specified cannot be filled, * Attempts to fill a specific amount of an order. If the entire amount specified cannot be filled,
@@ -329,13 +329,13 @@ export class ExchangeWrapper extends ContractWrapper {
assert.isBigNumber('takerTokenFillAmount', takerTokenFillAmount); assert.isBigNumber('takerTokenFillAmount', takerTokenFillAmount);
await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper); await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper);
const exchangeInstance = await this.getExchangeContractAsync(); const exchangeInstance = await this._getExchangeContractAsync();
await this.validateFillOrderAndThrowIfInvalidAsync(signedOrder, takerTokenFillAmount, takerAddress); await this._validateFillOrderAndThrowIfInvalidAsync(signedOrder, takerTokenFillAmount, takerAddress);
await this.validateFillOrKillOrderAndThrowIfInvalidAsync(signedOrder, exchangeInstance.address, await this._validateFillOrKillOrderAndThrowIfInvalidAsync(signedOrder, exchangeInstance.address,
takerTokenFillAmount); takerTokenFillAmount);
const [orderAddresses, orderValues] = ExchangeWrapper.getOrderAddressesAndValues(signedOrder); const [orderAddresses, orderValues] = ExchangeWrapper._getOrderAddressesAndValues(signedOrder);
const gas = await exchangeInstance.fillOrKill.estimateGas( const gas = await exchangeInstance.fillOrKill.estimateGas(
orderAddresses, orderAddresses,
@@ -360,7 +360,7 @@ export class ExchangeWrapper extends ContractWrapper {
gas, gas,
}, },
); );
this.throwErrorLogsAsErrors(response.logs); this._throwErrorLogsAsErrors(response.logs);
} }
/** /**
* Batch version of fillOrKill. Allows a taker to specify a batch of orders that will either be atomically * Batch version of fillOrKill. Allows a taker to specify a batch of orders that will either be atomically
@@ -373,15 +373,15 @@ export class ExchangeWrapper extends ContractWrapper {
takerAddress: string): Promise<void> { takerAddress: string): Promise<void> {
await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper); await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper);
assert.doesConformToSchema('orderFillOrKillRequests', orderFillOrKillRequests, orderFillOrKillRequestsSchema); assert.doesConformToSchema('orderFillOrKillRequests', orderFillOrKillRequests, orderFillOrKillRequestsSchema);
const exchangeInstance = await this.getExchangeContractAsync(); const exchangeInstance = await this._getExchangeContractAsync();
for (const request of orderFillOrKillRequests) { for (const request of orderFillOrKillRequests) {
await this.validateFillOrKillOrderAndThrowIfInvalidAsync(request.signedOrder, exchangeInstance.address, await this._validateFillOrKillOrderAndThrowIfInvalidAsync(request.signedOrder, exchangeInstance.address,
request.fillTakerAmount); request.fillTakerAmount);
} }
const orderAddressesValuesAndTakerTokenFillAmounts = _.map(orderFillOrKillRequests, request => { const orderAddressesValuesAndTakerTokenFillAmounts = _.map(orderFillOrKillRequests, request => {
return [ return [
...ExchangeWrapper.getOrderAddressesAndValues(request.signedOrder), ...ExchangeWrapper._getOrderAddressesAndValues(request.signedOrder),
request.fillTakerAmount, request.fillTakerAmount,
request.signedOrder.ecSignature.v, request.signedOrder.ecSignature.v,
request.signedOrder.ecSignature.r, request.signedOrder.ecSignature.r,
@@ -416,7 +416,7 @@ export class ExchangeWrapper extends ContractWrapper {
gas, gas,
}, },
); );
this.throwErrorLogsAsErrors(response.logs); this._throwErrorLogsAsErrors(response.logs);
} }
/** /**
* Cancel a given fill amount of an order. Cancellations are cumulative. * Cancel a given fill amount of an order. Cancellations are cumulative.
@@ -430,10 +430,10 @@ export class ExchangeWrapper extends ContractWrapper {
assert.isBigNumber('takerTokenCancelAmount', takerTokenCancelAmount); assert.isBigNumber('takerTokenCancelAmount', takerTokenCancelAmount);
await assert.isSenderAddressAsync('order.maker', order.maker, this.web3Wrapper); await assert.isSenderAddressAsync('order.maker', order.maker, this.web3Wrapper);
const exchangeInstance = await this.getExchangeContractAsync(); const exchangeInstance = await this._getExchangeContractAsync();
await this.validateCancelOrderAndThrowIfInvalidAsync(order, takerTokenCancelAmount); await this._validateCancelOrderAndThrowIfInvalidAsync(order, takerTokenCancelAmount);
const [orderAddresses, orderValues] = ExchangeWrapper.getOrderAddressesAndValues(order); const [orderAddresses, orderValues] = ExchangeWrapper._getOrderAddressesAndValues(order);
const gas = await exchangeInstance.cancel.estimateGas( const gas = await exchangeInstance.cancel.estimateGas(
orderAddresses, orderAddresses,
orderValues, orderValues,
@@ -451,7 +451,7 @@ export class ExchangeWrapper extends ContractWrapper {
gas, gas,
}, },
); );
this.throwErrorLogsAsErrors(response.logs); this._throwErrorLogsAsErrors(response.logs);
} }
/** /**
* Batch version of cancelOrderAsync. Atomically cancels multiple orders in a single transaction. * Batch version of cancelOrderAsync. Atomically cancels multiple orders in a single transaction.
@@ -467,17 +467,17 @@ export class ExchangeWrapper extends ContractWrapper {
assert.doesConformToSchema('orderCancellationRequests', orderCancellationRequests, assert.doesConformToSchema('orderCancellationRequests', orderCancellationRequests,
orderCancellationRequestsSchema); orderCancellationRequestsSchema);
for (const cancellationRequest of orderCancellationRequests) { for (const cancellationRequest of orderCancellationRequests) {
await this.validateCancelOrderAndThrowIfInvalidAsync( await this._validateCancelOrderAndThrowIfInvalidAsync(
cancellationRequest.order, cancellationRequest.takerTokenCancelAmount, cancellationRequest.order, cancellationRequest.takerTokenCancelAmount,
); );
} }
if (_.isEmpty(orderCancellationRequests)) { if (_.isEmpty(orderCancellationRequests)) {
return; // no-op return; // no-op
} }
const exchangeInstance = await this.getExchangeContractAsync(); const exchangeInstance = await this._getExchangeContractAsync();
const orderAddressesValuesAndTakerTokenCancelAmounts = _.map(orderCancellationRequests, cancellationRequest => { const orderAddressesValuesAndTakerTokenCancelAmounts = _.map(orderCancellationRequests, cancellationRequest => {
return [ return [
...ExchangeWrapper.getOrderAddressesAndValues(cancellationRequest.order), ...ExchangeWrapper._getOrderAddressesAndValues(cancellationRequest.order),
cancellationRequest.takerTokenCancelAmount, cancellationRequest.takerTokenCancelAmount,
]; ];
}); });
@@ -501,7 +501,7 @@ export class ExchangeWrapper extends ContractWrapper {
gas, gas,
}, },
); );
this.throwErrorLogsAsErrors(response.logs); this._throwErrorLogsAsErrors(response.logs);
} }
/** /**
* Subscribe to an event type emitted by the Exchange smart contract * Subscribe to an event type emitted by the Exchange smart contract
@@ -514,7 +514,7 @@ export class ExchangeWrapper extends ContractWrapper {
public async subscribeAsync(eventName: ExchangeEvents, subscriptionOpts: SubscriptionOpts, public async subscribeAsync(eventName: ExchangeEvents, subscriptionOpts: SubscriptionOpts,
indexFilterValues: IndexFilterValues, callback: EventCallback): indexFilterValues: IndexFilterValues, callback: EventCallback):
Promise<void> { Promise<void> {
const exchangeContract = await this.getExchangeContractAsync(); const exchangeContract = await this._getExchangeContractAsync();
let createLogEvent: CreateContractEvent; let createLogEvent: CreateContractEvent;
switch (eventName) { switch (eventName) {
case ExchangeEvents.LogFill: case ExchangeEvents.LogFill:
@@ -533,15 +533,15 @@ export class ExchangeWrapper extends ContractWrapper {
const logEventObj: ContractEventObj = createLogEvent(indexFilterValues, subscriptionOpts); const logEventObj: ContractEventObj = createLogEvent(indexFilterValues, subscriptionOpts);
logEventObj.watch(callback); logEventObj.watch(callback);
this.exchangeLogEventObjs.push(logEventObj); this._exchangeLogEventObjs.push(logEventObj);
} }
private async isValidSignatureUsingContractCallAsync(dataHex: string, ecSignature: ECSignature, private async _isValidSignatureUsingContractCallAsync(dataHex: string, ecSignature: ECSignature,
signerAddressHex: string): Promise<boolean> { signerAddressHex: string): Promise<boolean> {
assert.isHexString('dataHex', dataHex); assert.isHexString('dataHex', dataHex);
assert.doesConformToSchema('ecSignature', ecSignature, ecSignatureSchema); assert.doesConformToSchema('ecSignature', ecSignature, ecSignatureSchema);
assert.isETHAddressHex('signerAddressHex', signerAddressHex); assert.isETHAddressHex('signerAddressHex', signerAddressHex);
const exchangeInstance = await this.getExchangeContractAsync(); const exchangeInstance = await this._getExchangeContractAsync();
const isValidSignature = await exchangeInstance.isValidSignature.call( const isValidSignature = await exchangeInstance.isValidSignature.call(
signerAddressHex, signerAddressHex,
@@ -552,27 +552,27 @@ export class ExchangeWrapper extends ContractWrapper {
); );
return isValidSignature; return isValidSignature;
} }
private async getOrderHashHexAsync(order: Order|SignedOrder): Promise<string> { private async _getOrderHashHexAsync(order: Order|SignedOrder): Promise<string> {
const exchangeInstance = await this.getExchangeContractAsync(); const exchangeInstance = await this._getExchangeContractAsync();
const orderHashHex = utils.getOrderHashHex(order, exchangeInstance.address); const orderHashHex = utils.getOrderHashHex(order, exchangeInstance.address);
return orderHashHex; return orderHashHex;
} }
private async getOrderHashHexUsingContractCallAsync(order: Order|SignedOrder): Promise<string> { private async _getOrderHashHexUsingContractCallAsync(order: Order|SignedOrder): Promise<string> {
const exchangeInstance = await this.getExchangeContractAsync(); const exchangeInstance = await this._getExchangeContractAsync();
const [orderAddresses, orderValues] = ExchangeWrapper.getOrderAddressesAndValues(order); const [orderAddresses, orderValues] = ExchangeWrapper._getOrderAddressesAndValues(order);
const orderHashHex = await exchangeInstance.getOrderHash.call(orderAddresses, orderValues); const orderHashHex = await exchangeInstance.getOrderHash.call(orderAddresses, orderValues);
return orderHashHex; return orderHashHex;
} }
private async stopWatchingExchangeLogEventsAsync() { private async _stopWatchingExchangeLogEventsAsync() {
const stopWatchingPromises = _.map(this.exchangeLogEventObjs, logEventObj => { const stopWatchingPromises = _.map(this._exchangeLogEventObjs, logEventObj => {
return promisify(logEventObj.stopWatching, logEventObj)(); return promisify(logEventObj.stopWatching, logEventObj)();
}); });
await Promise.all(stopWatchingPromises); await Promise.all(stopWatchingPromises);
this.exchangeLogEventObjs = []; this._exchangeLogEventObjs = [];
} }
private async validateFillOrderAndThrowIfInvalidAsync(signedOrder: SignedOrder, private async _validateFillOrderAndThrowIfInvalidAsync(signedOrder: SignedOrder,
fillTakerAmount: BigNumber.BigNumber, fillTakerAmount: BigNumber.BigNumber,
senderAddress: string): Promise<void> { senderAddress: string): Promise<void> {
if (fillTakerAmount.eq(0)) { if (fillTakerAmount.eq(0)) {
throw new Error(ExchangeContractErrs.ORDER_REMAINING_FILL_AMOUNT_ZERO); throw new Error(ExchangeContractErrs.ORDER_REMAINING_FILL_AMOUNT_ZERO);
} }
@@ -583,23 +583,23 @@ export class ExchangeWrapper extends ContractWrapper {
if (signedOrder.expirationUnixTimestampSec.lessThan(currentUnixTimestampSec)) { if (signedOrder.expirationUnixTimestampSec.lessThan(currentUnixTimestampSec)) {
throw new Error(ExchangeContractErrs.ORDER_FILL_EXPIRED); throw new Error(ExchangeContractErrs.ORDER_FILL_EXPIRED);
} }
const zrxTokenAddress = await this.getZRXTokenAddressAsync(); const zrxTokenAddress = await this._getZRXTokenAddressAsync();
await this.validateFillOrderBalancesAndAllowancesAndThrowIfInvalidAsync(signedOrder, fillTakerAmount, await this._validateFillOrderBalancesAndAllowancesAndThrowIfInvalidAsync(signedOrder, fillTakerAmount,
senderAddress, zrxTokenAddress); senderAddress, zrxTokenAddress);
const wouldRoundingErrorOccur = await this.isRoundingErrorAsync( const wouldRoundingErrorOccur = await this._isRoundingErrorAsync(
signedOrder.takerTokenAmount, fillTakerAmount, signedOrder.makerTokenAmount, signedOrder.takerTokenAmount, fillTakerAmount, signedOrder.makerTokenAmount,
); );
if (wouldRoundingErrorOccur) { if (wouldRoundingErrorOccur) {
throw new Error(ExchangeContractErrs.ORDER_FILL_ROUNDING_ERROR); throw new Error(ExchangeContractErrs.ORDER_FILL_ROUNDING_ERROR);
} }
} }
private async validateCancelOrderAndThrowIfInvalidAsync( private async _validateCancelOrderAndThrowIfInvalidAsync(
order: Order, takerTokenCancelAmount: BigNumber.BigNumber): Promise<void> { order: Order, takerTokenCancelAmount: BigNumber.BigNumber): Promise<void> {
if (takerTokenCancelAmount.eq(0)) { if (takerTokenCancelAmount.eq(0)) {
throw new Error(ExchangeContractErrs.ORDER_CANCEL_AMOUNT_ZERO); throw new Error(ExchangeContractErrs.ORDER_CANCEL_AMOUNT_ZERO);
} }
const orderHash = await this.getOrderHashHexAsync(order); const orderHash = await this._getOrderHashHexAsync(order);
const unavailableAmount = await this.getUnavailableTakerAmountAsync(orderHash); const unavailableAmount = await this.getUnavailableTakerAmountAsync(orderHash);
if (order.takerTokenAmount.minus(unavailableAmount).eq(0)) { if (order.takerTokenAmount.minus(unavailableAmount).eq(0)) {
throw new Error(ExchangeContractErrs.ORDER_ALREADY_CANCELLED_OR_FILLED); throw new Error(ExchangeContractErrs.ORDER_ALREADY_CANCELLED_OR_FILLED);
@@ -609,9 +609,9 @@ export class ExchangeWrapper extends ContractWrapper {
throw new Error(ExchangeContractErrs.ORDER_CANCEL_EXPIRED); throw new Error(ExchangeContractErrs.ORDER_CANCEL_EXPIRED);
} }
} }
private async validateFillOrKillOrderAndThrowIfInvalidAsync(signedOrder: SignedOrder, private async _validateFillOrKillOrderAndThrowIfInvalidAsync(signedOrder: SignedOrder,
exchangeAddress: string, exchangeAddress: string,
fillTakerAmount: BigNumber.BigNumber) { fillTakerAmount: BigNumber.BigNumber) {
// Check that fillValue available >= fillTakerAmount // Check that fillValue available >= fillTakerAmount
const orderHashHex = utils.getOrderHashHex(signedOrder, exchangeAddress); const orderHashHex = utils.getOrderHashHex(signedOrder, exchangeAddress);
const unavailableTakerAmount = await this.getUnavailableTakerAmountAsync(orderHashHex); const unavailableTakerAmount = await this.getUnavailableTakerAmountAsync(orderHashHex);
@@ -629,17 +629,18 @@ export class ExchangeWrapper extends ContractWrapper {
* TODO: Throw errors before calling the smart contract for these edge-cases in order to minimize * TODO: Throw errors before calling the smart contract for these edge-cases in order to minimize
* the callers gas costs. * the callers gas costs.
*/ */
private async validateFillOrderBalancesAndAllowancesAndThrowIfInvalidAsync(signedOrder: SignedOrder, private async _validateFillOrderBalancesAndAllowancesAndThrowIfInvalidAsync(signedOrder: SignedOrder,
fillTakerAmount: BigNumber.BigNumber, fillTakerAmount: BigNumber.BigNumber,
senderAddress: string, senderAddress: string,
zrxTokenAddress: string): Promise<void> { zrxTokenAddress: string,
): Promise<void> {
const makerBalance = await this.tokenWrapper.getBalanceAsync(signedOrder.makerTokenAddress, const makerBalance = await this._tokenWrapper.getBalanceAsync(signedOrder.makerTokenAddress,
signedOrder.maker); signedOrder.maker);
const takerBalance = await this.tokenWrapper.getBalanceAsync(signedOrder.takerTokenAddress, senderAddress); const takerBalance = await this._tokenWrapper.getBalanceAsync(signedOrder.takerTokenAddress, senderAddress);
const makerAllowance = await this.tokenWrapper.getProxyAllowanceAsync(signedOrder.makerTokenAddress, const makerAllowance = await this._tokenWrapper.getProxyAllowanceAsync(signedOrder.makerTokenAddress,
signedOrder.maker); signedOrder.maker);
const takerAllowance = await this.tokenWrapper.getProxyAllowanceAsync(signedOrder.takerTokenAddress, const takerAllowance = await this._tokenWrapper.getProxyAllowanceAsync(signedOrder.takerTokenAddress,
senderAddress); senderAddress);
// exchangeRate is the price of one maker token denominated in taker tokens // exchangeRate is the price of one maker token denominated in taker tokens
@@ -659,12 +660,12 @@ export class ExchangeWrapper extends ContractWrapper {
throw new Error(ExchangeContractErrs.INSUFFICIENT_MAKER_ALLOWANCE); throw new Error(ExchangeContractErrs.INSUFFICIENT_MAKER_ALLOWANCE);
} }
const makerFeeBalance = await this.tokenWrapper.getBalanceAsync(zrxTokenAddress, const makerFeeBalance = await this._tokenWrapper.getBalanceAsync(zrxTokenAddress,
signedOrder.maker); signedOrder.maker);
const takerFeeBalance = await this.tokenWrapper.getBalanceAsync(zrxTokenAddress, senderAddress); const takerFeeBalance = await this._tokenWrapper.getBalanceAsync(zrxTokenAddress, senderAddress);
const makerFeeAllowance = await this.tokenWrapper.getProxyAllowanceAsync(zrxTokenAddress, const makerFeeAllowance = await this._tokenWrapper.getProxyAllowanceAsync(zrxTokenAddress,
signedOrder.maker); signedOrder.maker);
const takerFeeAllowance = await this.tokenWrapper.getProxyAllowanceAsync(zrxTokenAddress, const takerFeeAllowance = await this._tokenWrapper.getProxyAllowanceAsync(zrxTokenAddress,
senderAddress); senderAddress);
if (signedOrder.takerFee.greaterThan(takerFeeBalance)) { if (signedOrder.takerFee.greaterThan(takerFeeBalance)) {
@@ -680,34 +681,34 @@ export class ExchangeWrapper extends ContractWrapper {
throw new Error(ExchangeContractErrs.INSUFFICIENT_MAKER_FEE_ALLOWANCE); throw new Error(ExchangeContractErrs.INSUFFICIENT_MAKER_FEE_ALLOWANCE);
} }
} }
private throwErrorLogsAsErrors(logs: ContractEvent[]): void { private _throwErrorLogsAsErrors(logs: ContractEvent[]): void {
const errEvent = _.find(logs, {event: 'LogError'}); const errEvent = _.find(logs, {event: 'LogError'});
if (!_.isUndefined(errEvent)) { if (!_.isUndefined(errEvent)) {
const errCode = errEvent.args.errorId.toNumber(); const errCode = errEvent.args.errorId.toNumber();
const errMessage = this.exchangeContractErrCodesToMsg[errCode]; const errMessage = this._exchangeContractErrCodesToMsg[errCode];
throw new Error(errMessage); throw new Error(errMessage);
} }
} }
private async isRoundingErrorAsync(takerTokenAmount: BigNumber.BigNumber, private async _isRoundingErrorAsync(takerTokenAmount: BigNumber.BigNumber,
fillTakerAmount: BigNumber.BigNumber, fillTakerAmount: BigNumber.BigNumber,
makerTokenAmount: BigNumber.BigNumber): Promise<boolean> { makerTokenAmount: BigNumber.BigNumber): Promise<boolean> {
await assert.isUserAddressAvailableAsync(this.web3Wrapper); await assert.isUserAddressAvailableAsync(this.web3Wrapper);
const exchangeInstance = await this.getExchangeContractAsync(); const exchangeInstance = await this._getExchangeContractAsync();
const isRoundingError = await exchangeInstance.isRoundingError.call( const isRoundingError = await exchangeInstance.isRoundingError.call(
takerTokenAmount, fillTakerAmount, makerTokenAmount, takerTokenAmount, fillTakerAmount, makerTokenAmount,
); );
return isRoundingError; return isRoundingError;
} }
private async getExchangeContractAsync(): Promise<ExchangeContract> { private async _getExchangeContractAsync(): Promise<ExchangeContract> {
if (!_.isUndefined(this.exchangeContractIfExists)) { if (!_.isUndefined(this._exchangeContractIfExists)) {
return this.exchangeContractIfExists; return this._exchangeContractIfExists;
} }
const contractInstance = await this.instantiateContractIfExistsAsync((ExchangeArtifacts as any)); const contractInstance = await this._instantiateContractIfExistsAsync((ExchangeArtifacts as any));
this.exchangeContractIfExists = contractInstance as ExchangeContract; this._exchangeContractIfExists = contractInstance as ExchangeContract;
return this.exchangeContractIfExists; return this._exchangeContractIfExists;
} }
private async getZRXTokenAddressAsync(): Promise<string> { private async _getZRXTokenAddressAsync(): Promise<string> {
const exchangeInstance = await this.getExchangeContractAsync(); const exchangeInstance = await this._getExchangeContractAsync();
return exchangeInstance.ZRX.call(); return exchangeInstance.ZRX.call();
} }
} }

View File

@@ -6,19 +6,19 @@ import {ContractWrapper} from './contract_wrapper';
import * as TokenRegistryArtifacts from '../artifacts/TokenRegistry.json'; import * as TokenRegistryArtifacts from '../artifacts/TokenRegistry.json';
export class TokenRegistryWrapper extends ContractWrapper { export class TokenRegistryWrapper extends ContractWrapper {
private tokenRegistryContractIfExists?: TokenRegistryContract; private _tokenRegistryContractIfExists?: TokenRegistryContract;
constructor(web3Wrapper: Web3Wrapper) { constructor(web3Wrapper: Web3Wrapper) {
super(web3Wrapper); super(web3Wrapper);
} }
public invalidateContractInstance(): void { public invalidateContractInstance(): void {
delete this.tokenRegistryContractIfExists; delete this._tokenRegistryContractIfExists;
} }
/** /**
* Retrieves all the tokens currently listed in the Token Registry smart contract * Retrieves all the tokens currently listed in the Token Registry smart contract
* @return An array of JS objects that conform to the Token interface. * @return An array of JS objects that conform to the Token interface.
*/ */
public async getTokensAsync(): Promise<Token[]> { public async getTokensAsync(): Promise<Token[]> {
const tokenRegistryContract = await this.getTokenRegistryContractAsync(); const tokenRegistryContract = await this._getTokenRegistryContractAsync();
const addresses = await tokenRegistryContract.getTokenAddresses.call(); const addresses = await tokenRegistryContract.getTokenAddresses.call();
const tokenMetadataPromises: Array<Promise<TokenMetadata>> = _.map( const tokenMetadataPromises: Array<Promise<TokenMetadata>> = _.map(
@@ -37,12 +37,12 @@ export class TokenRegistryWrapper extends ContractWrapper {
}); });
return tokens; return tokens;
} }
private async getTokenRegistryContractAsync(): Promise<TokenRegistryContract> { private async _getTokenRegistryContractAsync(): Promise<TokenRegistryContract> {
if (!_.isUndefined(this.tokenRegistryContractIfExists)) { if (!_.isUndefined(this._tokenRegistryContractIfExists)) {
return this.tokenRegistryContractIfExists; return this._tokenRegistryContractIfExists;
} }
const contractInstance = await this.instantiateContractIfExistsAsync((TokenRegistryArtifacts as any)); const contractInstance = await this._instantiateContractIfExistsAsync((TokenRegistryArtifacts as any));
this.tokenRegistryContractIfExists = contractInstance as TokenRegistryContract; this._tokenRegistryContractIfExists = contractInstance as TokenRegistryContract;
return this.tokenRegistryContractIfExists; return this._tokenRegistryContractIfExists;
} }
} }

View File

@@ -11,13 +11,13 @@ import {TokenContract, ZeroExError} from '../types';
const ALLOWANCE_TO_ZERO_GAS_AMOUNT = 45730; const ALLOWANCE_TO_ZERO_GAS_AMOUNT = 45730;
export class TokenWrapper extends ContractWrapper { export class TokenWrapper extends ContractWrapper {
private tokenContractsByAddress: {[address: string]: TokenContract}; private _tokenContractsByAddress: {[address: string]: TokenContract};
constructor(web3Wrapper: Web3Wrapper) { constructor(web3Wrapper: Web3Wrapper) {
super(web3Wrapper); super(web3Wrapper);
this.tokenContractsByAddress = {}; this._tokenContractsByAddress = {};
} }
public invalidateContractInstances() { public invalidateContractInstances() {
this.tokenContractsByAddress = {}; this._tokenContractsByAddress = {};
} }
/** /**
* Retrieves an owner's ERC20 token balance. * Retrieves an owner's ERC20 token balance.
@@ -29,7 +29,7 @@ export class TokenWrapper extends ContractWrapper {
assert.isETHAddressHex('tokenAddress', tokenAddress); assert.isETHAddressHex('tokenAddress', tokenAddress);
await assert.isUserAddressAvailableAsync(this.web3Wrapper); await assert.isUserAddressAvailableAsync(this.web3Wrapper);
const tokenContract = await this.getTokenContractAsync(tokenAddress); const tokenContract = await this._getTokenContractAsync(tokenAddress);
let balance = await tokenContract.balanceOf.call(ownerAddress); let balance = await tokenContract.balanceOf.call(ownerAddress);
// Wrap BigNumbers returned from web3 with our own (later) version of BigNumber // Wrap BigNumbers returned from web3 with our own (later) version of BigNumber
balance = new BigNumber(balance); balance = new BigNumber(balance);
@@ -51,7 +51,7 @@ export class TokenWrapper extends ContractWrapper {
assert.isETHAddressHex('tokenAddress', tokenAddress); assert.isETHAddressHex('tokenAddress', tokenAddress);
assert.isBigNumber('amountInBaseUnits', amountInBaseUnits); assert.isBigNumber('amountInBaseUnits', amountInBaseUnits);
const tokenContract = await this.getTokenContractAsync(tokenAddress); const tokenContract = await this._getTokenContractAsync(tokenAddress);
// Hack: for some reason default estimated gas amount causes `base fee exceeds gas limit` exception // Hack: for some reason default estimated gas amount causes `base fee exceeds gas limit` exception
// on testrpc. Probably related to https://github.com/ethereumjs/testrpc/issues/294 // on testrpc. Probably related to https://github.com/ethereumjs/testrpc/issues/294
// TODO: Debug issue in testrpc and submit a PR, then remove this hack // TODO: Debug issue in testrpc and submit a PR, then remove this hack
@@ -74,7 +74,7 @@ export class TokenWrapper extends ContractWrapper {
assert.isETHAddressHex('tokenAddress', tokenAddress); assert.isETHAddressHex('tokenAddress', tokenAddress);
await assert.isUserAddressAvailableAsync(this.web3Wrapper); await assert.isUserAddressAvailableAsync(this.web3Wrapper);
const tokenContract = await this.getTokenContractAsync(tokenAddress); const tokenContract = await this._getTokenContractAsync(tokenAddress);
let allowanceInBaseUnits = await tokenContract.allowance.call(ownerAddress, spenderAddress); let allowanceInBaseUnits = await tokenContract.allowance.call(ownerAddress, spenderAddress);
// Wrap BigNumbers returned from web3 with our own (later) version of BigNumber // Wrap BigNumbers returned from web3 with our own (later) version of BigNumber
allowanceInBaseUnits = new BigNumber(allowanceInBaseUnits); allowanceInBaseUnits = new BigNumber(allowanceInBaseUnits);
@@ -89,7 +89,7 @@ export class TokenWrapper extends ContractWrapper {
assert.isETHAddressHex('ownerAddress', ownerAddress); assert.isETHAddressHex('ownerAddress', ownerAddress);
assert.isETHAddressHex('tokenAddress', tokenAddress); assert.isETHAddressHex('tokenAddress', tokenAddress);
const proxyAddress = await this.getProxyAddressAsync(); const proxyAddress = await this._getProxyAddressAsync();
const allowanceInBaseUnits = await this.getAllowanceAsync(tokenAddress, ownerAddress, proxyAddress); const allowanceInBaseUnits = await this.getAllowanceAsync(tokenAddress, ownerAddress, proxyAddress);
return allowanceInBaseUnits; return allowanceInBaseUnits;
} }
@@ -107,7 +107,7 @@ export class TokenWrapper extends ContractWrapper {
assert.isETHAddressHex('tokenAddress', tokenAddress); assert.isETHAddressHex('tokenAddress', tokenAddress);
assert.isBigNumber('amountInBaseUnits', amountInBaseUnits); assert.isBigNumber('amountInBaseUnits', amountInBaseUnits);
const proxyAddress = await this.getProxyAddressAsync(); const proxyAddress = await this._getProxyAddressAsync();
await this.setAllowanceAsync(tokenAddress, ownerAddress, proxyAddress, amountInBaseUnits); await this.setAllowanceAsync(tokenAddress, ownerAddress, proxyAddress, amountInBaseUnits);
} }
/** /**
@@ -124,7 +124,7 @@ export class TokenWrapper extends ContractWrapper {
assert.isETHAddressHex('toAddress', toAddress); assert.isETHAddressHex('toAddress', toAddress);
assert.isBigNumber('amountInBaseUnits', amountInBaseUnits); assert.isBigNumber('amountInBaseUnits', amountInBaseUnits);
const tokenContract = await this.getTokenContractAsync(tokenAddress); const tokenContract = await this._getTokenContractAsync(tokenAddress);
const fromAddressBalance = await this.getBalanceAsync(tokenAddress, fromAddress); const fromAddressBalance = await this.getBalanceAsync(tokenAddress, fromAddress);
if (fromAddressBalance.lessThan(amountInBaseUnits)) { if (fromAddressBalance.lessThan(amountInBaseUnits)) {
@@ -155,7 +155,7 @@ export class TokenWrapper extends ContractWrapper {
await assert.isSenderAddressAsync('senderAddress', senderAddress, this.web3Wrapper); await assert.isSenderAddressAsync('senderAddress', senderAddress, this.web3Wrapper);
assert.isBigNumber('amountInBaseUnits', amountInBaseUnits); assert.isBigNumber('amountInBaseUnits', amountInBaseUnits);
const tokenContract = await this.getTokenContractAsync(tokenAddress); const tokenContract = await this._getTokenContractAsync(tokenAddress);
const fromAddressAllowance = await this.getAllowanceAsync(tokenAddress, fromAddress, senderAddress); const fromAddressAllowance = await this.getAllowanceAsync(tokenAddress, fromAddress, senderAddress);
if (fromAddressAllowance.lessThan(amountInBaseUnits)) { if (fromAddressAllowance.lessThan(amountInBaseUnits)) {
@@ -171,17 +171,17 @@ export class TokenWrapper extends ContractWrapper {
from: senderAddress, from: senderAddress,
}); });
} }
private async getTokenContractAsync(tokenAddress: string): Promise<TokenContract> { private async _getTokenContractAsync(tokenAddress: string): Promise<TokenContract> {
let tokenContract = this.tokenContractsByAddress[tokenAddress]; let tokenContract = this._tokenContractsByAddress[tokenAddress];
if (!_.isUndefined(tokenContract)) { if (!_.isUndefined(tokenContract)) {
return tokenContract; return tokenContract;
} }
const contractInstance = await this.instantiateContractIfExistsAsync((TokenArtifacts as any), tokenAddress); const contractInstance = await this._instantiateContractIfExistsAsync((TokenArtifacts as any), tokenAddress);
tokenContract = contractInstance as TokenContract; tokenContract = contractInstance as TokenContract;
this.tokenContractsByAddress[tokenAddress] = tokenContract; this._tokenContractsByAddress[tokenAddress] = tokenContract;
return tokenContract; return tokenContract;
} }
private async getProxyAddressAsync() { private async _getProxyAddressAsync() {
const networkIdIfExists = await this.web3Wrapper.getNetworkIdIfExistsAsync(); const networkIdIfExists = await this.web3Wrapper.getNetworkIdIfExistsAsync();
const proxyNetworkConfigsIfExists = _.isUndefined(networkIdIfExists) ? const proxyNetworkConfigsIfExists = _.isUndefined(networkIdIfExists) ?
undefined : undefined :

View File

@@ -19,8 +19,8 @@ describe('ZeroEx library', () => {
const web3 = web3Factory.create(); const web3 = web3Factory.create();
const zeroEx = new ZeroEx(web3); const zeroEx = new ZeroEx(web3);
// 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();
expect((zeroEx.exchange as any).exchangeContractIfExists).to.not.be.undefined(); expect((zeroEx.exchange as any).exchangeContractIfExists).to.not.be.undefined();
expect((zeroEx.tokenRegistry as any).tokenRegistryContractIfExists).to.not.be.undefined(); expect((zeroEx.tokenRegistry as any).tokenRegistryContractIfExists).to.not.be.undefined();
@@ -57,14 +57,14 @@ describe('ZeroEx library', () => {
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(
(zeroEx.exchange as any).isValidSignatureUsingContractCallAsync('0x0', signature, address), (zeroEx.exchange as any)._isValidSignatureUsingContractCallAsync('0x0', signature, address),
).to.become(false); ).to.become(false);
}); });
it('should return false if the address doesn\'t pertain to the signature & data', async () => { it('should return false if the address doesn\'t pertain to the signature & data', async () => {
const validUnrelatedAddress = '0x8b0292B11a196601eD2ce54B665CaFEca0347D42'; const validUnrelatedAddress = '0x8b0292B11a196601eD2ce54B665CaFEca0347D42';
expect(ZeroEx.isValidSignature(dataHex, signature, validUnrelatedAddress)).to.be.false(); expect(ZeroEx.isValidSignature(dataHex, signature, validUnrelatedAddress)).to.be.false();
return expect( return expect(
(zeroEx.exchange as any).isValidSignatureUsingContractCallAsync(dataHex, signature, (zeroEx.exchange as any)._isValidSignatureUsingContractCallAsync(dataHex, signature,
validUnrelatedAddress), validUnrelatedAddress),
).to.become(false); ).to.become(false);
}); });
@@ -72,14 +72,14 @@ describe('ZeroEx library', () => {
const wrongSignature = _.assign({}, signature, {v: 28}); const wrongSignature = _.assign({}, signature, {v: 28});
expect(ZeroEx.isValidSignature(dataHex, wrongSignature, address)).to.be.false(); expect(ZeroEx.isValidSignature(dataHex, wrongSignature, address)).to.be.false();
return expect( return expect(
(zeroEx.exchange as any).isValidSignatureUsingContractCallAsync(dataHex, wrongSignature, address), (zeroEx.exchange as any)._isValidSignatureUsingContractCallAsync(dataHex, wrongSignature, address),
).to.become(false); ).to.become(false);
}); });
it('should return true if the signature does pertain to the dataHex & address', async () => { it('should return true if the signature does pertain to the dataHex & address', async () => {
const isValidSignatureLocal = ZeroEx.isValidSignature(dataHex, signature, address); const isValidSignatureLocal = ZeroEx.isValidSignature(dataHex, signature, address);
expect(isValidSignatureLocal).to.be.true(); expect(isValidSignatureLocal).to.be.true();
const isValidSignatureOnContract = await (zeroEx.exchange as any) const isValidSignatureOnContract = await (zeroEx.exchange as any)
.isValidSignatureUsingContractCallAsync(dataHex, signature, address); ._isValidSignatureUsingContractCallAsync(dataHex, signature, address);
return expect(isValidSignatureOnContract).to.be.true(); return expect(isValidSignatureOnContract).to.be.true();
}); });
}); });

View File

@@ -618,7 +618,7 @@ describe('ExchangeWrapper', () => {
); );
}); });
afterEach(async () => { afterEach(async () => {
await (zeroEx.exchange as any).stopWatchingExchangeLogEventsAsync(); await (zeroEx.exchange as any)._stopWatchingExchangeLogEventsAsync();
}); });
// Hack: Mocha does not allow a test to be both async and have a `done` callback // Hack: Mocha does not allow a test to be both async and have a `done` callback
// Since we need to await the receipt of the event in the `subscribeAsync` callback, // Since we need to await the receipt of the event in the `subscribeAsync` callback,
@@ -705,7 +705,7 @@ describe('ExchangeWrapper', () => {
); );
const orderHash = await zeroEx.getOrderHashHexAsync(signedOrder); const orderHash = await zeroEx.getOrderHashHexAsync(signedOrder);
const orderHashFromContract = await (zeroEx.exchange as any) const orderHashFromContract = await (zeroEx.exchange as any)
.getOrderHashHexUsingContractCallAsync(signedOrder); ._getOrderHashHexUsingContractCallAsync(signedOrder);
expect(orderHash).to.equal(orderHashFromContract); expect(orderHash).to.equal(orderHashFromContract);
}); });
}); });