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
56 changes: 54 additions & 2 deletions contracts/escrow_contract/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,32 @@ pub struct FeeUpdated {
pub new_fee: u32,
}

#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum EscrowState {
Pending,
Released,
Refunded,
Paused,
}

#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct EscrowRecord {
pub sender: Address,
pub driver: Address,
pub token: Address,
pub amount: i128,
pub status: EscrowState,
}

#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DisputeFavour {
Sender,
Driver,
}

#[contract]
pub struct EscrowContract;

Expand Down Expand Up @@ -322,7 +348,7 @@ impl EscrowContract {
&record.sender,
&record.amount,
);
record.status = EscrowStatus::Refunded;
record.status = EscrowState::Refunded;
save_escrow(&env, delivery_id, &record);
env.events().publish(
(events::escrow_refunded(&env), delivery_id),
Expand Down Expand Up @@ -396,10 +422,12 @@ impl EscrowContract {
);
record.status = EscrowStatus::Refunded;
}

save_escrow(&env, delivery_id, &record);

env.events().publish(
(events::dispute_resolved(&env), delivery_id),
(release_to_driver, caller),
(favour, admin),
);
}

Expand All @@ -415,5 +443,29 @@ impl EscrowContract {
}
}

fn load_escrow(env: &Env, delivery_id: u64) -> EscrowRecord {
env.storage()
.persistent()
.get(&DataKey::Escrow(delivery_id))
.expect("Escrow not found")
}

fn save_escrow(env: &Env, delivery_id: u64, record: &EscrowRecord) {
let key = DataKey::Escrow(delivery_id);
env.storage().persistent().set(&key, record);
env.storage().persistent().extend_ttl(
&key,
constants::ESCROW_TTL_THRESHOLD,
constants::ESCROW_TTL_EXTEND_TO,
);
}

fn require_admin(env: &Env, caller: &Address) {
let admin: Address = env.storage().instance().get(&DataKey::Admin).unwrap();
if *caller != admin {
panic!("Unauthorized");
}
}

#[cfg(test)]
mod test;
2 changes: 1 addition & 1 deletion contracts/escrow_contract/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;
use soroban_sdk::{
testutils::Address as _,
token::{Client as TokenClient, StellarAssetClient},
Address, Env,
Address, Env, IntoVal,
};

fn setup_env() -> (Env, Address) {
Expand Down
Loading