-
Notifications
You must be signed in to change notification settings - Fork 8
Feat/payment vault events #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| use soroban_sdk::{Env, symbol_short}; | ||
|
|
||
| pub fn session_finalized(env: &Env, booking_id: u64, actual_duration: u64, total_cost: i128) { | ||
| let topics = (symbol_short!("finalized"), booking_id); | ||
| env.events().publish(topics, (actual_duration, total_cost)); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,6 +6,27 @@ pub enum DataKey { | |||||||||||||||||||||||||||||||||||||||||
| Admin, | ||||||||||||||||||||||||||||||||||||||||||
| Token, | ||||||||||||||||||||||||||||||||||||||||||
| Oracle, | ||||||||||||||||||||||||||||||||||||||||||
| Booking(u64), // Booking ID -> Booking | ||||||||||||||||||||||||||||||||||||||||||
| BookingCounter, // Counter for generating unique booking IDs | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| #[contracttype] | ||||||||||||||||||||||||||||||||||||||||||
| #[derive(Clone, Debug, Eq, PartialEq)] | ||||||||||||||||||||||||||||||||||||||||||
| pub enum BookingStatus { | ||||||||||||||||||||||||||||||||||||||||||
| Pending, | ||||||||||||||||||||||||||||||||||||||||||
| Complete, | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| #[contracttype] | ||||||||||||||||||||||||||||||||||||||||||
| #[derive(Clone, Debug)] | ||||||||||||||||||||||||||||||||||||||||||
| pub struct Booking { | ||||||||||||||||||||||||||||||||||||||||||
| pub id: u64, | ||||||||||||||||||||||||||||||||||||||||||
| pub expert: Address, | ||||||||||||||||||||||||||||||||||||||||||
| pub user: Address, | ||||||||||||||||||||||||||||||||||||||||||
| pub rate: i128, // Payment per second | ||||||||||||||||||||||||||||||||||||||||||
| pub total_deposit: i128, // Total amount deposited by user | ||||||||||||||||||||||||||||||||||||||||||
| pub booked_duration: u64, // Booked duration in seconds | ||||||||||||||||||||||||||||||||||||||||||
| pub status: BookingStatus, | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // --- Admin --- | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -37,4 +58,36 @@ pub fn set_oracle(env: &Env, oracle: &Address) { | |||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| pub fn get_oracle(env: &Env) -> Address { | ||||||||||||||||||||||||||||||||||||||||||
| env.storage().instance().get(&DataKey::Oracle).unwrap() | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // --- Booking Counter --- | ||||||||||||||||||||||||||||||||||||||||||
| pub fn get_next_booking_id(env: &Env) -> u64 { | ||||||||||||||||||||||||||||||||||||||||||
| let current: u64 = env | ||||||||||||||||||||||||||||||||||||||||||
| .storage() | ||||||||||||||||||||||||||||||||||||||||||
| .instance() | ||||||||||||||||||||||||||||||||||||||||||
| .get(&DataKey::BookingCounter) | ||||||||||||||||||||||||||||||||||||||||||
| .unwrap_or(0); | ||||||||||||||||||||||||||||||||||||||||||
| let next = current + 1; | ||||||||||||||||||||||||||||||||||||||||||
| env.storage().instance().set(&DataKey::BookingCounter, &next); | ||||||||||||||||||||||||||||||||||||||||||
| next | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+64
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Storage type mismatch could cause booking ID reuse.
Consider using persistent storage for π§ Suggested fix pub fn get_next_booking_id(env: &Env) -> u64 {
let current: u64 = env
.storage()
- .instance()
+ .persistent()
.get(&DataKey::BookingCounter)
.unwrap_or(0);
let next = current + 1;
- env.storage().instance().set(&DataKey::BookingCounter, &next);
+ env.storage().persistent().set(&DataKey::BookingCounter, &next);
next
}π Committable suggestion
Suggested change
π€ Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // --- Bookings --- | ||||||||||||||||||||||||||||||||||||||||||
| pub fn save_booking(env: &Env, booking: &Booking) { | ||||||||||||||||||||||||||||||||||||||||||
| env.storage() | ||||||||||||||||||||||||||||||||||||||||||
| .persistent() | ||||||||||||||||||||||||||||||||||||||||||
| .set(&DataKey::Booking(booking.id), booking); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| pub fn get_booking(env: &Env, booking_id: u64) -> Option<Booking> { | ||||||||||||||||||||||||||||||||||||||||||
| env.storage() | ||||||||||||||||||||||||||||||||||||||||||
| .persistent() | ||||||||||||||||||||||||||||||||||||||||||
| .get(&DataKey::Booking(booking_id)) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| pub fn update_booking_status(env: &Env, booking_id: u64, status: BookingStatus) { | ||||||||||||||||||||||||||||||||||||||||||
| if let Some(mut booking) = get_booking(env, booking_id) { | ||||||||||||||||||||||||||||||||||||||||||
| booking.status = status; | ||||||||||||||||||||||||||||||||||||||||||
| save_booking(env, &booking); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing validation:
actual_durationcan exceedbooked_duration.The Oracle can pass an
actual_durationgreater thanbooked_duration, which would makerefundnegative (line 93) and triggerInvalidAmount. While this prevents overpayment, the error is misleading.Consider adding explicit validation to reject
actual_duration > booked_durationwith a clearer error, or capactual_durationatbooked_duration.π§ Option 1: Explicit validation
π§ Option 2: Cap duration
π Committable suggestion
π€ Prompt for AI Agents