Skip to main content

cw20_atomic_swap/
msg.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4use cosmwasm_std::Coin;
5use cw20::{Cw20Coin, Cw20ReceiveMsg, Expiration};
6
7#[derive(Serialize, Deserialize, JsonSchema)]
8pub struct InstantiateMsg {}
9
10#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
11#[serde(rename_all = "snake_case")]
12pub enum ExecuteMsg {
13    Create(CreateMsg),
14    /// Release sends all tokens to the recipient.
15    Release {
16        id: String,
17        /// This is the preimage, must be exactly 32 bytes in hex (64 chars)
18        /// to release: sha256(from_hex(preimage)) == from_hex(hash)
19        preimage: String,
20    },
21    /// Refund returns all remaining tokens to the original sender,
22    Refund {
23        id: String,
24    },
25    /// This accepts a properly-encoded ReceiveMsg from a cw20 contract
26    Receive(Cw20ReceiveMsg),
27}
28
29#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
30#[serde(rename_all = "snake_case")]
31pub enum ReceiveMsg {
32    Create(CreateMsg),
33}
34
35#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
36pub struct CreateMsg {
37    /// id is a human-readable name for the swap to use later.
38    /// 3-20 bytes of utf-8 text
39    pub id: String,
40    /// This is hex-encoded sha-256 hash of the preimage (must be 32*2 = 64 chars)
41    pub hash: String,
42    /// If approved, funds go to the recipient
43    pub recipient: String,
44    /// You can set expiration at time or at block height the contract is valid at.
45    /// After the contract is expired, it can be returned to the original funder.
46    pub expires: Expiration,
47}
48
49pub fn is_valid_name(name: &str) -> bool {
50    let bytes = name.as_bytes();
51    if bytes.len() < 3 || bytes.len() > 20 {
52        return false;
53    }
54    true
55}
56
57#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
58#[serde(rename_all = "snake_case")]
59pub enum QueryMsg {
60    /// Show all open swaps. Return type is ListResponse.
61    List {
62        start_after: Option<String>,
63        limit: Option<u32>,
64    },
65    /// Returns the details of the named swap, error if not created.
66    /// Return type: DetailsResponse.
67    Details { id: String },
68}
69
70#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
71pub struct ListResponse {
72    /// List all open swap ids
73    pub swaps: Vec<String>,
74}
75
76#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
77pub struct DetailsResponse {
78    /// Id of this swap
79    pub id: String,
80    /// This is hex-encoded sha-256 hash of the preimage (must be 32*2 = 64 chars)
81    pub hash: String,
82    /// If released, funds go to the recipient
83    pub recipient: String,
84    /// If refunded, funds go to the source
85    pub source: String,
86    /// Once a swap is expired, it can be returned to the original source (via "refund").
87    pub expires: Expiration,
88    /// Balance in native tokens or cw20 token, with human-readable address
89    pub balance: BalanceHuman,
90}
91
92#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
93pub enum BalanceHuman {
94    Native(Vec<Coin>),
95    Cw20(Cw20Coin),
96}