tg4_mixer/
msg.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4use cosmwasm_std::{Decimal as StdDecimal, Uint64};
5use tg4::{Member, MemberChangedHookMsg};
6
7use crate::error::ContractError;
8use crate::functions::{AlgebraicSigmoid, GeometricMean, PoEFunction, Sigmoid, SigmoidSqrt};
9
10#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug)]
11pub struct InstantiateMsg {
12    /// One of the groups we feed to the mixer function
13    pub left_group: String,
14    /// The other group we feed to the mixer function
15    pub right_group: String,
16    /// Preauthorize some hooks on init (only way to add them)
17    #[serde(default)]
18    pub preauths_hooks: u64,
19    /// Preauthorize slasher registration on init (only way to add them)
20    #[serde(default)]
21    pub preauths_slashing: u64,
22    /// Enum to store the proof-of-engagement function parameters used for this contract
23    pub function_type: PoEFunctionType,
24}
25
26#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
27#[serde(rename_all = "snake_case")]
28pub enum PoEFunctionType {
29    /// GeometricMean returns the geometric mean of staked amount and engagement points
30    GeometricMean {},
31    /// Sigmoid returns a sigmoid-like value of staked amount times engagement points.
32    /// See the Proof-of-Engagement white-paper for details.
33    Sigmoid {
34        max_points: Uint64,
35        p: StdDecimal,
36        s: StdDecimal,
37    },
38    /// SigmoidSqrt returns a sigmoid-like value of the geometric mean of staked amount and
39    /// engagement points.
40    /// It is equal to `Sigmoid` with `p = 0.5`, but implemented using integer sqrt instead of
41    /// fixed-point fractional power.
42    SigmoidSqrt { max_points: Uint64, s: StdDecimal },
43    /// `AlgebraicSigmoid` returns a sigmoid-like value of staked amount times engagement points.
44    /// It is similar to `Sigmoid`, but uses integer sqrt instead of a fixed-point exponential.
45    AlgebraicSigmoid {
46        max_points: Uint64,
47        a: StdDecimal,
48        p: StdDecimal,
49        s: StdDecimal,
50    },
51}
52
53impl PoEFunctionType {
54    pub fn to_poe_fn(&self) -> Result<Box<dyn PoEFunction>, ContractError> {
55        match self.clone() {
56            PoEFunctionType::GeometricMean {} => Ok(Box::new(GeometricMean::new())),
57            PoEFunctionType::Sigmoid { max_points, p, s } => {
58                Ok(Box::new(Sigmoid::new(max_points, p, s)?))
59            }
60            PoEFunctionType::SigmoidSqrt { max_points, s } => {
61                Ok(Box::new(SigmoidSqrt::new(max_points, s)?))
62            }
63            PoEFunctionType::AlgebraicSigmoid {
64                max_points,
65                a,
66                p,
67                s,
68            } => Ok(Box::new(AlgebraicSigmoid::new(max_points, a, p, s)?)),
69        }
70    }
71}
72
73#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug)]
74#[serde(rename_all = "snake_case")]
75pub enum ExecuteMsg {
76    /// This handles a callback from one of the linked groups
77    MemberChangedHook(MemberChangedHookMsg),
78    /// Add a new hook to be informed of all membership changes.
79    AddHook { addr: String },
80    /// Remove a hook. Must be called by the contract being removed
81    RemoveHook { addr: String },
82    /// Adds slasher for contract if there are enough `slasher_preauths` left
83    AddSlasher { addr: String },
84    /// Removes slasher for contract
85    RemoveSlasher { addr: String },
86    /// Slash engagement points from address
87    Slash { addr: String, portion: StdDecimal },
88}
89
90#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug)]
91#[serde(rename_all = "snake_case")]
92pub enum QueryMsg {
93    /// Return TotalPointsResponse
94    TotalPoints {},
95    /// Returns MemberListResponse
96    ListMembers {
97        start_after: Option<String>,
98        limit: Option<u32>,
99    },
100    /// Returns MemberListResponse, sorted by points descending
101    ListMembersByPoints {
102        start_after: Option<Member>,
103        limit: Option<u32>,
104    },
105    /// Returns MemberResponse
106    Member {
107        addr: String,
108        at_height: Option<u64>,
109    },
110    /// Shows all registered hooks. Returns HooksResponse.
111    Hooks {},
112    /// Which contracts we are listening to
113    Groups {},
114    /// Return the current number of preauths. Returns PreauthResponse.
115    Preauths {},
116    /// Points assigned by a PoE mixer function (used for benchmarking).
117    /// Returns MixerFunctionResponse.
118    MixerFunction {
119        stake: Uint64,
120        engagement: Uint64,
121        poe_function: Option<PoEFunctionType>,
122    },
123    /// Returns information (bool) whether given address is an active slasher
124    IsSlasher { addr: String },
125    /// Shows all active slashers as vector of addresses
126    ListSlashers {},
127}
128
129/// Return the two groups we are listening to
130#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug)]
131pub struct GroupsResponse {
132    pub left: String,
133    pub right: String,
134}
135
136#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug)]
137pub struct PreauthResponse {
138    pub preauths_hooks: u64,
139}
140
141#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug)]
142pub struct MixerFunctionResponse {
143    pub points: u64,
144}