Skip to main content

something_for_tests/
msg.rs

1use cosmwasm_std::{Binary, StdError, StdResult, Uint128};
2use cw20::{Cw20Coin, Expiration, MinterResponse};
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6#[derive(Serialize, Deserialize, JsonSchema)]
7pub struct InstantiateMsg {
8    pub name: String,
9    pub symbol: String,
10    pub decimals: u8,
11    pub initial_balances: Vec<Cw20Coin>,
12    pub mint: Option<MinterResponse>,
13}
14
15impl InstantiateMsg {
16    pub fn get_cap(&self) -> Option<Uint128> {
17        self.mint.as_ref().and_then(|v| v.cap)
18    }
19
20    pub fn validate(&self) -> StdResult<()> {
21        // Check name, symbol, decimals
22        if !is_valid_name(&self.name) {
23            return Err(StdError::generic_err(
24                "Name is not in the expected format (3-50 UTF-8 bytes)",
25            ));
26        }
27        if !is_valid_symbol(&self.symbol) {
28            return Err(StdError::generic_err(
29                "Ticker symbol is not in expected format [a-zA-Z\\-]{3,12}",
30            ));
31        }
32        if self.decimals > 18 {
33            return Err(StdError::generic_err("Decimals must not exceed 18"));
34        }
35        Ok(())
36    }
37}
38
39fn is_valid_name(name: &str) -> bool {
40    let bytes = name.as_bytes();
41    if bytes.len() < 3 || bytes.len() > 50 {
42        return false;
43    }
44    true
45}
46
47fn is_valid_symbol(symbol: &str) -> bool {
48    let bytes = symbol.as_bytes();
49    if bytes.len() < 3 || bytes.len() > 12 {
50        return false;
51    }
52    for byte in bytes.iter() {
53        if (*byte != 45) && (*byte < 65 || *byte > 90) && (*byte < 97 || *byte > 122) {
54            return false;
55        }
56    }
57    true
58}
59
60#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
61#[serde(rename_all = "snake_case")]
62pub enum ExecuteMsg {
63    /// Transfer is a base message to move tokens to another account without triggering actions
64    Transfer { recipient: String, amount: Uint128 },
65    /// Burn is a base message to destroy tokens forever
66    Burn { amount: Uint128 },
67    /// Send is a base message to transfer tokens to a contract and trigger an action
68    /// on the receiving contract.
69    Send {
70        contract: String,
71        amount: Uint128,
72        msg: Binary,
73    },
74    /// Only with the "mintable" extension. If authorized, creates amount new tokens
75    /// and adds to the recipient balance.
76    Mint { recipient: String, amount: Uint128 },
77    /// Only with "approval" extension. Allows spender to access an additional amount tokens
78    /// from the owner's (env.sender) account. If expires is Some(), overwrites current allowance
79    /// expiration with this one.
80    IncreaseAllowance {
81        spender: String,
82        amount: Uint128,
83        expires: Option<Expiration>,
84    },
85    /// Only with "approval" extension. Lowers the spender's access of tokens
86    /// from the owner's (env.sender) account by amount. If expires is Some(), overwrites current
87    /// allowance expiration with this one.
88    DecreaseAllowance {
89        spender: String,
90        amount: Uint128,
91        expires: Option<Expiration>,
92    },
93    /// Only with "approval" extension. Transfers amount tokens from owner -> recipient
94    /// if `env.sender` has sufficient pre-approval.
95    TransferFrom {
96        owner: String,
97        recipient: String,
98        amount: Uint128,
99    },
100    /// Only with "approval" extension. Sends amount tokens from owner -> contract
101    /// if `env.sender` has sufficient pre-approval.
102    SendFrom {
103        owner: String,
104        contract: String,
105        amount: Uint128,
106        msg: Binary,
107    },
108    /// Only with "approval" extension. Destroys tokens forever
109    BurnFrom { owner: String, amount: Uint128 },
110}
111
112#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
113#[serde(rename_all = "snake_case")]
114pub enum QueryMsg {
115    /// Returns the current balance of the given address, 0 if unset.
116    /// Return type: BalanceResponse.
117    Balance { address: String },
118    /// Returns metadata on the contract - name, decimals, supply, etc.
119    /// Return type: TokenInfoResponse.
120    TokenInfo {},
121    /// Only with "mintable" extension.
122    /// Returns who can mint and how much.
123    /// Return type: MinterResponse.
124    Minter {},
125    /// Only with "allowance" extension.
126    /// Returns how much spender can use from owner account, 0 if unset.
127    /// Return type: AllowanceResponse.
128    Allowance { owner: String, spender: String },
129    /// Only with "enumerable" extension (and "allowances")
130    /// Returns all allowances this owner has approved. Supports pagination.
131    /// Return type: AllAllowancesResponse.
132    AllAllowances {
133        owner: String,
134        start_after: Option<String>,
135        limit: Option<u32>,
136    },
137    /// Only with "enumerable" extension
138    /// Returns all accounts that have balances. Supports pagination.
139    /// Return type: AllAccountsResponse.
140    AllAccounts {
141        start_after: Option<String>,
142        limit: Option<u32>,
143    },
144}