Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Map and List fromBytes methods #469

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/types/StoredValue.test.ts

Large diffs are not rendered by default.

21 changes: 13 additions & 8 deletions src/types/clvalue/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ export class CLValueList {
* @param elements - Optional array of CLValues to initialize the list with.
* @returns A new CLValue instance containing CLTypeList and a CLValueList.
*/
public static newCLList(elementType: CLType, elements: CLValue[] = []): CLValue {
public static newCLList(
elementType: CLType,
elements: CLValue[] = []
): CLValue {
const listType = new CLTypeList(elementType);
const clValue = new CLValue(listType);
clValue.list = new CLValueList(listType, elements);
Expand All @@ -154,13 +157,15 @@ export class CLValueList {
const elements: CLValue[] = [];

for (let i = 0; i < size; i++) {
const {
result: inner,
bytes: innerBytes
} = CLValueParser.fromBytesByType(remainder, clType.elementsType);

elements.push(inner);
remainder = innerBytes;
if (remainder.length) {
const {
result: inner,
bytes: innerBytes
} = CLValueParser.fromBytesByType(remainder, clType.elementsType);

elements.push(inner);
remainder = innerBytes;
}
}

if (elements.length === 0) {
Expand Down
13 changes: 8 additions & 5 deletions src/types/clvalue/Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,17 @@ export class CLValueMap {
}

for (let i = 0; i < size; i++) {
const keyVal = CLValueParser.fromBytesByType(remainder, mapType.key);
remainder = keyVal?.bytes;
if (remainder.length) {
const keyVal = CLValueParser.fromBytesByType(remainder, mapType.key);

const valVal = CLValueParser.fromBytesByType(u32.bytes(), mapType.val);
remainder = keyVal?.bytes;

remainder = valVal.bytes;
const valVal = CLValueParser.fromBytesByType(u32.bytes(), mapType.val);

mapResult.append(keyVal?.result, valVal?.result);
remainder = valVal.bytes;

mapResult.append(keyVal?.result, valVal?.result);
}
}

return { result: mapResult, bytes: remainder };
Expand Down
Loading