Skip to content

Conversation

@ReneDuris
Copy link

This PR includes several improvements with no breaking changes:

Refactorings:
Merged duplicate guard utility functions
Added runtime type assertions in type system constructors for better error detection
Split generic types into separate files (genericOption.ts, genericList.ts)
Refactored ArgSerializer by extracting complex recursive logic into dedicated helper classes (BufferReader, BufferWriter, TypeValueReader, TypeValueWriter)

Bug Fix:
Fixed OptionValue type construction in option codec - was passing inner type instead of wrapped OptionType (bug exposed by new type assertions)

// BEFORE (incorrect):
decodeTopLevel(buffer: Buffer, type: Type): OptionValue {
    // type is U32Type (inner type), not OptionType!
    return new OptionValue(type, decoded);  // ❌ Wrong type
}

// AFTER (correct):
decodeTopLevel(buffer: Buffer, type: Type): OptionValue {
    const optionType = new OptionType(type);  // ✅ Wrap inner type
    return new OptionValue(optionType, decoded);  // ✅ Correct type
}

All changes maintain full backward compatibility. All tests passing.

Replace generic error message with descriptive ErrInvalidTuple
that explains the issue and provides context for debugging.

- Created ErrInvalidTuple error class
- Updated Tuple.fromItems to use new error
- Added test coverage for error cases

Fixed TODO: Define a better error for tuple validation.
Replace hardcoded array2-array256 factory entries with dynamic parsing
that can handle arbitrary array sizes automatically.

- Created parseArrayType helper function in typeExpressionParser
- Updated TypeMapper to use dynamic array parsing
- Removed 11 hardcoded array factory entries
- Added test coverage for arbitrary array sizes (array999, array1000, array5)

Fixed TODO: Array types are hardcoded (array2, array6, array8, etc.). Need dynamic parsing.
Move conversion logic from NativeSerializer to respective type classes
for better separation of concerns and encapsulation.

- Added BytesValue.fromNative() for bytes conversion
- Added StringValue.fromNative() for string conversion
- Added AddressValue.fromNative() for address conversion
- Added NumericalValue.fromNative() for numerical conversion
- Updated NativeSerializer to use new static methods

Fixed TODO: move logic to typesystem/bytes.ts
Fixed TODO: move logic to typesystem/string.ts
Fixed TODO: move logic to typesystem/address.ts
Fixed TODO: move logic to typesystem/numerical.ts
Consolidate guardValueIsSet and guardValueIsSetWithMessage into
a single function that handles both use cases automatically.

- Updated guardValueIsSet to accept either variable name or custom message
- Deprecated guardValueIsSetWithMessage (kept for backward compatibility)
- Added JSDoc documentation

Fixed TODO: merge with guardValueIsSetWithMessage
Fixed TODO: merge with guardValueIsSet
Add type validation in OptionalValue, CompositeValue, VariadicValue,
OptionValue, and List constructors to catch type mismatches early.

- Added type assertions in OptionalValue constructor
- Added type assertions in CompositeValue constructor
- Added type assertions in VariadicValue constructor
- Added type assertions in OptionValue constructor
- Added type assertions in List constructor
- Updated test to verify type validation correctly catches bugs

Fixed TODO: assert value is of type type.getFirstTypeParameter()
Fixed TODO: assert type of each item (wrt. type.getTypeParameters())
Fixed TODO: assert items are of type type.getFirstTypeParameter()
Split generic types into dedicated files for better code organization
and maintainability while preserving backward compatibility.

- Moved OptionType and OptionValue to genericOption.ts
- Moved ListType and List to genericList.ts
- Updated generic.ts to re-export from new files
- All tests pass, full backward compatibility maintained

Fixed TODO: Move to a new file, genericOption.ts
Fixed TODO: Move to a new file, genericList.ts
The OptionValueBinaryCodec.decodeTopLevel() was incorrectly passing the
inner type parameter (e.g., U32Type) directly to OptionValue constructor
instead of wrapping it in an OptionType.

This bug was hidden until type assertions were added in commit 71e165c.
The assertions exposed that OptionValue was being constructed with wrong
type, causing runtime errors when accessing type parameters.

- Wrap inner type with OptionType before creating OptionValue
- Import OptionType in option codec
- Ensures type consistency and proper type parameter access

Bug discovered by type assertions added in previous refactoring.
Extract complex recursive logic from ArgSerializer into dedicated helper
classes for better maintainability and testability. This is a non-breaking
refactor that keeps all public APIs unchanged.

Created helper classes:
- BufferReader: Sequential buffer reading with position tracking
- BufferWriter: Buffer accumulation for serialization
- TypeValueReader: Recursive reading logic for complex types
- TypeValueWriter: Recursive writing logic for complex types

Simplified ArgSerializer methods:
- buffersToValues(): Now uses BufferReader + TypeValueReader
- valuesToBuffers(): Now uses BufferWriter + TypeValueWriter

Benefits:
- Eliminated nested functions and complex closures
- Better separation of concerns
- Easier to test each component independently
- More maintainable code structure
- No breaking changes to public API

Fixed TODO: Refactor, split (function is quite complex) in buffersToValues
Fixed TODO: Refactor, split (function is quite complex) in valuesToBuffers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant