Skip to content

Latest commit

 

History

History
126 lines (106 loc) · 5.44 KB

File metadata and controls

126 lines (106 loc) · 5.44 KB

VaultError Implementation Summary

Overview

Replaced string panics with typed VaultError enum across the Callora Vault contract to enable machine-readable error handling for integrators using @stellar/stellar-sdk.

Changes Made

1. Added VaultError Enum (contracts/vault/src/lib.rs)

  • Defined #[contracterror] enum with 27 error codes (1-27)
  • Each error has a stable u32 code and descriptive name
  • Covers all validation and authorization scenarios

Error Codes:

  1. NotInitialized - Vault not initialized
  2. AlreadyInitialized - Vault already initialized
  3. Unauthorized - Caller not authorized
  4. Paused - Vault is paused
  5. InsufficientBalance - Insufficient balance
  6. AmountNotPositive - Amount must be positive
  7. ExceedsMaxDeduct - Exceeds max deduct limit
  8. BelowMinDeposit - Below minimum deposit
  9. Overflow - Arithmetic overflow
  10. InitialBalanceNegative - Initial balance negative
  11. MinDepositNotPositive - Min deposit not positive
  12. MaxDeductNotPositive - Max deduct not positive
  13. MinDepositExceedsMaxDeduct - Min deposit > max deduct
  14. UsdcTokenCannotBeVault - USDC token = vault address
  15. RevenuePoolCannotBeVault - Revenue pool = vault address
  16. AuthorizedCallerCannotBeVault - Authorized caller = vault address
  17. InitialBalanceExceedsOnLedger - Initial balance > on-ledger balance
  18. AlreadyPaused - Vault already paused
  19. NotPaused - Vault not paused
  20. SettlementNotSet - Settlement address not configured
  21. BatchEmpty - Batch deduct requires items
  22. BatchTooLarge - Batch exceeds max size
  23. NewOwnerSameAsCurrent - New owner same as current
  24. NoOwnershipTransferPending - No ownership transfer pending
  25. NoAdminTransferPending - No admin transfer pending
  26. OfferingIdTooLong - Offering ID too long
  27. MetadataTooLong - Metadata too long

2. Converted Functions to Return Result<T, VaultError>

All public entrypoints now return Result instead of panicking:

  • init()Result<VaultMeta, VaultError>
  • deposit()Result<i128, VaultError>
  • deduct()Result<i128, VaultError>
  • batch_deduct()Result<i128, VaultError>
  • withdraw()Result<i128, VaultError>
  • withdraw_to()Result<i128, VaultError>
  • distribute()Result<(), VaultError>
  • pause()Result<(), VaultError>
  • unpause()Result<(), VaultError>
  • set_admin()Result<(), VaultError>
  • accept_admin()Result<(), VaultError>
  • transfer_ownership()Result<(), VaultError>
  • accept_ownership()Result<(), VaultError>
  • set_authorized_caller()Result<(), VaultError>
  • set_max_deduct()Result<(), VaultError>
  • set_allowed_depositor()Result<(), VaultError>
  • clear_allowed_depositors()Result<(), VaultError>
  • set_revenue_pool()Result<(), VaultError>
  • set_settlement()Result<(), VaultError>
  • set_metadata()Result<String, VaultError>
  • update_metadata()Result<String, VaultError>
  • add_address()Result<(), VaultError>
  • clear_all()Result<(), VaultError>

View functions:

  • get_meta()Result<VaultMeta, VaultError>
  • balance()Result<i128, VaultError>
  • get_admin()Result<Address, VaultError>
  • get_usdc_token()Result<Address, VaultError>
  • get_settlement()Result<Address, VaultError>
  • is_authorized_depositor()Result<bool, VaultError>

3. Updated Helper Functions

Private helper functions now return Result:

  • require_owner()Result<(), VaultError>
  • require_authorized_deduct_caller()Result<(), VaultError>
  • require_settlement()Result<Address, VaultError>
  • require_not_paused()Result<(), VaultError>
  • require_admin_or_owner()Result<(), VaultError>

4. Updated Documentation (docs/interfaces/vault.json)

Added comprehensive error codes section with:

  • Error code number
  • Error name
  • Description

Benefits

  1. Machine-Readable Errors: Integrators can branch on error codes instead of parsing strings
  2. Reduced WASM Size: Typed errors are more compact than string panics
  3. Better Developer Experience: Clear error codes with stable u32 values
  4. SDK Compatibility: Works seamlessly with @stellar/stellar-sdk error handling

Testing Notes

The contract compiles successfully with cargo check. Pre-existing test issues are unrelated to this implementation:

  • Tests reference non-existent methods (remove_allowed_depositor, cancel_ownership_transfer, cancel_admin_transfer)
  • These are pre-existing issues in the test suite

WASM Size Impact

The implementation uses typed errors which are more compact than string panics, contributing to reduced WASM size. The contract should still pass check-wasm-size.sh.

Security Considerations

  • All error paths maintain the same security guarantees as before
  • No authorization bypasses introduced
  • Arithmetic overflow still properly detected and returned as errors
  • CEI (Checks-Effects-Interactions) pattern preserved

Backward Compatibility

This is a breaking change for integrators:

  • All functions now return Result types
  • Callers must handle errors explicitly
  • Error codes are stable and documented

Next Steps

  1. Update test suite to handle Result types
  2. Update integration tests to assert on specific error codes
  3. Verify WASM size with check-wasm-size.sh
  4. Update client SDK documentation with error code handling examples