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
110 changes: 108 additions & 2 deletions Contract/borrowing/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#![no_std]
use soroban_sdk::{contract, contractimpl, contracttype, Address, Bytes, BytesN, Env, Vec, Map, Symbol, IntoVal, Val};
use shared::errors::Error;
use shared::events::{LoanIssued, LoanRepaid, CollateralLocked, CollateralReleased};
use shared::events::{
AdminTransferPending, AdminTransferred, ContractInitialized,
LoanIssued, LoanRepaid, CollateralLocked, CollateralReleased,
};
use shared::types::{CollateralConfig, LoanInfo, LoanStatus, PoolAccounting};
use shared::utils::{SafeMath, TimeHelper, ValidationHelper};

Expand All @@ -17,6 +20,8 @@ pub enum BorrowingKey {
LendingPoolAddress,
VaultContractAddress,
CollateralLocked(BytesN<32>), // Track if collateral is locked for a loan
ContractAdmin,
PendingAdmin,
}

// Collateral ratio constant (150% = 15000 basis points)
Expand All @@ -28,9 +33,15 @@ pub struct BorrowingContract;

#[contractimpl]
impl BorrowingContract {
/// Initialize the borrowing contract with dependent contract addresses
/// Initialize the borrowing contract with an administrator and dependent contract addresses.
///
/// Can only be called once. The caller becomes the contract administrator.
///
/// # Auth
/// Requires authorization from the deployer (initial admin).
pub fn initialize(
env: Env,
admin: Address,
lending_pool_address: Address,
vault_contract_address: Address,
) -> Result<(), Error> {
Expand All @@ -39,20 +50,115 @@ impl BorrowingContract {
return Err(Error::AlreadyInitialized);
}

admin.require_auth();

env.storage().persistent().set(&BorrowingKey::ContractAdmin, &admin);
env.storage().persistent().set(&BorrowingKey::LendingPoolAddress, &lending_pool_address);
env.storage().persistent().set(&BorrowingKey::VaultContractAddress, &vault_contract_address);

env.events().publish(
ContractInitialized::topic(&env),
ContractInitialized {
admin,
timestamp: shared::utils::TimeHelper::now(&env),
},
);

Ok(())
}

/// Get the current contract administrator
pub fn get_contract_admin(env: Env) -> Result<Address, Error> {
env.storage()
.persistent()
.get(&BorrowingKey::ContractAdmin)
.ok_or(Error::NotInitialized)
}

/// Initiate a two-step administrator transfer.
///
/// # Auth
/// Requires authorization from the current administrator.
pub fn transfer_admin(env: Env, proposed_admin: Address) -> Result<(), Error> {
let current_admin: Address = env
.storage()
.persistent()
.get(&BorrowingKey::ContractAdmin)
.ok_or(Error::NotInitialized)?;
current_admin.require_auth();

if current_admin == proposed_admin {
return Err(Error::CannotTransferToSelf);
}

env.storage()
.persistent()
.set(&BorrowingKey::PendingAdmin, &proposed_admin);

env.events().publish(
AdminTransferPending::topic(&env),
AdminTransferPending {
current_admin,
proposed_admin,
timestamp: shared::utils::TimeHelper::now(&env),
},
);

Ok(())
}

/// Accept an administrator transfer.
///
/// # Auth
/// Requires authorization from the proposed administrator.
pub fn accept_admin(env: Env) -> Result<(), Error> {
let proposed_admin: Address = env
.storage()
.persistent()
.get(&BorrowingKey::PendingAdmin)
.ok_or(Error::NoAdminTransferPending)?;
proposed_admin.require_auth();

let previous_admin: Address = env
.storage()
.persistent()
.get(&BorrowingKey::ContractAdmin)
.ok_or(Error::NotInitialized)?;

env.storage().persistent().set(&BorrowingKey::ContractAdmin, &proposed_admin);
env.storage().persistent().remove(&BorrowingKey::PendingAdmin);

env.events().publish(
AdminTransferred::topic(&env),
AdminTransferred {
previous_admin,
new_admin: proposed_admin,
timestamp: shared::utils::TimeHelper::now(&env),
},
);

Ok(())
}

/// Configure collateral parameters for an asset
///
/// # Auth
/// Requires authorization from the contract administrator.
pub fn configure_collateral(
env: Env,
asset: BytesN<32>,
liquidation_threshold: i128,
loan_to_value: i128,
safety_factor: i128,
) -> Result<(), Error> {
// Require contract administrator authorization
let contract_admin: Address = env
.storage()
.persistent()
.get(&BorrowingKey::ContractAdmin)
.ok_or(Error::NotInitialized)?;
contract_admin.require_auth();

if !ValidationHelper::validate_interest_rate(liquidation_threshold) {
return Err(Error::InvalidParameters);
}
Expand Down
136 changes: 135 additions & 1 deletion Contract/lending/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
use soroban_sdk::{contract, contractimpl, contracttype, Address, BytesN, Env};
use shared::errors::Error;
use shared::events::{
BorrowingContractInitialized, InterestAccrued, PoolAccountingUpdated, PoolCreated,
AdminTransferPending, AdminTransferred, BorrowingContractInitialized,
ContractInitialized, InterestAccrued, PoolAccountingUpdated, PoolCreated,
PoolDeposit, PoolWithdrawal,
};
use shared::types::{
Expand All @@ -29,6 +30,8 @@ pub enum PoolKey {
AdminPermissions(Address),
PoolStatus(BytesN<32>),
BorrowingContract(BytesN<32>),
ContractAdmin,
PendingAdmin,
}

/// Lending contract for managing lending pools and interest.
Expand All @@ -40,13 +43,124 @@ pub use LendingContract;

#[contractimpl]
impl LendingContract {
/// Initialize the lending contract with an administrator.
///
/// Can only be called once. The caller becomes the contract administrator
/// who governs pool creation and admin grants.
///
/// # Auth
/// Requires authorization from the deployer (initial admin).
pub fn initialize(env: Env, admin: Address) -> Result<(), Error> {
if env.storage().persistent().has(&PoolKey::ContractAdmin) {
return Err(Error::AlreadyInitialized);
}

admin.require_auth();

env.storage().persistent().set(&PoolKey::ContractAdmin, &admin);

env.events().publish(
ContractInitialized::topic(&env),
ContractInitialized {
admin,
timestamp: TimeHelper::now(&env),
},
);

Ok(())
}

/// Get the current contract administrator
pub fn get_contract_admin(env: Env) -> Result<Address, Error> {
env.storage()
.persistent()
.get(&PoolKey::ContractAdmin)
.ok_or(Error::NotInitialized)
}

/// Initiate a two-step administrator transfer.
///
/// # Auth
/// Requires authorization from the current administrator.
pub fn transfer_admin(env: Env, proposed_admin: Address) -> Result<(), Error> {
let current_admin: Address = env
.storage()
.persistent()
.get(&PoolKey::ContractAdmin)
.ok_or(Error::NotInitialized)?;
current_admin.require_auth();

if current_admin == proposed_admin {
return Err(Error::CannotTransferToSelf);
}

env.storage()
.persistent()
.set(&PoolKey::PendingAdmin, &proposed_admin);

env.events().publish(
AdminTransferPending::topic(&env),
AdminTransferPending {
current_admin,
proposed_admin,
timestamp: TimeHelper::now(&env),
},
);

Ok(())
}

/// Accept an administrator transfer.
///
/// # Auth
/// Requires authorization from the proposed administrator.
pub fn accept_admin(env: Env) -> Result<(), Error> {
let proposed_admin: Address = env
.storage()
.persistent()
.get(&PoolKey::PendingAdmin)
.ok_or(Error::NoAdminTransferPending)?;
proposed_admin.require_auth();

let previous_admin: Address = env
.storage()
.persistent()
.get(&PoolKey::ContractAdmin)
.ok_or(Error::NotInitialized)?;

env.storage().persistent().set(&PoolKey::ContractAdmin, &proposed_admin);
env.storage().persistent().remove(&PoolKey::PendingAdmin);

env.events().publish(
AdminTransferred::topic(&env),
AdminTransferred {
previous_admin,
new_admin: proposed_admin,
timestamp: TimeHelper::now(&env),
},
);

Ok(())
}

/// Create a new lending pool for a specific asset
///
/// # Auth
/// Requires authorization from the contract administrator.
pub fn create_pool(
env: Env,
admin: Address,
asset: BytesN<32>,
interest_rate_bps: i128,
) -> Result<(), Error> {
// Require contract administrator authorization
let contract_admin: Address = env
.storage()
.persistent()
.get(&PoolKey::ContractAdmin)
.ok_or(Error::NotInitialized)?;
contract_admin.require_auth();

if !ValidationHelper::validate_interest_rate(interest_rate_bps) {
return Err(Error::InvalidInterestRate);
}
Expand Down Expand Up @@ -629,12 +743,22 @@ impl LendingContract {
}

/// Set rate limit for a pool
///
/// # Auth
/// Requires authorization from the contract administrator.
pub fn set_rate_limit(
env: Env,
pool_id: BytesN<32>,
max_ops: u64,
period_seconds: u64,
) -> Result<(), Error> {
let contract_admin: Address = env
.storage()
.persistent()
.get(&PoolKey::ContractAdmin)
.ok_or(Error::NotInitialized)?;
contract_admin.require_auth();

let rate_limit = RateLimit::new(max_ops, period_seconds);
env.storage()
.persistent()
Expand Down Expand Up @@ -673,7 +797,17 @@ impl LendingContract {
}

/// Grant admin permission
///
/// # Auth
/// Requires authorization from the contract administrator.
pub fn grant_admin(env: Env, admin: Address) -> Result<(), Error> {
let contract_admin: Address = env
.storage()
.persistent()
.get(&PoolKey::ContractAdmin)
.ok_or(Error::NotInitialized)?;
contract_admin.require_auth();

let permission = shared::types::Permission {
role: Role::Admin,
granted_at: TimeHelper::now(&env),
Expand Down
Loading