Repository navigation
feat: expose encode and decode methods - #102
Conversation
WalkthroughA new utility for constructing calldata objects was added and adopted across contract action methods, centralizing logic previously duplicated locally. Decoder and simplification logic for transactions was refactored into a dedicated module, with related functions now re-exported from the main entry point. Internal decoding implementations and unused imports were removed for modularity. Changes
Sequence Diagram(s)sequenceDiagram
participant ContractActions
participant CalldataEncoder
ContractActions->>CalldataEncoder: makeCalldataObject(method, args, kwargs)
CalldataEncoder-->>ContractActions: {method, args, kwargs} (non-empty only)
sequenceDiagram
participant TransactionActions
participant Decoders
TransactionActions->>Decoders: decodeTransaction(tx)
Decoders-->>TransactionActions: Decoded transaction object
TransactionActions->>Decoders: simplifyTransactionReceipt(tx)
Decoders-->>TransactionActions: Simplified receipt
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
npm error Exit handler never called! Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. ✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (3)
src/abi/calldata/encoder.ts (1)
167-170: Consider using a more idiomatic approach for checking object emptiness.The current implementation works correctly but could be more readable.
Consider using
Object.keys(kwargs).length > 0instead:} else { - let hasVal = false; - for (const _k in kwargs) { - hasVal = true; - break; - } - if (hasVal) { + if (Object.keys(kwargs).length > 0) { ret["kwargs"] = kwargs; } }src/transactions/decoders.ts (2)
59-73: Consider using a proper logging mechanism instead of console methods.Direct console usage in library code can clutter user applications and lacks configurability.
Consider accepting an optional logger parameter or using a logging library that can be configured by consumers:
- console.warn( - "[decodeInputData] WRITE: Unexpected RLP array length:", - rlpDecodedArray.length, - rlpDecodedArray, - ); + // Use a configurable logger or allow consumers to handle unexpected cases + // logger?.warn("[decodeInputData] WRITE: Unexpected RLP array length:", rlpDecodedArray.length);
213-216: Consider extracting the inclusion logic for better readability.The condition for determining whether to include a value is complex and spans multiple lines.
Consider extracting this logic into a helper function:
- const shouldInclude = simplifiedValue !== undefined && - !(typeof simplifiedValue === "object" && simplifiedValue !== null && Object.keys(simplifiedValue).length === 0); - - if (shouldInclude || simplifiedValue === 0) { + const shouldIncludeValue = (value: any): boolean => { + if (value === undefined) return false; + if (value === 0) return true; // Special case for numeric 0 + if (typeof value === "object" && value !== null && Object.keys(value).length === 0) return false; + return true; + }; + + if (shouldIncludeValue(simplifiedValue)) {
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
src/abi/calldata/encoder.ts(1 hunks)src/contracts/actions.ts(3 hunks)src/index.ts(1 hunks)src/transactions/actions.ts(4 hunks)src/transactions/decoders.ts(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (3)
src/abi/calldata/encoder.ts (1)
src/types/calldata.ts (1)
CalldataEncodable(13-23)
src/transactions/actions.ts (2)
src/index.ts (3)
decodeLocalnetTransaction(8-8)simplifyTransactionReceipt(7-7)decodeTransaction(6-6)src/types/transactions.ts (1)
GenLayerTransaction(142-256)
src/contracts/actions.ts (1)
src/abi/index.ts (1)
calldata(4-4)
🔇 Additional comments (7)
src/abi/calldata/encoder.ts (1)
144-178: Well-implemented calldata construction utility!The function correctly centralizes calldata object construction with proper handling of optional parameters and empty field omission. This aligns perfectly with the PR's goal of exposing key utilities.
src/contracts/actions.ts (1)
57-57: Correct adoption of centralized calldata construction.All three usages properly integrate the new
calldata.makeCalldataObjectfunction:
readContractandwriteContractcorrectly pass the function namedeployContractappropriately passesundefinedfor the method parameter when calling constructorsThe refactoring successfully eliminates code duplication.
Also applies to: 101-101, 137-137
src/index.ts (1)
4-9: Clean exposure of decoder functions at package root.The named exports provide an ergonomic API surface that aligns with JavaScript/TypeScript best practices. This successfully achieves the PR's goal of making transaction utilities easily discoverable.
src/transactions/actions.ts (1)
14-14: Proper delegation to centralized decoder module.The refactoring successfully moves complex decoding logic to a dedicated module while maintaining the same API contracts. All decoder function calls are correctly placed based on chain type and transaction requirements.
Also applies to: 48-48, 51-51, 80-80, 91-91
src/transactions/decoders.ts (3)
77-119: Comprehensive transaction decoding implementation.The function thoroughly handles all transaction fields with proper type conversions and status/result mapping. The consistent string conversion for numeric fields ensures API stability.
232-276: Well-structured localnet transaction decoder.The function properly handles all localnet-specific fields with appropriate error handling for individual field decoding failures. The preservation of original base64 data alongside decoded values is a good practice.
1-276: Excellent modularization of transaction decoding logic!This new module successfully centralizes and encapsulates all transaction decoding and simplification logic with:
- Clear separation of concerns between different decoder functions
- Comprehensive error handling
- Well-documented behavior
- Cross-language compatibility through field mappings
The refactoring achieves the PR's goals of improving API discoverability and maintainability.
Expose Encode and Decode Methods for Developer Use
Summary
This PR exposes key encode and decode methods at the root of the GenLayer JS library, making them directly accessible for developers. The refactor clarifies the separation between low-level serialization and high-level transaction decoding, centralizes calldata construction, and improves the public API for discoverability and ease of use. The decision to use direct named exports over namespace exports is based on best practices for developer ergonomics and clarity.
Changes
🚀 New Features
decodeInputDatadecodeTransactionsimplifyTransactionReceiptdecodeLocalnetTransactionmakeCalldataObjectfromsrc/abi/calldata/encoder.ts, making it available for all internal and external usage.🏗️ Implementation Details
src/abi/transactions.tsand is not exposed at the top level.src/transactions/decoders.tsand is now part of the public API.src/abi/calldata/encoder.tsanddecoder.ts.transactions.decodeTransaction).📁 File Structure
🧪 Testing
🔧 Usage
✨ Code Quality
src/index.tsfor ergonomic imports.🎯 Export Strategy Features
🛡️ Backward Compatibility
Testing: ✅ All tests pass
Type Safety: ✅ Full TypeScript integration
Documentation: ✅ Public API is clear and discoverable
Architecture: ✅ Follows modular, domain-driven structure
User Experience: ✅ Developers can now use encode/decode methods directly
Summary by CodeRabbit