hmip20/
receiver.rs

1#![allow(clippy::field_reassign_with_default)] // This is triggered in `#[derive(JsonSchema)]`
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6use cosmwasm_std::{to_binary, Binary, CosmosMsg, HumanAddr, StdResult, Uint128, WasmMsg};
7
8use crate::{contract::RESPONSE_BLOCK_SIZE, msg::space_pad};
9
10/// hmip20ReceiveMsg should be de/serialized under `Receive()` variant in a HandleMsg
11#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
12#[serde(rename_all = "snake_case")]
13pub struct Hmip20ReceiveMsg {
14    pub sender: HumanAddr,
15    pub from: HumanAddr,
16    pub amount: Uint128,
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub memo: Option<String>,
19    pub msg: Option<Binary>,
20}
21
22impl Hmip20ReceiveMsg {
23    pub fn new(
24        sender: HumanAddr,
25        from: HumanAddr,
26        amount: Uint128,
27        memo: Option<String>,
28        msg: Option<Binary>,
29    ) -> Self {
30        Self {
31            sender,
32            from,
33            amount,
34            memo,
35            msg,
36        }
37    }
38
39    /// serializes the message, and pads it to 256 bytes
40    pub fn into_binary(self) -> StdResult<Binary> {
41        let msg = ReceiverHandleMsg::Receive(self);
42        let mut data = to_binary(&msg)?;
43        space_pad(RESPONSE_BLOCK_SIZE, &mut data.0);
44        Ok(data)
45    }
46
47    /// creates a cosmos_msg sending this struct to the named contract
48    pub fn into_cosmos_msg(
49        self,
50        callback_code_hash: String,
51        contract_addr: HumanAddr,
52    ) -> StdResult<CosmosMsg> {
53        let msg = self.into_binary()?;
54        let execute = WasmMsg::Execute {
55            msg,
56            callback_code_hash,
57            contract_addr,
58            send: vec![],
59        };
60        Ok(execute.into())
61    }
62}
63
64// This is just a helper to properly serialize the above message
65#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
66#[serde(rename_all = "snake_case")]
67enum ReceiverHandleMsg {
68    Receive(Hmip20ReceiveMsg),
69}