-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathlib.rs
More file actions
961 lines (826 loc) · 36.4 KB
/
Copy pathlib.rs
File metadata and controls
961 lines (826 loc) · 36.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
#![no_std]
use soroban_sdk::{contract, contractimpl, Address, BytesN, Env, String, Vec};
// 1. Declare the foundational modules (Requirement: Modular Structure)
pub mod base {
pub mod channel;
pub mod errors;
pub mod events;
pub mod metadata_validation;
pub mod preferences;
pub mod reputation;
pub mod types;
}
pub mod interfaces {
pub mod autoshare;
}
// 2. Declare the main logic files where the functions are implemented
mod autoshare_logic;
mod channel_logic;
mod preferences_logic;
mod reputation_logic;
mod template_registry_logic;
#[cfg(test)]
pub mod mock_token;
#[contract]
pub struct AutoShareContract;
const VERSION: u32 = 1;
#[contractimpl]
impl AutoShareContract {
/// Returns the current version of the contract.
pub fn version(_env: Env) -> u32 {
VERSION
}
// ============================================================================
// Admin Management
// ============================================================================
/// Initializes the contract admin. Can only be called once.
pub fn initialize_admin(env: Env, admin: Address) {
autoshare_logic::initialize_admin(env, admin);
}
/// Pauses the contract. Only admin can call.
pub fn pause(env: Env, admin: Address) {
autoshare_logic::pause(env, admin).unwrap();
}
/// Unpauses the contract. Only admin can call.
pub fn unpause(env: Env, admin: Address) {
autoshare_logic::unpause(env, admin).unwrap();
}
/// Returns the current pause status.
pub fn get_paused_status(env: Env) -> bool {
autoshare_logic::get_paused_status(&env)
}
/// Registers a notification category in the on-chain registry.
pub fn register_category(
env: Env,
admin: Address,
category: base::events::NotificationCategory,
) {
autoshare_logic::register_category(env, admin, category).unwrap();
}
/// Returns all registered notification categories.
pub fn get_registered_categories(
env: Env,
) -> soroban_sdk::Vec<base::events::NotificationCategory> {
autoshare_logic::get_registered_categories(env)
}
/// Returns whether a notification category is registered.
pub fn is_category_registered(env: Env, category: base::events::NotificationCategory) -> bool {
autoshare_logic::is_category_registered(env, category)
}
// ============================================================================
// AutoShare Group Management
// ============================================================================
/// Creates a new AutoShare plan with payment.
/// Requirement: create_autoshare should store data, accept payment, and emit an event.
pub fn create(
env: Env,
id: BytesN<32>,
name: String,
creator: Address,
usage_count: u32,
payment_token: Address,
) {
autoshare_logic::create_autoshare(env, id, name, creator, usage_count, payment_token)
.unwrap();
}
/// Update members of an existing AutoShare plan.
/// Requirement: Only creator can update. Validates percentages.
pub fn update_members(
env: Env,
id: BytesN<32>,
caller: Address,
new_members: Vec<base::types::GroupMember>,
) {
autoshare_logic::update_members(env, id, caller, new_members).unwrap();
}
/// Retrieves an existing AutoShare plan.
/// Requirement: get_autoshare should return the plan details.
pub fn get(env: Env, id: BytesN<32>) -> base::types::AutoShareDetails {
autoshare_logic::get_autoshare(env, id).unwrap()
}
/// Retrieves all AutoShare groups.
pub fn get_all_groups(env: Env) -> Vec<base::types::AutoShareDetails> {
autoshare_logic::get_all_groups(env)
}
/// Retrieves all AutoShare groups created by a specific address.
pub fn get_groups_by_creator(env: Env, creator: Address) -> Vec<base::types::AutoShareDetails> {
autoshare_logic::get_groups_by_creator(env, creator)
}
/// Checks if an address is a member of a specific group.
pub fn is_group_member(env: Env, id: BytesN<32>, address: Address) -> bool {
autoshare_logic::is_group_member(env, id, address).unwrap()
}
/// Returns whether `wallet` is actively subscribed to the channel
/// identified by `id` — the channel must be active and `wallet` must be
/// its creator or a registered member. Read-only.
pub fn is_subscribed(env: Env, id: BytesN<32>, wallet: Address) -> bool {
autoshare_logic::is_subscribed(env, id, wallet).unwrap()
}
pub fn get_group_members(env: Env, id: BytesN<32>) -> Vec<base::types::GroupMember> {
autoshare_logic::get_group_members(env, id).unwrap()
}
/// Adds a member to a group with specified percentage.
pub fn add_group_member(
env: Env,
id: BytesN<32>,
caller: Address,
address: Address,
percentage: u32,
) {
autoshare_logic::add_group_member(env, id, caller, address, percentage).unwrap();
}
/// Deactivates a channel (group). Callable by the creator or the contract
/// admin. Historical membership and payment history remain visible; new
/// subscriptions (top-ups) are blocked until reactivated.
pub fn deactivate_group(env: Env, id: BytesN<32>, caller: Address) {
autoshare_logic::deactivate_group(env, id, caller).unwrap();
}
/// Activates a group. Only the creator can activate.
pub fn activate_group(env: Env, id: BytesN<32>, caller: Address) {
autoshare_logic::activate_group(env, id, caller).unwrap();
}
/// Returns whether a group is active.
pub fn is_group_active(env: Env, id: BytesN<32>) -> bool {
autoshare_logic::is_group_active(env, id).unwrap()
}
/// Returns the current admin address.
pub fn get_admin(env: Env) -> Address {
autoshare_logic::get_admin(env).unwrap()
}
/// Transfers admin rights to a new address. Only current admin can call.
/// Rejects transfers where new_admin == current_admin.
pub fn transfer_admin(env: Env, current_admin: Address, new_admin: Address) {
autoshare_logic::transfer_admin(env, current_admin, new_admin).unwrap();
}
// ============================================================================
// Two-Step Ownership Transfer (Issue #367)
// ============================================================================
/// Returns the address currently nominated as the pending owner, or `None`
/// if no two-step ownership transfer is currently in progress.
pub fn get_pending_owner(env: Env) -> Option<Address> {
autoshare_logic::get_pending_owner(env)
}
/// Initiates a two-step ownership transfer.
///
/// The current owner nominates `new_owner` as the pending owner. The transfer
/// is NOT final until `new_owner` calls `accept_ownership`. Until then the
/// current owner retains all privileges.
///
/// Rejects:
/// - Callers that are not the current owner (panics with `Unauthorized`).
/// - A `new_owner` equal to the current owner (`ZeroAddressTransfer`).
///
/// Emits: `OwnershipTransferInitiated { previous_owner, pending_owner }`.
pub fn initiate_ownership_transfer(env: Env, current_owner: Address, new_owner: Address) {
autoshare_logic::initiate_ownership_transfer(env, current_owner, new_owner).unwrap();
}
/// Completes a two-step ownership transfer.
///
/// Must be called by the address previously nominated via
/// `initiate_ownership_transfer`. On success the caller becomes the new
/// owner and the pending-owner slot is cleared.
///
/// Rejects:
/// - No pending transfer in progress (`NoPendingOwnershipTransfer`).
/// - Caller is not the pending owner (`NotPendingOwner`).
///
/// Emits: `OwnershipTransferred { previous_owner, new_owner }`.
pub fn accept_ownership(env: Env, new_owner: Address) {
autoshare_logic::accept_ownership(env, new_owner).unwrap();
}
/// Withdraws tokens from the contract. Only admin can call.
pub fn withdraw(env: Env, admin: Address, token: Address, amount: i128, recipient: Address) {
autoshare_logic::withdraw(env, admin, token, amount, recipient).unwrap();
}
/// Returns the contract's balance for a specified token.
pub fn get_contract_balance(env: Env, token: Address) -> i128 {
autoshare_logic::get_contract_balance(env, token)
}
// ============================================================================
// Token Management
// ============================================================================
/// Adds a supported payment token (admin only).
pub fn add_supported_token(env: Env, token: Address, admin: Address) {
autoshare_logic::add_supported_token(env, token, admin).unwrap();
}
/// Removes a supported payment token (admin only).
pub fn remove_supported_token(env: Env, token: Address, admin: Address) {
autoshare_logic::remove_supported_token(env, token, admin).unwrap();
}
/// Returns all supported payment tokens.
pub fn get_supported_tokens(env: Env) -> Vec<Address> {
autoshare_logic::get_supported_tokens(env)
}
/// Checks if a token is supported.
pub fn is_token_supported(env: Env, token: Address) -> bool {
autoshare_logic::is_token_supported(env, token)
}
// ============================================================================
// Payment Configuration
// ============================================================================
/// Sets the usage fee (admin only).
pub fn set_usage_fee(env: Env, fee: u32, admin: Address) {
autoshare_logic::set_usage_fee(env, fee, admin).unwrap();
}
/// Returns the current usage fee.
pub fn get_usage_fee(env: Env) -> u32 {
autoshare_logic::get_usage_fee(env)
}
// ============================================================================
// Subscription Management
// ============================================================================
/// Tops up a group's subscription with additional usages.
pub fn topup_subscription(
env: Env,
id: BytesN<32>,
additional_usages: u32,
payment_token: Address,
payer: Address,
) {
autoshare_logic::topup_subscription(env, id, additional_usages, payment_token, payer)
.unwrap();
}
/// Cancels an active notification subscription for a group.
///
/// The `subscriber` must be the group's creator or a current member. On
/// success the group is deactivated, its remaining usage count is zeroed, and
/// a `SubscriptionCancelled` event is emitted so off-chain consumers can
/// track the full subscription lifecycle.
pub fn cancel_subscription(env: Env, id: BytesN<32>, subscriber: Address) {
autoshare_logic::cancel_subscription(env, id, subscriber).unwrap();
}
// ============================================================================
// Payment History
// ============================================================================
/// Returns all payment history for a user.
pub fn get_user_payment_history(env: Env, user: Address) -> Vec<base::types::PaymentHistory> {
autoshare_logic::get_user_payment_history(env, user)
}
/// Returns all payment history for a group.
pub fn get_group_payment_history(env: Env, id: BytesN<32>) -> Vec<base::types::PaymentHistory> {
autoshare_logic::get_group_payment_history(env, id)
}
// ============================================================================
// Usage Tracking
// ============================================================================
/// Returns the remaining usages for a group.
pub fn get_remaining_usages(env: Env, id: BytesN<32>) -> u32 {
autoshare_logic::get_remaining_usages(env, id).unwrap()
}
/// Returns the total usages paid for a group.
pub fn get_total_usages_paid(env: Env, id: BytesN<32>) -> u32 {
autoshare_logic::get_total_usages_paid(env, id).unwrap()
}
/// Reduces the usage count by 1.
pub fn reduce_usage(env: Env, id: BytesN<32>) {
let caller = env.current_contract_address();
autoshare_logic::reduce_usage(env, id, caller).unwrap();
}
// ============================================================================
// Recipient Preference Management (Issue #178)
// ============================================================================
/// Returns the full notification preferences for `recipient`.
/// Returns all-enabled defaults if the recipient has never set preferences.
pub fn get_preferences(
env: Env,
recipient: Address,
) -> base::preferences::RecipientPreferences {
preferences_logic::get_preferences(env, recipient)
}
/// Atomically replace all channel and category preferences for `recipient`.
/// Caller must be `recipient` (auth required).
pub fn set_preferences(
env: Env,
recipient: Address,
channels: Vec<base::preferences::ChannelPreference>,
categories: Vec<base::preferences::CategoryPreference>,
) {
preferences_logic::set_preferences(env, recipient, channels, categories).unwrap();
}
/// Toggle a single delivery channel on or off.
/// Caller must be `recipient` (auth required).
pub fn set_channel_preference(
env: Env,
recipient: Address,
channel: base::preferences::DeliveryChannel,
enabled: bool,
) {
preferences_logic::set_channel_preference(env, recipient, channel, enabled).unwrap();
}
/// Toggle a single notification category on or off.
/// Caller must be `recipient` (auth required).
pub fn set_category_preference(
env: Env,
recipient: Address,
category: base::preferences::NotificationCategory,
enabled: bool,
) {
preferences_logic::set_category_preference(env, recipient, category, enabled).unwrap();
}
/// Reset all preferences to the all-enabled defaults.
/// Caller must be `recipient` (auth required).
pub fn reset_preferences(env: Env, recipient: Address) {
preferences_logic::reset_preferences(env, recipient).unwrap();
}
/// Returns true if the specified delivery channel is enabled for `recipient`.
pub fn is_channel_enabled(
env: Env,
recipient: Address,
channel: base::preferences::DeliveryChannel,
) -> bool {
preferences_logic::is_channel_enabled(env, recipient, channel)
}
/// Returns true if the specified notification category is enabled for `recipient`.
pub fn is_category_enabled(
env: Env,
recipient: Address,
category: base::preferences::NotificationCategory,
) -> bool {
preferences_logic::is_category_enabled(env, recipient, category)
}
// ============================================================================
// Scheduled Notification Management
// ============================================================================
/// Cancels a scheduled notification and emits a ScheduledNotificationCancelled event.
///
/// The `notification_id` uniquely identifies the notification being cancelled.
/// Callers must authenticate. The contract is paused-aware: cancellations are
/// rejected while the contract is paused.
pub fn cancel_notification(env: Env, notification_id: BytesN<32>, caller: Address) {
autoshare_logic::cancel_notification(env, notification_id, caller).unwrap();
}
// ============================================================================
// Notification Expiration
// ============================================================================
/// Schedules a notification on-chain that expires after `ttl_seconds`.
///
/// The notification becomes invalid once the ledger timestamp reaches
/// `created_at + ttl_seconds`. Metadata (title) is validated for consistency.
/// `priority` (High, Medium, or Low) determines the order in which
/// off-chain consumers should process and deliver the notification, and is
/// stored on-chain alongside it. Emits a `NotificationScheduled` event
/// carrying the assigned priority.
pub fn schedule_notification(
env: Env,
notification_id: BytesN<32>,
creator: Address,
ttl_seconds: u64,
title: String,
priority: base::events::NotificationPriority,
) {
autoshare_logic::schedule_notification(
env,
notification_id,
creator,
ttl_seconds,
title,
priority,
)
.unwrap();
}
/// Returns the stored details for a scheduled notification.
pub fn get_notification(
env: Env,
notification_id: BytesN<32>,
) -> base::types::ScheduledNotification {
autoshare_logic::get_notification(env, notification_id).unwrap()
}
/// Returns whether a scheduled notification has expired.
pub fn is_notification_expired(env: Env, notification_id: BytesN<32>) -> bool {
autoshare_logic::is_notification_expired(env, notification_id).unwrap()
}
/// Finalizes the expiry of a notification whose lifetime has elapsed,
/// emitting a `NotificationExpired` event. Callable by anyone.
pub fn expire_notification(env: Env, notification_id: BytesN<32>) {
autoshare_logic::expire_notification(env, notification_id).unwrap();
}
/// Confirms delivery of a scheduled notification.
///
/// Only the notification creator or the contract admin can confirm delivery.
/// The notification must exist, not already be revoked or expired, and not yet be marked delivered.
pub fn confirm_notification_delivery(env: Env, notification_id: BytesN<32>, caller: Address) {
autoshare_logic::confirm_notification_delivery(env, notification_id, caller).unwrap();
}
/// Recalls a scheduled notification before delivery confirmation.
///
/// Only the notification creator or the contract admin can recall a notification.
/// The notification must exist, not already be revoked or expired, and not yet be delivered.
pub fn recall_notification(env: Env, notification_id: BytesN<32>, caller: Address) {
autoshare_logic::recall_notification(env, notification_id, caller).unwrap();
}
/// Emits a `BatchProcessingCompleted` event for off-chain listeners.
pub fn emit_batch_completed(env: Env, batch_id: BytesN<32>, processed_count: u32) {
autoshare_logic::emit_batch_completed(env, batch_id, processed_count).unwrap();
}
// ============================================================================
// Batch Notification Creation
// ============================================================================
/// Creates multiple scheduled notifications in a single transaction.
///
/// `ids`, `ttl_seconds`, `titles`, and `priorities` must all have the same
/// length, must not be empty, and must not exceed 50 entries. Each
/// notification is stored with its own assigned priority (High, Medium, or
/// Low). Emits one `NotificationScheduled` event per notification (carrying
/// its assigned priority) plus a single `BatchNotificationsCreated` summary
/// event.
pub fn batch_schedule_notifications(
env: Env,
ids: Vec<BytesN<32>>,
creator: Address,
ttl_seconds: Vec<u64>,
titles: Vec<String>,
priorities: Vec<base::events::NotificationPriority>,
) {
autoshare_logic::batch_schedule_notifications(
env,
ids,
creator,
ttl_seconds,
titles,
priorities,
)
.unwrap();
}
// ============================================================================
// Audit Logging
// ============================================================================
/// Returns the full, immutable audit log in append order.
pub fn get_audit_log(env: Env) -> Vec<base::types::AuditRecord> {
autoshare_logic::get_audit_log(env)
}
/// Returns all audit records for a specific notification identifier.
pub fn get_notification_audit(
env: Env,
notification_id: BytesN<32>,
) -> Vec<base::types::AuditRecord> {
autoshare_logic::get_audit_records_for_notification(env, notification_id)
}
/// Records a delivery attempt for a notification in the audit log.
pub fn record_delivery_attempt(env: Env, notification_id: BytesN<32>, actor: Address) {
autoshare_logic::record_delivery_attempt(env, notification_id, actor).unwrap();
}
/// Records a delivery failure for a notification in the audit log.
pub fn record_delivery_failure(env: Env, notification_id: BytesN<32>, actor: Address) {
autoshare_logic::record_delivery_failure(env, notification_id, actor).unwrap();
}
/// Records that the recipient acknowledged a notification.
pub fn record_acknowledgment(env: Env, notification_id: BytesN<32>, actor: Address) {
autoshare_logic::record_acknowledgment(env, notification_id, actor).unwrap();
}
/// Revokes a scheduled notification, preventing any further interaction with it.
///
/// Only the notification creator or the contract admin can revoke a notification.
/// The notification must not already be revoked or expired. Emits a `NotificationRevoked` event.
pub fn revoke_notification(env: Env, notification_id: BytesN<32>, caller: Address) {
autoshare_logic::revoke_notification(env, notification_id, caller).unwrap();
}
/// Returns whether a scheduled notification has been revoked.
pub fn is_notification_revoked(env: Env, notification_id: BytesN<32>) -> bool {
autoshare_logic::is_notification_revoked(env, notification_id).unwrap()
}
/// Acknowledges multiple scheduled notifications in a single batch.
pub fn acknowledge_notifications(env: Env, caller: Address, notification_ids: Vec<BytesN<32>>) {
autoshare_logic::acknowledge_notifications(env, caller, notification_ids).unwrap();
}
/// Extends the expiration period of a scheduled notification by `extension_seconds`.
///
/// Only the notification creator or the contract admin can extend it.
/// The notification must exist, not already be revoked, and not have expired.
/// Emits a `NotificationExtended` event.
pub fn extend_notification_expiry(
env: Env,
notification_id: BytesN<32>,
caller: Address,
extension_seconds: u64,
) {
autoshare_logic::extend_notification_expiry(
env,
notification_id,
caller,
extension_seconds,
)
.unwrap();
}
// ============================================================================
// Notification Limits Configuration
// ============================================================================
/// Sets protocol-level notification limits (admin only).
/// Configurable limits include maximum payload size, expiration periods, and batch sizes.
/// Emits a `NotificationLimitsConfigured` event on successful configuration.
pub fn configure_notification_limits(
env: Env,
admin: Address,
max_payload_size: u32,
max_expiration_seconds: u64,
min_expiration_seconds: u64,
max_batch_size: u32,
) {
autoshare_logic::configure_notification_limits(
env,
admin,
max_payload_size,
max_expiration_seconds,
min_expiration_seconds,
max_batch_size,
)
.unwrap();
}
/// Returns the current notification limits.
pub fn get_notification_limits(env: Env) -> base::types::NotificationLimits {
autoshare_logic::get_notification_limits(env)
}
// ============================================================================
// Sender Reputation Tracking
// ============================================================================
/// Record a successful notification delivery for a sender.
/// Updates the sender's reputation score based on delivery history.
pub fn record_delivery_success(env: Env, sender: Address) {
reputation_logic::record_successful_delivery(&env, &sender).unwrap();
}
/// Record a failed notification delivery for a sender.
/// Decreases the sender's reputation score based on delivery history.
pub fn record_sender_delivery_failure(env: Env, sender: Address) {
reputation_logic::record_failed_delivery(&env, &sender).unwrap();
}
/// Get the current reputation score for a sender.
/// Score ranges from 0 (lowest) to 100 (highest).
pub fn get_sender_reputation_score(env: Env, sender: Address) -> i64 {
reputation_logic::get_reputation_score(&env, &sender).unwrap_or(50)
}
/// Get the complete reputation record for a sender.
/// Includes successful deliveries, failed deliveries, and current score.
pub fn get_sender_reputation(env: Env, sender: Address) -> base::reputation::SenderReputation {
reputation_logic::get_reputation(&env, &sender)
.unwrap_or_else(|_| base::reputation::SenderReputation::new(sender, env.ledger().timestamp()))
}
/// Get the reputation tier for a sender.
/// Tier levels: 0=Unverified, 1=Bronze, 2=Silver, 3=Gold, 4=Platinum
pub fn get_sender_reputation_tier(env: Env, sender: Address) -> u32 {
reputation_logic::get_reputation_tier(&env, &sender).unwrap_or(0)
}
// ============================================================================
// Template Registry (Issue #352)
// ============================================================================
/// Registers a new reusable notification template on-chain.
///
/// `id` must be unique — reverts with `AlreadyExists` if it is already taken.
/// `creator` becomes the sole owner and is the only address that can update it.
/// Emits a `TemplateRegistered` event on success.
pub fn register_template(
env: Env,
id: BytesN<32>,
creator: Address,
name: String,
content: String,
) {
template_registry_logic::register_template(env, id, creator, name, content).unwrap();
}
/// Updates the `name` and `content` of an existing template.
///
/// Only the original owner (creator) may call this function.
/// Reverts with `Unauthorized` for any other caller.
/// Reverts with `TemplateNotFound` if `id` is not registered.
/// Emits a `TemplateUpdated` event on success.
pub fn update_template(
env: Env,
id: BytesN<32>,
caller: Address,
name: String,
content: String,
) {
template_registry_logic::update_template(env, id, caller, name, content).unwrap();
}
/// Returns the full `NotificationTemplate` record for `id`.
///
/// Reverts with `TemplateNotFound` if `id` is not registered.
pub fn get_template(env: Env, id: BytesN<32>) -> base::types::NotificationTemplate {
template_registry_logic::get_template(env, id).unwrap()
}
/// Returns `true` if a template is registered under `id`, `false` otherwise.
///
/// This is a pure view function and never reverts.
pub fn template_exists(env: Env, id: BytesN<32>) -> bool {
template_registry_logic::template_exists(env, id)
}
// ============================================================================
// Schema Version Tracking (Issue #309)
// ============================================================================
/// Sets the on-chain notification schema version. Only the admin can call.
/// Emits a SchemaVersionSet event. Rejects versions outside the supported range.
pub fn set_schema_version(env: Env, admin: Address, schema_version: u32) {
autoshare_logic::set_schema_version(env, admin, schema_version).unwrap();
}
/// Returns the current on-chain schema version (0 if never set).
pub fn get_schema_version(env: Env) -> u32 {
autoshare_logic::get_schema_version(env)
}
/// Returns true if the given schema version is within the supported range.
pub fn is_version_supported(env: Env, version: u32) -> bool {
autoshare_logic::is_version_supported(env, version)
}
// ============================================================================
// Access Logging (Issue #312)
// ============================================================================
/// Emits a NotificationAccessed event for the specified notification.
/// Call whenever a protected notification record is read to build an immutable access trail.
pub fn record_notification_access(env: Env, notification_id: BytesN<32>, accessor: Address) {
autoshare_logic::record_notification_access(env, notification_id, accessor).unwrap();
}
// ============================================================================
// Channel Metadata Updates
// ============================================================================
/// Updates channel description and custom metadata. Restricted to the channel creator.
/// Emits `ChannelMetadataUpdated`. Existing subscribers / members are unaffected.
pub fn update_channel_metadata(
env: Env,
channel_id: BytesN<32>,
caller: Address,
description: String,
custom_fields: soroban_sdk::Map<String, String>,
) {
autoshare_logic::update_channel_metadata(
env,
channel_id,
caller,
description,
custom_fields,
)
.unwrap();
}
/// Returns channel metadata for an AutoShare group / channel.
pub fn get_channel_metadata(
env: Env,
channel_id: BytesN<32>,
) -> base::types::ChannelMetadata {
autoshare_logic::get_channel_metadata(env, channel_id).unwrap()
}
// ============================================================================
// Notification Versioning & Archive
// ============================================================================
/// Returns the current notification payload protocol version.
pub fn get_notification_version(env: Env) -> u32 {
autoshare_logic::get_notification_version(env)
}
/// Returns an archived notification by id (accessible after processing).
pub fn get_archived_notification(
env: Env,
notification_id: BytesN<32>,
) -> base::types::ArchivedNotification {
autoshare_logic::get_archived_notification(env, notification_id).unwrap()
}
}
#[cfg(test)]
#[path = "tests/test_utils.rs"]
pub mod test_utils;
#[cfg(test)]
mod tests {
// Preexisting broken suites temporarily excluded so new feature tests can compile.
// mod test_utils_test;
// mod storage_optimization_test;
// mod preferences_test;
// mod autoshare_test;
// mod pause_test;
// mod mock_token_test;
mod version_test;
// mod notification_test;
// mod expiration_test;
// mod revocation_test;
// mod ownership_transfer_test;
// mod notification_validation_test;
// mod category_registry_test;
// mod batch_notification_test;
// mod audit_log_test;
// mod payload_validation_test;
// mod batch_ack_test;
// mod fuzz_test;
mod schema_version_test;
// mod access_log_test;
// mod subscription_cancellation_test;
mod channel_metadata_test;
mod notification_version_test;
mod metadata_validation_test;
mod archive_notification_test;
// ============================================================================
// Notification Channel Subscriptions
// ============================================================================
/// Creates a notification channel and permanently stores the creator address.
pub fn create_channel(env: Env, id: BytesN<32>, name: String, creator: Address) {
channel_logic::create_channel(env, id, name, creator).unwrap();
}
/// Returns full channel metadata (creator, name, subscriber_count, etc.).
pub fn get_channel(env: Env, id: BytesN<32>) -> base::channel::NotificationChannel {
channel_logic::get_channel(env, id).unwrap()
}
/// Returns the wallet address that originally created the channel.
pub fn get_channel_creator(env: Env, id: BytesN<32>) -> Address {
channel_logic::get_channel_creator(env, id).unwrap()
}
/// Read-only view of the active subscriber count for a channel.
pub fn get_subscriber_count(env: Env, id: BytesN<32>) -> u32 {
channel_logic::get_subscriber_count(env, id).unwrap()
}
/// Returns whether `subscriber` is currently subscribed to the channel.
pub fn is_channel_subscriber(env: Env, id: BytesN<32>, subscriber: Address) -> bool {
channel_logic::is_channel_subscriber(env, id, subscriber)
}
/// Subscribe to a single notification channel.
pub fn subscribe(env: Env, channel_id: BytesN<32>, subscriber: Address) {
channel_logic::subscribe(env, channel_id, subscriber).unwrap();
}
/// Unsubscribe from a notification channel.
pub fn unsubscribe(env: Env, channel_id: BytesN<32>, subscriber: Address) {
channel_logic::unsubscribe(env, channel_id, subscriber).unwrap();
}
/// Subscribe to multiple channels in one transaction.
///
/// Individual failures are skipped without corrupting successful subscriptions.
/// See `docs/BATCH_SUBSCRIBE_GAS.md` for gas documentation.
pub fn batch_subscribe(
env: Env,
channel_ids: Vec<BytesN<32>>,
subscriber: Address,
) -> base::channel::BatchSubscribeResult {
channel_logic::batch_subscribe(env, channel_ids, subscriber).unwrap()
}
}
#[cfg(test)]
pub mod test_utils {
#[path = "../tests/test_utils.rs"]
mod inner;
pub use inner::*;
}
#[cfg(test)]
mod tests {
#[path = "tests/test_utils_test.rs"]
mod test_utils_test;
#[path = "tests/storage_optimization_test.rs"]
mod storage_optimization_test;
#[path = "tests/preferences_test.rs"]
mod preferences_test;
#[path = "tests/autoshare_test.rs"]
mod autoshare_test;
#[path = "tests/pause_test.rs"]
mod pause_test;
#[path = "tests/mock_token_test.rs"]
mod mock_token_test;
#[path = "tests/version_test.rs"]
mod version_test;
#[path = "tests/notification_test.rs"]
mod notification_test;
#[path = "tests/expiration_test.rs"]
mod expiration_test;
#[path = "tests/revocation_test.rs"]
mod revocation_test;
#[path = "tests/ownership_transfer_test.rs"]
mod ownership_transfer_test;
#[path = "tests/notification_validation_test.rs"]
mod notification_validation_test;
#[path = "tests/category_registry_test.rs"]
mod category_registry_test;
#[path = "tests/batch_notification_test.rs"]
mod batch_notification_test;
#[path = "tests/audit_log_test.rs"]
mod audit_log_test;
#[path = "tests/payload_validation_test.rs"]
mod payload_validation_test;
#[path = "tests/batch_ack_test.rs"]
mod batch_ack_test;
#[path = "tests/fuzz_test.rs"]
mod fuzz_test;
#[path = "tests/schema_version_test.rs"]
mod schema_version_test;
#[path = "tests/access_log_test.rs"]
mod access_log_test;
#[path = "tests/subscription_cancellation_test.rs"]
mod subscription_cancellation_test;
#[path = "tests/extended_coverage_test.rs"]
mod extended_coverage_test;
#[path = "tests/notification_validation_test.rs"]
mod notification_validation_test;
#[path = "tests/category_registry_test.rs"]
mod category_registry_test;
#[path = "tests/batch_notification_test.rs"]
mod batch_notification_test;
#[path = "tests/batch_event_test.rs"]
mod batch_event_test;
#[path = "tests/audit_log_test.rs"]
mod audit_log_test;
#[path = "tests/payload_validation_test.rs"]
mod payload_validation_test;
#[path = "tests/batch_ack_test.rs"]
mod batch_ack_test;
#[path = "tests/fuzz_test.rs"]
mod fuzz_test;
#[path = "tests/schema_version_test.rs"]
mod schema_version_test;
#[path = "tests/access_log_test.rs"]
mod access_log_test;
#[path = "../tests/template_registry_test.rs"]
mod template_registry_test;
#[path = "tests/subscription_cancellation_test.rs"]
mod subscription_cancellation_test;
#[path = "../tests/channel_subscription_test.rs"]
mod channel_subscription_test;
#[path = "tests/notification_lifetime_test.rs"]
mod notification_lifetime_test;
}