Skip to content
Open
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
25 changes: 21 additions & 4 deletions contracts/escrow/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![cfg(test)]

use super::*;
use crate::contract::{EscrowContract, EscrowContractClient};
use soroban_sdk::{
testutils::{storage::Persistent as _, Address as _, Ledger},
token, vec, Address, Env,
Expand Down Expand Up @@ -52,6 +53,21 @@ fn test_getters_return_not_initialized_before_initialize() {
assert_eq!(client.try_get_max_sponsors(), Err(Ok(Error::NotInitialized)));
}

#[test]
fn test_initialize_rejects_treasury_equal_to_contract() {
// #516 — InvalidTreasury: the treasury must not be the contract itself.
let env = Env::default();
env.mock_all_auths();
let contract_id = env.register(EscrowContract, ());
let client = EscrowContractClient::new(&env, &contract_id);
let admin = Address::generate(&env);
let oracle = Address::generate(&env);

let result = client.try_initialize(&admin, &oracle, &contract_id, &500u32, &None);
assert_eq!(result, Err(Ok(Error::InvalidTreasury)));
}


#[test]
fn test_initialize_rejects_fee_bps_above_10000() {
let env = Env::default();
Expand Down Expand Up @@ -369,8 +385,8 @@ fn test_adversarial_ordering_resistance() {
let admin = Address::generate(&env);
let oracle = Address::generate(&env);
let treasury = Address::generate(&env);
let contract_id = env.register(crate::EscrowContract, ());
let client = crate::EscrowContractClient::new(&env, &contract_id);
let contract_id = env.register(crate::contract::EscrowContract, ());
let client = crate::contract::EscrowContractClient::new(&env, &contract_id);

// Initialize with 0% fee to simplify fraction/dust calculations
client.initialize(&admin, &oracle, &treasury, &0u32, &None);
Expand Down Expand Up @@ -435,8 +451,8 @@ fn test_large_split_distributes_dust_by_largest_remainder() {
let admin = Address::generate(&env);
let oracle = Address::generate(&env);
let treasury = Address::generate(&env);
let contract_id = env.register(crate::EscrowContract, ());
let client = crate::EscrowContractClient::new(&env, &contract_id);
let contract_id = env.register(crate::contract::EscrowContract, ());
let client = crate::contract::EscrowContractClient::new(&env, &contract_id);
// 0% fee so the whole total is distributable.
client.initialize(&admin, &oracle, &treasury, &0u32, &None);

Expand Down Expand Up @@ -2152,6 +2168,7 @@ fn test_set_treasury_updates_treasury_address() {
#[test]
fn test_set_treasury_requires_admin_auth() {
let env = Env::default();
env.mock_all_auths();
let (_, admin, _treasury, client) = setup(&env);

let new_treasury = Address::generate(&env);
Expand Down
26 changes: 24 additions & 2 deletions contracts/maintenance-pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,19 +299,41 @@ mod contract {
return Err(Error::InactivityWindowNotElapsed);
}

if deposit.amount > pool.balance {
// #534 — refund the sponsor's pro-rata share of the REMAINING
// balance, not the full original deposit. The pool is fungible and
// withdraw pays rewards from it, so a full refund would let an early
// reclaimer drain later depositors' share of the spending.
// reclaimable = deposit.amount * pool.balance / total_deposited
let reclaimable = if pool.total_deposited > 0 {
deposit
.amount
.checked_mul(pool.balance)
.ok_or(Error::ArithmeticOverflow)?
.checked_div(pool.total_deposited)
.ok_or(Error::ArithmeticOverflow)?
} else {
deposit.amount
};
if reclaimable <= 0 {
return Err(Error::InsufficientBalance);
}

let token_client = token::Client::new(&env, &pool.token);
token_client.transfer(&env.current_contract_address(), &sponsor, &deposit.amount);
token_client.transfer(&env.current_contract_address(), &sponsor, &reclaimable);

pool.balance = pool
.balance
.checked_sub(reclaimable)
.ok_or(Error::ArithmeticOverflow)?;
pool.total_deposited = pool
.total_deposited
.checked_sub(deposit.amount)
.ok_or(Error::ArithmeticOverflow)?;
env.storage().persistent().set(&pkey, &pool);
extend_ttl(&env, &pkey);
// Clear the deposit record — otherwise the same deposit could be
// reclaimed twice.
env.storage().persistent().remove(&dkey);
extend_instance_ttl(&env);

Ok(())
Expand Down
Loading