From 0c906e54f824915aa8f5233237ef25399c93287b Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 1 Oct 2018 16:33:01 +0200 Subject: [PATCH] Properly toJSON() on StorageFunction (#179) * Properly toJSON() on StorageFunction * Add isZero(), cmp & toStringFormat * Update tests for UInt toString() --- packages/api-codec/src/codec/Struct.spec.js | 4 ++-- packages/api-codec/src/codec/UInt.spec.js | 2 +- packages/api-codec/src/codec/UInt.ts | 14 ++++++++++++-- packages/type-storage/src/utils/createFunction.ts | 3 ++- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/api-codec/src/codec/Struct.spec.js b/packages/api-codec/src/codec/Struct.spec.js index 7d273aa0dd..d177008f27 100644 --- a/packages/api-codec/src/codec/Struct.spec.js +++ b/packages/api-codec/src/codec/Struct.spec.js @@ -15,7 +15,7 @@ describe('Struct', () => { u32: U32 }) )({ txt: 'foo', u32: 0x123456 }).toString() - ).toEqual(`{txt: foo, u32: 0x00123456}`); + ).toEqual(`{txt: foo, u32: 1193046}`); }); it('exposes the types', () => { @@ -43,6 +43,6 @@ describe('Struct', () => { test.values().map((v) => v.toString() ) - ).toEqual(['bazzing', '0x00000045']); + ).toEqual(['bazzing', '69']); }); }); diff --git a/packages/api-codec/src/codec/UInt.spec.js b/packages/api-codec/src/codec/UInt.spec.js index d3bac12d00..e151b851ef 100644 --- a/packages/api-codec/src/codec/UInt.spec.js +++ b/packages/api-codec/src/codec/UInt.spec.js @@ -33,7 +33,7 @@ describe('UInt', () => { const u = new UInt('0x12', 16); expect(u.toHex()).toEqual('0x0012'); - expect(u.toString()).toEqual('0x0012'); + expect(u.toString()).toEqual('18'); }); it('converts to equivalents', () => { diff --git a/packages/api-codec/src/codec/UInt.ts b/packages/api-codec/src/codec/UInt.ts index 4adc1568d0..43a2c394ae 100644 --- a/packages/api-codec/src/codec/UInt.ts +++ b/packages/api-codec/src/codec/UInt.ts @@ -71,7 +71,7 @@ export default class UInt extends Base { } toJSON (): any { - return this.toString(); + return this.toHex(); } toU8a (isBare?: boolean): Uint8Array { @@ -79,7 +79,7 @@ export default class UInt extends Base { } toString (): string { - return this.toHex(); + return this.raw.toString(); } toBn (): BN { @@ -98,12 +98,22 @@ export default class UInt extends Base { : this.raw.add(bnToBn(other)); } + cmp (other: UInt | BN | number): number { + return other instanceof UInt + ? this.raw.cmp(other.raw) + : this.raw.cmp(bnToBn(other)); + } + div (other: UInt | BN | number): BN { return other instanceof UInt ? this.raw.div(other.raw) : this.raw.div(bnToBn(other)); } + isZero (): boolean { + return this.raw.isZero(); + } + lt (test: UInt | BN | number): boolean { return test instanceof UInt ? this.raw.lt(test.raw) diff --git a/packages/type-storage/src/utils/createFunction.ts b/packages/type-storage/src/utils/createFunction.ts index 0da4032989..4d9c3dc9aa 100644 --- a/packages/type-storage/src/utils/createFunction.ts +++ b/packages/type-storage/src/utils/createFunction.ts @@ -65,7 +65,8 @@ export default function createFunction ( } storageFn.meta = meta; - storageFn.toJSON = meta.toJSON; + storageFn.toJSON = (): any => + meta.toJSON(); return storageFn as StorageFunction; }