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
108 changes: 105 additions & 3 deletions contracts/borrowing-contract/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![no_std]
use soroban_sdk::{contract, contracterror, contractimpl, contracttype, token, Address, Env};
use soroban_sdk::{
contract, contracterror, contractimpl, contracttype, symbol_short, token, Address, Env,
};

mod test;

Expand All @@ -16,6 +18,59 @@ pub struct Loan {
pub is_active: bool,
}

// ─────────────────────────────────────────────────
// Events
// ─────────────────────────────────────────────────

#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BorrowEvent {
pub loan_id: u64,
pub borrower: Address,
pub principal: i128,
pub collateral_amount: i128,
pub collateral_token: Address,
pub interest_rate: u32,
pub due_date: u64,
pub timestamp: u64,
}

#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RepayEvent {
pub loan_id: u64,
pub borrower: Address,
pub amount_repaid: i128,
pub principal: i128,
pub interest_paid: i128,
pub collateral_returned: i128,
pub timestamp: u64,
}

#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LiquidationEvent {
pub loan_id: u64,
pub borrower: Address,
pub liquidator: Address,
pub amount_liquidated: i128,
pub collateral_seized: i128,
pub health_factor: u32,
pub timestamp: u64,
}

#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct InterestAccrualEvent {
pub loan_id: u64,
pub borrower: Address,
pub principal: i128,
pub interest_accrued: i128,
pub interest_rate: u32,
pub elapsed_seconds: u64,
pub timestamp: u64,
}

#[contracttype]
pub enum DataKey {
Admin,
Expand Down Expand Up @@ -117,20 +172,35 @@ impl BorrowingContract {
let loan_id = Self::get_next_loan_id(&env);

let loan = Loan {
borrower,
borrower: borrower.clone(),
principal,
interest_rate,
due_date,
amount_repaid: 0,
collateral_amount,
collateral_token,
collateral_token: collateral_token.clone(),
is_active: true,
};

env.storage()
.persistent()
.set(&DataKey::Loan(loan_id), &loan);

// Emit borrow event
env.events().publish(
(symbol_short!("LOAN"), symbol_short!("BORROW")),
BorrowEvent {
loan_id,
borrower: borrower.clone(),
principal,
collateral_amount,
collateral_token,
interest_rate,
due_date,
timestamp: env.ledger().timestamp(),
},
);

Ok(loan_id)
}

Expand All @@ -157,6 +227,24 @@ impl BorrowingContract {
);
}

// Emit repay event
env.events().publish(
(symbol_short!("LOAN"), symbol_short!("REPAY")),
RepayEvent {
loan_id,
borrower: loan.borrower.clone(),
amount_repaid: amount,
principal: loan.principal,
interest_paid: 0, // Interest calculation would be needed based on contract logic
collateral_returned: if loan.is_active {
0
} else {
loan.collateral_amount
},
timestamp: env.ledger().timestamp(),
},
);

env.storage()
.persistent()
.set(&DataKey::Loan(loan_id), &loan);
Expand Down Expand Up @@ -313,6 +401,20 @@ impl BorrowingContract {
.persistent()
.set(&DataKey::Loan(loan_id), &loan);

// Emit liquidation event
env.events().publish(
(symbol_short!("LOAN"), symbol_short!("LIQUIDATE")),
LiquidationEvent {
loan_id,
borrower: loan.borrower.clone(),
liquidator: liquidator.clone(),
amount_liquidated: liquidate_amount,
collateral_seized: liquidator_reward,
health_factor,
timestamp: env.ledger().timestamp(),
},
);

Ok(())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,100 @@
},
"failed_call": false
},
{
"event": {
"ext": "v0",
"contract_id": "0000000000000000000000000000000000000000000000000000000000000004",
"type_": "contract",
"body": {
"v0": {
"topics": [
{
"symbol": "LOAN"
},
{
"symbol": "BORROW"
}
],
"data": {
"map": [
{
"key": {
"symbol": "borrower"
},
"val": {
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAK3IM"
}
},
{
"key": {
"symbol": "collateral_amount"
},
"val": {
"i128": {
"hi": 0,
"lo": 1500
}
}
},
{
"key": {
"symbol": "collateral_token"
},
"val": {
"address": "CBUSYNQKASUYFWYC3M2GUEDMX4AIVWPALDBYJPNK6554BREHTGZ2IUNF"
}
},
{
"key": {
"symbol": "due_date"
},
"val": {
"u64": 1000000
}
},
{
"key": {
"symbol": "interest_rate"
},
"val": {
"u32": 5
}
},
{
"key": {
"symbol": "loan_id"
},
"val": {
"u64": 1
}
},
{
"key": {
"symbol": "principal"
},
"val": {
"i128": {
"hi": 0,
"lo": 1000
}
}
},
{
"key": {
"symbol": "timestamp"
},
"val": {
"u64": 0
}
}
]
}
}
}
},
"failed_call": false
},
{
"event": {
"ext": "v0",
Expand Down
Loading