diff --git a/crates/contracts/core/src/account_monitor/src/events.rs b/crates/contracts/core/src/account_monitor/src/events.rs new file mode 100644 index 0000000..0f4f912 --- /dev/null +++ b/crates/contracts/core/src/account_monitor/src/events.rs @@ -0,0 +1,10 @@ +#![no_std] +use soroban_sdk::{Address, Env, symbol_short}; + +pub fn low_balance_alert(env: &Env, account: Address, balance: u32) { + env.events().publish((symbol_short!("low_balance"),), (account, balance)); +} + +pub fn transaction_logged(env: &Env, account: Address, tx_count: u32) { + env.events().publish((symbol_short!("tx_logged"),), (account, tx_count)); +} \ No newline at end of file diff --git a/crates/contracts/core/src/account_monitor/src/lib.rs b/crates/contracts/core/src/account_monitor/src/lib.rs new file mode 100644 index 0000000..ab41d89 --- /dev/null +++ b/crates/contracts/core/src/account_monitor/src/lib.rs @@ -0,0 +1,56 @@ +#![no_std] + +mod storage; +mod events; +mod thresholds; + +use soroban_sdk::{contract, contractimpl, Env, Address, u32}; + +#[contract] +pub struct AccountMonitorContract; + +#[contractimpl] +impl AccountMonitorContract { + + // Initialize with master account address and low balance threshold + pub fn initialize(env: Env, master: Address, low_balance: u32) { + if env.storage().has(&storage::DataKey::MasterAccount) { + panic!("Already initialized"); + } + env.storage().set(&storage::DataKey::MasterAccount, &master); + env.storage().set(&storage::DataKey::TransactionCount, &0u32); + thresholds::set_low_balance_threshold(&env, low_balance); + } + + // Log a transaction + pub fn log_transaction(env: Env) { + let master: Address = env.storage().get(&storage::DataKey::MasterAccount).unwrap(); + let count: u32 = env.storage().get(&storage::DataKey::TransactionCount).unwrap_or(0); + let new_count = count + 1; + env.storage().set(&storage::DataKey::TransactionCount, &new_count); + events::transaction_logged(&env, master, new_count); + } + + // Check for low balance and emit alert if necessary + pub fn check_low_balance(env: Env, current_balance: u32) { + let master: Address = env.storage().get(&storage::DataKey::MasterAccount).unwrap(); + let threshold = thresholds::get_low_balance_threshold(&env); + if current_balance < threshold { + events::low_balance_alert(&env, master, current_balance); + } + } + + // Set / get threshold + pub fn set_low_balance_threshold(env: Env, threshold: u32) { + thresholds::set_low_balance_threshold(&env, threshold); + } + + pub fn get_low_balance_threshold(env: Env) -> u32 { + thresholds::get_low_balance_threshold(&env) + } + + // Get transaction count + pub fn get_transaction_count(env: Env) -> u32 { + env.storage().get(&storage::DataKey::TransactionCount).unwrap_or(0) + } +} \ No newline at end of file diff --git a/crates/contracts/core/src/account_monitor/src/storage.rs b/crates/contracts/core/src/account_monitor/src/storage.rs new file mode 100644 index 0000000..fb41023 --- /dev/null +++ b/crates/contracts/core/src/account_monitor/src/storage.rs @@ -0,0 +1,9 @@ +#![no_std] +use soroban_sdk::{contracttype, Address}; + +#[contracttype] +pub enum DataKey { + MasterAccount, + TransactionCount, + LowBalanceThreshold, +} \ No newline at end of file diff --git a/crates/contracts/core/src/account_monitor/src/thresholds.rs b/crates/contracts/core/src/account_monitor/src/thresholds.rs new file mode 100644 index 0000000..f617f22 --- /dev/null +++ b/crates/contracts/core/src/account_monitor/src/thresholds.rs @@ -0,0 +1,11 @@ +#![no_std] +use soroban_sdk::{Env, u32}; +use crate::storage::DataKey; + +pub fn set_low_balance_threshold(env: &Env, threshold: u32) { + env.storage().set(&DataKey::LowBalanceThreshold, &threshold); +} + +pub fn get_low_balance_threshold(env: &Env) -> u32 { + env.storage().get(&DataKey::LowBalanceThreshold).unwrap_or(0) +} \ No newline at end of file