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
Binary file not shown.
19,134 changes: 19,134 additions & 0 deletions apps/web/package-lock.json

Large diffs are not rendered by default.

148 changes: 124 additions & 24 deletions contracts/escrow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,18 @@ pub enum DataKey {
Job(u64),
Config, // Replaces separate Admin + AgentJudge entries
JobLock(u64),
<<<<<<< HEAD
Config,
Admin,
AgentJudge,
=======
>>>>>>> 5a2cc8d9734783cc04369634a657f1bd96408f1c
JobRegistry,
Locked,
MultisigConfig(u64), // Per-job multisig configuration
UpgradeAdmin,
<<<<<<< HEAD
=======
Treasury,
Amended(u64),
}
Expand Down Expand Up @@ -150,6 +158,7 @@ pub struct MilestonesAmendedEvent {
pub milestone_count: u32,
pub remaining_amount: i128,
pub amended_at: u64,
>>>>>>> 5a2cc8d9734783cc04369634a657f1bd96408f1c
}

#[contracttype]
Expand Down Expand Up @@ -328,6 +337,10 @@ fn enter_reentrancy_guard(env: &Env) -> ReentrancyGuard<'_> {
ReentrancyGuard { env }
}

fn exit_reentrancy_guard(env: &Env) {
env.storage().instance().remove(&DataKey::Locked);
}

fn checked_i128_add(lhs: i128, rhs: i128) -> Result<i128, EscrowError> {
lhs.checked_add(rhs).ok_or(EscrowError::InvalidInput)
}
Expand Down Expand Up @@ -377,6 +390,29 @@ impl EscrowContract {
env.storage().temporary().remove(&lock_key);
}

<<<<<<< HEAD
fn checked_sub_i128(env: &Env, a: i128, b: i128) -> Result<i128, EscrowError> {
a.checked_sub(b).ok_or_else(|| {
log!(env, "checked_sub_i128 underflow: {} - {}", a, b);
EscrowError::InvalidInput
})
}

fn checked_add_i128(env: &Env, a: i128, b: i128) -> Result<i128, EscrowError> {
a.checked_add(b).ok_or_else(|| {
log!(env, "checked_add_i128 overflow: {} + {}", a, b);
EscrowError::ArithmeticOverflow
})
}

fn assert_not_same_ledger_as_funding(env: &Env, job: &EscrowJob) -> Result<(), EscrowError> {
if job.funded_ledger_seq != 0 && env.ledger().sequence() == job.funded_ledger_seq {
return Err(EscrowError::InvalidState);
}
Ok(())
}

=======
fn payout_with_fee(env: &Env, _job_id: u64, job: &EscrowJob, amount: i128) -> Result<(), EscrowError> {
let token_client = token::Client::new(env, &job.token);
let mut freelancer_amount = amount;
Expand Down Expand Up @@ -415,6 +451,7 @@ impl EscrowContract {
fn assert_not_same_ledger_as_funding(_env: &Env, _job: &EscrowJob) -> Result<(), EscrowError> {
Ok(())
}
>>>>>>> 5a2cc8d9734783cc04369634a657f1bd96408f1c
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 @@ -663,11 +700,15 @@ impl EscrowContract {
.checked_mul(24)
.and_then(|h| h.checked_mul(60))
.and_then(|m| m.checked_mul(60))
<<<<<<< HEAD
.ok_or(EscrowError::ArithmeticError)?;
=======
.ok_or(EscrowError::ArithmeticError)?;
>>>>>>> 5a2cc8d9734783cc04369634a657f1bd96408f1c

let expires_at = now
.checked_add(expires_duration)
.ok_or(EscrowError::ArithmeticError)?;
let expires_at = now
.checked_add(expires_duration)
.ok_or(EscrowError::ArithmeticError)?;

let job = EscrowJob {
client: client.clone(),
Expand Down Expand Up @@ -894,21 +935,19 @@ let expires_at = now
.ok_or(EscrowError::JobNotFound)?;
Self::bump_job_ttl(&env, &key);

Self::assert_not_same_ledger_as_funding(&env, &job)?;
Self::assert_not_same_ledger_as_funding(&env, &job)?;

if !(job.status == EscrowStatus::Funded
|| job.status == EscrowStatus::WorkInProgress)
{
return Err(EscrowError::InvalidState);
}
if !(job.status == EscrowStatus::Funded || job.status == EscrowStatus::WorkInProgress) {
return Err(EscrowError::InvalidState);
}

if caller != job.client {
return Err(EscrowError::Unauthorized);
}
if caller != job.client {
return Err(EscrowError::Unauthorized);
}

if milestone_index >= job.milestones.len() {
return Err(EscrowError::InvalidInput);
}
if milestone_index >= job.milestones.len() {
return Err(EscrowError::InvalidInput);
}

let mut milestone = job
.milestones
Expand Down Expand Up @@ -950,6 +989,10 @@ if milestone_index >= job.milestones.len() {
env.storage().persistent().set(&key, &job);
Self::bump_job_ttl(&env, &key);
Self::exit_job_lock(&env, lock_key);
<<<<<<< HEAD

=======
>>>>>>> 5a2cc8d9734783cc04369634a657f1bd96408f1c
Ok(())
}

Expand Down Expand Up @@ -1013,13 +1056,11 @@ if milestone_index >= job.milestones.len() {
}

// 3. Job must still be active
Self::assert_not_same_ledger_as_funding(&env, &job)?;
Self::assert_not_same_ledger_as_funding(&env, &job)?;

if !(job.status == EscrowStatus::Funded
|| job.status == EscrowStatus::WorkInProgress)
{
return Err(EscrowError::InvalidState);
}
if !(job.status == EscrowStatus::Funded || job.status == EscrowStatus::WorkInProgress) {
return Err(EscrowError::InvalidState);
}

// 4. Prevent dispute if all funds are already released
if job.released_amount >= job.total_amount {
Expand Down Expand Up @@ -1172,6 +1213,10 @@ if !(job.status == EscrowStatus::Funded
env.storage().persistent().set(&key, &job);
Self::bump_job_ttl(&env, &key);
Self::exit_job_lock(&env, lock_key);
<<<<<<< HEAD

=======
>>>>>>> 5a2cc8d9734783cc04369634a657f1bd96408f1c
Ok(())
}

Expand All @@ -1180,7 +1225,7 @@ if !(job.status == EscrowStatus::Funded
client.require_auth();

let key = DataKey::Job(job_id);
let mut job: EscrowJob = env
let job: EscrowJob = env
.storage()
.persistent()
.get(&key)
Expand Down Expand Up @@ -1413,10 +1458,14 @@ if !(job.status == EscrowStatus::Funded
job_id,
remaining
);
env.storage().persistent().set(&key, &job);
Self::bump_job_ttl(&env, &key);
env.storage().persistent().set(&key, &job);
Self::bump_job_ttl(&env, &key);

<<<<<<< HEAD
exit_reentrancy_guard(&env);
=======
Self::exit_reentrancy_guard(&env);
>>>>>>> 5a2cc8d9734783cc04369634a657f1bd96408f1c
env.events().publish(
("escrow", "DisputeExpired"),
DisputeExpiredEvent {
Expand Down Expand Up @@ -1473,8 +1522,11 @@ Self::exit_reentrancy_guard(&env);
Ok((config.admin, config.agent_judge, job_registry))
}

<<<<<<< HEAD
=======


>>>>>>> 5a2cc8d9734783cc04369634a657f1bd96408f1c
/// Configure multisig for a job. Only callable by client during Setup phase.
pub fn configure_multisig(
env: Env,
Expand Down Expand Up @@ -1653,21 +1705,66 @@ Self::exit_reentrancy_guard(&env);

/// Returns the active platform fee in basis points (0 when unset).
pub fn get_fee_bps(env: Env) -> u32 {
<<<<<<< HEAD
env.storage().instance().get(&DataKey::FeeBps).unwrap_or(0)
=======
Self::bump_instance_ttl(&env);
if let Some(config) = env.storage().instance().get::<_, TreasuryConfig>(&DataKey::Treasury) {
config.fee_bps
} else {
0
}
>>>>>>> 5a2cc8d9734783cc04369634a657f1bd96408f1c
}

/// Returns the configured treasury address, if any.
pub fn get_treasury(env: Env) -> Option<Address> {
<<<<<<< HEAD
env.storage().instance().get(&DataKey::Treasury)
}

fn fee_bps(env: &Env) -> u32 {
env.storage()
.instance()
.get::<_, u32>(&DataKey::FeeBps)
.unwrap_or(0u32)
}

fn payout_with_fee(env: &Env, job: &EscrowJob, amount: i128) {
let treasury = env
.storage()
.instance()
.get::<_, Address>(&DataKey::Treasury);
let fee_bps = Self::fee_bps(env);
let fee_amount = if fee_bps > 0 {
amount
.checked_mul(fee_bps as i128)
.and_then(|value| value.checked_div(10_000))
.unwrap_or(0)
} else {
0
};
let payout_amount = amount.checked_sub(fee_amount).unwrap_or(0);
let token_client = token::Client::new(env, &job.token);

if fee_amount > 0 {
if let Some(treasury_addr) = treasury {
token_client.transfer(&env.current_contract_address(), &treasury_addr, &fee_amount);
}
}
if payout_amount > 0 {
token_client.transfer(
&env.current_contract_address(),
&job.freelancer,
&payout_amount,
);
=======
Self::bump_instance_ttl(&env);
if let Some(config) = env.storage().instance().get::<_, TreasuryConfig>(&DataKey::Treasury) {
Some(config.routing_address)
} else {
None
>>>>>>> 5a2cc8d9734783cc04369634a657f1bd96408f1c
}
}

Expand Down Expand Up @@ -1881,8 +1978,11 @@ Self::exit_reentrancy_guard(&env);

Ok(())
}
<<<<<<< HEAD
=======


>>>>>>> 5a2cc8d9734783cc04369634a657f1bd96408f1c
}

#[cfg(test)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,61 @@
"v0": {
"topics": [
{
<<<<<<< HEAD
"symbol": "fn_call"
},
{
"bytes": "d63a954726751a876d37290072af1ee723d7d761eec3bf4191849d2116acdc73"
},
{
"symbol": "decimals"
}
],
"data": "void"
}
}
},
"failed_call": true
},
{
"event": {
"ext": "v0",
"contract_id": "d63a954726751a876d37290072af1ee723d7d761eec3bf4191849d2116acdc73",
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "fn_return"
},
{
"symbol": "decimals"
}
],
"data": {
"u32": 7
}
}
}
},
"failed_call": true
},
{
"event": {
"ext": "v0",
"contract_id": "0000000000000000000000000000000000000000000000000000000000000006",
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "fn_return"
},
{
"symbol": "deposit"
=======
"symbol": "log"
>>>>>>> 5a2cc8d9734783cc04369634a657f1bd96408f1c
}
],
"data": {
Expand Down
Loading
Loading