1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
//! Entrypoint messages for the CW20 contract.
use super::Cw20Coin;
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{Addr, Binary, Uint128};
use cw_utils::Expiration;
use shared::prelude::*;

#[cw_serde]
pub struct InstantiateMsg {
    /************** Cw20 spec *******************/
    pub name: String,
    pub symbol: String,
    pub decimals: u8,
    pub initial_balances: Vec<Cw20Coin>,
    /// We make this mandatory since we always need an owner for these CW20s.
    pub minter: InstantiateMinter,
    pub marketing: Option<InstantiateMarketingInfo>,
}

impl InstantiateMsg {
    pub fn get_cap(&self) -> Option<Uint128> {
        self.minter.cap
    }

    pub fn validate(&self) -> anyhow::Result<()> {
        // Check name, symbol, decimals
        if !self.has_valid_name() {
            perp_bail!(
                ErrorId::MsgValidation,
                ErrorDomain::Cw20,
                "Name is not in the expected format (3-50 UTF-8 bytes)"
            );
        }
        if !self.has_valid_symbol() {
            perp_bail!(
                ErrorId::MsgValidation,
                ErrorDomain::Cw20,
                "Ticker symbol is not in expected format [a-zA-Z\\-]{{3,12}}"
            );
        }
        if self.decimals > 18 {
            perp_bail!(
                ErrorId::MsgValidation,
                ErrorDomain::Cw20,
                "Decimals must not exceed 18"
            );
        }
        if !self.has_valid_balances() {
            perp_bail!(
                ErrorId::MsgValidation,
                ErrorDomain::Cw20,
                "duplicate account balances"
            );
        }
        Ok(())
    }

    fn has_valid_name(&self) -> bool {
        let bytes = self.name.as_bytes();
        if bytes.len() < 3 || bytes.len() > 50 {
            return false;
        }
        true
    }

    fn has_valid_symbol(&self) -> bool {
        let bytes = self.symbol.as_bytes();
        if bytes.len() < 3 || bytes.len() > 12 {
            return false;
        }
        for byte in bytes.iter() {
            if (*byte != 45) && (*byte < 65 || *byte > 90) && (*byte < 97 || *byte > 122) {
                return false;
            }
        }
        true
    }

    fn has_valid_balances(&self) -> bool {
        let mut addresses = self
            .initial_balances
            .iter()
            .map(|c| &c.address)
            .collect::<Vec<_>>();
        addresses.sort();
        addresses.dedup();

        // check for duplicates
        addresses.len() == self.initial_balances.len()
    }
}

#[cw_serde]
pub enum ExecuteMsg {
    /************** Cw20 spec *******************/
    /// Transfer is a base message to move tokens to another account without triggering actions
    Transfer { recipient: RawAddr, amount: Uint128 },
    /// Burn is a base message to destroy tokens forever
    Burn { amount: Uint128 },
    /// Send is a base message to transfer tokens to a contract and trigger an action
    /// on the receiving contract.
    Send {
        contract: RawAddr,
        amount: Uint128,
        msg: Binary,
    },
    /// Allows spender to access an additional amount tokens
    /// from the owner's (env.sender) account. If expires is Some(), overwrites current allowance
    /// expiration with this one.
    IncreaseAllowance {
        spender: RawAddr,
        amount: Uint128,
        expires: Option<Expiration>,
    },
    /// Lowers the spender's access of tokens
    /// from the owner's (env.sender) account by amount. If expires is Some(), overwrites current
    /// allowance expiration with this one.
    DecreaseAllowance {
        spender: RawAddr,
        amount: Uint128,
        expires: Option<Expiration>,
    },
    /// Transfers amount tokens from owner -> recipient
    /// if `env.sender` has sufficient pre-approval.
    TransferFrom {
        owner: RawAddr,
        recipient: RawAddr,
        amount: Uint128,
    },
    /// Sends amount tokens from owner -> contract
    /// if `env.sender` has sufficient pre-approval.
    SendFrom {
        owner: RawAddr,
        contract: RawAddr,
        amount: Uint128,
        msg: Binary,
    },
    /// Destroys tokens forever
    BurnFrom { owner: RawAddr, amount: Uint128 },
    /// If authorized, creates amount new tokens
    /// and adds to the recipient balance.
    Mint { recipient: RawAddr, amount: Uint128 },
    /// This variant is according to spec. The current minter may set
    /// a new minter. Setting the minter to None will remove the
    /// token's minter forever.
    /// there is deliberately *not* a way to set the proprietary MinterKind
    /// so the only way to set the minter to MinterKind::MarketId is at
    /// instantiation
    ///
    /// Note: we require that there always be a minter, so this is not optional!
    UpdateMinter { new_minter: RawAddr },
    /// If authorized, updates marketing metadata.
    /// Setting None/null for any of these will leave it unchanged.
    /// Setting Some("") will clear this field on the contract storage
    UpdateMarketing {
        /// A URL pointing to the project behind this token.
        project: Option<String>,
        /// A longer description of the token and it's utility. Designed for tooltips or such
        description: Option<String>,
        /// The address (if any) who can update this data structure
        marketing: Option<String>,
    },
    /// If set as the "marketing" role on the contract, upload a new URL, SVG, or PNG for the token
    UploadLogo(Logo),
    /************** Proprietary *******************/
    /// Set factory addr
    SetMarket { addr: RawAddr },
}

#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
    /************** Cw20 spec *******************/
    /// * returns [BalanceResponse]
    ///
    /// The current balance of the given address, 0 if unset.
    #[returns(BalanceResponse)]
    Balance { address: RawAddr },

    /// * returns [TokenInfoResponse]
    ///
    /// Returns metadata on the contract - name, decimals, supply, etc.
    #[returns(TokenInfoResponse)]
    TokenInfo {},

    /// * returns [MinterResponse]
    ///
    /// Returns who can mint and the hard cap on maximum tokens after minting.
    #[returns(Option<MinterResponse>)]
    Minter {},

    /// * returns [AllowanceResponse]
    ///
    /// Returns how much spender can use from owner account, 0 if unset.
    #[returns(AllowanceResponse)]
    Allowance { owner: RawAddr, spender: RawAddr },

    /// * returns [AllAllowancesResponse]
    ///
    /// Returns all allowances this owner has approved. Supports pagination.
    #[returns(AllAllowancesResponse)]
    AllAllowances {
        owner: RawAddr,
        start_after: Option<RawAddr>,
        limit: Option<u32>,
    },

    /// * returns [AllSpenderAllowancesResponse]
    ///
    /// Returns all allowances this spender has been granted. Supports pagination.
    #[returns(AllSpenderAllowancesResponse)]
    AllSpenderAllowances {
        spender: RawAddr,
        start_after: Option<RawAddr>,
        limit: Option<u32>,
    },

    /// * returns [AllAccountsResponse]
    ///
    /// Returns all accounts that have balances. Supports pagination.
    #[returns(AllAccountsResponse)]
    AllAccounts {
        start_after: Option<RawAddr>,
        limit: Option<u32>,
    },

    /// * returns [MarketingInfoResponse]
    ///
    /// Returns more metadata on the contract to display in the client:
    /// - description, logo, project url, etc.
    #[returns(MarketingInfoResponse)]
    MarketingInfo {},

    /// * returns [DownloadLogoResponse]
    ///
    /// Downloads the embedded logo data (if stored on chain). Errors if no logo data is stored for this
    /// contract.
    #[returns(DownloadLogoResponse)]
    DownloadLogo {},

    /************** Proprietary *******************/
    /// * returns [cw2::ContractVersion]
    #[returns(cw2::ContractVersion)]
    Version {},
}

/// Placeholder migration message
#[cw_serde]
pub struct MigrateMsg {}

#[cw_serde]
#[derive(Eq)]
pub struct InstantiateMinter {
    pub minter: RawAddr,
    pub cap: Option<Uint128>,
}

/************** Proprietary but doesn't affect interop *******************/
/************** since queries are according to spec *******************/
/************** and only return addresses *******************/
#[cw_serde]
pub struct InstantiateMarketingInfo {
    pub project: Option<String>,
    pub description: Option<String>,
    pub marketing: Option<Addr>,
    pub logo: Option<Logo>,
}

#[cw_serde]
#[derive(Eq)]
pub struct BalanceResponse {
    pub balance: Uint128,
}

#[cw_serde]
#[derive(Eq)]
pub struct TokenInfoResponse {
    pub name: String,
    pub symbol: String,
    pub decimals: u8,
    pub total_supply: Uint128,
}

#[cw_serde]
#[derive(Default)]
pub struct AllowanceResponse {
    pub allowance: Uint128,
    pub expires: Expiration,
}

#[cw_serde]
#[derive(Eq)]
pub struct MinterResponse {
    pub minter: Addr,
    /// cap is a hard cap on total supply that can be achieved by minting.
    /// Note that this refers to total_supply.
    /// If None, there is unlimited cap.
    pub cap: Option<Uint128>,
}

#[cw_serde]
#[derive(Default)]
pub struct MarketingInfoResponse {
    /// A URL pointing to the project behind this token.
    pub project: Option<String>,
    /// A longer description of the token and it's utility. Designed for tooltips or such
    pub description: Option<String>,
    /// A link to the logo, or a comment there is an on-chain logo stored
    pub logo: Option<LogoInfo>,
    /// The address (if any) who can update this data structure
    pub marketing: Option<Addr>,
}

/// When we download an embedded logo, we get this response type.
/// We expect a SPA to be able to accept this info and display it.
#[cw_serde]
pub struct DownloadLogoResponse {
    pub mime_type: String,
    pub data: Binary,
}

#[cw_serde]
pub struct AllowanceInfo {
    pub spender: Addr,
    pub allowance: Uint128,
    pub expires: Expiration,
}

#[cw_serde]
#[derive(Default)]
pub struct AllAllowancesResponse {
    pub allowances: Vec<AllowanceInfo>,
}

#[cw_serde]
pub struct SpenderAllowanceInfo {
    pub owner: Addr,
    pub allowance: Uint128,
    pub expires: Expiration,
}

#[cw_serde]
#[derive(Default)]
pub struct AllSpenderAllowancesResponse {
    pub allowances: Vec<SpenderAllowanceInfo>,
}

#[cw_serde]
#[derive(Default)]
pub struct AllAccountsResponse {
    pub accounts: Vec<Addr>,
}

/// This is used for uploading logo data, or setting it in InstantiateData
#[cw_serde]
pub enum Logo {
    /// A reference to an externally hosted logo. Must be a valid HTTP or HTTPS URL.
    Url(String),
    /// Logo content stored on the blockchain. Enforce maximum size of 5KB on all variants
    Embedded(EmbeddedLogo),
}

/// This is used to store the logo on the blockchain in an accepted format.
/// Enforce maximum size of 5KB on all variants.
#[cw_serde]
pub enum EmbeddedLogo {
    /// Store the Logo as an SVG file. The content must conform to the spec
    /// at <https://en.wikipedia.org/wiki/Scalable_Vector_Graphics>
    ///
    /// (The contract should do some light-weight sanity-check validation)
    Svg(Binary),
    /// Store the Logo as a PNG file. This will likely only support up to 64x64 or so
    /// within the 5KB limit.
    Png(Binary),
}

/// This is used to display logo info, provide a link or inform there is one
/// that can be downloaded from the blockchain itself
#[cw_serde]
pub enum LogoInfo {
    /// A reference to an externally hosted logo. Must be a valid HTTP or HTTPS URL.
    Url(String),
    /// There is an embedded logo on the chain, make another call to download it.
    Embedded,
}

impl From<&Logo> for LogoInfo {
    fn from(logo: &Logo) -> Self {
        match logo {
            Logo::Url(url) => LogoInfo::Url(url.clone()),
            Logo::Embedded(_) => LogoInfo::Embedded,
        }
    }
}