Skip to content

Commit

Permalink
Merge pull request #469 from casper-ecosystem/feat-utils-and-fixes
Browse files Browse the repository at this point in the history
Fix Map and List fromBytes methods
  • Loading branch information
alexmyshchyshyn authored Dec 18, 2024
2 parents f80ddb4 + 5dcd2d9 commit 212a58f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
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

0 comments on commit 212a58f

Please sign in to comment.