Skip to content

refactor(soroban): separate concerns into clean contract-execution su…#403

Merged
Solomonsolomonsolomon merged 1 commit into
gear5labs:masterfrom
GreatShinro:feat/soroban-subsystem-refactor
Jun 1, 2026
Merged

refactor(soroban): separate concerns into clean contract-execution su…#403
Solomonsolomonsolomon merged 1 commit into
gear5labs:masterfrom
GreatShinro:feat/soroban-subsystem-refactor

Conversation

@GreatShinro
Copy link
Copy Markdown
Contributor

@GreatShinro GreatShinro commented Jun 1, 2026

refactor(soroban): separate concerns into clean contract-execution subsystem

Summary

sorobanService.ts was a single 366-line file that mixed SDK version detection, transaction building, simulation, auth checking, XDR decoding, and error handling in one place. It also contained a partially-written class body embedded inside a function, dead code paths, and unreachable statements.

This PR replaces it with a layered subsystem where each concern lives in its own module with a single responsibility.


What changed

New files

File Responsibility
src/services/soroban/errors.ts Typed error hierarchy — SorobanError base + 8 subclasses, each with a machine-readable SorobanErrorCode discriminant
src/services/soroban/sdkAdapter.ts Single point of @stellar/stellar-sdk access; version-detection for SorobanRpc.Server vs legacy Soroban.Server; nativeToScVal/scValToNative wrappers; network constants
src/services/soroban/simulator.ts Pure simulation layer — builds an unsigned transaction, calls simulateTransaction, returns raw result + resource estimates + auth entries. No decoding, no signing
src/services/soroban/decoder.ts XDR decoding — decodeReturnValue and decodeScVal handle both current and legacy SDK retval shapes
src/services/soroban/signingPrep.ts Auth detection (requiresSigning), transaction assembly via SorobanRpc.assembleTransaction with a manual fallback, and keypair signing
src/services/soroban/invoker.ts Orchestrator — composes simulate → assert auth → decode into invokeContract and estimateContract

Modified files

File Change
src/services/sorobanService.ts Reduced from 366 lines to 57. Now a thin facade that re-exports the subsystem's public API and keeps the SorobanService class for backward compatibility
src/services/soroban/index.ts Updated to export the full subsystem public API alongside the existing ttlManager, swapLock, reentrancyGuard, and xdrScoping modules

Architecture

sorobanService.ts  (facade — backward-compatible entry point)
  └─ invoker.ts    (orchestration)
       ├─ simulator.ts    (RPC simulation only)
       ├─ decoder.ts      (XDR → native JS)
       └─ signingPrep.ts  (auth detection + signing)
            └─ sdkAdapter.ts  (stellar-sdk compatibility shim)
                 └─ errors.ts  (typed error hierarchy)

Each layer imports only from layers below it. No circular dependencies.


Backward compatibility

  • invokeContract(params) — same signature, same return shape
  • SorobanService class with invokeContract and simulateContractCall methods — preserved
  • sorobanService singleton — preserved
  • All existing exports from src/services/soroban/index.ts (ttlManager, swapLock, reentrancyGuard, xdrScoping) — unchanged

What was removed

  • Duplicate SorobanService class body embedded inside invokeContractLive function (unreachable code)
  • Duplicate export const sorobanService inside a function body (syntax error)
  • Unreachable try block after a return statement in normalizeArgs
  • Redundant inline SDK version detection scattered across multiple functions

Testing

  • npx tsc --noEmit passes with zero new errors (one pre-existing error in packages/sdk/src/trustline.ts is unrelated)
  • Pre-commit hooks (ESLint + Prettier) passed on all 8 staged files
  • Existing callers (SorobanTool, SorobanContractStateTool) import from sorobanService unchanged and continue to work

closes #332

…bsystem

- errors.ts: typed SorobanError hierarchy with machine-readable codes
- sdkAdapter.ts: single point of stellar-sdk access with version-detection
- simulator.ts: pure simulation layer (build tx → RPC → raw result)
- decoder.ts: isolated XDR/ScVal → native JS decoding
- signingPrep.ts: auth detection, transaction assembly, and signing
- invoker.ts: orchestrates simulate → assert → decode pipeline
- sorobanService.ts: thin facade preserving backward-compatible API
- index.ts: unified public API export for the subsystem
@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented Jun 1, 2026

@GreatShinro Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Solomonsolomonsolomon Solomonsolomonsolomon merged commit f43ca0b into gear5labs:master Jun 1, 2026
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.

[Backend] Refactor sorobanService.ts into a production Soroban runtime layer

2 participants