forked from InsurNiffy/niff-Stellar-shurance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.rs
More file actions
82 lines (72 loc) · 2.28 KB
/
Copy pathstorage.rs
File metadata and controls
82 lines (72 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use soroban_sdk::{contracttype, Address, Env};
#[contracttype]
pub enum DataKey {
Admin,
Token,
/// (holder, policy_id) — policy_id is per-holder u32
Policy(Address, u32),
/// Per-holder policy counter; next policy_id = counter + 1
PolicyCounter(Address),
Claim(u64),
/// (claim_id, voter_address) → VoteOption
Vote(u64, Address),
/// Vec<Address> of all current active policyholders (voters)
Voters,
/// Global monotonic claim id counter
ClaimCounter,
}
pub fn set_admin(env: &Env, admin: &Address) {
env.storage().instance().set(&DataKey::Admin, admin);
}
/// Used by initialize and admin drain (feat/admin).
#[allow(dead_code)]
pub fn get_admin(env: &Env) -> Address {
env.storage().instance().get(&DataKey::Admin).unwrap()
}
pub fn set_token(env: &Env, token: &Address) {
env.storage().instance().set(&DataKey::Token, token);
}
/// Used by claim payout (feat/claim-voting).
#[allow(dead_code)]
pub fn get_token(env: &Env) -> Address {
env.storage().instance().get(&DataKey::Token).unwrap()
}
/// Returns the next policy_id for `holder` and increments the counter.
/// Used by feat/policy-lifecycle.
#[allow(dead_code)]
pub fn next_policy_id(env: &Env, holder: &Address) -> u32 {
let key = DataKey::PolicyCounter(holder.clone());
let next: u32 = env.storage().persistent().get(&key).unwrap_or(0) + 1;
env.storage().persistent().set(&key, &next);
next
}
/// Returns the next global claim_id and increments the counter.
/// Used by feat/claim-voting.
#[allow(dead_code)]
pub fn next_claim_id(env: &Env) -> u64 {
let next: u64 = env
.storage()
.instance()
.get(&DataKey::ClaimCounter)
.unwrap_or(0u64)
+ 1;
env.storage().instance().set(&DataKey::ClaimCounter, &next);
next
}
pub fn get_claim_counter(env: &Env) -> u64 {
env.storage()
.instance()
.get(&DataKey::ClaimCounter)
.unwrap_or(0u64)
}
pub fn get_policy_counter(env: &Env, holder: &Address) -> u32 {
env.storage()
.persistent()
.get(&DataKey::PolicyCounter(holder.clone()))
.unwrap_or(0u32)
}
pub fn has_policy(env: &Env, holder: &Address, policy_id: u32) -> bool {
env.storage()
.persistent()
.has(&DataKey::Policy(holder.clone(), policy_id))
}