contracts-wizard icon indicating copy to clipboard operation
contracts-wizard copied to clipboard

[Stellar] Add toggle to switch from default to explicit trait implementation

Open CoveMB opened this issue 1 month ago • 2 comments

Fixes #706

Summary

Enable UI and tooling to use explicit Soroban trait implementations instead of relying solely on #[default_impl], adding regression coverage and docs for the new option.

Motivation / Context

Contract authors asked for a way to inspect and customize every generated Soroban trait function instead of depending on #[default_impl]. This work exposes that capability in the builders, UI, and MCP tools without changing the default experience.

Changes

  • [x] Major feature: optional explicit trait implementations throughout Stellar contract builders, plus access-control and token module plumbing updates.
  • [x] Config / tooling: UI control section, AI descriptions, schema, and MCP tool changes so the new option can be toggled and validated end-to-end.

Description

  • packages/core/stellar/src/common-options.ts:10-36 and the builder defaults now carry an explicitImplementations flag so every contract kind can opt in to non-macro trait bodies.
  • packages/core/stellar/src/contract.ts:188-192 introduces addTraitForEachFunctions, letting trait mixins add all functions programmatically when macros are disabled.
  • Access-control helpers (packages/core/stellar/src/set-access-control.ts:18-235, packages/core/stellar/src/add-pausable.ts:4-44, packages/core/stellar/src/add-upgradeable.ts:4-45) accept the flag, generate Ownable/AccessControl function bodies, and still enforce the same role/owner checks.
  • Token builders propagate the choice through every feature (packages/core/stellar/src/fungible.ts:25-210, packages/core/stellar/src/non-fungible.ts:28-305, packages/core/stellar/src/stablecoin.ts:48-101) and update generation blueprints so option sweeps include the new mode.
  • UI/tooling surfaces the toggle (packages/ui/src/stellar/TraitImplementationSection.svelte:1-33 plus imports in each controls file) while schemas/prompts describe and validate it (packages/mcp/src/stellar/schemas.ts:24-35, packages/mcp/src/stellar/tools/fungible.ts:10-40, packages/common/src/ai/descriptions/stellar.ts:12-20).
  • New compile tests, scenario docs, and MCP tool suites cover explicit builds across fungible, non-fungible, and stablecoin variants (packages/core/stellar/src/*.compile.test.ts, packages/core/stellar/src/*.test.ts / .md, packages/mcp/src/stellar/tools/*.test.ts).

Affected Functions

  • packages/core/stellar/src/contract.ts:188 — new addTraitForEachFunctions helper to bulk-attach trait methods when macros are skipped.
  • packages/core/stellar/src/set-access-control.ts:18setAccessControl accepts the explicit flag and adds per-function trait impls for Ownable/AccessControl when needed.
  • packages/core/stellar/src/set-access-control.ts:84requireAccessControl forwards the explicit flag so pausable/upgradeable traits stay consistent.
  • packages/core/stellar/src/add-pausable.ts:4 & packages/core/stellar/src/add-upgradeable.ts:4 — feature mixins now require the explicit flag and pass it through to access control.
  • packages/core/stellar/src/fungible.ts:63 & :137 — base, burnable, and mintable helpers emit explicit trait functions when requested.
  • packages/core/stellar/src/non-fungible.ts:91 & :212 — non-fungible base, burnable/enumerable/consecutive modules support explicit implementations.
  • packages/core/stellar/src/stablecoin.ts:48 — limitation helpers forward the flag so allow/blocklist operations can be emitted explicitly.

Security Impact

  • ✅ No new security concerns; explicit implementations call the same helper libraries and still rely on requireAccessControl for gating.
  • [ ] Security-sensitive modifications: not applicable.

Testing

  • [x] Added compile regressions for each contract family with explicitImplementations enabled (packages/core/stellar/src/fungible.compile.test.ts, non-fungible.compile.test.ts, stablecoin.compile.test.ts).
  • [x] Added scenario/API tests, markdown snapshots, and MCP tool coverage for the new option (packages/core/stellar/src/*.test.ts, .md, .snap, and packages/mcp/src/stellar/tools/*.test.ts).

Manual verification:

  1. In the UI, toggle "Explicit trait methods" for each contract type and confirm the preview drops #[default_impl] and shows explicit bodies.
  2. Run pnpm test --filter='packages/core/stellar/src/*explicit*' and the relevant MCP tool tests to ensure the new flag compiles cleanly.

Backward Compatibility

  • ✅ No breaking changes; the wizard still uses #[default_impl] unless the new toggle is enabled.
  • ✅ No migration needed; existing saved configs remain valid and ignore the new option.

CoveMB avatar Nov 11 '25 19:11 CoveMB