-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #144 from chipshort/cw721-receiver-contract
Add cw721-receiver contract
- Loading branch information
Showing
13 changed files
with
429 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[alias] | ||
wasm = "build --release --lib --target wasm32-unknown-unknown" | ||
unit-test = "test --lib" | ||
schema = "run --bin schema" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
[package] | ||
name = "cw721-receiver-tester" | ||
description = "A basic contract that can receive a cw721 token for testing purposes" | ||
authors = ["Christoph Otter <[email protected]>"] | ||
version = { workspace = true } | ||
edition = { workspace = true } | ||
license = { workspace = true } | ||
repository = { workspace = true } | ||
homepage = { workspace = true } | ||
documentation = { workspace = true } | ||
rust-version = { workspace = true } | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
[features] | ||
# for more explicit tests, cargo test --features=backtraces | ||
backtraces = ["cosmwasm-std/backtraces"] | ||
# use library feature to disable all instantiate/execute/query exports | ||
library = [] | ||
|
||
[dependencies] | ||
cosmwasm-schema = { workspace = true } | ||
cosmwasm-std = { workspace = true } | ||
cw721 = { workspace = true } | ||
schemars = { workspace = true } | ||
serde = { workspace = true } | ||
thiserror = { workspace = true } | ||
|
||
[dev-dependencies] | ||
cw-multi-test = { workspace = true } | ||
cw721-base = { workspace = true, features = ["library"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Cw721 Receiver | ||
|
||
This contract can receive a cw721 token sent via the `SendNft` message. | ||
It expects a json message of either `"succeed"` or `"fail"` (mind the quotes). | ||
So an example message would look like this: | ||
|
||
```json | ||
{ | ||
"send_nft": { | ||
"contract": "CW721_CONTRACT_ADDR", | ||
"token_id": "test", | ||
"msg": "InN1Y2NlZWQi" // <- base64 encoded "succeed" | ||
} | ||
} | ||
``` | ||
|
||
In case of `"succeed"` the contract returns a response with its input data as | ||
attributes and data. In case of `"fail"` the contract returns an error. |
65 changes: 65 additions & 0 deletions
65
contracts/cw721-receiver-tester/schema/cw721-receiver-tester.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
{ | ||
"contract_name": "cw721-receiver-tester", | ||
"contract_version": "0.18.0", | ||
"idl_version": "1.0.0", | ||
"instantiate": { | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"title": "InstantiateMsg", | ||
"type": "object", | ||
"additionalProperties": false | ||
}, | ||
"execute": { | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"title": "ExecuteMsg", | ||
"oneOf": [ | ||
{ | ||
"type": "object", | ||
"required": [ | ||
"receive_nft" | ||
], | ||
"properties": { | ||
"receive_nft": { | ||
"$ref": "#/definitions/Cw721ReceiveMsg" | ||
} | ||
}, | ||
"additionalProperties": false | ||
} | ||
], | ||
"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>. See also <https://github.com/CosmWasm/cosmwasm/blob/main/docs/MESSAGE_TYPES.md>.", | ||
"type": "string" | ||
}, | ||
"Cw721ReceiveMsg": { | ||
"description": "Cw721ReceiveMsg should be de/serialized under `Receive()` variant in a ExecuteMsg", | ||
"type": "object", | ||
"required": [ | ||
"msg", | ||
"sender", | ||
"token_id" | ||
], | ||
"properties": { | ||
"msg": { | ||
"$ref": "#/definitions/Binary" | ||
}, | ||
"sender": { | ||
"type": "string" | ||
}, | ||
"token_id": { | ||
"type": "string" | ||
} | ||
}, | ||
"additionalProperties": false | ||
} | ||
} | ||
}, | ||
"query": { | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"title": "QueryMsg", | ||
"type": "string", | ||
"enum": [] | ||
}, | ||
"migrate": null, | ||
"sudo": null, | ||
"responses": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use cosmwasm_schema::write_api; | ||
|
||
use cw721_receiver_tester::msg::{ExecuteMsg, InstantiateMsg, QueryMsg}; | ||
|
||
fn main() { | ||
write_api! { | ||
instantiate: InstantiateMsg, | ||
execute: ExecuteMsg, | ||
query: QueryMsg, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#[cfg(not(feature = "library"))] | ||
use cosmwasm_std::entry_point; | ||
use cosmwasm_std::{from_json, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult}; | ||
|
||
use crate::error::ContractError; | ||
use crate::msg::{ExecuteMsg, InnerMsg, InstantiateMsg, QueryMsg}; | ||
|
||
#[cfg_attr(not(feature = "library"), entry_point)] | ||
pub fn instantiate( | ||
_deps: DepsMut, | ||
_env: Env, | ||
_info: MessageInfo, | ||
_msg: InstantiateMsg, | ||
) -> Result<Response, ContractError> { | ||
Ok(Response::default()) | ||
} | ||
|
||
#[cfg_attr(not(feature = "library"), entry_point)] | ||
pub fn execute( | ||
_deps: DepsMut, | ||
_env: Env, | ||
_info: MessageInfo, | ||
msg: ExecuteMsg, | ||
) -> Result<Response, ContractError> { | ||
match msg { | ||
ExecuteMsg::ReceiveNft(receive_msg) => { | ||
let inner: InnerMsg = from_json(&receive_msg.msg)?; | ||
match inner { | ||
InnerMsg::Succeed => Ok(Response::new() | ||
.add_attributes([ | ||
("action", "receive_nft"), | ||
("token_id", receive_msg.token_id.as_str()), | ||
("sender", receive_msg.sender.as_str()), | ||
("msg", receive_msg.msg.to_base64().as_str()), | ||
]) | ||
.set_data( | ||
[ | ||
receive_msg.token_id, | ||
receive_msg.sender, | ||
receive_msg.msg.to_base64(), | ||
] | ||
.concat() | ||
.as_bytes(), | ||
)), | ||
InnerMsg::Fail => Err(ContractError::Failed {}), | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg_attr(not(feature = "library"), entry_point)] | ||
pub fn query(_deps: Deps, _env: Env, _msg: QueryMsg) -> StdResult<Binary> { | ||
unimplemented!() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use cosmwasm_std::StdError; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum ContractError { | ||
#[error("{0}")] | ||
Std(#[from] StdError), | ||
|
||
#[error("I failed because you asked me to do so")] | ||
Failed {}, | ||
// Add any other custom errors you like here. | ||
// Look at https://docs.rs/thiserror/1.0.21/thiserror/ for details. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub mod contract; | ||
mod error; | ||
pub mod msg; | ||
|
||
pub use crate::error::ContractError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use cosmwasm_schema::{cw_serde, QueryResponses}; | ||
use cw721::Cw721ReceiveMsg; | ||
|
||
#[cw_serde] | ||
pub struct InstantiateMsg {} | ||
|
||
#[cw_serde] | ||
pub enum ExecuteMsg { | ||
ReceiveNft(Cw721ReceiveMsg), | ||
} | ||
|
||
#[cw_serde] | ||
pub enum InnerMsg { | ||
Succeed, | ||
Fail, | ||
} | ||
|
||
#[cw_serde] | ||
#[derive(QueryResponses)] | ||
pub enum QueryMsg {} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use cosmwasm_std::{from_json, to_json_binary}; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn inner_msg_json() { | ||
let json = to_json_binary(&InnerMsg::Succeed).unwrap(); | ||
assert_eq!(json, br#""succeed""#); | ||
let msg: InnerMsg = from_json(&json).unwrap(); | ||
assert_eq!(msg, InnerMsg::Succeed); | ||
} | ||
} |
Oops, something went wrong.