diff --git a/src/smartcontracts/nativeSerializer.spec.ts b/src/smartcontracts/nativeSerializer.spec.ts index 221f7b05..72df6c90 100644 --- a/src/smartcontracts/nativeSerializer.spec.ts +++ b/src/smartcontracts/nativeSerializer.spec.ts @@ -430,6 +430,34 @@ describe("test native serializer", () => { assert.deepEqual(typedValues[1].valueOf(), []); }); + it("should accept null or undefined for option types and optionals", async () => { + const endpoint = AbiRegistry.create({ + endpoints: [ + { + name: "foo", + inputs: [ + { + type: "Option", + }, + { + type: "optional", + }, + ], + outputs: [], + }, + ], + }).getEndpoint("foo"); + + const typedValuesUsingNull = NativeSerializer.nativeToTypedValues([null, null], endpoint); + const typedValuesUsingUndefined = NativeSerializer.nativeToTypedValues([undefined, undefined], endpoint); + + assert.deepEqual(typedValuesUsingNull, typedValuesUsingUndefined); + assert.deepEqual(typedValuesUsingNull[0].getType(), new OptionType(new NullType())); + assert.deepEqual(typedValuesUsingNull[0].valueOf(), null); + assert.deepEqual(typedValuesUsingNull[1].getType(), new OptionalType(new U32Type())); + assert.deepEqual(typedValuesUsingNull[1].valueOf(), null); + }); + it("should perform type inference (enums)", async () => { const abiRegistry = AbiRegistry.create({ endpoints: [ diff --git a/src/smartcontracts/nativeSerializer.ts b/src/smartcontracts/nativeSerializer.ts index b3d098b1..21e263f9 100644 --- a/src/smartcontracts/nativeSerializer.ts +++ b/src/smartcontracts/nativeSerializer.ts @@ -205,7 +205,7 @@ export namespace NativeSerializer { } function toOptionValue(native: any, type: Type, errorContext: ArgumentErrorContext): TypedValue { - if (native == null) { + if (native == null || native === undefined) { return OptionValue.newMissing(); } let converted = convertToTypedValue(native, type.getFirstTypeParameter(), errorContext); @@ -213,7 +213,7 @@ export namespace NativeSerializer { } function toOptionalValue(native: any, type: Type, errorContext: ArgumentErrorContext): TypedValue { - if (native == null) { + if (native == null || native === undefined) { return new OptionalValue(type); } let converted = convertToTypedValue(native, type.getFirstTypeParameter(), errorContext);