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
10 changes: 10 additions & 0 deletions crates/contracts/core/src/account_monitor/src/events.rs
Original file line number Diff line number Diff line change
@@ -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));
}
56 changes: 56 additions & 0 deletions crates/contracts/core/src/account_monitor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
9 changes: 9 additions & 0 deletions crates/contracts/core/src/account_monitor/src/storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![no_std]
use soroban_sdk::{contracttype, Address};

#[contracttype]
pub enum DataKey {
MasterAccount,
TransactionCount,
LowBalanceThreshold,
}
11 changes: 11 additions & 0 deletions crates/contracts/core/src/account_monitor/src/thresholds.rs
Original file line number Diff line number Diff line change
@@ -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)
}
Loading