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
36 changes: 33 additions & 3 deletions contracts/escrow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,18 @@ pub struct ReleaseMilestoneEvent {
pub released_at: u64,
}

/// Structured release event emitted by both release_milestone and release_funds.
/// sequence_nonce is a per-job monotonically incrementing counter that lets
/// off-chain indexers detect duplicate processing attempts.
#[contracttype]
#[derive(Clone)]
pub struct ReleaseEvent {
pub job_id: u64,
pub released_amount: i128,
pub sequence_nonce: u64,
pub recipient: Address,
}

#[contracttype]
#[derive(Clone)]
pub struct OpenDisputeEvent {
Expand Down Expand Up @@ -309,6 +321,18 @@ impl EscrowContract {
})
}

/// Increments and returns the per-job release sequence nonce.
/// Uses checked arithmetic; panics with ArithmeticOverflow on u64 wrap.
fn next_nonce(env: &Env, job_id: u64) -> u64 {
let key = DataKey::SequenceNonce(job_id);
let current: u64 = env.storage().instance().get(&key).unwrap_or(0u64);
let next = current
.checked_add(1)
.unwrap_or_else(|| panic_with_error!(env, EscrowError::ArithmeticOverflow));
env.storage().instance().set(&key, &next);
next
}

fn sync_dispute_to_job_registry(env: &Env, job_id: u64) -> Result<(), EscrowError> {
Self::bump_instance_ttl(env);
let Some(registry_contract) = env
Expand Down Expand Up @@ -750,10 +774,16 @@ impl EscrowContract {

exit_reentrancy_guard(&env);

// Emit event
// Emit structured release event for idempotent off-chain indexers
let nonce = Self::next_nonce(&env, job_id);
env.events().publish(
("escrow", "ReleaseMilestone"),
(job_id, idx, milestone.amount, env.ledger().timestamp()),
("escrow", "Release"),
ReleaseEvent {
job_id,
released_amount: milestone.amount,
sequence_nonce: nonce,
recipient: job.freelancer,
},
);

Ok(())
Expand Down
Loading
Loading