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
59 changes: 45 additions & 14 deletions contracts/contracts/escrow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ use soroban_sdk::{
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[contracttype]
pub enum MilestoneStatus {
Pending = 0,
Created = 0,
Funded = 1,
Submitted = 2,
Approved = 3,
Released = 4,
Refunded = 5,
Disputed = 6,
AutoExpired = 7,
InProgress = 2,
Submitted = 3,
Approved = 4,
Released = 5,
Refunded = 6,
Disputed = 7,
Resolved = 8,
AutoExpired = 9,
}

#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -97,6 +99,16 @@ pub struct EscrowFunded {
pub amount: i128,
}

#[contractevent]
pub struct MilestoneStarted {
#[topic]
pub contract_id: Address,
#[topic]
pub milestone_id: u32,
#[topic]
pub actor: Address,
}

#[contractevent]
pub struct MilestoneSubmitted {
#[topic]
Expand Down Expand Up @@ -276,7 +288,7 @@ impl EscrowContract {
for i in 0..ids.len() {
let id = ids.get(i).unwrap();
let mut milestone: Milestone = env.storage().instance().get(&DataKey::Milestone(id)).ok_or(Error::MilestoneNotFound)?;
if milestone.status == MilestoneStatus::Pending {
if milestone.status == MilestoneStatus::Created {
milestone.status = MilestoneStatus::Funded;
}
env.storage().instance().set(&DataKey::Milestone(id), &milestone);
Expand All @@ -295,7 +307,7 @@ impl EscrowContract {
Ok(())
}

pub fn submit_milestone(env: Env, milestone_id: u32) -> Result<(), Error> {
pub fn start_milestone(env: Env, milestone_id: u32) -> Result<(), Error> {
let freelancer: Address = env.storage().instance().get(&DataKey::Freelancer).ok_or(Error::NotInitialized)?;
freelancer.require_auth();

Expand All @@ -304,6 +316,29 @@ impl EscrowContract {
if milestone.status != MilestoneStatus::Funded {
return Err(Error::InvalidMilestoneStatus);
}

milestone.status = MilestoneStatus::InProgress;
env.storage().instance().set(&DataKey::Milestone(milestone_id), &milestone);

MilestoneStarted {
contract_id: env.current_contract_address(),
milestone_id,
actor: freelancer,
}
.publish(&env);

Ok(())
}

pub fn submit_milestone(env: Env, milestone_id: u32) -> Result<(), Error> {
let freelancer: Address = env.storage().instance().get(&DataKey::Freelancer).ok_or(Error::NotInitialized)?;
freelancer.require_auth();

let mut milestone: Milestone = env.storage().instance().get(&DataKey::Milestone(milestone_id)).ok_or(Error::MilestoneNotFound)?;

if milestone.status != MilestoneStatus::InProgress {
return Err(Error::InvalidMilestoneStatus);
}
if milestone.deadline > 0 && env.ledger().timestamp() > milestone.deadline {
return Err(Error::DeadlineExceeded);
}
Expand Down Expand Up @@ -509,11 +544,7 @@ impl EscrowContract {
}

let transfer_amount = milestone.amount;
milestone.status = if release_to_freelancer {
MilestoneStatus::Released
} else {
MilestoneStatus::Refunded
};
milestone.status = MilestoneStatus::Resolved;
env.storage().instance().set(&DataKey::Milestone(milestone_id), &milestone);

let mut balance: i128 = env.storage().instance().get(&DataKey::EscrowBalance).unwrap_or(0);
Expand Down
44 changes: 27 additions & 17 deletions contracts/contracts/escrow/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn milestone(env: &Env, id: u32, amount: i128) -> Milestone {
id,
deadline: 0,
amount,
status: MilestoneStatus::Pending,
status: MilestoneStatus::Created,
description: String::from_str(env, "Security milestone"),
client_approved: false,
freelancer_approved: false,
Expand All @@ -77,6 +77,7 @@ fn initialize_single_milestone(setup: &TestSetup, amount: i128) {

fn fully_approve_single_milestone(setup: &TestSetup) {
setup.escrow_client.fund();
setup.escrow_client.start_milestone(&1);
setup.escrow_client.submit_milestone(&1);
setup.escrow_client.approve(&1);
setup.escrow_client.freelancer_confirm(&1);
Expand All @@ -92,7 +93,7 @@ fn test_happy_path() {
id: 1,
deadline: 0,
amount: 100,
status: MilestoneStatus::Pending,
status: MilestoneStatus::Created,
description: String::from_str(&env, "Milestone 1"),
client_approved: false,
freelancer_approved: false,
Expand All @@ -101,7 +102,7 @@ fn test_happy_path() {
id: 2,
deadline: 0,
amount: 200,
status: MilestoneStatus::Pending,
status: MilestoneStatus::Created,
description: String::from_str(&env, "Milestone 2"),
client_approved: false,
freelancer_approved: false,
Expand All @@ -121,7 +122,7 @@ fn test_happy_path() {

let fetched_milestones = escrow.get_milestones();
assert_eq!(fetched_milestones.len(), 2);
assert_eq!(fetched_milestones.get(0).unwrap().status, MilestoneStatus::Pending);
assert_eq!(fetched_milestones.get(0).unwrap().status, MilestoneStatus::Created);

// Fund
escrow.fund();
Expand All @@ -136,6 +137,7 @@ fn test_happy_path() {
assert_eq!(updated_milestones.get(0).unwrap().status, MilestoneStatus::Funded);

// Submit Milestone 1
escrow.start_milestone(&1);
escrow.submit_milestone(&1);
assert_eq!(escrow.get_milestones().get(0).unwrap().status, MilestoneStatus::Submitted);

Expand Down Expand Up @@ -164,7 +166,7 @@ fn test_voluntary_refund() {
id: 1,
deadline: 0,
amount: 250,
status: MilestoneStatus::Pending,
status: MilestoneStatus::Created,
description: String::from_str(&env, "Project Work"),
client_approved: false,
freelancer_approved: false,
Expand Down Expand Up @@ -197,7 +199,7 @@ fn test_dispute_and_resolve_to_freelancer() {
id: 1,
deadline: 0,
amount: 400,
status: MilestoneStatus::Pending,
status: MilestoneStatus::Created,
description: String::from_str(&env, "High Value Milestone"),
client_approved: false,
freelancer_approved: false,
Expand All @@ -206,6 +208,7 @@ fn test_dispute_and_resolve_to_freelancer() {

escrow.initialize(&setup.admin, &setup.client, &setup.freelancer, &setup.arbiter, &setup.token_address, &milestones);
escrow.fund();
escrow.start_milestone(&1);
escrow.submit_milestone(&1);
escrow.approve(&1);

Expand All @@ -220,7 +223,7 @@ fn test_dispute_and_resolve_to_freelancer() {
let token_client = token::Client::new(&env, &setup.token_address);
assert_eq!(token_client.balance(&setup.freelancer), 400);
assert_eq!(token_client.balance(&setup.client), 600);
assert_eq!(escrow.get_milestones().get(0).unwrap().status, MilestoneStatus::Released);
assert_eq!(escrow.get_milestones().get(0).unwrap().status, MilestoneStatus::Resolved);
}

#[test]
Expand All @@ -233,7 +236,7 @@ fn test_dispute_and_resolve_to_client() {
id: 1,
deadline: 0,
amount: 400,
status: MilestoneStatus::Pending,
status: MilestoneStatus::Created,
description: String::from_str(&env, "High Value Milestone"),
client_approved: false,
freelancer_approved: false,
Expand All @@ -242,6 +245,7 @@ fn test_dispute_and_resolve_to_client() {

escrow.initialize(&setup.admin, &setup.client, &setup.freelancer, &setup.arbiter, &setup.token_address, &milestones);
escrow.fund();
escrow.start_milestone(&1);
escrow.submit_milestone(&1);

// Freelancer disputes milestone (perhaps client won't approve)
Expand All @@ -255,7 +259,7 @@ fn test_dispute_and_resolve_to_client() {
let token_client = token::Client::new(&env, &setup.token_address);
assert_eq!(token_client.balance(&setup.client), 1000);
assert_eq!(token_client.balance(&setup.freelancer), 0);
assert_eq!(escrow.get_milestones().get(0).unwrap().status, MilestoneStatus::Refunded);
assert_eq!(escrow.get_milestones().get(0).unwrap().status, MilestoneStatus::Resolved);
}

#[test]
Expand All @@ -269,7 +273,7 @@ fn test_double_initialization_fails() {
id: 1,
deadline: 0,
amount: 100,
status: MilestoneStatus::Pending,
status: MilestoneStatus::Created,
description: String::from_str(&env, "Milestone"),
client_approved: false,
freelancer_approved: false,
Expand All @@ -292,7 +296,7 @@ fn test_zero_amount_fails() {
id: 1,
deadline: 0,
amount: 0, // Zero amount
status: MilestoneStatus::Pending,
status: MilestoneStatus::Created,
description: String::from_str(&env, "Invalid Milestone"),
client_approved: false,
freelancer_approved: false,
Expand All @@ -313,7 +317,7 @@ fn test_unauthorized_release_fails() {
id: 1,
deadline: 0,
amount: 100,
status: MilestoneStatus::Pending,
status: MilestoneStatus::Created,
description: String::from_str(&env, "Milestone"),
client_approved: false,
freelancer_approved: false,
Expand All @@ -322,6 +326,7 @@ fn test_unauthorized_release_fails() {

escrow.initialize(&setup.admin, &setup.client, &setup.freelancer, &setup.arbiter, &setup.token_address, &milestones);
escrow.fund();
escrow.start_milestone(&1);
escrow.submit_milestone(&1);
escrow.approve(&1);

Expand All @@ -339,7 +344,7 @@ fn test_version() {
id: 1,
deadline: 0,
amount: 100,
status: MilestoneStatus::Pending,
status: MilestoneStatus::Created,
description: String::from_str(&env, "Milestone"),
client_approved: false,
freelancer_approved: false,
Expand All @@ -362,7 +367,7 @@ fn test_release_without_approval_fails() {
id: 1,
deadline: 0,
amount: 100,
status: MilestoneStatus::Pending,
status: MilestoneStatus::Created,
description: String::from_str(&env, "Milestone"),
client_approved: false,
freelancer_approved: false,
Expand All @@ -371,6 +376,7 @@ fn test_release_without_approval_fails() {

escrow.initialize(&setup.admin, &setup.client, &setup.freelancer, &setup.arbiter, &setup.token_address, &milestones);
escrow.fund();
escrow.start_milestone(&1);
escrow.submit_milestone(&1);
// Missing client approval - should fail with InsufficientApprovals (error code 9)
escrow.release(&1, &setup.client);
Expand All @@ -387,7 +393,7 @@ fn test_double_client_approval_fails() {
id: 1,
deadline: 0,
amount: 100,
status: MilestoneStatus::Pending,
status: MilestoneStatus::Created,
description: String::from_str(&env, "Milestone"),
client_approved: false,
freelancer_approved: false,
Expand All @@ -396,6 +402,7 @@ fn test_double_client_approval_fails() {

escrow.initialize(&setup.admin, &setup.client, &setup.freelancer, &setup.arbiter, &setup.token_address, &milestones);
escrow.fund();
escrow.start_milestone(&1);
escrow.submit_milestone(&1);
escrow.approve(&1);
// Once approved, the milestone is no longer in Submitted state.
Expand All @@ -413,7 +420,7 @@ fn test_double_freelancer_confirmation_fails() {
id: 1,
deadline: 0,
amount: 100,
status: MilestoneStatus::Pending,
status: MilestoneStatus::Created,
description: String::from_str(&env, "Milestone"),
client_approved: false,
freelancer_approved: false,
Expand All @@ -422,6 +429,7 @@ fn test_double_freelancer_confirmation_fails() {

escrow.initialize(&setup.admin, &setup.client, &setup.freelancer, &setup.arbiter, &setup.token_address, &milestones);
escrow.fund();
escrow.start_milestone(&1);
escrow.submit_milestone(&1);
escrow.approve(&1);
escrow.freelancer_confirm(&1);
Expand All @@ -439,7 +447,7 @@ fn test_dispute_clears_approvals() {
id: 1,
deadline: 0,
amount: 400,
status: MilestoneStatus::Pending,
status: MilestoneStatus::Created,
description: String::from_str(&env, "High Value Milestone"),
client_approved: false,
freelancer_approved: false,
Expand All @@ -448,6 +456,7 @@ fn test_dispute_clears_approvals() {

escrow.initialize(&setup.admin, &setup.client, &setup.freelancer, &setup.arbiter, &setup.token_address, &milestones);
escrow.fund();
escrow.start_milestone(&1);
escrow.submit_milestone(&1);
escrow.approve(&1);

Expand Down Expand Up @@ -572,6 +581,7 @@ fn test_successful_security_events_are_emitted() {
1
);

setup.escrow_client.start_milestone(&1);
setup.escrow_client.submit_milestone(&1);
assert_eq!(
env.events()
Expand Down
Loading
Loading