Skip to main content

tg4_stake/
msg.rs

1use cosmwasm_std::{Coin, Decimal, Uint128};
2use schemars::JsonSchema;
3use serde::{Deserialize, Serialize};
4use tg_utils::{Duration, Expiration};
5
6pub use crate::claim::Claim;
7use tg4::Member;
8
9const fn default_auto_return_limit() -> u64 {
10    20
11}
12
13#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug)]
14pub struct InstantiateMsg {
15    /// Denom of the token to stake
16    pub denom: String,
17    pub tokens_per_point: Uint128,
18    pub min_bond: Uint128,
19    /// Unbounding period in seconds
20    pub unbonding_period: u64,
21
22    // admin can only add/remove hooks and slashers, not change other parameters
23    pub admin: Option<String>,
24    // or you can simply pre-authorize a number of hooks (to be done in the following messages)
25    #[serde(default)]
26    pub preauths_hooks: u64,
27    // and you can pre-authorize a number of slashers the same way
28    #[serde(default)]
29    pub preauths_slashing: u64,
30    /// Limits how much claims would be automatically returned at end of block, 20 by default.
31    /// Setting this to 0 disables auto returning claims.
32    #[serde(default = "default_auto_return_limit")]
33    pub auto_return_limit: u64,
34}
35
36#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug)]
37#[serde(rename_all = "snake_case")]
38pub enum ExecuteMsg {
39    /// Bond will bond all staking tokens sent with the message and update membership points.
40    /// The optional `vesting_tokens` will be staked (delegated) as well, if set.
41    Bond { vesting_tokens: Option<Coin> },
42    /// Unbond will start the unbonding process for the given number of tokens.
43    /// The sender immediately loses points from these tokens, and can claim them
44    /// back to his wallet after `unbonding_period`.
45    /// Tokens will be unbonded from the liquid stake first, and then from the vesting stake
46    /// if available.
47    Unbond { tokens: Coin },
48    /// Claim is used to claim your native and vesting tokens that you previously "unbonded"
49    /// after the contract-defined waiting period (eg. 1 week)
50    Claim {},
51
52    /// Change the admin
53    UpdateAdmin { admin: Option<String> },
54    /// Add a new hook to be informed of all membership changes. Must be called by Admin
55    AddHook { addr: String },
56    /// Remove a hook. Must be called by Admin
57    RemoveHook { addr: String },
58    /// Add a new slasher. Must be called by Admin
59    AddSlasher { addr: String },
60    /// Remove a slasher. Must be called by Admin
61    RemoveSlasher { addr: String },
62    Slash {
63        addr: String,
64        // between (0.0, 1.0]
65        portion: Decimal,
66    },
67}
68
69#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug)]
70#[serde(rename_all = "snake_case")]
71pub enum QueryMsg {
72    /// Returns config
73    Configuration {},
74    /// Claims shows the tokens in process of unbonding for this address
75    Claims {
76        address: String,
77        limit: Option<u32>,
78        start_after: Option<Expiration>,
79    },
80    /// Shows the number of liquid and vesting tokens currently staked by this address.
81    /// Returns StakedResponse.
82    Staked { address: String },
83    /// Returns the unbonding period of this contract.
84    /// Returns UnbondingPeriodResponse.
85    UnbondingPeriod {},
86
87    /// Return AdminResponse
88    Admin {},
89    /// Returns TotalPointsResponse. This is the amount of tokens bonded divided by
90    /// tokens_per_point.
91    TotalPoints {},
92    /// Returns MemberListResponse
93    ListMembers {
94        start_after: Option<String>,
95        limit: Option<u32>,
96    },
97    /// Returns MemberListResponse, sorted by points descending.
98    ListMembersByPoints {
99        start_after: Option<Member>,
100        limit: Option<u32>,
101    },
102    /// Returns MemberResponse
103    Member {
104        addr: String,
105        at_height: Option<u64>,
106    },
107    /// Shows all registered hooks. Returns HooksResponse.
108    Hooks {},
109    /// Return the current number of preauths. Returns PreauthResponse.
110    Preauths {},
111    /// Returns information (bool) about whether a given address is an active slasher
112    IsSlasher { addr: String },
113    /// Returns all active slashers as a vector of addresses.
114    ListSlashers {},
115}
116
117#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
118pub struct StakedResponse {
119    pub liquid: Coin,
120    pub vesting: Coin,
121}
122
123#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug)]
124pub struct PreauthResponse {
125    pub preauths_hooks: u64,
126}
127
128#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug)]
129pub struct UnbondingPeriodResponse {
130    pub unbonding_period: Duration,
131}
132
133#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
134pub struct ClaimsResponse {
135    pub claims: Vec<Claim>,
136}
137
138#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
139pub struct Undelegation {
140    pub addr: String,
141    pub amount: Uint128,
142}
143
144#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug)]
145#[serde(rename_all = "snake_case")]
146pub struct MigrateMsg {
147    pub tokens_per_point: Option<Uint128>,
148    pub min_bond: Option<Uint128>,
149    pub unbonding_period: Option<u64>,
150    pub auto_return_limit: Option<u64>,
151    pub undelegations: Option<Vec<Undelegation>>,
152}
153
154#[cfg(test)]
155mod tests {
156    use super::*;
157
158    use cosmwasm_std::to_vec;
159    use tg_utils::Duration;
160
161    #[test]
162    fn unbonding_period_serializes_in_seconds() {
163        let res = UnbondingPeriodResponse {
164            unbonding_period: Duration::new(12345),
165        };
166        let json = to_vec(&res).unwrap();
167        assert_eq!(&json, br#"{"unbonding_period":12345}"#);
168    }
169}