Skip to content

Conversation

@pasevin
Copy link
Collaborator

@pasevin pasevin commented Oct 16, 2025

Midnight Adapter: Auto Simple View Rendering

Overview

This PR implements Phase 5 (US3): Auto Simple View Rendering for the Midnight adapter, completing the core functionality for automatically rendering and querying read-only contract functions in the UI Builder.

Key Features

✅ View Function Validation

  • Early detection of view functions via isMidnightViewFunction() check
  • Defense-in-depth validation in queryMidnightViewFunction() handler
  • Proper separation of read-only vs state-modifying functions

✅ Query Execution

  • Dynamic loading of contract ledger() function from compiled module
  • Contract state querying via Midnight indexer
  • Support for both parameter-less and parameterized view functions

✅ Output Formatting

  • Consistent formatting of diverse result types
  • Primitives (numbers, strings, booleans) display without JSON quotes
  • BigInt values handled correctly
  • Null/undefined and complex types (arrays, objects) properly formatted

✅ Configuration Management

  • getWalletConfigIfAvailable() relocated to src/configuration/provider.ts
  • RPC endpoint precedence: user override > wallet config > network defaults
  • Privacy-respecting integration with Midnight Wallet

✅ Comprehensive Testing

  • 56 unit tests covering all Phase 5 requirements
  • View function validation tests
  • Query handler input validation
  • Output formatter edge cases and error handling

Technical Highlights

Browser Compatibility

  • Extended pnpm patches for Node.js-specific dependencies
  • Vite configuration with WASM and CommonJS-to-ESM support
  • Removed unnecessary polyfills from index.html

Module Resolution

  • Global NetworkId override for module singleton workaround
  • Dynamic contract module loading with new Function()
  • Proper error handling for version mismatches

Validation Strategy

  • Strict address validation (bech32m format for users, hex prefix for contracts)
  • Early schema validation to catch issues before execution
  • Comprehensive error messages for debugging

Files Changed

New Files

  • src/configuration/provider.ts - Wallet configuration retrieval
  • src/validation/address.ts - Address validation logic
  • src/validation/index.ts - Validation module barrel
  • src/query/__tests__/view-checker.test.ts - View function tests (12 tests)
  • src/query/__tests__/handler.test.ts - Handler validation tests (8 tests)
  • src/transform/__tests__/output-formatter.test.ts - Output formatting tests (36 tests)

Modified Files

  • src/adapter.ts - Updated to pass contract module to query handler
  • src/query/handler.ts - Implemented complete query orchestration with provider setup
  • src/query/executor.ts - Added ledger function dynamic loading
  • src/transform/output-formatter.ts - Centralized result formatting logic
  • src/networks/testnet.ts - Added networkId configuration
  • src/utils/index.ts - Removed address validation exports
  • src/query/index.ts - Made executor/provider internal
  • tsup.config.ts - External WASM packages configuration
  • packages/types/src/networks/config.ts - Extended MidnightNetworkConfig type

Deleted Files

  • src/query/provider.ts - Logic moved to handler
  • src/utils/address-validation.ts - Moved to validation/address.ts
  • src/utils/__tests__/address-validation.test.ts - Moved to validation tests

Testing

All 56 tests pass:

✓ view-checker.test.ts (12 tests)
✓ handler.test.ts (8 tests)
✓ output-formatter.test.ts (36 tests)

Test Files  3 passed (3)
Tests  56 passed (56)

Commits

  • ad54ceea - test(adapter-midnight): add comprehensive Phase 5 unit tests for view functions
  • f0fe2a9b - chore(deps): update pnpm lock file
  • 6e313ad7 - feat(adapter-midnight): consolidate provider config and add view function validation
  • 470202db - fix(adapter-midnight): add pnpm patches for Midnight SDK browser compatibility

Related Issues

Closes #4 (Add Midnight Adapter)

Migration Notes

No breaking changes for consumers. The Phase 4 foundation is fully compatible with Phase 5 additions.

Next Steps

  • Phase 6: Write transaction support (transaction construction and signing)
  • Integration testing with live Midnight testnet
  • Performance optimization for large contract schemas

…atibility

- Patch @midnight-ntwrk/compact-runtime for Node.js polyfills
- Patch @midnight-ntwrk/midnight-js-indexer-public-data-provider for
  Apollo Client ESM, cross-fetch, and Network ID module singleton
- Patch @midnight-ntwrk/midnight-js-network-id for peer dependency
- Patch @midnight-ntwrk/midnight-js-types for peer dependency
- Patch @midnight-ntwrk/midnight-js-utils for peer dependency
- Add tsup.config.ts external WASM packages configuration
- Add comprehensive documentation in MIDNIGHT-SDK-PATCHES.md
…tion validation

- Move getWalletConfigIfAvailable to configuration module for proper alignment
- Consolidate provider configuration logic in query handler
- Add view function validation (isViewFunction check) and early address validation
- Implement RPC endpoint precedence: user override > wallet config > network config
- Add network ID handling with numeric mapping support
- Add derivation logic for indexer URIs from RPC endpoints as fallback
- Update network config type to include networkId enum value
- Align query handler pattern with EVM and Stellar adapters

This completes Phase 5 (US3) for auto-rendering simple view functions with proper
validation and wallet privacy preference integration.
… functions

Add 56 unit tests covering Phase 5 (US3) - Auto Simple View Rendering:

- View Function Validation (12 tests)
  ✓ Identifies read-only vs state-modifying functions
  ✓ Handles parameter-less and parameterized views
  ✓ Detects edge cases (empty functions, complex types, options)

- Query Handler Input Validation (8 tests)
  ✓ Validates contract schema structure
  ✓ Verifies address format detection
  ✓ Tests function existence checks and view-only enforcement

- Output Formatter (36 tests)
  ✓ Primitives (numbers, strings, booleans) display without quotes
  ✓ BigInt values formatted correctly
  ✓ Null/undefined handling
  ✓ Complex types (arrays, objects, nested structures)
  ✓ Error handling and edge cases

All tests follow Vitest patterns and align with existing test infrastructure.
Addresses Phase 5 requirements for defense-in-depth validation,
view-function enforcement, and consistent output formatting.
@pasevin pasevin marked this pull request as ready for review October 16, 2025 19:58
@pasevin pasevin requested a review from a team as a code owner October 16, 2025 19:58
@pasevin pasevin changed the title feat(adapter-midnight): Phase 5 - Auto Simple View Rendering feat(adapter-midnight): 3 Auto Simple View Rendering Oct 16, 2025
@pasevin pasevin changed the title feat(adapter-midnight): 3 Auto Simple View Rendering Midnight v1 – 03 Auto Simple View Rendering Oct 16, 2025
@pasevin pasevin requested a review from Copilot October 16, 2025 20:00
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 48 out of 49 changed files in this pull request and generated 7 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@pasevin pasevin requested a review from Copilot October 17, 2025 08:04
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 48 out of 49 changed files in this pull request and generated 9 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@pasevin pasevin added do not merge midnight Midnight Ecosystem Related Issues labels Oct 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not merge midnight Midnight Ecosystem Related Issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant