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
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions contracts/buyback/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use soroban_sdk::{
contract, contractimpl, contracttype, symbol_short, Address, BytesN, Env, Symbol, Vec,
};

use brain_storm_shared::access;
use brain_storm_shared::{access, math};

// =============================================================================
// Storage keys
Expand Down Expand Up @@ -377,7 +377,7 @@ impl BuybackContract {
if bst_price == 0 {
return 0;
}
(xlm_amount.checked_mul(1_000_000).unwrap_or(xlm_amount)) / bst_price
brain_storm_shared::math::checked_mul_div_i128(xlm_amount, 1_000_000, bst_price)
}

fn get_bst_price(env: &Env) -> i128 {
Expand Down
2 changes: 1 addition & 1 deletion contracts/royalty_distribution/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct RoyaltyPayment {
}

const EVT_DOMAIN: Symbol = symbol_short!("royalty");
const DISTRIBUTE: Symbol = symbol_short!("distributed");
const DISTRIBUTE: Symbol = symbol_short!("distribtd");
const WITHDRAW: Symbol = symbol_short!("withdrawn");
const SPLIT_SET: Symbol = symbol_short!("split_set");

Expand Down
4 changes: 2 additions & 2 deletions contracts/royalty_distribution/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(test)]
mod tests {
use crate::{RoyaltyDistributionContract, RoyaltyDistributionContractClient};
use soroban_sdk::{testutils::Address as _, Address, Env};
use soroban_sdk::{testutils::{Address as _, Events}, Address, Env, FromVal};

fn setup() -> (Env, RoyaltyDistributionContractClient<'static>, Address) {
let env = Env::default();
Expand Down Expand Up @@ -243,7 +243,7 @@ mod tests {
let newest = events.get(events.len() - 1).unwrap();
let topics = newest.1;
assert_eq!(soroban_sdk::Symbol::from_val(&env, &topics.get(0).unwrap()), soroban_sdk::symbol_short!("royalty"));
assert_eq!(soroban_sdk::Symbol::from_val(&env, &topics.get(1).unwrap()), soroban_sdk::symbol_short!("distributed"));
assert_eq!(soroban_sdk::Symbol::from_val(&env, &topics.get(1).unwrap()), soroban_sdk::symbol_short!("distribtd"));
}

#[test]
Expand Down
70 changes: 69 additions & 1 deletion contracts/scholarship_fund/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,72 @@ mod tests {
let (_, client, _) = setup();
assert!(client.get_application(&999).is_none());
}
}
// ── Edge Case Tests ─────────────────────────────────────────────
#[test]
fn test_partial_disbursement_succeeds() {
let (env, client, admin) = setup();
let donor = Address::generate(&env);
client.donate(&donor, &5000);

let student = Address::generate(&env);
let app_id = client.apply_for_scholarship(&student, &2000);
client.approve_application(&admin, &app_id);
client.distribute_scholarship(&admin, &app_id);

assert_eq!(client.get_fund_balance(), 3000);
let app = client.get_application(&app_id).unwrap();
assert_eq!(app.status, ApplicationStatus::Distributed);
}

#[test]
fn test_full_disbursement() {
let (env, client, admin) = setup();
let donor = Address::generate(&env);
client.donate(&donor, &5000);

let student = Address::generate(&env);
let app_id = client.apply_for_scholarship(&student, &5000);
client.approve_application(&admin, &app_id);
client.distribute_scholarship(&admin, &app_id);

assert_eq!(client.get_fund_balance(), 0);
let app = client.get_application(&app_id).unwrap();
assert_eq!(app.status, ApplicationStatus::Distributed);
}

#[test]
#[should_panic(expected = "Insufficient fund balance")]
fn test_over_withdrawal_panics() {
let (env, client, admin) = setup();
let donor = Address::generate(&env);
client.donate(&donor, &1000);

let student = Address::generate(&env);
let app_id = client.apply_for_scholarship(&student, &5000);
client.approve_application(&admin, &app_id);
client.distribute_scholarship(&admin, &app_id);
}

#[test]
fn test_fund_depletion_boundary() {
let (env, client, admin) = setup();
let donor = Address::generate(&env);
client.donate(&donor, &5000);

let student1 = Address::generate(&env);
let app_id1 = client.apply_for_scholarship(&student1, &5000);
client.approve_application(&admin, &app_id1);
client.distribute_scholarship(&admin, &app_id1);

assert_eq!(client.get_fund_balance(), 0);

// Second disbursement should fail with insufficient fund
let student2 = Address::generate(&env);
let app_id2 = client.apply_for_scholarship(&student2, &100);
client.approve_application(&admin, &app_id2);
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
client.distribute_scholarship(&admin, &app_id2);
}));
assert!(result.is_err(), "Expected panic on disbursement beyond depleted fund");
}
}
18 changes: 12 additions & 6 deletions contracts/shared/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
[package]
name = "shared"
name = "brain-storm-shared"
version = "0.1.0"
edition = "2021"

[dependencies]
soroban-sdk = { version = "21.0.0", default-features = false }

[lib]
crate-type = ["cdylib", "rlib"]

[features]
# Compiles `SharedContract` (the deployable RBAC contract). On by default so
# this crate still builds as a standalone wasm. Contracts that depend on this
# crate purely for its helper modules must use `default-features = false`, or
# `SharedContract`'s entry points get exported from their wasm too.
default = ["contract"]
contract = []
testutils = ["soroban-sdk/testutils"]

[lints]
workspace = true
[dependencies]
soroban-sdk = { version = "21.0.0", features = ["alloc"] }

[dev-dependencies]
soroban-sdk = { version = "21.0.0", features = ["testutils"] }
2 changes: 1 addition & 1 deletion contracts/shared/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use soroban_sdk::contracttype;

/// Common errors that can occur across all contracts
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SharedError {
// ===== Authorization Errors =====
/// Caller is not authorized to perform this action
Expand Down
2 changes: 1 addition & 1 deletion contracts/shared/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use soroban_sdk::{testutils::Address as _, Address, Env};
use soroban_sdk::{testutils::{Address as _, Events}, Address, Env, FromVal};

#[test]
fn emits_standard_two_topic_payload() {
Expand Down
Loading
Loading