Skip to content
Merged
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
30 changes: 30 additions & 0 deletions contracts/financing_pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ impl FinancingPoolContract {
// Standardized financing pool event
events::pool_opened(&env, &marketplace, invoice_id, &token, pool.face_value);

// Update protocol stats
let mut stats: ProtocolStats = env.storage().instance().get(&DataKey::ProtocolStats)
.unwrap_or(ProtocolStats { pools_opened: 0, total_repaid: 0, pools_defaulted: 0, active_pools: 0 });
stats.pools_opened = stats.pools_opened.saturating_add(1);
stats.active_pools = stats.active_pools.saturating_add(1);
env.storage().instance().set(&DataKey::ProtocolStats, &stats);

// Transition NFT status to Funded
nft_client.set_funded(&env.current_contract_address(), &invoice_id);

Expand Down Expand Up @@ -411,6 +418,15 @@ impl FinancingPoolContract {
let token_client = token::Client::new(&env, &token);
token_client.transfer(&payer, &env.current_contract_address(), &amount);

// Update protocol stats
let mut stats: ProtocolStats = env.storage().instance().get(&DataKey::ProtocolStats)
.unwrap_or(ProtocolStats { pools_opened: 0, total_repaid: 0, pools_defaulted: 0, active_pools: 0 });
stats.total_repaid = stats.total_repaid.saturating_add(effective_amount);
if should_close {
stats.active_pools = stats.active_pools.saturating_sub(1);
}
env.storage().instance().set(&DataKey::ProtocolStats, &stats);

// Standardized repayment event
events::repayment_made(&env, invoice_id, &payer, amount);

Expand Down Expand Up @@ -515,6 +531,13 @@ impl FinancingPoolContract {

events::invoice_defaulted(&env, invoice_id, &admin);

// Update protocol stats
let mut stats: ProtocolStats = env.storage().instance().get(&DataKey::ProtocolStats)
.unwrap_or(ProtocolStats { pools_opened: 0, total_repaid: 0, pools_defaulted: 0, active_pools: 0 });
stats.pools_defaulted = stats.pools_defaulted.saturating_add(1);
stats.active_pools = stats.active_pools.saturating_sub(1);
env.storage().instance().set(&DataKey::ProtocolStats, &stats);

// Automatically record the default against the SME in the risk registry
let invoice = nft_client.get_invoice(&invoice_id);
if let Some(rr_contract) = env
Expand Down Expand Up @@ -673,6 +696,13 @@ impl FinancingPoolContract {
kora_invoice_nft::InvoiceNftContractClient::new(&env, &nft_contract);
nft_client.set_repaid(&env.current_contract_address(), &invoice_id);

// Update protocol stats
let mut stats: ProtocolStats = env.storage().instance().get(&DataKey::ProtocolStats)
.unwrap_or(ProtocolStats { pools_opened: 0, total_repaid: 0, pools_defaulted: 0, active_pools: 0 });
stats.total_repaid = stats.total_repaid.saturating_add(offer.amount);
stats.active_pools = stats.active_pools.saturating_sub(1);
env.storage().instance().set(&DataKey::ProtocolStats, &stats);

env.storage()
.persistent()
.remove(&DataKey::EarlySettlement(invoice_id));
Expand Down
Loading