Skip to content
Open
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
24 changes: 22 additions & 2 deletions contracts/inheritance-contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<InheritancePlan, Error> {
#[contractquery]
pub fn get_plan(env: Env, owner: Address) -> Result<PlanInfo, Error> {
let key = DataKey::Plan(owner.clone());
if !env.storage().persistent().has(&key) {
return Err(Error::PlanNotFound);
Expand All @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion contracts/inheritance-contract/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading