tg_bindings/
msg.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4use cosmwasm_std::{Binary, Coin, CosmosMsg, CustomMsg, Uint128};
5
6use crate::gov::GovProposal;
7use crate::hooks::PrivilegeMsg;
8
9#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug)]
10#[serde(rename_all = "snake_case")]
11/// A number of Custom messages that can be returned by 'privileged' contracts.
12/// Returning them from any other contract will return an error and abort the transaction.
13pub enum TgradeMsg {
14    /// request or release some privileges, such as BeginBlocker or TokenMinter
15    Privilege(PrivilegeMsg),
16    /// privileged contracts can mint arbitrary native tokens (extends BankMsg)
17    MintTokens {
18        denom: String,
19        amount: Uint128,
20        recipient: String,
21    },
22    /// as well as adjust tendermint consensus params
23    ConsensusParams(ConsensusParams),
24    /// Run another contract in "sudo" mode (extends WasmMsg)
25    WasmSudo {
26        contract_addr: String,
27        /// msg is the json-encoded SudoMsg struct (as raw Binary).
28        /// Note the contract may support different variants than the base TgradeSudoMsg,
29        /// which defines the base chain->contract interface
30        msg: Binary,
31    },
32    /// This will execute an approved proposal in the Cosmos SDK "Gov Router".
33    /// That allows access to many of the system internals, like sdk params or x/upgrade,
34    /// as well as privileged access to the wasm module (eg. mark module privileged)
35    ExecuteGovProposal {
36        title: String,
37        description: String,
38        proposal: GovProposal,
39    },
40    /// This will stake funds from the sender's vesting account. Requires `Delegator` privilege.
41    Delegate { funds: Coin, staker: String },
42    /// This will unstake funds to the recipient's vesting account. Requires `Delegator` privilege.
43    Undelegate { funds: Coin, recipient: String },
44}
45
46/// See https://github.com/tendermint/tendermint/blob/v0.34.8/proto/tendermint/abci/types.proto#L282-L289
47/// These are various Tendermint Consensus Params that can be adjusted by EndBlockers
48/// If any of them are set to Some, then the blockchain will set those as new parameter for tendermint consensus.
49///
50/// Note: we are not including ValidatorParams, which is used to change the allowed pubkey types for validators
51#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug, Default)]
52pub struct ConsensusParams {
53    pub block: Option<BlockParams>,
54    pub evidence: Option<EvidenceParams>,
55}
56
57#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug, Default)]
58pub struct BlockParams {
59    /// Maximum number of bytes (over all tx) to be included in a block
60    pub max_bytes: Option<i64>,
61    /// Maximum gas (over all tx) to be executed in one block.
62    /// If set, more txs may be included in a block, but when executing, all tx after this is limit
63    /// are consumed will immediately error
64    pub max_gas: Option<i64>,
65}
66
67#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug, Default)]
68pub struct EvidenceParams {
69    /// Max age of evidence, in blocks.
70    pub max_age_num_blocks: Option<i64>,
71
72    /// Max age of evidence, in seconds.
73    /// It should correspond with an app's "unbonding period"
74    pub max_age_duration: Option<i64>,
75
76    /// Maximum number of bytes of evidence to be included in a block
77    pub max_bytes: Option<i64>,
78}
79
80// we provide some constructor helpers for some common parameter changes
81impl ConsensusParams {
82    /// set -1 for unlimited, positive number for a gas limit over all txs in a block
83    pub fn max_block_gas(gas: i64) -> Self {
84        ConsensusParams {
85            block: Some(BlockParams {
86                max_bytes: None,
87                max_gas: Some(gas),
88            }),
89            evidence: None,
90        }
91    }
92
93    /// set -1 for unlimited, positive number for a gas limit over all txs in a block
94    pub fn max_block_size(bytes: i64) -> Self {
95        ConsensusParams {
96            block: Some(BlockParams {
97                max_bytes: Some(bytes),
98                max_gas: None,
99            }),
100            evidence: None,
101        }
102    }
103
104    pub fn max_evidence_age(seconds: i64) -> Self {
105        ConsensusParams {
106            block: None,
107            evidence: Some(EvidenceParams {
108                max_age_num_blocks: None,
109                max_age_duration: Some(seconds),
110                max_bytes: None,
111            }),
112        }
113    }
114}
115
116impl From<TgradeMsg> for CosmosMsg<TgradeMsg> {
117    fn from(msg: TgradeMsg) -> CosmosMsg<TgradeMsg> {
118        CosmosMsg::Custom(msg)
119    }
120}
121
122impl From<PrivilegeMsg> for TgradeMsg {
123    fn from(msg: PrivilegeMsg) -> TgradeMsg {
124        TgradeMsg::Privilege(msg)
125    }
126}
127
128impl From<PrivilegeMsg> for CosmosMsg<TgradeMsg> {
129    fn from(msg: PrivilegeMsg) -> CosmosMsg<TgradeMsg> {
130        CosmosMsg::Custom(TgradeMsg::from(msg))
131    }
132}
133
134impl From<ConsensusParams> for CosmosMsg<TgradeMsg> {
135    fn from(params: ConsensusParams) -> CosmosMsg<TgradeMsg> {
136        CosmosMsg::Custom(TgradeMsg::ConsensusParams(params))
137    }
138}
139
140impl CustomMsg for TgradeMsg {}