forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
1387 lines (1211 loc) · 53.8 KB
/
Copy pathlib.rs
File metadata and controls
1387 lines (1211 loc) · 53.8 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
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! # Campaign Contract
//!
//! Multi-token reward campaigns with participant management and M-of-N upgrade support.
//!
//! ## Event Schema (v1)
//! All events include `schema_version` as the first data element.
//!
//! | topics | data |
//! |---------------------------------|---------------------------------------------------------|
//! | `("camp", "created")` | `(v, id, owner, reward_count, max_participants)` |
//! | `("camp", "activated")` | `(v, id, owner)` |
//! | `("camp", "deactivated")` | `(v, id, owner)` |
//! | `("camp", "joined")` | `(v, id, participant)` |
//! | `("camp", "reward_issued")` | `(v, id, participant, reward_count)` |
//! | `("camp", "paused")` | `(v, admin)` |
//! | `("camp", "unpaused")` | `(v, admin)` |
//! | `("camp", "upgraded")` | `(v, new_wasm_hash)` |
#![no_std]
use soroban_sdk::{
contract, contractimpl, contracttype, symbol_short, Address, BytesN, Env, Vec,
};
// ── Constants ────────────────────────────────────────────────────────────────
const MAX_TOKENS: u32 = 5;
/// Schema version for all events emitted by this contract.
pub const EVENT_SCHEMA_VERSION: u32 = 1;
// ── Storage Keys ─────────────────────────────────────────────────────────────
#[contracttype]
#[derive(Clone)]
pub enum DataKey {
/// Instance: admin address.
Admin,
Campaign(u64),
Participants(u64),
Joined(u64, Address),
Paused,
/// Multisig signers for upgrade authorization
Signers,
/// Minimum approvals required for upgrade
Threshold,
/// Pending upgrade approvals: wasm_hash -> Vec<Address>
UpgradeApprovals(BytesN<32>),
}
// ── Campaign status ───────────────────────────────────────────────────────────
/// Lifecycle state of a campaign.
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CampaignStatus {
/// Accepting reward distributions.
Active,
/// Temporarily halted; can be resumed.
Paused,
/// Permanently closed; no further distributions allowed.
Ended,
}
// ── Campaign data ─────────────────────────────────────────────────────────────
/// Full on-chain representation of a merchant reward campaign.
#[contracttype]
#[derive(Clone, Debug)]
pub struct Campaign {
/// Address of the merchant that owns this campaign.
pub owner: Address,
/// SEP-41 token contract address used for reward payouts.
pub token: Address,
/// Reward amount issued per qualifying action (in token base units).
pub reward_per_action: i128,
/// Ledger sequence number at which the campaign becomes active.
pub start_ledger: u32,
/// Ledger sequence number after which the campaign is considered expired.
pub end_ledger: u32,
/// Maximum total tokens that may be distributed from this campaign.
pub max_budget: i128,
/// Tokens already distributed; incremented by [`deduct_budget`].
pub spent_budget: i128,
/// Current lifecycle state.
pub status: CampaignStatus,
}
// ── Event helpers ─────────────────────────────────────────────────────────────
fn emit_campaign_created(
env: &Env,
id: u64,
owner: &Address,
reward_count: u32,
max_participants: u32,
) {
env.events().publish(
(symbol_short!("camp"), symbol_short!("created")),
(EVENT_SCHEMA_VERSION, id, owner.clone(), reward_count, max_participants),
);
}
fn emit_campaign_activated(env: &Env, id: u64, owner: &Address) {
env.events().publish(
(symbol_short!("camp"), symbol_short!("activated")),
(EVENT_SCHEMA_VERSION, id, owner.clone()),
);
}
fn emit_campaign_deactivated(env: &Env, id: u64, owner: &Address) {
env.events().publish(
(symbol_short!("camp"), symbol_short!("deactivated")),
(EVENT_SCHEMA_VERSION, id, owner.clone()),
);
}
fn emit_campaign_joined(env: &Env, id: u64, participant: &Address) {
env.events().publish(
(symbol_short!("camp"), symbol_short!("joined")),
(EVENT_SCHEMA_VERSION, id, participant.clone()),
);
}
fn emit_reward_issued(env: &Env, id: u64, participant: &Address, reward_count: u32) {
env.events().publish(
(symbol_short!("camp"), symbol_short!("rwd_issued")),
(EVENT_SCHEMA_VERSION, id, participant.clone(), reward_count),
);
}
fn emit_paused(env: &Env, admin: &Address) {
env.events().publish(
(symbol_short!("camp"), symbol_short!("paused")),
(EVENT_SCHEMA_VERSION, admin.clone()),
);
}
fn emit_unpaused(env: &Env, admin: &Address) {
env.events().publish(
(symbol_short!("camp"), symbol_short!("unpaused")),
(EVENT_SCHEMA_VERSION, admin.clone()),
);
}
fn emit_contract_upgraded(env: &Env, new_wasm_hash: &BytesN<32>) {
env.events().publish(
(symbol_short!("camp"), symbol_short!("upgraded")),
(EVENT_SCHEMA_VERSION, new_wasm_hash.clone()),
);
}
// ── Contract ──────────────────────────────────────────────────────────────────
#[contract]
pub struct CampaignContract;
#[contractimpl]
impl CampaignContract {
// ── Initialization ────────────────────────────────────────────────────────
/// One-time contract setup. Sets the admin and initializes the campaign counter.
///
/// # Errors
/// - [`ContractError::AlreadyInitialized`] if called more than once.
pub fn initialize(env: Env, admin: Address) -> Result<(), ContractError> {
/// Initialize the contract with an admin and upgrade multisig config.
///
/// # Parameters
/// - `admin` – Admin address for pause/unpause operations.
/// - `signers` – Multisig signer set for upgrade authorization.
/// - `threshold` – Minimum approvals required to execute an upgrade.
pub fn initialize(env: Env, admin: Address, signers: Vec<Address>, threshold: u32) {
if env.storage().instance().has(&DataKey::Admin) {
return Err(ContractError::AlreadyInitialized);
}
assert!(threshold >= 1, "threshold must be at least 1");
assert!(
signers.len() >= threshold,
"signers count must be >= threshold"
);
env.storage().instance().set(&DataKey::Admin, &admin);
env.storage().instance().set(&DataKey::Signers, &signers);
env.storage().instance().set(&DataKey::Threshold, &threshold);
}
// ── Internal helpers ──────────────────────────────────────────────────────
/// Loads the admin address from instance storage.
fn load_admin(env: &Env) -> Result<Address, ContractError> {
env.storage()
.instance()
.get(&DataKey::Admin)
.ok_or(ContractError::NotInitialized)
}
/// Returns `true` when the contract-level pause is active.
fn contract_is_paused(env: &Env) -> bool {
env.storage()
.instance()
.get(&DataKey::Paused)
.unwrap_or(false)
}
/// Fails with [`ContractError::ContractPaused`] when the contract is paused.
fn require_not_paused(env: &Env) -> Result<(), ContractError> {
if Self::contract_is_paused(env) {
return Err(ContractError::ContractPaused);
fn is_paused_internal(env: &Env) -> bool {
env.storage().instance().get(&DataKey::Paused).unwrap_or(false)
}
fn require_not_paused(env: &Env) {
if Self::is_paused_internal(env) {
panic!("contract is paused");
}
Ok(())
}
/// Loads a campaign from persistent storage.
fn load_campaign(env: &Env, id: u64) -> Result<Campaign, ContractError> {
env.storage()
.persistent()
.get(&DataKey::Campaign(id))
.ok_or(ContractError::CampaignNotFound)
}
/// Persists a campaign and extends its TTL.
fn save_campaign(env: &Env, id: u64, campaign: &Campaign) {
let key = DataKey::Campaign(id);
env.storage().persistent().set(&key, campaign);
env.storage()
.persistent()
.extend_ttl(&key, PERSISTENT_TTL, PERSISTENT_TTL);
}
/// Returns `true` if `caller` is the campaign owner or the contract admin.
fn is_owner_or_admin(
env: &Env,
caller: &Address,
campaign: &Campaign,
) -> Result<bool, ContractError> {
let admin = Self::load_admin(env)?;
Ok(caller == &campaign.owner || caller == &admin)
}
// ── Campaign management ───────────────────────────────────────────────────
/// Create a new reward campaign.
///
/// The caller becomes the campaign owner. The campaign starts in
/// [`CampaignStatus::Active`] state immediately.
///
/// # Parameters
/// - `owner` – Merchant address (must authorize).
/// - `token` – SEP-41 reward token contract address.
/// - `reward_per_action` – Tokens issued per qualifying action (> 0).
/// - `start_ledger` – First ledger at which rewards may be issued.
/// - `end_ledger` – Ledger after which the campaign expires.
/// - `max_budget` – Maximum total tokens distributable (> 0).
///
/// # Returns
/// The new campaign id (`u64`), starting at `1`.
///
/// # Events
/// Emits `("campaign", "created")` with data
/// `(id, owner, token, reward_per_action, start_ledger, end_ledger, max_budget)`.
///
/// # Errors
/// - [`ContractError::ContractPaused`] — contract is paused.
/// - [`ContractError::InvalidRewardAmount`] — `reward_per_action <= 0`.
/// - [`ContractError::InvalidBudget`] — `max_budget <= 0`.
/// - [`ContractError::InvalidLedgerRange`] — `start_ledger >= end_ledger`.
/// Create a new campaign with multiple token rewards (up to 5).
///
/// # Events
/// Emits `("camp", "created")` with `(schema_version, id, owner, reward_count, max_participants)`.
pub fn create_campaign(
env: Env,
owner: Address,
token: Address,
reward_per_action: i128,
start_ledger: u32,
end_ledger: u32,
max_budget: i128,
) -> Result<u64, ContractError> {
Self::require_not_paused(&env)?;
owner.require_auth();
if reward_per_action <= 0 {
return Err(ContractError::InvalidRewardAmount);
if rewards.len() > MAX_TOKENS {
panic!("too many tokens");
}
if max_budget <= 0 {
return Err(ContractError::InvalidBudget);
}
if start_ledger >= end_ledger {
return Err(ContractError::InvalidLedgerRange);
}
// Assign the next id.
let count: u64 = env
.storage()
.instance()
.get(&DataKey::CampaignCount)
.unwrap_or(0);
let id = count + 1;
env.storage().instance().set(&DataKey::CampaignCount, &id);
let campaign = Campaign {
owner: owner.clone(),
token: token.clone(),
reward_per_action,
start_ledger,
end_ledger,
max_budget,
spent_budget: 0,
status: CampaignStatus::Active,
for reward in rewards.iter() {
if reward.amount <= 0 {
panic!("reward amount must be positive");
}
}
let reward_count = rewards.len() as u32;
let data = CampaignData {
owner: owner.clone(),
rewards,
active: false,
completed: false,
max_participants,
current_participants: 0,
};
Self::save_campaign(&env, id, &campaign);
// Emit two events to stay within Soroban's tuple-size limits:
// first the identifiers, then the budget/ledger parameters.
env.events().publish(
(symbol_short!("campaign"), symbol_short!("created")),
(id, owner, token, reward_per_action),
);
env.events().publish(
(symbol_short!("campaign"), symbol_short!("crt_meta")),
(id, start_ledger, end_ledger, max_budget),
);
Ok(id)
}
/// Pause an active campaign, temporarily halting reward distributions.
///
/// Only the campaign owner or the contract admin may call this.
///
/// # Parameters
/// - `caller` – Address performing the action (must authorize).
/// - `id` – Campaign identifier.
///
/// # Events
/// Emits `("campaign", "paused")` with data `(id, caller)`.
///
/// # Errors
/// - [`ContractError::ContractPaused`] — contract is paused.
/// - [`ContractError::CampaignNotFound`] — no campaign with this id.
/// - [`ContractError::Unauthorized`] — caller is not owner or admin.
/// - [`ContractError::CampaignAlreadyEnded`] — campaign is permanently ended.
/// - [`ContractError::CampaignAlreadyPaused`]— campaign is already paused.
pub fn pause_campaign(env: Env, caller: Address, id: u64) -> Result<(), ContractError> {
Self::require_not_paused(&env)?;
caller.require_auth();
let mut campaign = Self::load_campaign(&env, id)?;
if !Self::is_owner_or_admin(&env, &caller, &campaign)? {
return Err(ContractError::Unauthorized);
}
if campaign.status == CampaignStatus::Ended {
return Err(ContractError::CampaignAlreadyEnded);
}
if campaign.status == CampaignStatus::Paused {
return Err(ContractError::CampaignAlreadyPaused);
}
campaign.status = CampaignStatus::Paused;
Self::save_campaign(&env, id, &campaign);
env.events().publish(
(symbol_short!("campaign"), symbol_short!("paused")),
(id, caller),
);
Ok(())
}
emit_campaign_created(&env, id, &owner, reward_count, max_participants);
}
/// Activate or deactivate a campaign. Only owner can call.
///
/// # Events
/// Emits `("camp", "activated")` or `("camp", "deactivated")`.
pub fn set_active(env: Env, id: u64, active: bool) {
Self::require_not_paused(&env);
let key = DataKey::Campaign(id);
let mut data: CampaignData = env.storage().persistent().get(&key).expect("campaign not found");
data.owner.require_auth();
data.active = active;
env.storage().persistent().set(&key, &data);
env.storage().persistent().extend_ttl(&key, 2_678_400, 2_678_400);
if active {
emit_campaign_activated(&env, id, &data.owner);
} else {
emit_campaign_deactivated(&env, id, &data.owner);
}
}
/// Join an active campaign.
///
/// # Events
/// Emits `("camp", "joined")` with `(schema_version, id, participant)`.
pub fn join_campaign(env: Env, id: u64, participant: Address) {
Self::require_not_paused(&env);
participant.require_auth();
let key = DataKey::Campaign(id);
let mut data: CampaignData = env.storage().persistent().get(&key).expect("campaign not found");
/// Resume a paused campaign, re-enabling reward distributions.
///
/// Only the campaign owner or the contract admin may call this.
///
/// # Parameters
/// - `caller` – Address performing the action (must authorize).
/// - `id` – Campaign identifier.
///
/// # Events
/// Emits `("campaign", "resumed")` with data `(id, caller)`.
///
/// # Errors
/// - [`ContractError::ContractPaused`] — contract is paused.
/// - [`ContractError::CampaignNotFound`] — no campaign with this id.
/// - [`ContractError::Unauthorized`] — caller is not owner or admin.
/// - [`ContractError::CampaignAlreadyEnded`] — campaign is permanently ended.
/// - [`ContractError::CampaignNotPaused`] — campaign is not currently paused.
/// - [`ContractError::CampaignExpired`] — campaign end ledger has passed.
pub fn resume_campaign(env: Env, caller: Address, id: u64) -> Result<(), ContractError> {
Self::require_not_paused(&env)?;
caller.require_auth();
let mut campaign = Self::load_campaign(&env, id)?;
if !Self::is_owner_or_admin(&env, &caller, &campaign)? {
return Err(ContractError::Unauthorized);
}
if campaign.status == CampaignStatus::Ended {
return Err(ContractError::CampaignAlreadyEnded);
}
if campaign.status != CampaignStatus::Paused {
return Err(ContractError::CampaignNotPaused);
}
// Prevent resuming an already-expired campaign.
if env.ledger().sequence() > campaign.end_ledger {
return Err(ContractError::CampaignExpired);
}
campaign.status = CampaignStatus::Active;
Self::save_campaign(&env, id, &campaign);
env.events().publish(
(symbol_short!("campaign"), symbol_short!("resumed")),
(id, caller),
);
Ok(())
}
/// Permanently end a campaign. This action is irreversible.
///
/// Only the campaign owner or the contract admin may call this.
///
/// # Parameters
/// - `caller` – Address performing the action (must authorize).
/// - `id` – Campaign identifier.
///
/// # Events
/// Emits `("campaign", "ended")` with data `(id, caller, spent_budget)`.
///
/// # Errors
/// - [`ContractError::ContractPaused`] — contract is paused.
/// - [`ContractError::CampaignNotFound`] — no campaign with this id.
/// - [`ContractError::Unauthorized`] — caller is not owner or admin.
/// - [`ContractError::CampaignAlreadyEnded`] — campaign is already ended.
pub fn end_campaign(env: Env, caller: Address, id: u64) -> Result<(), ContractError> {
Self::require_not_paused(&env)?;
caller.require_auth();
let mut campaign = Self::load_campaign(&env, id)?;
if !Self::is_owner_or_admin(&env, &caller, &campaign)? {
return Err(ContractError::Unauthorized);
data.current_participants += 1;
env.storage().persistent().set(&key, &data);
env.storage().persistent().extend_ttl(&key, 2_678_400, 2_678_400);
env.storage().persistent().set(&joined_key, &true);
let mut participants: Vec<Address> = env
.storage()
.persistent()
.get(&DataKey::Participants(id))
.unwrap();
participants.push_back(participant.clone());
env.storage().persistent().set(&DataKey::Participants(id), &participants);
env.storage()
.persistent()
.extend_ttl(&DataKey::Participants(id), 2_678_400, 2_678_400);
emit_campaign_joined(&env, id, &participant);
}
/// Distribute all configured rewards to a participant atomically.
///
/// # Events
/// Emits `("camp", "rwd_issued")` with `(schema_version, id, participant, reward_count)`.
pub fn distribute_reward(env: Env, id: u64, participant: Address) {
Self::require_not_paused(&env);
let key = DataKey::Campaign(id);
let data: CampaignData = env.storage().persistent().get(&key).expect("campaign not found");
data.owner.require_auth();
let joined_key = DataKey::Joined(id, participant.clone());
if !env.storage().persistent().has(&joined_key) {
panic!("participant not in campaign");
}
if campaign.status == CampaignStatus::Ended {
return Err(ContractError::CampaignAlreadyEnded);
}
let spent = campaign.spent_budget;
campaign.status = CampaignStatus::Ended;
Self::save_campaign(&env, id, &campaign);
env.events().publish(
(symbol_short!("campaign"), symbol_short!("ended")),
(id, caller, spent),
);
Ok(())
}
/// Deduct `amount` from the campaign budget when a reward is distributed.
///
/// Called by the distribution contract (or admin) each time a reward is
/// issued. Fails gracefully when the budget is exhausted so the caller can
/// handle the shortfall without reverting the entire transaction.
///
/// # Parameters
/// - `caller` – Address performing the deduction (must be owner or admin).
/// - `id` – Campaign identifier.
/// - `amount` – Tokens being distributed (> 0).
///
/// # Errors
/// - [`ContractError::ContractPaused`] — contract is paused.
/// - [`ContractError::CampaignNotFound`] — no campaign with this id.
/// - [`ContractError::Unauthorized`] — caller is not owner or admin.
/// - [`ContractError::CampaignNotActive`] — campaign is paused or ended.
/// - [`ContractError::CampaignExpired`] — campaign end ledger has passed.
/// - [`ContractError::AmountMustBePositive`]— `amount <= 0`.
/// - [`ContractError::InsufficientBudget`] — remaining budget < amount.
/// - [`ContractError::Overflow`] — arithmetic overflow.
pub fn deduct_budget(
env: Env,
caller: Address,
id: u64,
amount: i128,
) -> Result<i128, ContractError> {
Self::require_not_paused(&env)?;
caller.require_auth();
let mut campaign = Self::load_campaign(&env, id)?;
if !Self::is_owner_or_admin(&env, &caller, &campaign)? {
return Err(ContractError::Unauthorized);
}
if campaign.status != CampaignStatus::Active {
return Err(ContractError::CampaignNotActive);
}
if env.ledger().sequence() > campaign.end_ledger {
return Err(ContractError::CampaignExpired);
}
if amount <= 0 {
return Err(ContractError::AmountMustBePositive);
}
let remaining = campaign
.max_budget
.checked_sub(campaign.spent_budget)
.ok_or(ContractError::Overflow)?;
if remaining < amount {
return Err(ContractError::InsufficientBudget);
}
campaign.spent_budget = campaign
.spent_budget
.checked_add(amount)
.ok_or(ContractError::Overflow)?;
let new_remaining = campaign.max_budget - campaign.spent_budget;
Self::save_campaign(&env, id, &campaign);
Ok(new_remaining)
}
// ── Contract-level pause ──────────────────────────────────────────────────
/// Pause all state-changing contract operations. Admin only.
///
/// # Errors
/// - [`ContractError::NotInitialized`] — contract not initialized.
/// - [`ContractError::Unauthorized`] — caller is not the admin.
pub fn pause_contract(env: Env, caller: Address) -> Result<(), ContractError> {
caller.require_auth();
let admin = Self::load_admin(&env)?;
if caller != admin {
return Err(ContractError::Unauthorized);
}
env.storage().instance().set(&DataKey::Paused, &true);
Ok(())
}
/// Unpause contract operations. Admin only.
///
/// # Errors
/// - [`ContractError::NotInitialized`] — contract not initialized.
/// - [`ContractError::Unauthorized`] — caller is not the admin.
pub fn unpause_contract(env: Env, caller: Address) -> Result<(), ContractError> {
caller.require_auth();
let admin = Self::load_admin(&env)?;
if caller != admin {
return Err(ContractError::Unauthorized);
}
env.storage().instance().set(&DataKey::Paused, &false);
Ok(())
}
// ── Read-only helpers ─────────────────────────────────────────────────────
/// Returns the full [`Campaign`] struct for a given id.
///
/// # Errors
/// - [`ContractError::CampaignNotFound`] — no campaign with this id.
pub fn get_campaign(env: Env, id: u64) -> Result<Campaign, ContractError> {
let campaign = Self::load_campaign(&env, id)?;
// Extend TTL on read so hot campaigns don't expire from storage.
env.storage()
.persistent()
.extend_ttl(&DataKey::Campaign(id), PERSISTENT_TTL, PERSISTENT_TTL);
Ok(campaign)
}
/// Returns the remaining budget for a campaign (max_budget − spent_budget).
///
/// # Errors
/// - [`ContractError::CampaignNotFound`] — no campaign with this id.
pub fn remaining_budget(env: Env, id: u64) -> Result<i128, ContractError> {
let campaign = Self::load_campaign(&env, id)?;
Ok(campaign.max_budget - campaign.spent_budget)
}
/// Returns the total number of campaigns created.
pub fn campaign_count(env: Env) -> u64 {
env.storage()
.instance()
.get(&DataKey::CampaignCount)
.unwrap_or(0)
}
/// Returns `true` if the contract-level pause is active.
pub fn is_contract_paused(env: Env) -> bool {
Self::contract_is_paused(&env)
let reward_count = data.rewards.len() as u32;
emit_reward_issued(&env, id, &participant, reward_count);
}
pub fn get_campaign(env: Env, id: u64) -> CampaignData {
let key = DataKey::Campaign(id);
let data = env.storage().persistent().get(&key).expect("campaign not found");
env.storage().persistent().extend_ttl(&key, 2_678_400, 2_678_400);
data
}
/// Pauses all contract operations. Only admin can call.
///
/// # Events
/// Emits `("camp", "paused")` with `(schema_version, admin)`.
pub fn pause(env: Env) {
let admin = Self::get_admin(&env);
admin.require_auth();
env.storage().instance().set(&DataKey::Paused, &true);
emit_paused(&env, &admin);
}
/// Unpauses contract operations. Only admin can call.
///
/// # Events
/// Emits `("camp", "unpaused")` with `(schema_version, admin)`.
pub fn unpause(env: Env) {
let admin = Self::get_admin(&env);
admin.require_auth();
env.storage().instance().set(&DataKey::Paused, &false);
emit_unpaused(&env, &admin);
}
pub fn is_paused(env: Env) -> bool {
Self::is_paused_internal(&env)
}
// ── Upgrade (M-of-N multisig) ─────────────────────────────────────────────
/// Approve a pending WASM upgrade. Executes when threshold is reached.
///
/// # Events
/// Emits `("camp", "upgraded")` with `(schema_version, new_wasm_hash)` when threshold is met.
///
/// # Panics
/// - `"not an authorized signer"` if `signer` is not in the signer set.
/// - `"already approved"` if `signer` has already approved this hash.
pub fn approve_upgrade(env: Env, signer: Address, new_wasm_hash: BytesN<32>) {
signer.require_auth();
let signers: Vec<Address> = env
.storage()
.instance()
.get(&DataKey::Signers)
.expect("not initialized");
let is_authorized = signers.iter().any(|s| s == signer);
assert!(is_authorized, "not an authorized signer");
let approval_key = DataKey::UpgradeApprovals(new_wasm_hash.clone());
let mut approvals: Vec<Address> = env
.storage()
.instance()
.get(&approval_key)
.unwrap_or(Vec::new(&env));
let already_approved = approvals.iter().any(|a| a == signer);
assert!(!already_approved, "already approved");
approvals.push_back(signer);
env.storage().instance().set(&approval_key, &approvals);
let threshold: u32 = env
.storage()
.instance()
.get(&DataKey::Threshold)
.unwrap_or(1);
if approvals.len() >= threshold {
env.storage().instance().remove(&approval_key);
emit_contract_upgraded(&env, &new_wasm_hash);
env.deployer().update_current_contract_wasm(new_wasm_hash);
}
}
pub fn get_upgrade_approvals(env: Env, new_wasm_hash: BytesN<32>) -> u32 {
let approval_key = DataKey::UpgradeApprovals(new_wasm_hash);
let approvals: Vec<Address> = env
.storage()
.instance()
.get(&approval_key)
.unwrap_or(Vec::new(&env));
approvals.len()
}
pub fn get_threshold(env: Env) -> u32 {
env.storage()
.instance()
.get(&DataKey::Threshold)
.unwrap_or(1)
}
pub fn get_signers(env: Env) -> Vec<Address> {
env.storage()
.instance()
.get(&DataKey::Signers)
.unwrap_or(Vec::new(&env))
}
}
// ── Tests ─────────────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use super::*;
use errors::ContractError;
use soroban_sdk::{testutils::{Address as _, Ledger}, Env};
use soroban_sdk::testutils::Address as _;
// ── Helpers ───────────────────────────────────────────────────────────────
fn setup() -> (Env, Address, Address, CampaignContractClient<'static>) {
let env = Env::default();
env.mock_all_auths();
let contract_id = env.register(CampaignContract, ());
let client = CampaignContractClient::new(env, &contract_id);
client.initialize(&admin, &soroban_sdk::vec![env, admin.clone()], &1);
(admin, client)
}
/// Creates a default campaign starting at ledger 1, ending at ledger 1000.
fn make_campaign(
env: &Env,
client: &CampaignContractClient,
token: &Address,
) -> (u64, Address) {
let owner = Address::generate(env);
let id = client
.create_campaign(&owner, token, &100, &1, &1000, &10_000)
.unwrap();
(id, owner)
}
// ── initialize ────────────────────────────────────────────────────────────
#[test]
fn test_initialize_ok() {
let env = Env::default();
env.mock_all_auths();
let contract_id = env.register(CampaignContract, ());
let client = CampaignContractClient::new(&env, &contract_id);
let admin = Address::generate(&env);
assert_eq!(client.initialize(&admin), Ok(()));
}
#[test]
fn test_initialize_twice_returns_already_initialized() {
let (_, admin, _, client) = setup();
let result = client.initialize(&admin);
assert_eq!(result, Err(ContractError::AlreadyInitialized));
}
// ── create_campaign ───────────────────────────────────────────────────────
let (_admin, client) = setup(&env);
let owner = Address::generate(&env);
let token1 = Address::generate(&env);
let token2 = Address::generate(&env);
let id = 1u64;
let rewards = soroban_sdk::vec![
&env,
TokenReward { token: token1.clone(), amount: 100 },
TokenReward { token: token2.clone(), amount: 50 },
];
client.create_campaign(&id, &owner, &rewards, &2);
let data = client.get_campaign(&id);
assert_eq!(data.owner, owner);
assert_eq!(data.active, false);
assert_eq!(data.rewards.len(), 2);
client.set_active(&id, &true);
assert_eq!(client.get_campaign(&id).active, true);
let alice = Address::generate(&env);
let bob = Address::generate(&env);
client.join_campaign(&id, &alice);
client.join_campaign(&id, &bob);
#[test]
fn test_create_campaign_ok() {
let (env, _admin, token, client) = setup();
let owner = Address::generate(&env);
let id = client
.create_campaign(&owner, &token, &50, &1, &500, &5_000)
.unwrap();
assert_eq!(id, 1);
assert_eq!(client.campaign_count(), 1);
let c = client.get_campaign(&id).unwrap();
assert_eq!(c.owner, owner);
assert_eq!(c.token, token);
assert_eq!(c.reward_per_action, 50);
assert_eq!(c.start_ledger, 1);
assert_eq!(c.end_ledger, 500);
assert_eq!(c.max_budget, 5_000);
assert_eq!(c.spent_budget, 0);
assert_eq!(c.status, CampaignStatus::Active);
}
#[test]
fn test_create_campaign_ids_increment() {
let (env, _admin, token, client) = setup();
let owner = Address::generate(&env);
let id1 = client.create_campaign(&owner, &token, &10, &1, &100, &1_000).unwrap();
let id2 = client.create_campaign(&owner, &token, &10, &1, &100, &1_000).unwrap();
assert_eq!(id1, 1);
assert_eq!(id2, 2);
client.distribute_reward(&id, &alice);
}
#[test]
fn test_create_campaign_invalid_reward_amount() {
let (env, _admin, token, client) = setup();
let owner = Address::generate(&env);
let result = client.create_campaign(&owner, &token, &0, &1, &100, &1_000);
assert_eq!(result, Err(ContractError::InvalidRewardAmount));
}
#[test]
fn test_create_campaign_negative_reward_amount() {
let (env, _admin, token, client) = setup();
let owner = Address::generate(&env);
let result = client.create_campaign(&owner, &token, &-1, &1, &100, &1_000);
assert_eq!(result, Err(ContractError::InvalidRewardAmount));
}
#[test]
fn test_create_campaign_invalid_budget() {
let (env, _admin, token, client) = setup();
let tokens: Vec<Address> = (0..5).map(|_| Address::generate(&env)).collect();
let id = 1u64;
let rewards = soroban_sdk::vec![
&env,
TokenReward { token: tokens[0].clone(), amount: 100 },
TokenReward { token: tokens[1].clone(), amount: 200 },
TokenReward { token: tokens[2].clone(), amount: 300 },
TokenReward { token: tokens[3].clone(), amount: 400 },
TokenReward { token: tokens[4].clone(), amount: 500 },
];
client.create_campaign(&id, &owner, &rewards, &1);
let data = client.get_campaign(&id);
assert_eq!(data.rewards.len(), 5);
}
#[test]
#[should_panic(expected = "too many tokens")]
fn test_max_tokens_exceeded() {
let env = Env::default();
let (_admin, client) = setup(&env);
let owner = Address::generate(&env);
let tokens: Vec<Address> = (0..6).map(|_| Address::generate(&env)).collect();
let id = 1u64;
let rewards = soroban_sdk::vec![
&env,
TokenReward { token: tokens[0].clone(), amount: 100 },
TokenReward { token: tokens[1].clone(), amount: 100 },
TokenReward { token: tokens[2].clone(), amount: 100 },
TokenReward { token: tokens[3].clone(), amount: 100 },
TokenReward { token: tokens[4].clone(), amount: 100 },
TokenReward { token: tokens[5].clone(), amount: 100 },
];
client.create_campaign(&id, &owner, &rewards, &1);
}
#[test]
#[should_panic(expected = "campaign is full")]
fn test_campaign_full() {
let env = Env::default();
let (_admin, client) = setup(&env);
let owner = Address::generate(&env);
let result = client.create_campaign(&owner, &token, &10, &1, &100, &0);
assert_eq!(result, Err(ContractError::InvalidBudget));
}
#[test]
fn test_create_campaign_invalid_ledger_range_equal() {
let (env, _admin, token, client) = setup();
let owner = Address::generate(&env);
let result = client.create_campaign(&owner, &token, &10, &100, &100, &1_000);
assert_eq!(result, Err(ContractError::InvalidLedgerRange));
}
let rewards = soroban_sdk::vec![&env, TokenReward { token: token.clone(), amount: 100 }];
client.create_campaign(&id, &owner, &rewards, &1);
client.set_active(&id, &true);
#[test]
fn test_create_campaign_invalid_ledger_range_start_after_end() {
let (env, _admin, token, client) = setup();
let owner = Address::generate(&env);
let result = client.create_campaign(&owner, &token, &10, &200, &100, &1_000);
assert_eq!(result, Err(ContractError::InvalidLedgerRange));
}
#[test]
fn test_create_campaign_while_contract_paused() {
let (env, admin, token, client) = setup();
client.pause_contract(&admin).unwrap();
let owner = Address::generate(&env);
let result = client.create_campaign(&owner, &token, &10, &1, &100, &1_000);
assert_eq!(result, Err(ContractError::ContractPaused));
}
// ── pause_campaign ────────────────────────────────────────────────────────
#[test]
fn test_pause_campaign_by_owner() {
let (env, _admin, token, client) = setup();
let (id, owner) = make_campaign(&env, &client, &token);
client.pause_campaign(&owner, &id).unwrap();
assert_eq!(client.get_campaign(&id).unwrap().status, CampaignStatus::Paused);
}