Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Callora-Contracts
Submodule Callora-Contracts added at 7cee06
9 changes: 8 additions & 1 deletion contracts/settlement/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ pub struct DeveloperBalance {
pub balance: i128,
}

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

Expand Down
72 changes: 19 additions & 53 deletions contracts/vault/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,9 @@ pub struct CalloraVault;
#[contractimpl]
impl CalloraVault {
#[allow(clippy::too_many_arguments)]
pub fn init(
env: Env,
owner: Address,
usdc_token: Address,
initial_balance: Option<i128>,
authorized_caller: Option<Address>,
min_deposit: Option<i128>,
revenue_pool: Option<Address>,
max_deduct: Option<i128>,
) -> VaultMeta {
pub fn init(env: Env, owner: Address, usdc_token: Address, initial_balance: Option<i128>,
authorized_caller: Option<Address>, min_deposit: Option<i128>,
revenue_pool: Option<Address>, max_deduct: Option<i128>) -> VaultMeta {
owner.require_auth();
let inst = env.storage().instance();
if inst.has(&StorageKey::Meta) {
Expand Down Expand Up @@ -109,10 +102,7 @@ impl CalloraVault {
}

pub fn get_admin(env: Env) -> Address {
env.storage()
.instance()
.get(&StorageKey::Admin)
.expect("vault not initialized")
env.storage().instance().get(&StorageKey::Admin).expect("vault not initialized")
}

pub fn set_admin(env: Env, caller: Address, new_admin: Address) {
Expand Down Expand Up @@ -167,15 +157,11 @@ impl CalloraVault {
panic!("insufficient USDC balance");
}
usdc.transfer(&env.current_contract_address(), &to, &amount);
env.events()
.publish((Symbol::new(&env, "distribute"), to), amount);
env.events().publish((Symbol::new(&env, "distribute"), to), amount);
}

pub fn get_meta(env: Env) -> VaultMeta {
env.storage()
.instance()
.get(&StorageKey::Meta)
.unwrap_or_else(|| panic!("vault not initialized"))
env.storage().instance().get(&StorageKey::Meta).unwrap_or_else(|| panic!("vault not initialized"))
}

pub fn set_allowed_depositor(env: Env, caller: Address, depositor: Option<Address>) {
Expand Down Expand Up @@ -221,10 +207,7 @@ impl CalloraVault {
}

pub fn get_allowed_depositors(env: Env) -> Vec<Address> {
env.storage()
.instance()
.get(&StorageKey::DepositorList)
.unwrap_or(Vec::new(&env))
env.storage().instance().get(&StorageKey::DepositorList).unwrap_or(Vec::new(&env))
}

pub fn set_authorized_caller(env: Env, caller: Address) {
Expand Down Expand Up @@ -252,15 +235,15 @@ impl CalloraVault {
Self::require_admin_or_owner(env.clone(), &caller);
assert!(Self::is_paused(env.clone()), "vault not paused");
env.storage().instance().set(&StorageKey::Paused, &false);
env.events()
.publish((Symbol::new(&env, "vault_unpaused"), caller), ());
env.events().publish((Symbol::new(&env, "vault_unpaused"), caller), ());
}

pub fn is_paused(env: Env) -> bool {
env.storage()
.instance()
.get(&StorageKey::Paused)
.unwrap_or(false)
env.storage().instance().get(&StorageKey::Paused).unwrap_or(false)
}

pub fn get_max_deduct(env: Env) -> i128 {
env.storage().instance().get(&StorageKey::MaxDeduct).unwrap_or(DEFAULT_MAX_DEDUCT)
}

pub fn get_max_deduct(env: Env) -> i128 {
Expand Down Expand Up @@ -293,10 +276,7 @@ impl CalloraVault {
let usdc = token::Client::new(&env, &usdc_addr);
usdc.transfer(&caller, &env.current_contract_address(), &amount);
let mut meta = Self::get_meta(env.clone());
meta.balance = meta
.balance
.checked_add(amount)
.unwrap_or_else(|| panic!("balance overflow"));
meta.balance = meta.balance.checked_add(amount).unwrap_or_else(|| panic!("balance overflow"));
env.storage().instance().set(&StorageKey::Meta, &meta);
env.events().publish(
(Symbol::new(&env, "deposit"), caller.clone()),
Expand Down Expand Up @@ -500,9 +480,7 @@ impl CalloraVault {
}

pub fn get_settlement(env: Env) -> Address {
env.storage()
.instance()
.get(&StorageKey::Settlement)
env.storage().instance().get(&StorageKey::Settlement)
.unwrap_or_else(|| panic!("settlement address not set"))
}

Expand Down Expand Up @@ -559,13 +537,8 @@ impl CalloraVault {
.instance()
.get(&StorageKey::Metadata(offering_id.clone()))
.unwrap_or(String::from_str(&env, ""));
env.storage()
.instance()
.set(&StorageKey::Metadata(offering_id.clone()), &metadata);
env.events().publish(
(Symbol::new(&env, "metadata_updated"), offering_id, caller),
(old, metadata.clone()),
);
env.storage().instance().set(&StorageKey::Metadata(offering_id.clone()), &metadata);
env.events().publish((Symbol::new(&env, "metadata_updated"), offering_id, caller), (old, metadata.clone()));
metadata
}

Expand All @@ -589,16 +562,9 @@ impl CalloraVault {
}

fn require_admin_or_owner(env: Env, caller: &Address) {
let admin: Address = env
.storage()
.instance()
.get(&StorageKey::Admin)
.expect("vault not initialized");
let admin: Address = env.storage().instance().get(&StorageKey::Admin).expect("vault not initialized");
let meta = Self::get_meta(env);
assert!(
*caller == admin || *caller == meta.owner,
"unauthorized: caller is not admin or owner"
);
assert!(*caller == admin || *caller == meta.owner, "unauthorized: caller is not admin or owner");
}
}

Expand Down
Loading
Loading