cw_voting/
state.rs

1use cosmwasm_std::{Addr, Storage, Uint128};
2use cosmwasm_storage::{
3    bucket, bucket_read, singleton, singleton_read, Bucket, ReadonlyBucket, ReadonlySingleton,
4    Singleton,
5};
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8
9static CONFIG_KEY: &[u8] = b"config";
10static POLL_KEY: &[u8] = b"polls";
11static BANK_KEY: &[u8] = b"bank";
12
13#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
14pub struct State {
15    pub denom: String,
16    pub owner: Addr,
17    pub poll_count: u64,
18    pub staked_tokens: Uint128,
19}
20
21#[derive(Default, Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
22pub struct TokenManager {
23    pub token_balance: Uint128,             // total staked balance
24    pub locked_tokens: Vec<(u64, Uint128)>, //maps poll_id to weight voted
25    pub participated_polls: Vec<u64>,       // poll_id
26}
27
28#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
29pub struct Voter {
30    pub vote: String,
31    pub weight: Uint128,
32}
33
34#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
35pub enum PollStatus {
36    InProgress,
37    Tally,
38    Passed,
39    Rejected,
40}
41
42#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
43pub struct Poll {
44    pub creator: Addr,
45    pub status: PollStatus,
46    pub quorum_percentage: Option<u8>,
47    pub yes_votes: Uint128,
48    pub no_votes: Uint128,
49    pub voters: Vec<Addr>,
50    pub voter_info: Vec<Voter>,
51    pub end_height: u64,
52    pub start_height: Option<u64>,
53    pub description: String,
54}
55
56pub fn config(storage: &mut dyn Storage) -> Singleton<State> {
57    singleton(storage, CONFIG_KEY)
58}
59
60pub fn config_read(storage: &dyn Storage) -> ReadonlySingleton<State> {
61    singleton_read(storage, CONFIG_KEY)
62}
63
64pub fn poll(storage: &mut dyn Storage) -> Bucket<Poll> {
65    bucket(storage, POLL_KEY)
66}
67
68pub fn poll_read(storage: &dyn Storage) -> ReadonlyBucket<Poll> {
69    bucket_read(storage, POLL_KEY)
70}
71
72pub fn bank(storage: &mut dyn Storage) -> Bucket<TokenManager> {
73    bucket(storage, BANK_KEY)
74}
75
76pub fn bank_read(storage: &dyn Storage) -> ReadonlyBucket<TokenManager> {
77    bucket_read(storage, BANK_KEY)
78}