diff --git a/CHANGELOG.md b/CHANGELOG.md index e2136b6997..861885cb28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## 1.31.0-beta.x + +- Add `toBigInt()` (JS built-in `BigInt`) on `Int/Uint`, & `Compact<*>` types + ## 1.30.1 Aug 24, 2020 Upgrade priority: Low, unless using the `proposeParachain` module. diff --git a/packages/types/src/codec/AbstractInt.ts b/packages/types/src/codec/AbstractInt.ts index 1554ea2d47..58ceed6882 100644 --- a/packages/types/src/codec/AbstractInt.ts +++ b/packages/types/src/codec/AbstractInt.ts @@ -138,6 +138,13 @@ export default abstract class AbstractInt extends BN implements Codec { return u8a.length === (this.#bitLength / 8); } + /** + * @description Returns a BigInt representation of the number + */ + public toBigInt (): BigInt { + return BigInt(this.toString()); + } + /** * @description Returns the BN representation of the number. (Compatibility) */ diff --git a/packages/types/src/codec/Compact.spec.ts b/packages/types/src/codec/Compact.spec.ts index 26d1bc8b6f..437cc5a3d0 100644 --- a/packages/types/src/codec/Compact.spec.ts +++ b/packages/types/src/codec/Compact.spec.ts @@ -162,6 +162,14 @@ describe('Compact', (): void => { it('unwraps to the wrapped value', (): void => { expect(new (Compact.with(U32))(registry, 12345).unwrap() instanceof U32).toBe(true); }); + + it('has a valid toBn interface', (): void => { + expect(new (Compact.with('u128'))(registry, '12345678987654321').toBn().eq(new BN('12345678987654321'))).toBe(true); + }); + + it('has a valid toBigInt interface', (): void => { + expect(new (Compact.with('u128'))(registry, 12345678987654321n).toBigInt() === 12345678987654321n).toBe(true); + }); }); describe('helpers', (): void => { diff --git a/packages/types/src/codec/Compact.ts b/packages/types/src/codec/Compact.ts index 155272b4bd..0ad21b20e8 100644 --- a/packages/types/src/codec/Compact.ts +++ b/packages/types/src/codec/Compact.ts @@ -117,6 +117,13 @@ export default class Compact implements ICompact ); } + /** + * @description Returns a BigInt representation of the number + */ + public toBigInt (): BigInt { + return BigInt(this.toString()); + } + /** * @description Returns the BN representation of the number */ diff --git a/packages/types/src/codec/Date.spec.ts b/packages/types/src/codec/Date.spec.ts index 39ae36fdee..77b0790096 100644 --- a/packages/types/src/codec/Date.spec.ts +++ b/packages/types/src/codec/Date.spec.ts @@ -26,11 +26,12 @@ describe('Date', (): void => { }); describe('encode', (): void => { - const testEncode = (to: 'toBn' | 'toISOString' | 'toNumber' | CodecTo, expected: BN | number | string | Uint8Array): void => + const testEncode = (to: 'toBigInt' | 'toBn' | 'toISOString' | 'toNumber' | CodecTo, expected: BigInt | BN | number | string | Uint8Array): void => it(`can encode ${to}`, (): void => { expect(new CodecDate(registry, 421)[to]()).toEqual(expected); }); + testEncode('toBigInt', 421n); testEncode('toBn', new BN(421)); testEncode('toJSON', 421); testEncode('toISOString', '1970-01-01T00:07:01.000Z'); diff --git a/packages/types/src/codec/Date.ts b/packages/types/src/codec/Date.ts index d62267f5fb..e36596ce7e 100644 --- a/packages/types/src/codec/Date.ts +++ b/packages/types/src/codec/Date.ts @@ -81,6 +81,13 @@ export default class CodecDate extends Date implements Codec { return BITLENGTH; } + /** + * @description Returns a BigInt representation of the number + */ + public toBigInt (): BigInt { + return BigInt(this.toNumber()); + } + /** * @description Returns the BN representation of the timestamp */ diff --git a/packages/types/src/codec/Int.spec.ts b/packages/types/src/codec/Int.spec.ts index 00d5980222..81e65e0665 100644 --- a/packages/types/src/codec/Int.spec.ts +++ b/packages/types/src/codec/Int.spec.ts @@ -8,6 +8,12 @@ import Int from './Int'; describe('Int', (): void => { const registry = new TypeRegistry(); + it('provides a toBigInt interface', (): void => { + expect( + new Int(registry, -1234).toBigInt() + ).toEqual(-1234n); + }); + it('provides a toBn interface', (): void => { expect( new Int(registry, -1234).toBn().toNumber() diff --git a/packages/types/src/codec/UInt.spec.ts b/packages/types/src/codec/UInt.spec.ts index 6c32dadb2a..166f9c337f 100644 --- a/packages/types/src/codec/UInt.spec.ts +++ b/packages/types/src/codec/UInt.spec.ts @@ -28,6 +28,12 @@ describe('UInt', (): void => { ).toEqual('123,456,789,123,456,789,123,456,789'); }); + it('provides a toBigInt interface', (): void => { + expect( + new UInt(registry, 9876543210123456789n).toBigInt() + ).toEqual(9876543210123456789n); + }); + it('provides a toBn interface', (): void => { expect( new UInt(registry, 987).toBn().toNumber() diff --git a/packages/types/src/types/interfaces.ts b/packages/types/src/types/interfaces.ts index c706ff318b..f7e55ee52e 100644 --- a/packages/types/src/types/interfaces.ts +++ b/packages/types/src/types/interfaces.ts @@ -9,6 +9,7 @@ import { FunctionMetadataLatest } from '../interfaces/metadata'; import { ArgsDef, Codec } from './codec'; export interface ICompact extends Codec { + toBigInt (): BigInt; toBn (): BN; toNumber (): number; unwrap (): T;