feat(vault-contract): add view functions, events, and ROI#26
feat(vault-contract): add view functions, events, and ROI#26AnoukRImola wants to merge 2 commits intoTrustless-Work:developfrom
Conversation
|
@AnoukRImola is attempting to deploy a commit to the Trustless Work Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthroughAdds getter and view functions, event emission system, typed storage keys, and ROI preview capabilities to the Vault contract. Renames storage key "price" to "roi_percentage" and replaces string-based keys with a typed enum. Introduces ClaimEvent and AvailabilityChangedEvent for external tracking. Changes
Sequence DiagramsequenceDiagram
actor Client
participant Vault as VaultContract
participant Storage as Storage Keys<br/>(DataKey Enum)
participant Events as Event Emitter
rect rgba(100, 150, 200, 0.5)
Note over Client,Events: Claim Flow with ROI & Event Emission
Client->>Vault: claim(beneficiary)
activate Vault
Vault->>Storage: read(Enabled)
Storage-->>Vault: enabled_flag
alt Vault Disabled
Vault-->>Client: Error
else Vault Enabled
Vault->>Storage: read(TotalTokensRedeemed)
Storage-->>Vault: previous_total
Vault->>Storage: read(RoiPercentage)
Storage-->>Vault: roi_pct
Note over Vault: Compute USDC = tokens × roi_pct
Vault->>Storage: read(UsdcAddress)
Storage-->>Vault: usdc_addr
Note over Vault: Validate vault balance ≥ usdc_amount
alt Insufficient Balance
Vault-->>Client: Error
else Sufficient Balance
Vault->>Storage: write(TotalTokensRedeemed, new_total)
Vault->>Events: emit_claim(ClaimEvent)
Events-->>Vault: ✓
Vault-->>Client: Success
end
end
deactivate Vault
end
rect rgba(150, 100, 200, 0.5)
Note over Client,Events: Availability Toggle with Event
Client->>Vault: availability_for_exchange(admin, enabled)
activate Vault
Vault->>Storage: validate(admin)
alt Not Admin
Vault-->>Client: Error
else Is Admin
Vault->>Storage: write(Enabled, enabled)
Vault->>Events: emit_availability_changed(AvailabilityChangedEvent)
Events-->>Vault: ✓
Vault-->>Client: Success
end
deactivate Vault
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~50 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@apps/smart-contracts/contracts/vault-contract/src/test.rs`:
- Around line 524-549: The test currently asserts any event was emitted, causing
false positives from token transfer events; update
test_availability_change_emits_event to filter env.events().all() by the vault
contract address (vault.address) and the first topic symbol matching "avail"
using Symbol::try_from_val/TryFromVal on topics.get(0), then assert that the
filtered list is non-empty; follow the same pattern used for claim events
(filtering by vault.address and topic "claim") to locate the specific vault
event.
🧹 Nitpick comments (2)
apps/smart-contracts/contracts/vault-contract/src/vault.rs (2)
60-80: Consider validatingroi_percentagerange in the constructor.The
roi_percentageparameter accepts anyi128value, including negative values (which would penalize claimants) or extremely large values (which could cause overflow in the calculationtoken_balance * (100 + roi_percentage)).Consider adding validation to ensure sensible bounds:
💡 Suggested validation
pub fn __constructor( env: Env, admin: Address, enabled: bool, roi_percentage: i128, token: Address, usdc: Address, ) { + // Validate ROI percentage is within reasonable bounds + if roi_percentage < 0 || roi_percentage > 1000 { + panic!("ROI percentage must be between 0 and 1000"); + } + env.storage().instance().set(&DataKey::Admin, &admin);
171-171: Consider extracting ROI calculation to a helper function (optional).The USDC amount calculation
(token_balance * (100 + roi_percentage)) / 100is duplicated inclaim()(line 171) andpreview_claim()(line 303). Extracting this to a private helper would improve maintainability:♻️ Optional: Extract helper function
impl VaultContract { /// Calculates the USDC amount for a given token balance and ROI percentage. fn calculate_usdc_payout(token_balance: i128, roi_percentage: i128) -> i128 { (token_balance * (100 + roi_percentage)) / 100 } // Then use in claim(): // let usdc_amount = Self::calculate_usdc_payout(token_balance, roi_percentage); // And in preview_claim(): // let usdc_amount = if token_balance > 0 { // Self::calculate_usdc_payout(token_balance, roi_percentage) // } else { 0 }; }Also applies to: 302-303
Feat(vault-contract): Add view functions, events, and ROI preview for v2 improvements
Close: #17
Description
This PR implements the Vault Contract v2 improvements as defined in the PRD,
transforming it from a minimalistic ROI-distribution contract into a fully
observable, dashboard-friendly, and indexer-compatible module.
Summary
Changes
New Files
File: storage_types.rs
Description: Typed DataKey enum replacing raw string storage keys
────────────────────────────────────────
File: events.rs
Description: ClaimEvent and AvailabilityChangedEvent structs with emission
helpers
New View Functions
fn get_admin(env: Env) -> Address
fn is_enabled(env: Env) -> bool
fn get_roi_percentage(env: Env) -> i128
fn get_token_address(env: Env) -> Address
fn get_usdc_address(env: Env) -> Address
fn get_vault_usdc_balance(env: Env) -> i128
fn get_total_tokens_redeemed(env: Env) -> i128
Preview Function
fn preview_claim(env: Env, beneficiary: Address) -> ClaimPreview
Returns: token_balance, usdc_amount, roi_amount, vault_has_sufficient_balance,
claim_enabled
Vault Overview
fn get_vault_overview(env: Env) -> VaultOverview
Returns a complete snapshot of the vault state in a single call.
Events
usdc_received, roi_percentage
Test Plan
ROI)
Test Results
23 tests passing ✅

Summary by CodeRabbit
Release Notes
New Features
Tests
✏️ Tip: You can customize this high-level summary in your review settings.