From 744a9b89312c825ee94195106b9aa8aa11f43b77 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 25 Oct 2017 22:22:23 +0300 Subject: Upgrade bignumber to the version with native typings and remove typings --- src/utils/abi_decoder.ts | 2 +- src/utils/assert.ts | 6 +++--- src/utils/constants.ts | 2 +- src/utils/exchange_transfer_simulator.ts | 21 +++++++++++---------- src/utils/order_validation_utils.ts | 23 ++++++++++++----------- src/utils/utils.ts | 6 +++--- 6 files changed, 31 insertions(+), 29 deletions(-) (limited to 'src/utils') diff --git a/src/utils/abi_decoder.ts b/src/utils/abi_decoder.ts index a6c45bee7..247ba0e5b 100644 --- a/src/utils/abi_decoder.ts +++ b/src/utils/abi_decoder.ts @@ -1,6 +1,6 @@ import * as Web3 from 'web3'; import * as _ from 'lodash'; -import * as BigNumber from 'bignumber.js'; +import BigNumber from 'bignumber.js'; import {AbiType, DecodedLogArgs, LogWithDecodedArgs, RawLog, SolidityTypes, ContractEventArgs} from '../types'; import * as SolidityCoder from 'web3/lib/solidity/coder'; diff --git a/src/utils/assert.ts b/src/utils/assert.ts index 099f4490f..286105345 100644 --- a/src/utils/assert.ts +++ b/src/utils/assert.ts @@ -1,5 +1,5 @@ import * as _ from 'lodash'; -import * as BigNumber from 'bignumber.js'; +import BigNumber from 'bignumber.js'; import * as Web3 from 'web3'; import {Web3Wrapper} from '../web3_wrapper'; import {SchemaValidator, Schema} from '0x-json-schemas'; @@ -7,8 +7,8 @@ import {SchemaValidator, Schema} from '0x-json-schemas'; const HEX_REGEX = /^0x[0-9A-F]*$/i; export const assert = { - isBigNumber(variableName: string, value: BigNumber.BigNumber): void { - const isBigNumber = _.isObject(value) && value.isBigNumber; + isBigNumber(variableName: string, value: BigNumber): void { + const isBigNumber = _.isObject(value) && (value as any).isBigNumber; this.assert(isBigNumber, this.typeAssertionMessage(variableName, 'BigNumber', value)); }, isUndefined(value: any, variableName?: string): void { diff --git a/src/utils/constants.ts b/src/utils/constants.ts index a066fe869..3de3f5bc1 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -1,4 +1,4 @@ -import * as BigNumber from 'bignumber.js'; +import BigNumber from 'bignumber.js'; export const constants = { NULL_ADDRESS: '0x0000000000000000000000000000000000000000', diff --git a/src/utils/exchange_transfer_simulator.ts b/src/utils/exchange_transfer_simulator.ts index db12abd29..89b23c8ab 100644 --- a/src/utils/exchange_transfer_simulator.ts +++ b/src/utils/exchange_transfer_simulator.ts @@ -1,4 +1,5 @@ import * as _ from 'lodash'; +import BigNumber from 'bignumber.js'; import {ExchangeContractErrs, TradeSide, TransferType} from '../types'; import {TokenWrapper} from '../contract_wrappers/token_wrapper'; @@ -37,12 +38,12 @@ export class BalanceAndProxyAllowanceLazyStore { protected _token: TokenWrapper; private _balance: { [tokenAddress: string]: { - [userAddress: string]: BigNumber.BigNumber, + [userAddress: string]: BigNumber, }, }; private _proxyAllowance: { [tokenAddress: string]: { - [userAddress: string]: BigNumber.BigNumber, + [userAddress: string]: BigNumber, }, }; constructor(token: TokenWrapper) { @@ -50,7 +51,7 @@ export class BalanceAndProxyAllowanceLazyStore { this._balance = {}; this._proxyAllowance = {}; } - protected async getBalanceAsync(tokenAddress: string, userAddress: string): Promise { + protected async getBalanceAsync(tokenAddress: string, userAddress: string): Promise { if (_.isUndefined(this._balance[tokenAddress]) || _.isUndefined(this._balance[tokenAddress][userAddress])) { const balance = await this._token.getBalanceAsync(tokenAddress, userAddress); this.setBalance(tokenAddress, userAddress, balance); @@ -58,13 +59,13 @@ export class BalanceAndProxyAllowanceLazyStore { const cachedBalance = this._balance[tokenAddress][userAddress]; return cachedBalance; } - protected setBalance(tokenAddress: string, userAddress: string, balance: BigNumber.BigNumber): void { + protected setBalance(tokenAddress: string, userAddress: string, balance: BigNumber): void { if (_.isUndefined(this._balance[tokenAddress])) { this._balance[tokenAddress] = {}; } this._balance[tokenAddress][userAddress] = balance; } - protected async getProxyAllowanceAsync(tokenAddress: string, userAddress: string): Promise { + protected async getProxyAllowanceAsync(tokenAddress: string, userAddress: string): Promise { if (_.isUndefined(this._proxyAllowance[tokenAddress]) || _.isUndefined(this._proxyAllowance[tokenAddress][userAddress])) { const proxyAllowance = await this._token.getProxyAllowanceAsync(tokenAddress, userAddress); @@ -73,7 +74,7 @@ export class BalanceAndProxyAllowanceLazyStore { const cachedProxyAllowance = this._proxyAllowance[tokenAddress][userAddress]; return cachedProxyAllowance; } - protected setProxyAllowance(tokenAddress: string, userAddress: string, proxyAllowance: BigNumber.BigNumber): void { + protected setProxyAllowance(tokenAddress: string, userAddress: string, proxyAllowance: BigNumber): void { if (_.isUndefined(this._proxyAllowance[tokenAddress])) { this._proxyAllowance[tokenAddress] = {}; } @@ -92,7 +93,7 @@ export class ExchangeTransferSimulator extends BalanceAndProxyAllowanceLazyStore * @param transferType Is it a fee payment or a value transfer */ public async transferFromAsync(tokenAddress: string, from: string, to: string, - amountInBaseUnits: BigNumber.BigNumber, tradeSide: TradeSide, + amountInBaseUnits: BigNumber, tradeSide: TradeSide, transferType: TransferType): Promise { const balance = await this.getBalanceAsync(tokenAddress, from); const proxyAllowance = await this.getProxyAllowanceAsync(tokenAddress, from); @@ -107,19 +108,19 @@ export class ExchangeTransferSimulator extends BalanceAndProxyAllowanceLazyStore await this.increaseBalanceAsync(tokenAddress, to, amountInBaseUnits); } private async decreaseProxyAllowanceAsync(tokenAddress: string, userAddress: string, - amountInBaseUnits: BigNumber.BigNumber): Promise { + amountInBaseUnits: BigNumber): Promise { const proxyAllowance = await this.getProxyAllowanceAsync(tokenAddress, userAddress); if (!proxyAllowance.eq(this._token.UNLIMITED_ALLOWANCE_IN_BASE_UNITS)) { this.setProxyAllowance(tokenAddress, userAddress, proxyAllowance.minus(amountInBaseUnits)); } } private async increaseBalanceAsync(tokenAddress: string, userAddress: string, - amountInBaseUnits: BigNumber.BigNumber): Promise { + amountInBaseUnits: BigNumber): Promise { const balance = await this.getBalanceAsync(tokenAddress, userAddress); this.setBalance(tokenAddress, userAddress, balance.plus(amountInBaseUnits)); } private async decreaseBalanceAsync(tokenAddress: string, userAddress: string, - amountInBaseUnits: BigNumber.BigNumber): Promise { + amountInBaseUnits: BigNumber): Promise { const balance = await this.getBalanceAsync(tokenAddress, userAddress); this.setBalance(tokenAddress, userAddress, balance.minus(amountInBaseUnits)); } diff --git a/src/utils/order_validation_utils.ts b/src/utils/order_validation_utils.ts index b7eae7e2f..f03703c4e 100644 --- a/src/utils/order_validation_utils.ts +++ b/src/utils/order_validation_utils.ts @@ -1,4 +1,5 @@ import * as _ from 'lodash'; +import BigNumber from 'bignumber.js'; import {ExchangeContractErrs, SignedOrder, Order, ZeroExError, TradeSide, TransferType} from '../types'; import {ZeroEx} from '../0x'; import {TokenWrapper} from '../contract_wrappers/token_wrapper'; @@ -16,7 +17,7 @@ export class OrderValidationUtils { } public async validateOrderFillableOrThrowAsync( exchangeTradeEmulator: ExchangeTransferSimulator, signedOrder: SignedOrder, zrxTokenAddress: string, - expectedFillTakerTokenAmount?: BigNumber.BigNumber): Promise { + expectedFillTakerTokenAmount?: BigNumber): Promise { const orderHash = utils.getOrderHashHex(signedOrder); const unavailableTakerTokenAmount = await this.exchangeWrapper.getUnavailableTakerAmountAsync(orderHash); this.validateRemainingFillAmountNotZeroOrThrow( @@ -48,8 +49,8 @@ export class OrderValidationUtils { } public async validateFillOrderThrowIfInvalidAsync( exchangeTradeEmulator: ExchangeTransferSimulator, signedOrder: SignedOrder, - fillTakerTokenAmount: BigNumber.BigNumber, takerAddress: string, - zrxTokenAddress: string): Promise { + fillTakerTokenAmount: BigNumber, takerAddress: string, + zrxTokenAddress: string): Promise { if (fillTakerTokenAmount.eq(0)) { throw new Error(ExchangeContractErrs.OrderFillAmountZero); } @@ -83,7 +84,7 @@ export class OrderValidationUtils { } public async validateFillOrKillOrderThrowIfInvalidAsync( exchangeTradeEmulator: ExchangeTransferSimulator, signedOrder: SignedOrder, - fillTakerTokenAmount: BigNumber.BigNumber, takerAddress: string, zrxTokenAddress: string): Promise { + fillTakerTokenAmount: BigNumber, takerAddress: string, zrxTokenAddress: string): Promise { const filledTakerTokenAmount = await this.validateFillOrderThrowIfInvalidAsync( exchangeTradeEmulator, signedOrder, fillTakerTokenAmount, takerAddress, zrxTokenAddress, ); @@ -92,8 +93,8 @@ export class OrderValidationUtils { } } public async validateCancelOrderThrowIfInvalidAsync(order: Order, - cancelTakerTokenAmount: BigNumber.BigNumber, - unavailableTakerTokenAmount: BigNumber.BigNumber, + cancelTakerTokenAmount: BigNumber, + unavailableTakerTokenAmount: BigNumber, ): Promise { if (cancelTakerTokenAmount.eq(0)) { throw new Error(ExchangeContractErrs.OrderCancelAmountZero); @@ -108,7 +109,7 @@ export class OrderValidationUtils { } public async validateFillOrderBalancesAllowancesThrowIfInvalidAsync( exchangeTradeEmulator: ExchangeTransferSimulator, signedOrder: SignedOrder, - fillTakerTokenAmount: BigNumber.BigNumber, senderAddress: string, zrxTokenAddress: string): Promise { + fillTakerTokenAmount: BigNumber, senderAddress: string, zrxTokenAddress: string): Promise { const fillMakerTokenAmount = this.getPartialAmount( fillTakerTokenAmount, signedOrder.takerTokenAmount, @@ -142,20 +143,20 @@ export class OrderValidationUtils { ); } private validateRemainingFillAmountNotZeroOrThrow( - takerTokenAmount: BigNumber.BigNumber, unavailableTakerTokenAmount: BigNumber.BigNumber, + takerTokenAmount: BigNumber, unavailableTakerTokenAmount: BigNumber, ) { if (takerTokenAmount.eq(unavailableTakerTokenAmount)) { throw new Error(ExchangeContractErrs.OrderRemainingFillAmountZero); } } - private validateOrderNotExpiredOrThrow(expirationUnixTimestampSec: BigNumber.BigNumber) { + private validateOrderNotExpiredOrThrow(expirationUnixTimestampSec: BigNumber) { const currentUnixTimestampSec = utils.getCurrentUnixTimestamp(); if (expirationUnixTimestampSec.lessThan(currentUnixTimestampSec)) { throw new Error(ExchangeContractErrs.OrderFillExpired); } } - private getPartialAmount(numerator: BigNumber.BigNumber, denominator: BigNumber.BigNumber, - target: BigNumber.BigNumber): BigNumber.BigNumber { + private getPartialAmount(numerator: BigNumber, denominator: BigNumber, + target: BigNumber): BigNumber { const fillMakerTokenAmount = numerator .mul(target) .div(denominator) diff --git a/src/utils/utils.ts b/src/utils/utils.ts index f2bf74860..280f3e979 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -2,7 +2,7 @@ import * as _ from 'lodash'; import * as ethABI from 'ethereumjs-abi'; import * as ethUtil from 'ethereumjs-util'; import {Order, SignedOrder, SolidityTypes} from '../types'; -import * as BigNumber from 'bignumber.js'; +import BigNumber from 'bignumber.js'; import BN = require('bn.js'); export const utils = { @@ -12,7 +12,7 @@ export const utils = { * expects values of Solidity type `uint` to be passed as type `BN`. * We do not use BN anywhere else in the codebase. */ - bigNumberToBN(value: BigNumber.BigNumber) { + bigNumberToBN(value: BigNumber) { return new BN(value.toString(), 10); }, consoleLog(message: string): void { @@ -49,7 +49,7 @@ export const utils = { const hashHex = ethUtil.bufferToHex(hashBuff); return hashHex; }, - getCurrentUnixTimestamp(): BigNumber.BigNumber { + getCurrentUnixTimestamp(): BigNumber { return new BigNumber(Date.now() / 1000); }, }; -- cgit v1.2.3