Skip to content

Commit b4eeb63

Browse files
Merge pull request #218 from Vvictor-commits/test/vault-fuzz-deposit-deduct
Test/vault fuzz deposit deduct
2 parents fe65505 + 07ad289 commit b4eeb63

4 files changed

Lines changed: 687 additions & 54 deletions

File tree

Callora-Contracts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 7cee06e7566fbcae7966486147cf306e89ac12bb

contracts/settlement/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@ pub struct DeveloperBalance {
1010
pub balance: i128,
1111
}
1212

13-
/// Global pool balance tracking
13+
/// Global pool balance tracking.
14+
///
15+
/// `last_updated` is set to `env.ledger().timestamp()` on every
16+
/// `receive_payment` call that credits the pool (`to_pool = true`).
17+
/// It is also set at `init` time. It is **not** updated when payments
18+
/// are routed to individual developer balances.
1419
#[contracttype]
1520
#[derive(Clone, Debug, PartialEq)]
1621
pub struct GlobalPool {
1722
pub total_balance: i128,
23+
/// Ledger timestamp of the last pool credit. Useful for analytics
24+
/// and staleness checks.
1825
pub last_updated: u64,
1926
}
2027

contracts/vault/src/lib.rs

Lines changed: 19 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,9 @@ pub struct CalloraVault;
4545
#[contractimpl]
4646
impl CalloraVault {
4747
#[allow(clippy::too_many_arguments)]
48-
pub fn init(
49-
env: Env,
50-
owner: Address,
51-
usdc_token: Address,
52-
initial_balance: Option<i128>,
53-
authorized_caller: Option<Address>,
54-
min_deposit: Option<i128>,
55-
revenue_pool: Option<Address>,
56-
max_deduct: Option<i128>,
57-
) -> VaultMeta {
48+
pub fn init(env: Env, owner: Address, usdc_token: Address, initial_balance: Option<i128>,
49+
authorized_caller: Option<Address>, min_deposit: Option<i128>,
50+
revenue_pool: Option<Address>, max_deduct: Option<i128>) -> VaultMeta {
5851
owner.require_auth();
5952
let inst = env.storage().instance();
6053
if inst.has(&StorageKey::Meta) {
@@ -117,10 +110,7 @@ impl CalloraVault {
117110
}
118111

119112
pub fn get_admin(env: Env) -> Address {
120-
env.storage()
121-
.instance()
122-
.get(&StorageKey::Admin)
123-
.expect("vault not initialized")
113+
env.storage().instance().get(&StorageKey::Admin).expect("vault not initialized")
124114
}
125115

126116
pub fn set_admin(env: Env, caller: Address, new_admin: Address) {
@@ -175,15 +165,11 @@ impl CalloraVault {
175165
panic!("insufficient USDC balance");
176166
}
177167
usdc.transfer(&env.current_contract_address(), &to, &amount);
178-
env.events()
179-
.publish((Symbol::new(&env, "distribute"), to), amount);
168+
env.events().publish((Symbol::new(&env, "distribute"), to), amount);
180169
}
181170

182171
pub fn get_meta(env: Env) -> VaultMeta {
183-
env.storage()
184-
.instance()
185-
.get(&StorageKey::Meta)
186-
.unwrap_or_else(|| panic!("vault not initialized"))
172+
env.storage().instance().get(&StorageKey::Meta).unwrap_or_else(|| panic!("vault not initialized"))
187173
}
188174

189175
pub fn set_allowed_depositor(env: Env, caller: Address, depositor: Option<Address>) {
@@ -229,10 +215,7 @@ impl CalloraVault {
229215
}
230216

231217
pub fn get_allowed_depositors(env: Env) -> Vec<Address> {
232-
env.storage()
233-
.instance()
234-
.get(&StorageKey::DepositorList)
235-
.unwrap_or(Vec::new(&env))
218+
env.storage().instance().get(&StorageKey::DepositorList).unwrap_or(Vec::new(&env))
236219
}
237220

238221
pub fn set_authorized_caller(env: Env, caller: Address) {
@@ -260,15 +243,15 @@ impl CalloraVault {
260243
Self::require_admin_or_owner(env.clone(), &caller);
261244
assert!(Self::is_paused(env.clone()), "vault not paused");
262245
env.storage().instance().set(&StorageKey::Paused, &false);
263-
env.events()
264-
.publish((Symbol::new(&env, "vault_unpaused"), caller), ());
246+
env.events().publish((Symbol::new(&env, "vault_unpaused"), caller), ());
265247
}
266248

267249
pub fn is_paused(env: Env) -> bool {
268-
env.storage()
269-
.instance()
270-
.get(&StorageKey::Paused)
271-
.unwrap_or(false)
250+
env.storage().instance().get(&StorageKey::Paused).unwrap_or(false)
251+
}
252+
253+
pub fn get_max_deduct(env: Env) -> i128 {
254+
env.storage().instance().get(&StorageKey::MaxDeduct).unwrap_or(DEFAULT_MAX_DEDUCT)
272255
}
273256

274257
pub fn get_max_deduct(env: Env) -> i128 {
@@ -301,10 +284,7 @@ impl CalloraVault {
301284
let usdc = token::Client::new(&env, &usdc_addr);
302285
usdc.transfer(&caller, &env.current_contract_address(), &amount);
303286
let mut meta = Self::get_meta(env.clone());
304-
meta.balance = meta
305-
.balance
306-
.checked_add(amount)
307-
.unwrap_or_else(|| panic!("balance overflow"));
287+
meta.balance = meta.balance.checked_add(amount).unwrap_or_else(|| panic!("balance overflow"));
308288
env.storage().instance().set(&StorageKey::Meta, &meta);
309289
env.events().publish(
310290
(Symbol::new(&env, "deposit"), caller.clone()),
@@ -514,9 +494,7 @@ impl CalloraVault {
514494
}
515495

516496
pub fn get_settlement(env: Env) -> Address {
517-
env.storage()
518-
.instance()
519-
.get(&StorageKey::Settlement)
497+
env.storage().instance().get(&StorageKey::Settlement)
520498
.unwrap_or_else(|| panic!("settlement address not set"))
521499
}
522500

@@ -573,13 +551,8 @@ impl CalloraVault {
573551
.instance()
574552
.get(&StorageKey::Metadata(offering_id.clone()))
575553
.unwrap_or(String::from_str(&env, ""));
576-
env.storage()
577-
.instance()
578-
.set(&StorageKey::Metadata(offering_id.clone()), &metadata);
579-
env.events().publish(
580-
(Symbol::new(&env, "metadata_updated"), offering_id, caller),
581-
(old, metadata.clone()),
582-
);
554+
env.storage().instance().set(&StorageKey::Metadata(offering_id.clone()), &metadata);
555+
env.events().publish((Symbol::new(&env, "metadata_updated"), offering_id, caller), (old, metadata.clone()));
583556
metadata
584557
}
585558

@@ -603,16 +576,9 @@ impl CalloraVault {
603576
}
604577

605578
fn require_admin_or_owner(env: Env, caller: &Address) {
606-
let admin: Address = env
607-
.storage()
608-
.instance()
609-
.get(&StorageKey::Admin)
610-
.expect("vault not initialized");
579+
let admin: Address = env.storage().instance().get(&StorageKey::Admin).expect("vault not initialized");
611580
let meta = Self::get_meta(env);
612-
assert!(
613-
*caller == admin || *caller == meta.owner,
614-
"unauthorized: caller is not admin or owner"
615-
);
581+
assert!(*caller == admin || *caller == meta.owner, "unauthorized: caller is not admin or owner");
616582
}
617583
}
618584

0 commit comments

Comments
 (0)