Skip to content

Commit

Permalink
Merge pull request #144 from chipshort/cw721-receiver-contract
Browse files Browse the repository at this point in the history
Add cw721-receiver contract
  • Loading branch information
JakeHartnell authored Feb 7, 2024
2 parents 378ae39 + 258790c commit e63a7bb
Show file tree
Hide file tree
Showing 13 changed files with 429 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ jobs:
for C in ./contracts/*/
do
echo "Compiling `basename $C`..."
(cd $C && cargo build --release --target wasm32-unknown-unknown --locked)
(cd $C && cargo build --release --target wasm32-unknown-unknown --lib --locked)
done
- run:
name: Install check_contract
Expand Down
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ incremental = false
codegen-units = 1
incremental = false

[profile.release.package.cw721-receiver]
codegen-units = 1
incremental = false

[profile.release]
rpath = false
lto = true
Expand Down
4 changes: 4 additions & 0 deletions contracts/cw721-receiver-tester/.cargo/config
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"
32 changes: 32 additions & 0 deletions contracts/cw721-receiver-tester/Cargo.toml
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"] }
18 changes: 18 additions & 0 deletions contracts/cw721-receiver-tester/README.md
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 contracts/cw721-receiver-tester/schema/cw721-receiver-tester.json
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": {}
}
11 changes: 11 additions & 0 deletions contracts/cw721-receiver-tester/src/bin/schema.rs
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,
}
}
54 changes: 54 additions & 0 deletions contracts/cw721-receiver-tester/src/contract.rs
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!()
}
13 changes: 13 additions & 0 deletions contracts/cw721-receiver-tester/src/error.rs
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.
}
5 changes: 5 additions & 0 deletions contracts/cw721-receiver-tester/src/lib.rs
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;
35 changes: 35 additions & 0 deletions contracts/cw721-receiver-tester/src/msg.rs
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);
}
}
Loading

0 comments on commit e63a7bb

Please sign in to comment.