From 6f5b852bf71cb4cf6cd50c5be05858fc28ba59cc Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 29 Dec 2020 10:53:11 +0100 Subject: [PATCH] Work around incorrect contracts.call RPC handler (#2998) --- packages/types/src/codec/AbstractInt.ts | 8 +++++--- packages/types/src/codec/UInt.spec.ts | 2 +- packages/types/src/create/createType.spec.ts | 7 +++++-- .../types/src/interfaces/identity/Registration.spec.ts | 2 +- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/types/src/codec/AbstractInt.ts b/packages/types/src/codec/AbstractInt.ts index 5918c09a19..14d4504ad4 100644 --- a/packages/types/src/codec/AbstractInt.ts +++ b/packages/types/src/codec/AbstractInt.ts @@ -13,7 +13,7 @@ export const DEFAULT_UINT_BITS = 64; // Maximum allowed integer for JS is 2^53 - 1, set limit at 52 // In this case however, we always print any >32 as hex -const MAX_NUMBER_BITS = 32; +const MAX_NUMBER_BITS = 52; const MUL_P = new BN(1_00_00); const FORMATTERS: [string, BN][] = [ @@ -191,8 +191,10 @@ export abstract class AbstractInt extends BN implements Codec { */ public toJSON (onlyHex = false): any { // FIXME this return type should by string | number, however BN returns string - // Here if we want to have the actual used bits, it is super.bitLength() - return onlyHex || (this.#bitLength > MAX_NUMBER_BITS) + // Options here are + // - super.bitLength() - the actual used bits + // - this.#bitLength - the type bits (this should be used, however contracts RPC is problematic) + return onlyHex || (super.bitLength() > MAX_NUMBER_BITS) ? this.toHex() : this.toNumber(); } diff --git a/packages/types/src/codec/UInt.spec.ts b/packages/types/src/codec/UInt.spec.ts index 7bcb91e6ae..eb3341a26e 100644 --- a/packages/types/src/codec/UInt.spec.ts +++ b/packages/types/src/codec/UInt.spec.ts @@ -74,7 +74,7 @@ describe('UInt', (): void => { it('converts to JSON representation based on size', (): void => { expect(new UInt(registry, '0x12345678', 32).toJSON()).toEqual(0x12345678); - expect(new UInt(registry, '0x1234567890', 64).toJSON()).toEqual('0x0000001234567890'); + expect(new UInt(registry, '0x1234567890', 64).toJSON()).toEqual(78187493520); // '0x0000001234567890'); expect(new UInt(registry, '0x1234567890abcdef', 64).toJSON()).toEqual('0x1234567890abcdef'); }); diff --git a/packages/types/src/create/createType.spec.ts b/packages/types/src/create/createType.spec.ts index b31e33fcfe..b9e80a932d 100644 --- a/packages/types/src/create/createType.spec.ts +++ b/packages/types/src/create/createType.spec.ts @@ -33,7 +33,7 @@ describe('createType', (): void => { }]); expect(struct.toJSON()).toEqual({ - balance: '0x000000000000000000000000000004d2', + balance: 1234, // '0x000000000000000000000000000004d2', index: 16 }); expect(struct.toRawType()).toEqual(raw); @@ -78,7 +78,10 @@ describe('createType', (): void => { it('allows creation of a Tuple', (): void => { expect( createTypeUnsafe(registry, '(Balance,u32)', [[1234, 5678]]).toJSON() - ).toEqual(['0x000000000000000000000000000004d2', 5678]); + ).toEqual([ + 1234, // '0x000000000000000000000000000004d2', + 5678 + ]); }); it('allows creation for a UInt', (): void => { diff --git a/packages/types/src/interfaces/identity/Registration.spec.ts b/packages/types/src/interfaces/identity/Registration.spec.ts index 04bbfdaa02..31d5a3d1c3 100644 --- a/packages/types/src/interfaces/identity/Registration.spec.ts +++ b/packages/types/src/interfaces/identity/Registration.spec.ts @@ -17,7 +17,7 @@ describe('Registration', (): void => { '039911039922039944039955039933000003996600' ).toJSON() ).toEqual({ - deposit: '0x0000000000000000000009184e72a000', + deposit: 10000000000000, // '0x0000000000000000000009184e72a000', info: { additional: [], display: { Raw: '0x9911' },