aboutsummaryrefslogtreecommitdiffstats
path: root/packages/0x.js/src/utils
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2017-12-22 04:24:54 +0800
committerFabio Berger <me@fabioberger.com>2017-12-22 04:24:54 +0800
commite01c0f054d2dbb043aec8b4cb8e1c47f83bd5eb9 (patch)
tree241b630db5044974cc17130f149ca64728d9c619 /packages/0x.js/src/utils
parentd725de72861c6a6218c7f4822a339175a2da7403 (diff)
parentcb3582289ff94857d5956bbd71dbf68ee3f42ecf (diff)
downloaddexon-0x-contracts-e01c0f054d2dbb043aec8b4cb8e1c47f83bd5eb9.tar.gz
dexon-0x-contracts-e01c0f054d2dbb043aec8b4cb8e1c47f83bd5eb9.tar.zst
dexon-0x-contracts-e01c0f054d2dbb043aec8b4cb8e1c47f83bd5eb9.zip
Merge branch 'development' into fix/docLinks
* development: Update and standardize contracts README Add to CHANGELOG Refactor toBaseUnitAmount so that it throws if user supplies unitAmount with too many decimals Make assertion stricter so that one cannot submit invalid baseUnit amounts to `toUnitAmount` Add some missed underscores, update changelog and comments Add new underscore-privates rule to @0xproject/tslint-config and fix lint errors # Conflicts: # packages/website/ts/pages/documentation/documentation.tsx # packages/website/ts/pages/shared/nested_sidebar_menu.tsx
Diffstat (limited to 'packages/0x.js/src/utils')
-rw-r--r--packages/0x.js/src/utils/abi_decoder.ts18
-rw-r--r--packages/0x.js/src/utils/exchange_transfer_simulator.ts52
-rw-r--r--packages/0x.js/src/utils/order_state_utils.ts26
-rw-r--r--packages/0x.js/src/utils/order_validation_utils.ts36
4 files changed, 66 insertions, 66 deletions
diff --git a/packages/0x.js/src/utils/abi_decoder.ts b/packages/0x.js/src/utils/abi_decoder.ts
index f26b057f0..6d15f1d6f 100644
--- a/packages/0x.js/src/utils/abi_decoder.ts
+++ b/packages/0x.js/src/utils/abi_decoder.ts
@@ -6,9 +6,9 @@ import * as SolidityCoder from 'web3/lib/solidity/coder';
import {AbiType, ContractEventArgs, DecodedLogArgs, LogWithDecodedArgs, RawLog, SolidityTypes} from '../types';
export class AbiDecoder {
- private savedABIs: Web3.AbiDefinition[] = [];
- private methodIds: {[signatureHash: string]: Web3.EventAbi} = {};
- private static padZeros(address: string) {
+ private _savedABIs: Web3.AbiDefinition[] = [];
+ private _methodIds: {[signatureHash: string]: Web3.EventAbi} = {};
+ private static _padZeros(address: string) {
let formatted = address;
if (_.startsWith(formatted, '0x')) {
formatted = formatted.slice(2);
@@ -18,13 +18,13 @@ export class AbiDecoder {
return `0x${formatted}`;
}
constructor(abiArrays: Web3.AbiDefinition[][]) {
- _.map(abiArrays, this.addABI.bind(this));
+ _.map(abiArrays, this._addABI.bind(this));
}
// This method can only decode logs from the 0x & ERC20 smart contracts
public tryToDecodeLogOrNoop<ArgsType extends ContractEventArgs>(
log: Web3.LogEntry): LogWithDecodedArgs<ArgsType>|RawLog {
const methodId = log.topics[0];
- const event = this.methodIds[methodId];
+ const event = this._methodIds[methodId];
if (_.isUndefined(event)) {
return log;
}
@@ -41,7 +41,7 @@ export class AbiDecoder {
// Indexed parameters are stored in topics. Non-indexed ones in decodedData
let value = param.indexed ? log.topics[topicsIndex++] : decodedData[dataIndex++];
if (param.type === SolidityTypes.Address) {
- value = AbiDecoder.padZeros(new BigNumber(value).toString(16));
+ value = AbiDecoder._padZeros(new BigNumber(value).toString(16));
} else if (param.type === SolidityTypes.Uint256 ||
param.type === SolidityTypes.Uint8 ||
param.type === SolidityTypes.Uint) {
@@ -56,14 +56,14 @@ export class AbiDecoder {
args: decodedParams,
};
}
- private addABI(abiArray: Web3.AbiDefinition[]): void {
+ private _addABI(abiArray: Web3.AbiDefinition[]): void {
_.map(abiArray, (abi: Web3.AbiDefinition) => {
if (abi.type === AbiType.Event) {
const signature = `${abi.name}(${_.map(abi.inputs, input => input.type).join(',')})`;
const signatureHash = new Web3().sha3(signature);
- this.methodIds[signatureHash] = abi;
+ this._methodIds[signatureHash] = abi;
}
});
- this.savedABIs = this.savedABIs.concat(abiArray);
+ this._savedABIs = this._savedABIs.concat(abiArray);
}
}
diff --git a/packages/0x.js/src/utils/exchange_transfer_simulator.ts b/packages/0x.js/src/utils/exchange_transfer_simulator.ts
index 2574bd9ac..8143112aa 100644
--- a/packages/0x.js/src/utils/exchange_transfer_simulator.ts
+++ b/packages/0x.js/src/utils/exchange_transfer_simulator.ts
@@ -34,16 +34,16 @@ const ERR_MSG_MAPPING = {
};
export class ExchangeTransferSimulator {
- private store: BalanceAndProxyAllowanceLazyStore;
- private UNLIMITED_ALLOWANCE_IN_BASE_UNITS: BigNumber;
- private static throwValidationError(failureReason: FailureReason, tradeSide: TradeSide,
- transferType: TransferType): never {
+ private _store: BalanceAndProxyAllowanceLazyStore;
+ private _UNLIMITED_ALLOWANCE_IN_BASE_UNITS: BigNumber;
+ private static _throwValidationError(failureReason: FailureReason, tradeSide: TradeSide,
+ transferType: TransferType): never {
const errMsg = ERR_MSG_MAPPING[failureReason][tradeSide][transferType];
throw new Error(errMsg);
}
constructor(token: TokenWrapper, defaultBlock: BlockParamLiteral) {
- this.store = new BalanceAndProxyAllowanceLazyStore(token, defaultBlock);
- this.UNLIMITED_ALLOWANCE_IN_BASE_UNITS = token.UNLIMITED_ALLOWANCE_IN_BASE_UNITS;
+ this._store = new BalanceAndProxyAllowanceLazyStore(token, defaultBlock);
+ this._UNLIMITED_ALLOWANCE_IN_BASE_UNITS = token.UNLIMITED_ALLOWANCE_IN_BASE_UNITS;
}
/**
* Simulates transferFrom call performed by a proxy
@@ -57,33 +57,33 @@ export class ExchangeTransferSimulator {
public async transferFromAsync(tokenAddress: string, from: string, to: string,
amountInBaseUnits: BigNumber, tradeSide: TradeSide,
transferType: TransferType): Promise<void> {
- const balance = await this.store.getBalanceAsync(tokenAddress, from);
- const proxyAllowance = await this.store.getProxyAllowanceAsync(tokenAddress, from);
+ const balance = await this._store.getBalanceAsync(tokenAddress, from);
+ const proxyAllowance = await this._store.getProxyAllowanceAsync(tokenAddress, from);
if (proxyAllowance.lessThan(amountInBaseUnits)) {
- ExchangeTransferSimulator.throwValidationError(FailureReason.ProxyAllowance, tradeSide, transferType);
+ ExchangeTransferSimulator._throwValidationError(FailureReason.ProxyAllowance, tradeSide, transferType);
}
if (balance.lessThan(amountInBaseUnits)) {
- ExchangeTransferSimulator.throwValidationError(FailureReason.Balance, tradeSide, transferType);
+ ExchangeTransferSimulator._throwValidationError(FailureReason.Balance, tradeSide, transferType);
}
- await this.decreaseProxyAllowanceAsync(tokenAddress, from, amountInBaseUnits);
- await this.decreaseBalanceAsync(tokenAddress, from, amountInBaseUnits);
- await this.increaseBalanceAsync(tokenAddress, to, amountInBaseUnits);
+ await this._decreaseProxyAllowanceAsync(tokenAddress, from, amountInBaseUnits);
+ await this._decreaseBalanceAsync(tokenAddress, from, amountInBaseUnits);
+ await this._increaseBalanceAsync(tokenAddress, to, amountInBaseUnits);
}
- private async decreaseProxyAllowanceAsync(tokenAddress: string, userAddress: string,
- amountInBaseUnits: BigNumber): Promise<void> {
- const proxyAllowance = await this.store.getProxyAllowanceAsync(tokenAddress, userAddress);
- if (!proxyAllowance.eq(this.UNLIMITED_ALLOWANCE_IN_BASE_UNITS)) {
- this.store.setProxyAllowance(tokenAddress, userAddress, proxyAllowance.minus(amountInBaseUnits));
+ private async _decreaseProxyAllowanceAsync(tokenAddress: string, userAddress: string,
+ amountInBaseUnits: BigNumber): Promise<void> {
+ const proxyAllowance = await this._store.getProxyAllowanceAsync(tokenAddress, userAddress);
+ if (!proxyAllowance.eq(this._UNLIMITED_ALLOWANCE_IN_BASE_UNITS)) {
+ this._store.setProxyAllowance(tokenAddress, userAddress, proxyAllowance.minus(amountInBaseUnits));
}
}
- private async increaseBalanceAsync(tokenAddress: string, userAddress: string,
- amountInBaseUnits: BigNumber): Promise<void> {
- const balance = await this.store.getBalanceAsync(tokenAddress, userAddress);
- this.store.setBalance(tokenAddress, userAddress, balance.plus(amountInBaseUnits));
+ private async _increaseBalanceAsync(tokenAddress: string, userAddress: string,
+ amountInBaseUnits: BigNumber): Promise<void> {
+ const balance = await this._store.getBalanceAsync(tokenAddress, userAddress);
+ this._store.setBalance(tokenAddress, userAddress, balance.plus(amountInBaseUnits));
}
- private async decreaseBalanceAsync(tokenAddress: string, userAddress: string,
- amountInBaseUnits: BigNumber): Promise<void> {
- const balance = await this.store.getBalanceAsync(tokenAddress, userAddress);
- this.store.setBalance(tokenAddress, userAddress, balance.minus(amountInBaseUnits));
+ private async _decreaseBalanceAsync(tokenAddress: string, userAddress: string,
+ amountInBaseUnits: BigNumber): Promise<void> {
+ const balance = await this._store.getBalanceAsync(tokenAddress, userAddress);
+ this._store.setBalance(tokenAddress, userAddress, balance.minus(amountInBaseUnits));
}
}
diff --git a/packages/0x.js/src/utils/order_state_utils.ts b/packages/0x.js/src/utils/order_state_utils.ts
index a0985d028..981f9e96c 100644
--- a/packages/0x.js/src/utils/order_state_utils.ts
+++ b/packages/0x.js/src/utils/order_state_utils.ts
@@ -18,9 +18,9 @@ import {
const ACCEPTABLE_RELATIVE_ROUNDING_ERROR = 0.0001;
export class OrderStateUtils {
- private balanceAndProxyAllowanceLazyStore: BalanceAndProxyAllowanceLazyStore;
- private orderFilledCancelledLazyStore: OrderFilledCancelledLazyStore;
- private static validateIfOrderIsValid(signedOrder: SignedOrder, orderRelevantState: OrderRelevantState): void {
+ private _balanceAndProxyAllowanceLazyStore: BalanceAndProxyAllowanceLazyStore;
+ private _orderFilledCancelledLazyStore: OrderFilledCancelledLazyStore;
+ private static _validateIfOrderIsValid(signedOrder: SignedOrder, orderRelevantState: OrderRelevantState): void {
const unavailableTakerTokenAmount = orderRelevantState.cancelledTakerTokenAmount.add(
orderRelevantState.filledTakerTokenAmount,
);
@@ -53,14 +53,14 @@ export class OrderStateUtils {
}
constructor(balanceAndProxyAllowanceLazyStore: BalanceAndProxyAllowanceLazyStore,
orderFilledCancelledLazyStore: OrderFilledCancelledLazyStore) {
- this.balanceAndProxyAllowanceLazyStore = balanceAndProxyAllowanceLazyStore;
- this.orderFilledCancelledLazyStore = orderFilledCancelledLazyStore;
+ this._balanceAndProxyAllowanceLazyStore = balanceAndProxyAllowanceLazyStore;
+ this._orderFilledCancelledLazyStore = orderFilledCancelledLazyStore;
}
public async getOrderStateAsync(signedOrder: SignedOrder): Promise<OrderState> {
const orderRelevantState = await this.getOrderRelevantStateAsync(signedOrder);
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
try {
- OrderStateUtils.validateIfOrderIsValid(signedOrder, orderRelevantState);
+ OrderStateUtils._validateIfOrderIsValid(signedOrder, orderRelevantState);
const orderState: OrderStateValid = {
isValid: true,
orderHash,
@@ -81,23 +81,23 @@ export class OrderStateUtils {
// If we pass it from the instantiator - there is no opportunity to get it there
// because JS doesn't support async constructors.
// Moreover - it's cached under the hood so it's equivalent to an async constructor.
- const exchange = (this.orderFilledCancelledLazyStore as any).exchange as ExchangeWrapper;
+ const exchange = (this._orderFilledCancelledLazyStore as any)._exchange as ExchangeWrapper;
const zrxTokenAddress = exchange.getZRXTokenAddress();
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
- const makerBalance = await this.balanceAndProxyAllowanceLazyStore.getBalanceAsync(
+ const makerBalance = await this._balanceAndProxyAllowanceLazyStore.getBalanceAsync(
signedOrder.makerTokenAddress, signedOrder.maker,
);
- const makerProxyAllowance = await this.balanceAndProxyAllowanceLazyStore.getProxyAllowanceAsync(
+ const makerProxyAllowance = await this._balanceAndProxyAllowanceLazyStore.getProxyAllowanceAsync(
signedOrder.makerTokenAddress, signedOrder.maker,
);
- const makerFeeBalance = await this.balanceAndProxyAllowanceLazyStore.getBalanceAsync(
+ const makerFeeBalance = await this._balanceAndProxyAllowanceLazyStore.getBalanceAsync(
zrxTokenAddress, signedOrder.maker,
);
- const makerFeeProxyAllowance = await this.balanceAndProxyAllowanceLazyStore.getProxyAllowanceAsync(
+ const makerFeeProxyAllowance = await this._balanceAndProxyAllowanceLazyStore.getProxyAllowanceAsync(
zrxTokenAddress, signedOrder.maker,
);
- const filledTakerTokenAmount = await this.orderFilledCancelledLazyStore.getFilledTakerAmountAsync(orderHash);
- const cancelledTakerTokenAmount = await this.orderFilledCancelledLazyStore.getCancelledTakerAmountAsync(
+ const filledTakerTokenAmount = await this._orderFilledCancelledLazyStore.getFilledTakerAmountAsync(orderHash);
+ const cancelledTakerTokenAmount = await this._orderFilledCancelledLazyStore.getCancelledTakerAmountAsync(
orderHash,
);
const unavailableTakerTokenAmount = await exchange.getUnavailableTakerAmountAsync(orderHash);
diff --git a/packages/0x.js/src/utils/order_validation_utils.ts b/packages/0x.js/src/utils/order_validation_utils.ts
index 9185eef2b..ad82d85b4 100644
--- a/packages/0x.js/src/utils/order_validation_utils.ts
+++ b/packages/0x.js/src/utils/order_validation_utils.ts
@@ -10,7 +10,7 @@ import {utils} from '../utils/utils';
import {ExchangeTransferSimulator} from './exchange_transfer_simulator';
export class OrderValidationUtils {
- private exchangeWrapper: ExchangeWrapper;
+ private _exchangeWrapper: ExchangeWrapper;
public static validateCancelOrderThrowIfInvalid(
order: Order, cancelTakerTokenAmount: BigNumber, unavailableTakerTokenAmount: BigNumber,
): void {
@@ -29,7 +29,7 @@ export class OrderValidationUtils {
exchangeTradeEmulator: ExchangeTransferSimulator, signedOrder: SignedOrder,
fillTakerTokenAmount: BigNumber, senderAddress: string, zrxTokenAddress: string,
): Promise<void> {
- const fillMakerTokenAmount = OrderValidationUtils.getPartialAmount(
+ const fillMakerTokenAmount = OrderValidationUtils._getPartialAmount(
fillTakerTokenAmount,
signedOrder.takerTokenAmount,
signedOrder.makerTokenAmount,
@@ -42,7 +42,7 @@ export class OrderValidationUtils {
signedOrder.takerTokenAddress, senderAddress, signedOrder.maker, fillTakerTokenAmount,
TradeSide.Taker, TransferType.Trade,
);
- const makerFeeAmount = OrderValidationUtils.getPartialAmount(
+ const makerFeeAmount = OrderValidationUtils._getPartialAmount(
fillTakerTokenAmount,
signedOrder.takerTokenAmount,
signedOrder.makerFee,
@@ -51,7 +51,7 @@ export class OrderValidationUtils {
zrxTokenAddress, signedOrder.maker, signedOrder.feeRecipient, makerFeeAmount, TradeSide.Maker,
TransferType.Fee,
);
- const takerFeeAmount = OrderValidationUtils.getPartialAmount(
+ const takerFeeAmount = OrderValidationUtils._getPartialAmount(
fillTakerTokenAmount,
signedOrder.takerTokenAmount,
signedOrder.takerFee,
@@ -61,21 +61,21 @@ export class OrderValidationUtils {
TransferType.Fee,
);
}
- private static validateRemainingFillAmountNotZeroOrThrow(
+ private static _validateRemainingFillAmountNotZeroOrThrow(
takerTokenAmount: BigNumber, unavailableTakerTokenAmount: BigNumber,
) {
if (takerTokenAmount.eq(unavailableTakerTokenAmount)) {
throw new Error(ExchangeContractErrs.OrderRemainingFillAmountZero);
}
}
- private static validateOrderNotExpiredOrThrow(expirationUnixTimestampSec: BigNumber) {
+ private static _validateOrderNotExpiredOrThrow(expirationUnixTimestampSec: BigNumber) {
const currentUnixTimestampSec = utils.getCurrentUnixTimestampSec();
if (expirationUnixTimestampSec.lessThan(currentUnixTimestampSec)) {
throw new Error(ExchangeContractErrs.OrderFillExpired);
}
}
- private static getPartialAmount(numerator: BigNumber, denominator: BigNumber,
- target: BigNumber): BigNumber {
+ private static _getPartialAmount(numerator: BigNumber, denominator: BigNumber,
+ target: BigNumber): BigNumber {
const fillMakerTokenAmount = numerator
.mul(target)
.div(denominator)
@@ -83,22 +83,22 @@ export class OrderValidationUtils {
return fillMakerTokenAmount;
}
constructor(exchangeWrapper: ExchangeWrapper) {
- this.exchangeWrapper = exchangeWrapper;
+ this._exchangeWrapper = exchangeWrapper;
}
public async validateOrderFillableOrThrowAsync(
exchangeTradeEmulator: ExchangeTransferSimulator, signedOrder: SignedOrder, zrxTokenAddress: string,
expectedFillTakerTokenAmount?: BigNumber): Promise<void> {
const orderHash = utils.getOrderHashHex(signedOrder);
- const unavailableTakerTokenAmount = await this.exchangeWrapper.getUnavailableTakerAmountAsync(orderHash);
- OrderValidationUtils.validateRemainingFillAmountNotZeroOrThrow(
+ const unavailableTakerTokenAmount = await this._exchangeWrapper.getUnavailableTakerAmountAsync(orderHash);
+ OrderValidationUtils._validateRemainingFillAmountNotZeroOrThrow(
signedOrder.takerTokenAmount, unavailableTakerTokenAmount,
);
- OrderValidationUtils.validateOrderNotExpiredOrThrow(signedOrder.expirationUnixTimestampSec);
+ OrderValidationUtils._validateOrderNotExpiredOrThrow(signedOrder.expirationUnixTimestampSec);
let fillTakerTokenAmount = signedOrder.takerTokenAmount.minus(unavailableTakerTokenAmount);
if (!_.isUndefined(expectedFillTakerTokenAmount)) {
fillTakerTokenAmount = expectedFillTakerTokenAmount;
}
- const fillMakerTokenAmount = OrderValidationUtils.getPartialAmount(
+ const fillMakerTokenAmount = OrderValidationUtils._getPartialAmount(
fillTakerTokenAmount,
signedOrder.takerTokenAmount,
signedOrder.makerTokenAmount,
@@ -107,7 +107,7 @@ export class OrderValidationUtils {
signedOrder.makerTokenAddress, signedOrder.maker, signedOrder.taker, fillMakerTokenAmount,
TradeSide.Maker, TransferType.Trade,
);
- const makerFeeAmount = OrderValidationUtils.getPartialAmount(
+ const makerFeeAmount = OrderValidationUtils._getPartialAmount(
fillTakerTokenAmount,
signedOrder.takerTokenAmount,
signedOrder.makerFee,
@@ -128,14 +128,14 @@ export class OrderValidationUtils {
if (!ZeroEx.isValidSignature(orderHash, signedOrder.ecSignature, signedOrder.maker)) {
throw new Error(ZeroExError.InvalidSignature);
}
- const unavailableTakerTokenAmount = await this.exchangeWrapper.getUnavailableTakerAmountAsync(orderHash);
- OrderValidationUtils.validateRemainingFillAmountNotZeroOrThrow(
+ const unavailableTakerTokenAmount = await this._exchangeWrapper.getUnavailableTakerAmountAsync(orderHash);
+ OrderValidationUtils._validateRemainingFillAmountNotZeroOrThrow(
signedOrder.takerTokenAmount, unavailableTakerTokenAmount,
);
if (signedOrder.taker !== constants.NULL_ADDRESS && signedOrder.taker !== takerAddress) {
throw new Error(ExchangeContractErrs.TransactionSenderIsNotFillOrderTaker);
}
- OrderValidationUtils.validateOrderNotExpiredOrThrow(signedOrder.expirationUnixTimestampSec);
+ OrderValidationUtils._validateOrderNotExpiredOrThrow(signedOrder.expirationUnixTimestampSec);
const remainingTakerTokenAmount = signedOrder.takerTokenAmount.minus(unavailableTakerTokenAmount);
const filledTakerTokenAmount = remainingTakerTokenAmount.lessThan(fillTakerTokenAmount) ?
remainingTakerTokenAmount :
@@ -144,7 +144,7 @@ export class OrderValidationUtils {
exchangeTradeEmulator, signedOrder, filledTakerTokenAmount, takerAddress, zrxTokenAddress,
);
- const wouldRoundingErrorOccur = await this.exchangeWrapper.isRoundingErrorAsync(
+ const wouldRoundingErrorOccur = await this._exchangeWrapper.isRoundingErrorAsync(
filledTakerTokenAmount, signedOrder.takerTokenAmount, signedOrder.makerTokenAmount,
);
if (wouldRoundingErrorOccur) {