Skip to content

Commit

Permalink
Fix after self review.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibancioiu committed Aug 31, 2023
1 parent dc18d3a commit a79a6e8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/smartcontracts/argSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ export class ArgSerializer {
let typedValue = readValue(type.getFirstTypeParameter());
return new OptionalValue(type, typedValue);
} else if (type.hasExactClass(VariadicType.ClassName)) {
let variadicType = <VariadicType>type;
let typedValues = [];
const variadicType = <VariadicType>type;
const typedValues = [];

if (variadicType.isCounted) {
const count: number = readValue(new U32Type()).valueOf().toNumber();
Expand Down Expand Up @@ -166,8 +166,8 @@ export class ArgSerializer {
handleValue(valueAsOptional.getTypedValue());
}
} else if (value.hasExactClass(VariadicValue.ClassName)) {
let valueAsVariadic = <VariadicValue>value;
let variadicType = <VariadicType>valueAsVariadic.getType();
const valueAsVariadic = <VariadicValue>value;
const variadicType = <VariadicType>valueAsVariadic.getType();

if (variadicType.isCounted) {
const countValue = new U32Value(valueAsVariadic.getItems().length);
Expand Down
32 changes: 30 additions & 2 deletions src/smartcontracts/resultsParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { LogLevel, Logger } from "../logger";
import { ArgSerializer } from "./argSerializer";
import { ResultsParser } from "./resultsParser";
import { ReturnCode } from "./returnCode";
import { BigUIntType, BigUIntValue, EndpointDefinition, EndpointModifiers, EndpointParameterDefinition, TypedValue, U32Type, U32Value, U64Type, U64Value, VariadicType, VariadicValue } from "./typesystem";
import { BigUIntType, BigUIntValue, EndpointDefinition, EndpointModifiers, EndpointParameterDefinition, StringType, StringValue, TypedValue, U32Type, U32Value, U64Type, U64Value, VariadicType, VariadicValue } from "./typesystem";
import { BytesType, BytesValue } from "./typesystem/bytes";

const KnownReturnCodes: string[] = [
Expand Down Expand Up @@ -112,7 +112,7 @@ describe("test smart contract results parser", () => {
assert.deepEqual(bundle.values[0], VariadicValue.fromItems(new U32Value(42), new U32Value(43)));
});

it("should parse query response (counted-variadic arguments)", async () => {
it("should parse query response (one counted-variadic arguments)", async () => {
const endpointModifiers = new EndpointModifiers("", []);
const outputParameters = [new EndpointParameterDefinition("a", "a", new VariadicType(new U32Type(), true))];
const endpoint = new EndpointDefinition("foo", [], outputParameters, endpointModifiers);
Expand All @@ -128,6 +128,34 @@ describe("test smart contract results parser", () => {
assert.deepEqual(bundle.values[0], VariadicValue.fromItemsCounted(new U32Value(42), new U32Value(43)));
});

it("should parse query response (multiple counted-variadic arguments)", async () => {
const endpointModifiers = new EndpointModifiers("", []);
const outputParameters = [
new EndpointParameterDefinition("a", "a", new VariadicType(new U32Type(), true)),
new EndpointParameterDefinition("b", "b", new VariadicType(new StringType(), true)),
new EndpointParameterDefinition("c", "c", new BigUIntType()),

];
const endpoint = new EndpointDefinition("foo", [], outputParameters, endpointModifiers);
const queryResponse = new ContractQueryResponse({
returnData: [
Buffer.from([2]).toString("base64"),
Buffer.from([42]).toString("base64"),
Buffer.from([43]).toString("base64"),
Buffer.from([3]).toString("base64"),
Buffer.from("a").toString("base64"),
Buffer.from("b").toString("base64"),
Buffer.from("c").toString("base64"),
Buffer.from([42]).toString("base64")
]
});

const bundle = parser.parseQueryResponse(queryResponse, endpoint);
assert.deepEqual(bundle.values[0], VariadicValue.fromItemsCounted(new U32Value(42), new U32Value(43)));
assert.deepEqual(bundle.values[1], VariadicValue.fromItemsCounted(new StringValue("a"), new StringValue("b"), new StringValue("c")));
assert.deepEqual(bundle.values[2], new BigUIntValue(42));
});

it("should parse contract outcome", async () => {
let endpointModifiers = new EndpointModifiers("", []);
let outputParameters = [
Expand Down

0 comments on commit a79a6e8

Please sign in to comment.