Skip to content

Conversation

@sudo-owen
Copy link
Collaborator

Summary

This PR fixes 8 categories of TypeScript compilation errors in the Solidity transpiler, reducing errors from 39 to 23. It also adds EIP712 runtime support for contracts using typed structured data hashing, and refactors the runtime module structure to avoid circular dependencies.

Key Changes

TypeScript Compilation Fixes (Batch 2)

  • Array .length to BigInt: Wraps array .length property in BigInt() when assigned to bigint variables, excluding EnumerableSetLib types that already return bigint
  • address(uint160(...)) Pattern: Converts bigint to hex address strings using `0x${(expr).toString(16).padStart(40, "0")}`
  • Interface Casts with Method Calls: Casts interface patterns like ICPU(address(this)).calculateMove() to any to allow method calls
  • new string(length) Pattern: Transpiles Solidity's new string(length) to empty string "" and handles new bytes(length)
  • bytes32("STRING") Conversion: Properly converts string literals cast to bytes32 to hex encoding
  • Override Modifier on Interface Methods: Excludes interface methods from get_all_inherited_methods() to prevent false override errors
  • bytes32 Constants as Hex Strings: Generates string values instead of BigInt for bytes32 hex literals
  • ABI Type Inference for address() Casts: Infers address(this) in abi.encode as 'address' type instead of 'uint256'

EIP712 Runtime Support

  • Added new EIP712.ts runtime module with full EIP-712 typed data hashing implementation
  • Includes domain separator computation, typed data hashing, and EIP-5267 support
  • Added EIP712 configuration to runtime-replacements.json with mixin support for multiple inheritance
  • Supports both static and dynamic domain name/version via _domainNameAndVersionMayChange()

Runtime Module Refactoring

  • Created new base.ts module containing core Contract class and utilities
  • Moved Storage, EventStream, ContractAddressRegistry, and ADDRESS_ZERO to base.ts
  • Updated runtime modules (Ownable, ECDSA, EnumerableSetLib) to import from ./base instead of ./index
  • Prevents circular dependency issues when runtime replacements extend Contract

Ownable Mixin Support

  • Added mixin field to Ownable configuration in runtime-replacements.json
  • Enables multiple inheritance pattern for contracts extending both a primary class and Ownable

Dependencies

  • Added vitest (^4.0.18) to dev dependencies for testing

Remaining Issues

Reduced from 39 to 23 errors. Main categories still needing fixes:

  1. Missing EIP712 module (now partially addressed with runtime implementation)
  2. Multiple inheritance with Ownable/EIP712 (requires mixin patterns)
  3. Struct field type inference in abi.encode
  4. viem type casting for 0x${string} addresses
  5. Record vs Map return type mismatches

Testing

The changes maintain backward compatibility with existing transpiled code while fixing specific TypeScript compilation patterns identified in the codebase.

https://claude.ai/code/session_01Q76KPUo9pMEzA4YARpnPw1

Major fixes to reduce TypeScript compilation errors from 39 to 23:

1. Array .length to BigInt conversion - wrap in BigInt() when assigned to
   bigint variables, with specific EnumerableSetLib type exclusions

2. address(uint160(...)) pattern - detect numeric casts and convert bigint
   to hex address string instead of accessing _contractAddress on bigint

3. Interface casts with method calls - ICPU(address(this)).method() now
   casts to 'any' to allow interface method calls on abstract classes

4. new string(length) pattern - Solidity's string allocation now returns
   empty string since TypeScript can't use 'new string()'

5. bytes32("STRING") conversion - string literals cast to bytes32 are
   properly converted to hex encoding

6. Override modifier on interface methods - exclude interface methods from
   inherited_methods check for TypeScript's override keyword

7. bytes32 constants as hex strings - generate proper hex string values
   instead of BigInt for bytes32 state variables

8. ABI type inference for address() casts - properly infer 'address' type
   for address(this) and similar patterns in abi.encode

https://claude.ai/code/session_01Q76KPUo9pMEzA4YARpnPw1
Key fixes:
- Created runtime/base.ts to fix circular dependency issues
- Added EIP712 runtime replacement module
- Fixed runtime replacement imports (Ownable, ECDSA, EIP712)
- Improved struct field type inference in ABI encoding
- Added proper 0x${string} casting for addresses and bytes32
- Fixed multiple inheritance with Ownable mixin generation
- Changed mapping types from Map to Record for consistency
- Fixed address() cast for struct fields already typed as address
- Improved blockhash/keccak256 ABI type inference

Reduced TypeScript errors from 39+ to 4.

https://claude.ai/code/session_01Q76KPUo9pMEzA4YARpnPw1
- Add TypeCast handling in _convert_abi_value for address/bytes32 casts
- Add string type inference in _infer_single_abi_type
- Update _emitEvent signature to accept variadic args

All transpiled TypeScript files now compile without errors.

https://claude.ai/code/session_01Q76KPUo9pMEzA4YARpnPw1
- Move mixin definitions from hard-coded sol2ts.py to runtime-replacements.json
- Add "mixin" field to runtime replacement interface definitions
- Update TypeScriptCodeGenerator to load and apply mixins from JSON config
- Any runtime replacement class with a mixin field will automatically be
  inlined when used as a secondary base class

This allows future multiple inheritance cases to be handled by simply
adding mixin code to the JSON config, rather than modifying the transpiler.

https://claude.ai/code/session_01Q76KPUo9pMEzA4YARpnPw1
- Generate factory functions for each struct (e.g., createDefaultBattleConfig())
  to handle mapping access returning undefined instead of zero-initialized structs
- Fix storage struct references to persist modifications using ??= pattern
- Add integration tests for Engine with proper mocks for RNGOracle, TeamRegistry,
  Validator, Ruleset, and Matchmaker
- Clean up old test files and replace with comprehensive integration.test.ts
- Make runtime Contract class properties public for test access

All 13 integration tests now pass.

https://claude.ai/code/session_01Q76KPUo9pMEzA4YARpnPw1
- Document Batch 3 fixes (issues #15-23):
  - Struct factory functions for mapping access
  - Storage reference write-back semantics
  - EIP712 runtime module
  - Multiple inheritance mixin system
  - Circular dependency resolution
  - Improved ABI type inference
  - viem type casting
  - Runtime contract properties made public
  - Comprehensive integration test suite

- Update Remaining Issues section to show all resolved
- Expand Testing section with new test suites and commands

https://claude.ai/code/session_01Q76KPUo9pMEzA4YARpnPw1
1. Bigint as Record index type (TS2538)
   - Add Number() conversion for numeric mapping keys in storage init statements
   - Check key_type to determine if Number() wrapping is needed

2. Local struct factory calls
   - Check current_local_structs before adding Structs. prefix
   - Local structs (defined in contract) use createDefault*() directly

3. EnumerableSetLib initialization
   - AddressSet and Uint256Set use new constructor syntax
   - Instead of Structs.createDefaultAddressSet(), use new AddressSet()

All 19 TypeScript errors are now resolved. 31 tests pass.

https://claude.ai/code/session_01Q76KPUo9pMEzA4YARpnPw1
- Document bigint Record index conversion fix
- Document local struct factory call fix
- Document EnumerableSetLib initialization fix
- Update status: 0 TypeScript errors, 31 tests pass

https://claude.ai/code/session_01Q76KPUo9pMEzA4YARpnPw1
…work

- Remove extensive fix history (26 items) - now in git history
- Expand Known Limitations section with tables for:
  - Parser limitations with workarounds
  - Semantic differences between Solidity and TypeScript
  - Type system gaps and their status
  - Files requiring runtime replacements
- Restructure Future Work with priority levels
- Streamline documentation for better readability

https://claude.ai/code/session_01Q76KPUo9pMEzA4YARpnPw1
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.

3 participants