Skip to content

Commit 1ec7f6f

Browse files
Merge PR #379: refactor: introduce VaultError contracterror enum (admin; conflicts auto-resolved -X theirs)
2 parents c2998f6 + f7a2087 commit 1ec7f6f

3 files changed

Lines changed: 521 additions & 296 deletions

File tree

VAULT_ERROR_IMPLEMENTATION.md

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# VaultError Implementation Summary
2+
3+
## Overview
4+
Replaced string panics with typed `VaultError` enum across the Callora Vault contract to enable machine-readable error handling for integrators using @stellar/stellar-sdk.
5+
6+
## Changes Made
7+
8+
### 1. Added VaultError Enum (`contracts/vault/src/lib.rs`)
9+
- Defined `#[contracterror]` enum with 27 error codes (1-27)
10+
- Each error has a stable u32 code and descriptive name
11+
- Covers all validation and authorization scenarios
12+
13+
### Error Codes:
14+
1. **NotInitialized** - Vault not initialized
15+
2. **AlreadyInitialized** - Vault already initialized
16+
3. **Unauthorized** - Caller not authorized
17+
4. **Paused** - Vault is paused
18+
5. **InsufficientBalance** - Insufficient balance
19+
6. **AmountNotPositive** - Amount must be positive
20+
7. **ExceedsMaxDeduct** - Exceeds max deduct limit
21+
8. **BelowMinDeposit** - Below minimum deposit
22+
9. **Overflow** - Arithmetic overflow
23+
10. **InitialBalanceNegative** - Initial balance negative
24+
11. **MinDepositNotPositive** - Min deposit not positive
25+
12. **MaxDeductNotPositive** - Max deduct not positive
26+
13. **MinDepositExceedsMaxDeduct** - Min deposit > max deduct
27+
14. **UsdcTokenCannotBeVault** - USDC token = vault address
28+
15. **RevenuePoolCannotBeVault** - Revenue pool = vault address
29+
16. **AuthorizedCallerCannotBeVault** - Authorized caller = vault address
30+
17. **InitialBalanceExceedsOnLedger** - Initial balance > on-ledger balance
31+
18. **AlreadyPaused** - Vault already paused
32+
19. **NotPaused** - Vault not paused
33+
20. **SettlementNotSet** - Settlement address not configured
34+
21. **BatchEmpty** - Batch deduct requires items
35+
22. **BatchTooLarge** - Batch exceeds max size
36+
23. **NewOwnerSameAsCurrent** - New owner same as current
37+
24. **NoOwnershipTransferPending** - No ownership transfer pending
38+
25. **NoAdminTransferPending** - No admin transfer pending
39+
26. **OfferingIdTooLong** - Offering ID too long
40+
27. **MetadataTooLong** - Metadata too long
41+
42+
### 2. Converted Functions to Return Result<T, VaultError>
43+
All public entrypoints now return `Result` instead of panicking:
44+
- `init()``Result<VaultMeta, VaultError>`
45+
- `deposit()``Result<i128, VaultError>`
46+
- `deduct()``Result<i128, VaultError>`
47+
- `batch_deduct()``Result<i128, VaultError>`
48+
- `withdraw()``Result<i128, VaultError>`
49+
- `withdraw_to()``Result<i128, VaultError>`
50+
- `distribute()``Result<(), VaultError>`
51+
- `pause()``Result<(), VaultError>`
52+
- `unpause()``Result<(), VaultError>`
53+
- `set_admin()``Result<(), VaultError>`
54+
- `accept_admin()``Result<(), VaultError>`
55+
- `transfer_ownership()``Result<(), VaultError>`
56+
- `accept_ownership()``Result<(), VaultError>`
57+
- `set_authorized_caller()``Result<(), VaultError>`
58+
- `set_max_deduct()``Result<(), VaultError>`
59+
- `set_allowed_depositor()``Result<(), VaultError>`
60+
- `clear_allowed_depositors()``Result<(), VaultError>`
61+
- `set_revenue_pool()``Result<(), VaultError>`
62+
- `set_settlement()``Result<(), VaultError>`
63+
- `set_metadata()``Result<String, VaultError>`
64+
- `update_metadata()``Result<String, VaultError>`
65+
- `add_address()``Result<(), VaultError>`
66+
- `clear_all()``Result<(), VaultError>`
67+
68+
View functions:
69+
- `get_meta()``Result<VaultMeta, VaultError>`
70+
- `balance()``Result<i128, VaultError>`
71+
- `get_admin()``Result<Address, VaultError>`
72+
- `get_usdc_token()``Result<Address, VaultError>`
73+
- `get_settlement()``Result<Address, VaultError>`
74+
- `is_authorized_depositor()``Result<bool, VaultError>`
75+
76+
### 3. Updated Helper Functions
77+
Private helper functions now return `Result`:
78+
- `require_owner()``Result<(), VaultError>`
79+
- `require_authorized_deduct_caller()``Result<(), VaultError>`
80+
- `require_settlement()``Result<Address, VaultError>`
81+
- `require_not_paused()``Result<(), VaultError>`
82+
- `require_admin_or_owner()``Result<(), VaultError>`
83+
84+
### 4. Updated Documentation (`docs/interfaces/vault.json`)
85+
Added comprehensive error codes section with:
86+
- Error code number
87+
- Error name
88+
- Description
89+
90+
## Benefits
91+
92+
1. **Machine-Readable Errors**: Integrators can branch on error codes instead of parsing strings
93+
2. **Reduced WASM Size**: Typed errors are more compact than string panics
94+
3. **Better Developer Experience**: Clear error codes with stable u32 values
95+
4. **SDK Compatibility**: Works seamlessly with @stellar/stellar-sdk error handling
96+
97+
## Testing Notes
98+
99+
The contract compiles successfully with `cargo check`. Pre-existing test issues are unrelated to this implementation:
100+
- Tests reference non-existent methods (`remove_allowed_depositor`, `cancel_ownership_transfer`, `cancel_admin_transfer`)
101+
- These are pre-existing issues in the test suite
102+
103+
## WASM Size Impact
104+
105+
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`.
106+
107+
## Security Considerations
108+
109+
- All error paths maintain the same security guarantees as before
110+
- No authorization bypasses introduced
111+
- Arithmetic overflow still properly detected and returned as errors
112+
- CEI (Checks-Effects-Interactions) pattern preserved
113+
114+
## Backward Compatibility
115+
116+
This is a breaking change for integrators:
117+
- All functions now return `Result` types
118+
- Callers must handle errors explicitly
119+
- Error codes are stable and documented
120+
121+
## Next Steps
122+
123+
1. Update test suite to handle `Result` types
124+
2. Update integration tests to assert on specific error codes
125+
3. Verify WASM size with `check-wasm-size.sh`
126+
4. Update client SDK documentation with error code handling examples

0 commit comments

Comments
 (0)