diff --git a/contracts/inheritance-contract/src/lib.rs b/contracts/inheritance-contract/src/lib.rs index eb8099ad0..603237a67 100644 --- a/contracts/inheritance-contract/src/lib.rs +++ b/contracts/inheritance-contract/src/lib.rs @@ -43,8 +43,17 @@ pub struct Plan { pub timelock_duration: u64, } +// Alias for backward compatibility pub type InheritancePlan = Plan; +/// Response type for get_plan query, includes the plan and remaining time until grace period expires. +#[contracttype] +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct PlanInfo { + pub plan: Plan, + pub remaining_time: u64, +} + #[contracttype] #[derive(Clone, Debug, Eq, PartialEq)] pub enum DataKey { @@ -247,7 +256,8 @@ impl InheritanceContract { /// Retrieve the current inheritance plan data. /// Contributors: Query plan storage, dynamically projects the accumulated yield. - pub fn get_plan(env: Env, owner: Address) -> Result { + #[contractquery] + pub fn get_plan(env: Env, owner: Address) -> Result { let key = DataKey::Plan(owner.clone()); if !env.storage().persistent().has(&key) { return Err(Error::PlanNotFound); @@ -256,7 +266,17 @@ impl InheritanceContract { let plan: Plan = env.storage().persistent().get(&key).unwrap(); Self::extend_plan_ttl(&env, &key); - Ok(plan) + // Calculate remaining time until grace period expires. + let current_time = env.ledger().timestamp(); + let elapsed = current_time.saturating_sub(plan.last_ping); + let remaining_time = if plan.grace_period > elapsed { + plan.grace_period - elapsed + } else { + 0u64 + }; + + let response = PlanInfo { plan, remaining_time }; + Ok(response) } /// Trigger payout to all beneficiaries once the plan is claimable. diff --git a/contracts/inheritance-contract/src/test.rs b/contracts/inheritance-contract/src/test.rs index b441860e2..b5f4be1ba 100644 --- a/contracts/inheritance-contract/src/test.rs +++ b/contracts/inheritance-contract/src/test.rs @@ -52,7 +52,8 @@ fn test_create_plan_success() { assert_eq!(token_client.balance(&contract_id), 1500); // Verify stored plan - let plan = client.get_plan(&owner); + let plan_info = client.get_plan(&owner); + let plan = plan_info.plan; assert_eq!(plan.owner, owner); assert_eq!(plan.token, token_id); assert_eq!(plan.amount, 1500);