tg_utils/
member_indexes.rs

1use cosmwasm_std::Addr;
2
3use cw_controllers::Admin;
4use cw_storage_plus::{Index, IndexList, IndexedSnapshotMap, Item, MultiIndex, Strategy};
5
6use tg4::{MemberInfo, TOTAL_KEY};
7
8use crate::{Hooks, Preauth, Slashers};
9
10pub const ADMIN: Admin = Admin::new("admin");
11pub const HOOKS: Hooks = Hooks::new("tg4-hooks");
12pub const PREAUTH_HOOKS: Preauth = Preauth::new("tg4-preauth");
13pub const SLASHERS: Slashers = Slashers::new("tg4-slashers");
14pub const PREAUTH_SLASHING: Preauth = Preauth::new("tg4-preauth_slashing");
15pub const TOTAL: Item<u64> = Item::new(TOTAL_KEY);
16
17pub struct MemberIndexes<'a> {
18    // Points (multi-)index (deserializing the (hidden) pk to Addr)
19    pub points: MultiIndex<'a, u64, MemberInfo, Addr>,
20}
21
22impl<'a> IndexList<MemberInfo> for MemberIndexes<'a> {
23    fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<MemberInfo>> + '_> {
24        let v: Vec<&dyn Index<MemberInfo>> = vec![&self.points];
25        Box::new(v.into_iter())
26    }
27}
28
29/// Indexed snapshot map for members.
30/// This allows to query the map members, sorted by points.
31/// The points index is a `MultiIndex`, as there can be multiple members with the same points.
32/// The points index is not snapshotted; only the current points are indexed at any given time.
33pub fn members<'a>() -> IndexedSnapshotMap<'a, &'a Addr, MemberInfo, MemberIndexes<'a>> {
34    let indexes = MemberIndexes {
35        points: MultiIndex::new(|_, mi| mi.points, tg4::MEMBERS_KEY, "members__points"),
36    };
37    IndexedSnapshotMap::new(
38        tg4::MEMBERS_KEY,
39        tg4::MEMBERS_CHECKPOINTS,
40        tg4::MEMBERS_CHANGELOG,
41        Strategy::EveryBlock,
42        indexes,
43    )
44}