abstract_cw20/
receiver.rs

1use cosmwasm_schema::cw_serde;
2use cosmwasm_std::{to_json_binary, Binary, CosmosMsg, StdResult, Uint128, WasmMsg};
3
4/// Cw20ReceiveMsg should be de/serialized under `Receive()` variant in a ExecuteMsg
5#[cw_serde]
6
7pub struct Cw20ReceiveMsg {
8    pub sender: String,
9    pub amount: Uint128,
10    pub msg: Binary,
11}
12
13impl Cw20ReceiveMsg {
14    /// serializes the message
15    pub fn into_json_binary(self) -> StdResult<Binary> {
16        let msg = ReceiverExecuteMsg::Receive(self);
17        to_json_binary(&msg)
18    }
19
20    /// creates a cosmos_msg sending this struct to the named contract
21    pub fn into_cosmos_msg<T: Into<String>>(self, contract_addr: T) -> StdResult<CosmosMsg> {
22        let msg = self.into_json_binary()?;
23        let execute = WasmMsg::Execute {
24            contract_addr: contract_addr.into(),
25            msg,
26            funds: vec![],
27        };
28        Ok(execute.into())
29    }
30}
31
32// This is just a helper to properly serialize the above message
33#[cw_serde]
34
35enum ReceiverExecuteMsg {
36    Receive(Cw20ReceiveMsg),
37}