Skip to content

Commit

Permalink
Add Cw721RecieveMsg
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Aug 21, 2020
1 parent 2d07697 commit 7864b38
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
5 changes: 3 additions & 2 deletions packages/cw721/examples/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use cosmwasm_schema::{export_schema, remove_schemas, schema_for};

use cw721::{
AllNftInfoResponse, ApprovedForAllResponse, ContractInfoResponse, Cw721HandleMsg,
Cw721QueryMsg, NftInfoResponse, NumTokensResponse, OwnerOfResponse, TokensResponse,
Cw721QueryMsg, Cw721ReceiveMsg, NftInfoResponse, NumTokensResponse, OwnerOfResponse,
TokensResponse,
};

fn main() {
Expand All @@ -16,7 +17,7 @@ fn main() {

export_schema(&schema_for!(Cw721HandleMsg), &out_dir);
export_schema(&schema_for!(Cw721QueryMsg), &out_dir);
// export_schema(&schema_for!(Cw20ReceiveMsg), &out_dir);
export_schema(&schema_for!(Cw721ReceiveMsg), &out_dir);
export_schema(&schema_for!(AllNftInfoResponse), &out_dir);
export_schema(&schema_for!(ApprovedForAllResponse), &out_dir);
export_schema(&schema_for!(ContractInfoResponse), &out_dir);
Expand Down
40 changes: 40 additions & 0 deletions packages/cw721/schema/cw721_receive_msg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Cw721ReceiveMsg",
"description": "Cw721ReceiveMsg should be de/serialized under `Receive()` variant in a HandleMsg",
"type": "object",
"required": [
"amount",
"sender"
],
"properties": {
"amount": {
"$ref": "#/definitions/Uint128"
},
"msg": {
"anyOf": [
{
"$ref": "#/definitions/Binary"
},
{
"type": "null"
}
]
},
"sender": {
"$ref": "#/definitions/HumanAddr"
}
},
"definitions": {
"Binary": {
"description": "Binary is a wrapper around Vec<u8> to add base64 de/serialization with serde. It also adds some helper methods to help encode inline.\n\nThis is only needed as serde-json-{core,wasm} has a horrible encoding for Vec<u8>",
"type": "string"
},
"HumanAddr": {
"type": "string"
},
"Uint128": {
"type": "string"
}
}
}
3 changes: 2 additions & 1 deletion packages/cw721/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
mod msg;
mod query;
mod receiver;

// pub use crate::helpers::{Cw20CanonicalContract, Cw20Contract};
pub use crate::msg::{Cw721HandleMsg, Expiration};
pub use crate::query::{
AllNftInfoResponse, ApprovedForAllResponse, ContractInfoResponse, Cw721QueryMsg,
NftInfoResponse, NumTokensResponse, OwnerOfResponse, TokensResponse,
};
// pub use crate::receiver::Cw20ReceiveMsg;
pub use crate::receiver::Cw721ReceiveMsg;

#[cfg(test)]
mod tests {
Expand Down
40 changes: 40 additions & 0 deletions packages/cw721/src/receiver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::{to_binary, Binary, CosmosMsg, HumanAddr, StdResult, Uint128, WasmMsg};

/// Cw721ReceiveMsg should be de/serialized under `Receive()` variant in a HandleMsg
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
#[serde(rename_all = "snake_case")]
pub struct Cw721ReceiveMsg {
pub sender: HumanAddr,
pub amount: Uint128,
pub msg: Option<Binary>,
}

impl Cw721ReceiveMsg {
/// serializes the message
pub fn into_binary(self) -> StdResult<Binary> {
let msg = ReceiverHandleMsg::ReceiveNft(self);
to_binary(&msg)
}

/// creates a cosmos_msg sending this struct to the named contract
pub fn into_cosmos_msg(self, contract_addr: HumanAddr) -> StdResult<CosmosMsg> {
let msg = self.into_binary()?;
let execute = WasmMsg::Execute {
contract_addr,
msg,
send: vec![],
};
Ok(execute.into())
}
}

/// This is just a helper to properly serialize the above message.
/// The actual receiver should include this variant in the larger HandleMsg enum
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
#[serde(rename_all = "snake_case")]
enum ReceiverHandleMsg {
ReceiveNft(Cw721ReceiveMsg),
}

0 comments on commit 7864b38

Please sign in to comment.