feat: implement session top-up functionality to extend booking duration#57
Conversation
…on and deduct additional tokens.
📝 WalkthroughWalkthroughThe PR implements a new Changes
Sequence DiagramsequenceDiagram
participant User
participant Contract
participant Storage
participant TokenSystem
participant EventLog
User->>Contract: top_up_session(user, booking_id, additional_duration)
Contract->>Contract: Validate contract not paused
Contract->>Contract: Verify user authorization
Contract->>Storage: Fetch booking by booking_id
Contract->>Contract: Check booking exists & is Pending
Contract->>Contract: Calculate extra_cost = rate_per_second × additional_duration
Contract->>TokenSystem: Transfer extra_cost tokens from user to vault
Contract->>Storage: Update booking.total_deposit += extra_cost
Contract->>Storage: Update booking.max_duration += additional_duration
Contract->>Storage: Persist updated booking
Contract->>EventLog: Emit session_topped_up event
EventLog-->>User: Event published
Contract-->>User: Return Ok(())
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 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🧪 Generate unit tests (beta)
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.
🧹 Nitpick comments (2)
contracts/payment-vault-contract/src/test.rs (2)
1452-1481: Consider adding balance assertions for robustness.The test correctly validates that a non-owner cannot top up, but doesn't verify that no tokens were transferred. Adding balance assertions would strengthen the test.
💡 Optional enhancement
let result = client.try_top_up_session(&other_user, &booking_id, &900); assert!(result.is_err()); + + // Verify no tokens were transferred + let initial_deposit = rate_per_second * (initial_duration as i128); + assert_eq!(token.balance(&client.address), initial_deposit); + assert_eq!(token.balance(&other_user), 40_000); }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@contracts/payment-vault-contract/src/test.rs` around lines 1452 - 1481, Add assertions in test_top_up_wrong_user_fails to verify no token transfer occurred: capture balances (via token.balance_of or equivalent) for the payer (other_user), the intended payee or vault, and optionally the original user before calling client.try_top_up_session, then assert those balances are unchanged after the failed try_top_up_session call and that result.is_err() remains true; reference token.mint, token.balance_of, client.try_top_up_session and booking_id to locate where to insert the pre- and post-call balance checks.
1406-1481: Consider adding tests for edge cases and started sessions.The test suite covers the happy path and wrong-user rejection but omits several scenarios:
- Top-up after
mark_session_started— This would confirm the intended "live session" behavior sincestarted_atcan be set while status is stillPending.- Top-up on finalized/cancelled booking — Validates
BookingNotPendingerror.- Top-up with
additional_duration = 0— ValidatesInvalidAmounterror.- Top-up when contract is paused — Validates
ContractPausederror.Would you like me to generate these additional test cases?
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@contracts/payment-vault-contract/src/test.rs` around lines 1406 - 1481, Add four tests around try_top_up_session: (1) test_top_up_after_mark_started — create a booking, call mark_session_started on the booking (so started_at is set while status remains Pending) then call try_top_up_session and assert it returns Err (the live-session behavior you expect); (2) test_top_up_on_finalized_or_cancelled — create a booking, call cancel_booking or whatever transitions it to a non-Pending state, then call try_top_up_session and assert it returns BookingNotPending; (3) test_top_up_zero_duration — create a booking and call try_top_up_session with additional_duration = 0 and assert it returns InvalidAmount; (4) test_top_up_when_paused — init client, create a booking, pause the contract (use the contract pause method) then call try_top_up_session and assert it returns ContractPaused; reuse existing helpers (create_client, book_session, token.mint) and assert specific error variants from try_top_up_session.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@contracts/payment-vault-contract/src/test.rs`:
- Around line 1452-1481: Add assertions in test_top_up_wrong_user_fails to
verify no token transfer occurred: capture balances (via token.balance_of or
equivalent) for the payer (other_user), the intended payee or vault, and
optionally the original user before calling client.try_top_up_session, then
assert those balances are unchanged after the failed try_top_up_session call and
that result.is_err() remains true; reference token.mint, token.balance_of,
client.try_top_up_session and booking_id to locate where to insert the pre- and
post-call balance checks.
- Around line 1406-1481: Add four tests around try_top_up_session: (1)
test_top_up_after_mark_started — create a booking, call mark_session_started on
the booking (so started_at is set while status remains Pending) then call
try_top_up_session and assert it returns Err (the live-session behavior you
expect); (2) test_top_up_on_finalized_or_cancelled — create a booking, call
cancel_booking or whatever transitions it to a non-Pending state, then call
try_top_up_session and assert it returns BookingNotPending; (3)
test_top_up_zero_duration — create a booking and call try_top_up_session with
additional_duration = 0 and assert it returns InvalidAmount; (4)
test_top_up_when_paused — init client, create a booking, pause the contract (use
the contract pause method) then call try_top_up_session and assert it returns
ContractPaused; reuse existing helpers (create_client, book_session, token.mint)
and assert specific error variants from try_top_up_session.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 018a0883-7ad3-4dfc-b974-dbfb6d962570
📒 Files selected for processing (4)
contracts/payment-vault-contract/src/contract.rscontracts/payment-vault-contract/src/events.rscontracts/payment-vault-contract/src/lib.rscontracts/payment-vault-contract/src/test.rs
Graceful Session Extensions (Top-Ups)
Overview
This PR implements the requested "Graceful Session Extensions" feature for the
payment-vault-contract. This functionality allows users to increase the maximum duration of a pending (or live) session by depositing additional funds without needing to disconnect or create a new booking ID.Changes
Verification
payment-vault-contract.extra_cost.Closes #33
Summary by CodeRabbit
New Features
Tests