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
25 changes: 13 additions & 12 deletions contracts/assetsup/src/audit.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use soroban_sdk::{contracttype, Address, BytesN, Env, String, Vec};

use crate::types::ActionType;

#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DataKey {
Expand All @@ -11,19 +9,20 @@ pub enum DataKey {
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AuditEntry {
pub actor: Address,
pub action: ActionType,
pub timestamp: u64,
pub note: String,
pub action: String,
pub actor: Address,
pub details: String,
}

#[allow(dead_code)]
pub fn log_action(
/// Internal function to append an audit log entry for an asset
/// This function is used by various modules to record significant events
pub(crate) fn append_audit_log(
env: &Env,
asset_id: &BytesN<32>,
action: String,
actor: Address,
action: ActionType,
note: String,
details: String,
) {
let key = DataKey::AuditLog(asset_id.clone());
let mut log: Vec<AuditEntry> = env
Expand All @@ -33,16 +32,18 @@ pub fn log_action(
.unwrap_or_else(|| Vec::new(env));

let entry = AuditEntry {
actor,
action,
timestamp: env.ledger().timestamp(),
note,
action,
actor,
details,
};

log.push_back(entry);
env.storage().persistent().set(&key, &log);
}

/// Public function to retrieve the audit log for an asset
/// Returns an empty vector if no history exists
pub fn get_asset_log(env: &Env, asset_id: &BytesN<32>) -> Vec<AuditEntry> {
let key = DataKey::AuditLog(asset_id.clone());
env.storage()
Expand Down
30 changes: 29 additions & 1 deletion contracts/assetsup/src/insurance.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#![allow(dead_code)]

use crate::audit;
use crate::Error;
use soroban_sdk::{contracttype, log, Address, BytesN, Env, Vec};
use soroban_sdk::{contracttype, log, Address, BytesN, Env, String, Vec};

#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -123,6 +124,15 @@ pub fn create_policy(env: Env, policy: InsurancePolicy) -> Result<(), Error> {
list.push_back(policy.policy_id.clone());
store.set(&DataKey::AssetPolicies(policy.asset_id.clone()), &list);

// Append audit log
audit::append_audit_log(
&env,
&policy.asset_id,
String::from_str(&env, "INSURANCE_POLICY_CREATED"),
policy.insurer.clone(),
String::from_str(&env, "Insurance policy created"),
);

log!(&env, "PolicyCreated: {:?}", policy.policy_id);
Ok(())
}
Expand All @@ -147,6 +157,15 @@ pub fn cancel_policy(env: Env, policy_id: BytesN<32>, caller: Address) -> Result
policy.status = PolicyStatus::Cancelled;
store.set(&key, &policy);

// Append audit log
audit::append_audit_log(
&env,
&policy.asset_id,
String::from_str(&env, "INSURANCE_POLICY_CANCELLED"),
caller,
String::from_str(&env, "Insurance policy cancelled"),
);

log!(&env, "PolicyCancelled: {:?}", policy_id);
Ok(())
}
Expand Down Expand Up @@ -244,6 +263,15 @@ pub fn renew_policy(

store.set(&key, &policy);

// Append audit log
audit::append_audit_log(
&env,
&policy.asset_id,
String::from_str(&env, "INSURANCE_POLICY_RENEWED"),
insurer,
String::from_str(&env, "Insurance policy renewed"),
);

log!(&env, "PolicyRenewed: {:?}", policy_id);
Ok(())
}
Expand Down
36 changes: 36 additions & 0 deletions contracts/assetsup/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ impl AssetUpContract {
.persistent()
.set(&DataKey::TotalAssetCount, &total_count);

// Append audit log
audit::append_audit_log(
&env,
&asset.id,
String::from_str(&env, "ASSET_REGISTERED"),
caller.clone(),
String::from_str(&env, "Asset registered by authorized registrar"),
);

// Emit event
env.events().publish(
(symbol_short!("asset_reg"),),
Expand Down Expand Up @@ -247,6 +256,15 @@ impl AssetUpContract {

store.set(&key, &asset);

// Append audit log
audit::append_audit_log(
&env,
&asset_id,
String::from_str(&env, "METADATA_UPDATED"),
caller.clone(),
String::from_str(&env, "Asset metadata updated"),
);

// Emit event
env.events().publish(
(symbol_short!("asset_upd"),),
Expand Down Expand Up @@ -313,6 +331,15 @@ impl AssetUpContract {
asset.status = AssetStatus::Transferred;
store.set(&key, &asset);

// Append audit log
audit::append_audit_log(
&env,
&asset_id,
String::from_str(&env, "OWNERSHIP_TRANSFERRED"),
caller.clone(),
String::from_str(&env, "Asset ownership transferred to new owner"),
);

// Emit event
env.events().publish(
(symbol_short!("asset_tx"),),
Expand Down Expand Up @@ -345,6 +372,15 @@ impl AssetUpContract {
asset.status = AssetStatus::Retired;
store.set(&key, &asset);

// Append audit log
audit::append_audit_log(
&env,
&asset_id,
String::from_str(&env, "ASSET_RETIRED"),
caller.clone(),
String::from_str(&env, "Asset retired from active use"),
);

// Emit event
env.events().publish(
(symbol_short!("asset_ret"),),
Expand Down
Loading