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
2 changes: 1 addition & 1 deletion packages/circle/src/tests/test_integration.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![cfg(test)]

use soroban_sdk::testutils::Address as _;
use soroban_sdk::{Address, BytesN, Env, String, Vec};
use soroban_sdk::{Address, Env, String};

/// Integration test: factory deploys circle -> members join -> contribute -> trigger payout -> fee sent to treasury
/// This test is currently a stub due to circular dependencies between contracts in the test environment.
Expand Down
211 changes: 63 additions & 148 deletions packages/escrow-swap/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use soroban_sdk::{token::Client as TokenClient, Address, BytesN, Env, Vec};
use soroban_sdk::{token::Client as TokenClient, Address, BytesN, Env, Map, Vec};
use crate::types::*;
use common::pause;

Expand All @@ -12,36 +12,11 @@ pub fn init(env: &Env, admin: &Address) -> Result<(), EscrowError> {
}
env.storage().instance().set(&DataKey::Admin, admin);
env.storage().instance().set(&DataKey::NextSwapId, &0u64);
env.storage().persistent().set(&DataKey::SwapRequests, &Vec::<SwapRequest>::new(env));
env.storage().persistent().set(&DataKey::SwapRequests, &Map::<u64, SwapRequest>::new(env));
Ok(())
}

/// Creates a new escrow swap between two parties.
///
/// # Arguments
/// * `env` - The contract environment
/// * `initiator` - The address creating the swap
/// * `responder` - The address expected to accept the swap
/// * `token_a` - The token address deposited by the initiator
/// * `token_b` - The token address to be deposited by the responder
/// * `initiator_amount` - Amount of token_a to deposit (must be positive)
/// * `responder_amount` - Amount of token_b the responder must deposit (must be positive)
/// * `hash_lock` - SHA-256 hash of the secret that will be required to accept the swap
/// * `time_lock` - Unix timestamp deadline for accepting the swap (must be in the future)
///
/// # Returns
/// * `Ok(u64)` with the new swap ID if successful
/// * `Err(EscrowError)` if any validation fails
///
/// # Validation
/// * initiator and responder must be different addresses
/// * initiator_amount and responder_amount must be positive
/// * time_lock must be strictly greater than current timestamp (deadline is exclusive for creation)
/// * The contract must not be paused
///
/// # Deadline Semantics
/// The deadline for creation is exclusive: `time_lock <= now` is rejected.
/// This means the swap must have a future deadline at creation time.
pub fn create_swap(
env: &Env,
initiator: &Address,
Expand Down Expand Up @@ -84,8 +59,8 @@ pub fn create_swap(
created_at: now,
};

let mut swaps: Vec<SwapRequest> = env.storage().persistent().get(&DataKey::SwapRequests).unwrap_or_else(|| Vec::new(env));
swaps.push_back(swap);
let mut swaps: Map<u64, SwapRequest> = env.storage().persistent().get(&DataKey::SwapRequests).unwrap_or_else(|| Map::new(env));
swaps.set(next_id, swap);
env.storage().persistent().set(&DataKey::SwapRequests, &swaps);
env.storage().instance().set(&DataKey::NextSwapId, &next_id.checked_add(1).ok_or(EscrowError::InvalidAmount)?);

Expand All @@ -107,69 +82,37 @@ pub fn create_swap(
}

/// Accepts a pending swap by providing the secret that matches the hash lock.
///
/// # Arguments
/// * `env` - The contract environment
/// * `id` - The swap ID to accept
/// * `responder` - The address of the responder accepting the swap (must match the swap's responder)
/// * `secret` - The secret that produces the hash lock when hashed with SHA-256
///
/// # Returns
/// * `Ok(())` if the swap was successfully accepted
/// * `Err(EscrowError)` if any validation fails
///
/// # Validation
/// * The swap must be in PENDING status
/// * The caller must be the swap's responder
/// * The provided secret must match the swap's hash lock
/// * The current timestamp must be strictly before the swap's time_lock (deadline is inclusive)
/// * The contract must not be paused
///
/// # Deadline Semantics
/// The deadline is inclusive: acceptance is rejected if `now >= time_lock`.
/// This means the swap cannot be accepted at or after the exact deadline timestamp.
pub fn accept_swap(env: &Env, id: u64, responder: &Address, secret: BytesN<32>) -> Result<(), EscrowError> {
pause::when_not_paused(env).map_err(|_| EscrowError::ContractPaused)?;
let _guard = common::reentrancy::ReentrancyGuard::new(env).map_err(|_| EscrowError::NotInitialized)?;
responder.require_auth();

let mut swaps: Vec<SwapRequest> = env.storage().persistent().get(&DataKey::SwapRequests).ok_or(EscrowError::NotInitialized)?;
let mut found = false;
let mut swaps: Map<u64, SwapRequest> = env.storage().persistent().get(&DataKey::SwapRequests).ok_or(EscrowError::NotInitialized)?;
let mut s = swaps.get(id).ok_or(EscrowError::SwapNotFound)?;

for i in 0..swaps.len() {
let mut s = swaps.get(i).ok_or(EscrowError::VecAccessError)?;
if s.id == id {
if s.status != STATUS_PENDING {
return Err(EscrowError::SwapNotActive);
}
if s.responder != *responder {
return Err(EscrowError::Unauthorized);
}
let secret_bytes = soroban_sdk::Bytes::from_array(env, &secret.to_array());
let computed_hash: BytesN<32> = env.crypto().sha256(&secret_bytes).into();
if computed_hash != s.hash_lock {
return Err(EscrowError::HashLockMismatch);
}
let now = env.ledger().timestamp();
if now >= s.time_lock {
return Err(EscrowError::TimeLockExpired);
}

s.status = STATUS_ACTIVE;
swaps.set(i, s.clone());
found = true;
if s.status != STATUS_PENDING {
return Err(EscrowError::SwapNotActive);
}
if s.responder != *responder {
return Err(EscrowError::Unauthorized);
}
let secret_bytes = soroban_sdk::Bytes::from_array(env, &secret.to_array());
let computed_hash: BytesN<32> = env.crypto().sha256(&secret_bytes).into();
if computed_hash != s.hash_lock {
return Err(EscrowError::HashLockMismatch);
}
let now = env.ledger().timestamp();
if now >= s.time_lock {
return Err(EscrowError::TimeLockExpired);
}

let token_b_client = TokenClient::new(env, &s.token_b);
token_b_client.transfer(responder, &env.current_contract_address(), &s.responder_amount);
s.status = STATUS_ACTIVE;
swaps.set(id, s.clone());

SwapAccepted { id, responder: responder.clone() }.publish(env);
break;
}
}
let token_b_client = TokenClient::new(env, &s.token_b);
token_b_client.transfer(responder, &env.current_contract_address(), &s.responder_amount);

if !found {
return Err(EscrowError::SwapNotFound);
}
SwapAccepted { id, responder: responder.clone() }.publish(env);
env.storage().persistent().set(&DataKey::SwapRequests, &swaps);
Ok(())
}
Expand All @@ -179,36 +122,25 @@ pub fn complete_swap(env: &Env, id: u64, caller: &Address) -> Result<(), EscrowE
let _guard = common::reentrancy::ReentrancyGuard::new(env).map_err(|_| EscrowError::NotInitialized)?;
caller.require_auth();

let mut swaps: Vec<SwapRequest> = env.storage().persistent().get(&DataKey::SwapRequests).ok_or(EscrowError::NotInitialized)?;
let mut found = false;

for i in 0..swaps.len() {
let mut s = swaps.get(i).ok_or(EscrowError::VecAccessError)?;
if s.id == id {
if s.status != STATUS_ACTIVE {
return Err(EscrowError::SwapNotActive);
}
if *caller != s.initiator && *caller != s.responder {
return Err(EscrowError::Unauthorized);
}
s.status = STATUS_COMPLETED;
swaps.set(i, s.clone());
found = true;
let mut swaps: Map<u64, SwapRequest> = env.storage().persistent().get(&DataKey::SwapRequests).ok_or(EscrowError::NotInitialized)?;
let mut s = swaps.get(id).ok_or(EscrowError::SwapNotFound)?;

let token_a_client = TokenClient::new(env, &s.token_a);
token_a_client.transfer(&env.current_contract_address(), &s.responder, &s.initiator_amount);
if s.status != STATUS_ACTIVE {
return Err(EscrowError::SwapNotActive);
}
if *caller != s.initiator && *caller != s.responder {
return Err(EscrowError::Unauthorized);
}
s.status = STATUS_COMPLETED;
swaps.set(id, s.clone());

let token_b_client = TokenClient::new(env, &s.token_b);
token_b_client.transfer(&env.current_contract_address(), &s.initiator, &s.responder_amount);
let token_a_client = TokenClient::new(env, &s.token_a);
token_a_client.transfer(&env.current_contract_address(), &s.responder, &s.initiator_amount);

SwapCompleted { id, initiator: s.initiator.clone(), responder: s.responder.clone() }.publish(env);
break;
}
}
let token_b_client = TokenClient::new(env, &s.token_b);
token_b_client.transfer(&env.current_contract_address(), &s.initiator, &s.responder_amount);

if !found {
return Err(EscrowError::SwapNotFound);
}
SwapCompleted { id, initiator: s.initiator.clone(), responder: s.responder.clone() }.publish(env);
env.storage().persistent().set(&DataKey::SwapRequests, &swaps);
Ok(())
}
Expand All @@ -218,58 +150,41 @@ pub fn cancel_swap(env: &Env, id: u64, caller: &Address) -> Result<(), EscrowErr
let _guard = common::reentrancy::ReentrancyGuard::new(env).map_err(|_| EscrowError::NotInitialized)?;
caller.require_auth();

let mut swaps: Vec<SwapRequest> = env.storage().persistent().get(&DataKey::SwapRequests).ok_or(EscrowError::NotInitialized)?;
let mut found = false;

for i in 0..swaps.len() {
let mut s = swaps.get(i).ok_or(EscrowError::VecAccessError)?;
if s.id == id {
if s.status == STATUS_COMPLETED || s.status == STATUS_CANCELLED {
return Err(EscrowError::SwapNotActive);
}
if *caller != s.initiator && *caller != s.responder {
return Err(EscrowError::Unauthorized);
}

let previous_status = s.status;
s.status = STATUS_CANCELLED;
swaps.set(i, s.clone());
found = true;
let mut swaps: Map<u64, SwapRequest> = env.storage().persistent().get(&DataKey::SwapRequests).ok_or(EscrowError::NotInitialized)?;
let mut s = swaps.get(id).ok_or(EscrowError::SwapNotFound)?;

let token_a_client = TokenClient::new(env, &s.token_a);
token_a_client.transfer(&env.current_contract_address(), &s.initiator, &s.initiator_amount);
if s.status == STATUS_COMPLETED || s.status == STATUS_CANCELLED {
return Err(EscrowError::SwapNotActive);
}
if *caller != s.initiator && *caller != s.responder {
return Err(EscrowError::Unauthorized);
}

let previous_status = s.status;
s.status = STATUS_CANCELLED;
swaps.set(id, s.clone());

if previous_status == STATUS_ACTIVE {
let token_b_client = TokenClient::new(env, &s.token_b);
token_b_client.transfer(&env.current_contract_address(), &s.responder, &s.responder_amount);
}
let token_a_client = TokenClient::new(env, &s.token_a);
token_a_client.transfer(&env.current_contract_address(), &s.initiator, &s.initiator_amount);

SwapCancelled { id }.publish(env);
break;
}
if previous_status == STATUS_ACTIVE {
let token_b_client = TokenClient::new(env, &s.token_b);
token_b_client.transfer(&env.current_contract_address(), &s.responder, &s.responder_amount);
}

if !found {
return Err(EscrowError::SwapNotFound);
}
SwapCancelled { id }.publish(env);
env.storage().persistent().set(&DataKey::SwapRequests, &swaps);
Ok(())
}

pub fn get_swap(env: &Env, id: u64) -> Result<SwapRequest, EscrowError> {
let swaps: Vec<SwapRequest> = env.storage().persistent().get(&DataKey::SwapRequests).ok_or(EscrowError::NotInitialized)?;
for i in 0..swaps.len() {
if let Some(s) = swaps.get(i) {
if s.id == id {
return Ok(s);
}
}
}
Err(EscrowError::SwapNotFound)
let swaps: Map<u64, SwapRequest> = env.storage().persistent().get(&DataKey::SwapRequests).ok_or(EscrowError::NotInitialized)?;
swaps.get(id).ok_or(EscrowError::SwapNotFound)
}

pub fn get_swaps(env: &Env) -> Vec<SwapRequest> {
env.storage().persistent().get(&DataKey::SwapRequests).unwrap_or_else(|| Vec::new(env))
let swaps: Map<u64, SwapRequest> = env.storage().persistent().get(&DataKey::SwapRequests).unwrap_or_else(|| Map::new(env));
swaps.values()
}

pub fn pause(env: &Env, admin: &Address) -> Result<(), EscrowError> {
Expand Down
Loading