diff --git a/CHANGELOG.md b/CHANGELOG.md index 2354916a9e..d2972d0ef8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ Contributed: Changes: +- Align Option `.toJSON` output with Substrate input - Prepare pre-v13 types for optional injection diff --git a/packages/types/src/codec/Option.spec.ts b/packages/types/src/codec/Option.spec.ts index 217bfe3a21..a5b433c5d8 100644 --- a/packages/types/src/codec/Option.spec.ts +++ b/packages/types/src/codec/Option.spec.ts @@ -102,6 +102,24 @@ describe('Option', (): void => { ).toEqual(new Uint8Array([0])); }); + it('converts toJSON() as null without', (): void => { + expect( + new Option(registry, Text).toJSON() + ).toEqual(null); + }); + + it('converts toJSON() as non-null with Bytes', (): void => { + expect( + new Option(registry, Bytes, 'abcde').toJSON() + ).toEqual('0x6162636465'); + }); + + it('converts toJSON() as non-null with Text', (): void => { + expect( + new Option(registry, Text, 'abcde').toJSON() + ).toEqual('abcde'); + }); + describe('utils', (): void => { const test = new Option(registry, Text, '1234'); diff --git a/packages/types/src/codec/Option.ts b/packages/types/src/codec/Option.ts index b2f0a80c52..9bf1f1cbdf 100644 --- a/packages/types/src/codec/Option.ts +++ b/packages/types/src/codec/Option.ts @@ -155,7 +155,9 @@ export class Option implements IOption { * @description Converts the Object to JSON, typically used for RPC transfers */ public toJSON (): AnyJson { - return this.#raw.toJSON(); + return this.isNone + ? null + : this.#raw.toJSON(); } /**