diff options
Diffstat (limited to 'packages/utils/src')
-rw-r--r-- | packages/utils/src/abi_decoder.ts | 2 | ||||
-rw-r--r-- | packages/utils/src/abi_encoder/evm_data_types/bool.ts | 4 | ||||
-rw-r--r-- | packages/utils/src/abi_encoder/evm_data_types/int.ts | 4 | ||||
-rw-r--r-- | packages/utils/src/abi_encoder/evm_data_types/uint.ts | 2 | ||||
-rw-r--r-- | packages/utils/src/abi_encoder/utils/math.ts | 12 | ||||
-rw-r--r-- | packages/utils/src/sign_typed_data_utils.ts | 5 |
6 files changed, 16 insertions, 13 deletions
diff --git a/packages/utils/src/abi_decoder.ts b/packages/utils/src/abi_decoder.ts index 2da46db35..28b6418d8 100644 --- a/packages/utils/src/abi_decoder.ts +++ b/packages/utils/src/abi_decoder.ts @@ -69,7 +69,7 @@ export class AbiDecoder { } if (param.type === SolidityTypes.Address) { const baseHex = 16; - value = addressUtils.padZeros(new BigNumber(value).toString(baseHex)); + value = addressUtils.padZeros(new BigNumber((value as string).toLowerCase()).toString(baseHex)); } else if (param.type === SolidityTypes.Uint256 || param.type === SolidityTypes.Uint) { value = new BigNumber(value); } else if (param.type === SolidityTypes.Uint8) { diff --git a/packages/utils/src/abi_encoder/evm_data_types/bool.ts b/packages/utils/src/abi_encoder/evm_data_types/bool.ts index 7f91f34e6..23298bc88 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/bool.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/bool.ts @@ -37,11 +37,11 @@ export class BoolDataType extends AbstractBlobDataType { const valueBuf = calldata.popWord(); const valueHex = ethUtil.bufferToHex(valueBuf); const valueNumber = new BigNumber(valueHex, constants.HEX_BASE); - if (!(valueNumber.equals(0) || valueNumber.equals(1))) { + if (!(valueNumber.isEqualTo(0) || valueNumber.isEqualTo(1))) { throw new Error(`Failed to decode boolean. Expected 0x0 or 0x1, got ${valueHex}`); } /* tslint:disable boolean-naming */ - const value: boolean = !valueNumber.equals(0); + const value: boolean = !valueNumber.isEqualTo(0); /* tslint:enable boolean-naming */ return value; } diff --git a/packages/utils/src/abi_encoder/evm_data_types/int.ts b/packages/utils/src/abi_encoder/evm_data_types/int.ts index cc3164b18..f8be1f778 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/int.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/int.ts @@ -38,8 +38,8 @@ export class IntDataType extends AbstractBlobDataType { throw new Error(`Tried to instantiate Int with bad input: ${dataItem}`); } this._width = IntDataType._decodeWidthFromType(dataItem.type); - this._minValue = new BigNumber(2).toPower(this._width - 1).times(-1); - this._maxValue = new BigNumber(2).toPower(this._width - 1).sub(1); + this._minValue = new BigNumber(2).exponentiatedBy(this._width - 1).times(-1); + this._maxValue = new BigNumber(2).exponentiatedBy(this._width - 1).minus(1); } public encodeValue(value: BigNumber | string | number): Buffer { diff --git a/packages/utils/src/abi_encoder/evm_data_types/uint.ts b/packages/utils/src/abi_encoder/evm_data_types/uint.ts index 8e382e8dc..a82aa789e 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/uint.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/uint.ts @@ -38,7 +38,7 @@ export class UIntDataType extends AbstractBlobDataType { throw new Error(`Tried to instantiate UInt with bad input: ${dataItem}`); } this._width = UIntDataType._decodeWidthFromType(dataItem.type); - this._maxValue = new BigNumber(2).toPower(this._width).sub(1); + this._maxValue = new BigNumber(2).exponentiatedBy(this._width).minus(1); } public encodeValue(value: BigNumber | string | number): Buffer { diff --git a/packages/utils/src/abi_encoder/utils/math.ts b/packages/utils/src/abi_encoder/utils/math.ts index d84983c5b..a2a79e2a8 100644 --- a/packages/utils/src/abi_encoder/utils/math.ts +++ b/packages/utils/src/abi_encoder/utils/math.ts @@ -1,7 +1,7 @@ -import BigNumber from 'bignumber.js'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; +import { BigNumber } from '../../configured_bignumber'; import { constants } from '../utils/constants'; function sanityCheckBigNumberRange( @@ -10,10 +10,12 @@ function sanityCheckBigNumberRange( maxValue: BigNumber, ): void { const value = new BigNumber(value_, 10); - if (value.greaterThan(maxValue)) { + if (value.isGreaterThan(maxValue)) { throw new Error(`Tried to assign value of ${value}, which exceeds max value of ${maxValue}`); - } else if (value.lessThan(minValue)) { + } else if (value.isLessThan(minValue)) { throw new Error(`Tried to assign value of ${value}, which exceeds min value of ${minValue}`); + } else if (value.isNaN()) { + throw new Error(`Tried to assign NaN value`); } } function bigNumberToPaddedBuffer(value: BigNumber): Buffer { @@ -30,7 +32,7 @@ function bigNumberToPaddedBuffer(value: BigNumber): Buffer { export function encodeNumericValue(value_: BigNumber | string | number): Buffer { const value = new BigNumber(value_, 10); // Case 1/2: value is non-negative - if (value.greaterThanOrEqualTo(0)) { + if (value.isGreaterThanOrEqualTo(0)) { const encodedPositiveValue = bigNumberToPaddedBuffer(value); return encodedPositiveValue; } @@ -74,7 +76,7 @@ export function decodeNumericValue(encodedValue: Buffer, minValue: BigNumber): B const valueHex = ethUtil.bufferToHex(encodedValue); // Case 1/3: value is definitely non-negative because of numeric boundaries const value = new BigNumber(valueHex, constants.HEX_BASE); - if (!minValue.lessThan(0)) { + if (!minValue.isLessThan(0)) { return value; } // Case 2/3: value is non-negative because there is no leading 1 (encoded as two's-complement) diff --git a/packages/utils/src/sign_typed_data_utils.ts b/packages/utils/src/sign_typed_data_utils.ts index 6963b9084..adb66622d 100644 --- a/packages/utils/src/sign_typed_data_utils.ts +++ b/packages/utils/src/sign_typed_data_utils.ts @@ -1,8 +1,9 @@ +import { EIP712Object, EIP712ObjectValue, EIP712TypedData, EIP712Types } from '@0x/types'; import * as ethUtil from 'ethereumjs-util'; import * as ethers from 'ethers'; import * as _ from 'lodash'; -import { EIP712Object, EIP712ObjectValue, EIP712TypedData, EIP712Types } from '@0x/types'; +import { BigNumber } from './configured_bignumber'; export const signTypedDataUtils = { /** @@ -70,7 +71,7 @@ export const signTypedDataUtils = { return ethers.utils.defaultAbiCoder.encode(encodedTypes, encodedValues); }, _normalizeValue(type: string, value: any): EIP712ObjectValue { - const normalizedValue = type === 'uint256' && _.isObject(value) && value.isBigNumber ? value.toString() : value; + const normalizedValue = type === 'uint256' && BigNumber.isBigNumber(value) ? value.toString() : value; return normalizedValue; }, _typeHash(primaryType: string, types: EIP712Types): Buffer { |