Skip to content

Commit

Permalink
Merge pull request #307 from michavie/variadic-serialization-fix
Browse files Browse the repository at this point in the history
Fix variadic serialization when no value given
  • Loading branch information
andreibancioiu authored Jun 27, 2023
2 parents a59d380 + d568b9c commit 7c12033
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/smartcontracts/nativeSerializer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,36 @@ describe("test native serializer", () => {
assert.deepEqual(typedValues[2].getType(), new ListType(new TupleType(new U8Type(), new BooleanType())));
assert.deepEqual(typedValues[2].valueOf(), [{ field0: new BigNumber(44), field1: false }, { field0: new BigNumber(45), field1: true }]);
});

it('should accept no value for variadic types', async () => {
const endpoint = AbiRegistry.create({
endpoints: [
{
name: 'foo',
inputs: [
{
type: 'u64',
},
{
name: 'features',
type: 'variadic<bytes>',
multi_arg: true,
},
],
outputs: [],
},
],
}).getEndpoint('foo');

// Using both native JavaScript objects and typed values
const typedValues = NativeSerializer.nativeToTypedValues(
[42],
endpoint
);

assert.deepEqual(typedValues[0].getType(), new U64Type());
assert.deepEqual(typedValues[0].valueOf(), new BigNumber(42));
assert.deepEqual(typedValues[1].getType(), new VariadicType(new BytesType()));
assert.deepEqual(typedValues[1].valueOf(), []);
});
});
2 changes: 1 addition & 1 deletion src/smartcontracts/nativeSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export namespace NativeSerializer {
const lastEndpointParamIndex = parameters.length - 1;
const argAtIndex = args[lastEndpointParamIndex];

if (argAtIndex.belongsToTypesystem) {
if (argAtIndex?.belongsToTypesystem) {
const isVariadicValue = argAtIndex.hasClassOrSuperclass(VariadicValue.ClassName);
if (!isVariadicValue) {
throw new ErrInvalidArgument(`Wrong argument type for endpoint ${endpoint.name}: typed value provided; expected variadic type, have ${argAtIndex.getClassName()}`);
Expand Down

0 comments on commit 7c12033

Please sign in to comment.