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
2 changes: 2 additions & 0 deletions contracts/invoice-escrow/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ pub enum Error {
Overflow = 13,
/// Escrow has been cancelled by the seller.
EscrowCancelled = 14,
/// Payer is not the authorized debtor for this invoice.
InvalidPayer = 15,
}
42 changes: 28 additions & 14 deletions contracts/invoice-escrow/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,40 @@ pub fn escrow_created(
env: &Env,
inv_id: Symbol,
seller: &Address,
amount: i128,
debtor: &Address,
face_value: i128,
purchase_price: i128,
due_dt: u64,
token: &Address,
inv_token: &Address,
) {
env.events().publish(
(Symbol::new(env, "escrow_created"),),
(inv_id.clone(), seller, amount, due_dt, token, inv_token),
(
inv_id.clone(),
seller,
debtor,
face_value,
purchase_price,
due_dt,
token,
inv_token,
),
);
}

/// Publish escrow_funded event.
pub fn escrow_funded(env: &Env, inv_id: Symbol, funder: &Address, amount: i128) {
/// Publish escrow_funded event with partial funding info.
pub fn escrow_funded(
env: &Env,
inv_id: Symbol,
funder: &Address,
amount: i128,
funded_amt: i128,
purchase_price: i128,
) {
env.events().publish(
(Symbol::new(env, "escrow_funded"),),
(inv_id, funder, amount),
(inv_id, funder, amount, funded_amt, purchase_price),
);
}

Expand All @@ -41,19 +59,15 @@ pub fn payment_settled(
}

/// Publish refund event.
pub fn escrow_refunded(env: &Env, inv_id: Symbol, funder: &Address, amount: i128) {
env.events().publish(
(Symbol::new(env, "escrow_refunded"),),
(inv_id, funder, amount),
);
pub fn escrow_refunded(env: &Env, inv_id: Symbol, amount: i128) {
env.events()
.publish((Symbol::new(env, "escrow_refunded"),), (inv_id, amount));
}

/// Publish escrow_cancelled event (invoice_id, seller).
pub fn escrow_cancelled(env: &Env, inv_id: Symbol, seller: &Address) {
env.events().publish(
(Symbol::new(env, "escrow_cancelled"),),
(inv_id, seller),
);
env.events()
.publish((Symbol::new(env, "escrow_cancelled"),), (inv_id, seller));
}

/// Publish platform fee update event with old and new basis points.
Expand Down
14 changes: 10 additions & 4 deletions contracts/invoice-escrow/src/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ fn test_integration_escrow_lifecycle_happy_path() {
escrow_client.create_escrow(
&invoice_id,
&seller,
&payer,
&amount,
&amount,
&due_date,
&payment_token_id.address(),
&inv_token_id,
);

// 8. Fund Escrow (Buyer buys the invoice)
escrow_client.fund_escrow(&invoice_id, &buyer);
escrow_client.fund_escrow(&invoice_id, &buyer, &amount);

// Verify buyer received invoice tokens and paid payment tokens
assert_eq!(inv_token_client.balance(&buyer), amount);
Expand Down Expand Up @@ -141,13 +143,15 @@ fn test_integration_refund_lifecycle() {
escrow_client.create_escrow(
&invoice_id,
&seller,
&seller,
&amount,
&amount,
&due_date,
&payment_token_id.address(),
&inv_token_id,
);

escrow_client.fund_escrow(&invoice_id, &buyer);
escrow_client.fund_escrow(&invoice_id, &buyer, &amount);

// Attempt refund before due date (should fail)
let res = escrow_client.try_refund(&invoice_id);
Expand Down Expand Up @@ -178,6 +182,7 @@ fn test_integration_token_locked_during_active_escrow() {
let admin = Address::generate(&env);
let seller = Address::generate(&env);
let buyer = Address::generate(&env);
let payer = Address::generate(&env);

let escrow_id = env.register(InvoiceEscrow, ());
let escrow_client = InvoiceEscrowClient::new(&env, &escrow_id);
Expand Down Expand Up @@ -208,6 +213,8 @@ fn test_integration_token_locked_during_active_escrow() {
escrow_client.create_escrow(
&invoice_id,
&seller,
&payer,
&amount,
&amount,
&due_date,
&payment_token_id.address(),
Expand All @@ -217,7 +224,7 @@ fn test_integration_token_locked_during_active_escrow() {
// Token is locked even before funding (initialized locked)
assert!(inv_token_client.transfer_locked());

escrow_client.fund_escrow(&invoice_id, &buyer);
escrow_client.fund_escrow(&invoice_id, &buyer, &amount);

// Token is still locked after funding — transfers are blocked while invoice is active
assert!(inv_token_client.transfer_locked());
Expand All @@ -226,7 +233,6 @@ fn test_integration_token_locked_during_active_escrow() {
assert!(result.is_err());

// After settlement, token unlocks
let payer = Address::generate(&env);
payment_token_asset.mint(&payer, &amount);
escrow_client.record_payment(&invoice_id, &payer, &amount);

Expand Down
Loading
Loading