diff options
author | Greg Hysen <greg.hysen@gmail.com> | 2018-11-26 09:25:04 +0800 |
---|---|---|
committer | Greg Hysen <greg.hysen@gmail.com> | 2018-11-29 08:38:11 +0800 |
commit | ebaf9dd275403cdecfb3364876737fcbcd0eab82 (patch) | |
tree | 4c0e8e7f3c461e0a4d3ef2e38c2d97ea8b0f8c43 /packages/utils/src/abi_encoder | |
parent | d2d89adbddaec435ddb65545a86fc4dc981de521 (diff) | |
download | dexon-0x-contracts-ebaf9dd275403cdecfb3364876737fcbcd0eab82.tar dexon-0x-contracts-ebaf9dd275403cdecfb3364876737fcbcd0eab82.tar.gz dexon-0x-contracts-ebaf9dd275403cdecfb3364876737fcbcd0eab82.tar.bz2 dexon-0x-contracts-ebaf9dd275403cdecfb3364876737fcbcd0eab82.tar.lz dexon-0x-contracts-ebaf9dd275403cdecfb3364876737fcbcd0eab82.tar.xz dexon-0x-contracts-ebaf9dd275403cdecfb3364876737fcbcd0eab82.tar.zst dexon-0x-contracts-ebaf9dd275403cdecfb3364876737fcbcd0eab82.zip |
Removed abstract Number class.
Diffstat (limited to 'packages/utils/src/abi_encoder')
-rw-r--r-- | packages/utils/src/abi_encoder/evm_data_types/int.ts | 42 | ||||
-rw-r--r-- | packages/utils/src/abi_encoder/evm_data_types/number.ts | 41 | ||||
-rw-r--r-- | packages/utils/src/abi_encoder/evm_data_types/uint.ts | 43 |
3 files changed, 67 insertions, 59 deletions
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 457c41b28..83aeacd66 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/int.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/int.ts @@ -2,29 +2,53 @@ import { DataItem } from 'ethereum-types'; import { BigNumber } from '../../configured_bignumber'; -import { DataTypeFactory } from '../abstract_data_types'; +import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; +import { RawCalldata } from '../calldata'; +import * as Constants from '../utils/constants'; +import * as EncoderMath from '../utils/math'; -import { Number } from './number'; - -export class Int extends Number { +export class Int extends PayloadDataType { private static readonly _MATCHER = RegExp( '^int(8|16|24|32|40|48|56|64|72|88|96|104|112|120|128|136|144|152|160|168|176|184|192|200|208|216|224|232|240|248|256){0,1}$', ); + private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; + private static readonly _MAX_WIDTH: number = 256; + private static readonly _DEFAULT_WIDTH: number = Int._MAX_WIDTH; + private _width: number; + private _minValue: BigNumber; + private _maxValue: BigNumber; public static matchType(type: string): boolean { return Int._MATCHER.test(type); } + private static _decodeWidthFromType(type: string): number { + const matches = Int._MATCHER.exec(type); + const width = (matches !== null && matches.length === 2 && matches[1] !== undefined) + ? parseInt(matches[1], Constants.DEC_BASE) + : Int._DEFAULT_WIDTH; + return width; + } + public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { - super(dataItem, Int._MATCHER, dataTypeFactory); + super(dataItem, dataTypeFactory, Int._SIZE_KNOWN_AT_COMPILE_TIME); + if (!Int.matchType(dataItem.type)) { + throw new Error(`Tried to instantiate Int with bad input: ${dataItem}`); + } + this._width = Int._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); } - public getMaxValue(): BigNumber { - return new BigNumber(2).toPower(this._width - 1).sub(1); + public encodeValue(value: BigNumber | string | number): Buffer { + const encodedValue = EncoderMath.safeEncodeNumericValue(value, this._minValue, this._maxValue); + return encodedValue; } - public getMinValue(): BigNumber { - return new BigNumber(2).toPower(this._width - 1).times(-1); + public decodeValue(calldata: RawCalldata): BigNumber { + const valueBuf = calldata.popWord(); + const value = EncoderMath.safeDecodeNumericValue(valueBuf, this._minValue, this._maxValue); + return value; } public getSignature(): string { diff --git a/packages/utils/src/abi_encoder/evm_data_types/number.ts b/packages/utils/src/abi_encoder/evm_data_types/number.ts deleted file mode 100644 index 053a574e3..000000000 --- a/packages/utils/src/abi_encoder/evm_data_types/number.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { DataItem } from 'ethereum-types'; -import * as _ from 'lodash'; - -import { BigNumber } from '../../configured_bignumber'; -import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; -import { RawCalldata } from '../calldata'; -import * as Constants from '../utils/constants'; -import * as EncoderMath from '../utils/math'; - -export abstract class Number extends PayloadDataType { - private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; - private static readonly _MAX_WIDTH: number = 256; - private static readonly _DEFAULT_WIDTH: number = Number._MAX_WIDTH; - protected _width: number; - - constructor(dataItem: DataItem, matcher: RegExp, dataTypeFactory: DataTypeFactory) { - super(dataItem, dataTypeFactory, Number._SIZE_KNOWN_AT_COMPILE_TIME); - const matches = matcher.exec(dataItem.type); - if (matches === null) { - throw new Error(`Tried to instantiate Number with bad input: ${dataItem}`); - } - this._width = - matches !== null && matches.length === 2 && matches[1] !== undefined - ? parseInt(matches[1], Constants.DEC_BASE) - : (this._width = Number._DEFAULT_WIDTH); - } - - public encodeValue(value: BigNumber | string | number): Buffer { - const encodedValue = EncoderMath.safeEncodeNumericValue(value, this.getMinValue(), this.getMaxValue()); - return encodedValue; - } - - public decodeValue(calldata: RawCalldata): BigNumber { - const valueBuf = calldata.popWord(); - const value = EncoderMath.safeDecodeNumericValue(valueBuf, this.getMinValue(), this.getMaxValue()); - return value; - } - - public abstract getMaxValue(): BigNumber; - public abstract getMinValue(): BigNumber; -} 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 c2b6e214a..832ab075c 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/uint.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/uint.ts @@ -2,32 +2,57 @@ import { DataItem } from 'ethereum-types'; import { BigNumber } from '../../configured_bignumber'; -import { DataTypeFactory } from '../abstract_data_types'; +import { DataTypeFactory, PayloadDataType } from '../abstract_data_types'; +import { RawCalldata } from '../calldata'; +import * as Constants from '../utils/constants'; +import * as EncoderMath from '../utils/math'; -import { Number } from './number'; - -export class UInt extends Number { +export class UInt extends PayloadDataType { private static readonly _MATCHER = RegExp( '^uint(8|16|24|32|40|48|56|64|72|88|96|104|112|120|128|136|144|152|160|168|176|184|192|200|208|216|224|232|240|248|256){0,1}$', ); + private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; + private static readonly _MAX_WIDTH: number = 256; + private static readonly _DEFAULT_WIDTH: number = UInt._MAX_WIDTH; + private _width: number; + private _minValue: BigNumber; + private _maxValue: BigNumber; public static matchType(type: string): boolean { return UInt._MATCHER.test(type); } + private static _decodeWidthFromType(type: string): number { + const matches = UInt._MATCHER.exec(type); + const width = (matches !== null && matches.length === 2 && matches[1] !== undefined) + ? parseInt(matches[1], Constants.DEC_BASE) + : UInt._DEFAULT_WIDTH; + return width; + } + public constructor(dataItem: DataItem, dataTypeFactory: DataTypeFactory) { - super(dataItem, UInt._MATCHER, dataTypeFactory); + super(dataItem, dataTypeFactory, UInt._SIZE_KNOWN_AT_COMPILE_TIME); + if (!UInt.matchType(dataItem.type)) { + throw new Error(`Tried to instantiate UInt with bad input: ${dataItem}`); + } + this._width = UInt._decodeWidthFromType(dataItem.type); + this._minValue = new BigNumber(0); + this._maxValue = new BigNumber(2).toPower(this._width).sub(1); } - public getMaxValue(): BigNumber { - return new BigNumber(2).toPower(this._width).sub(1); + public encodeValue(value: BigNumber | string | number): Buffer { + const encodedValue = EncoderMath.safeEncodeNumericValue(value, this._minValue, this._maxValue); + return encodedValue; } - public getMinValue(): BigNumber { - return new BigNumber(0); + public decodeValue(calldata: RawCalldata): BigNumber { + const valueBuf = calldata.popWord(); + const value = EncoderMath.safeDecodeNumericValue(valueBuf, this._minValue, this._maxValue); + return value; } public getSignature(): string { return `uint${this._width}`; } } + |