Skip to content

Commit

Permalink
Fix cw2981 to use customMsg
Browse files Browse the repository at this point in the history
  • Loading branch information
SylvestreG committed Jan 13, 2022
1 parent 8d11daf commit 18915ed
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 113 deletions.
5 changes: 3 additions & 2 deletions contracts/cw2981-royalties/examples/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::env::current_dir;
use std::fs::create_dir_all;

use cosmwasm_schema::{export_schema, export_schema_with_title, remove_schemas, schema_for};
use cosmwasm_std::Empty;

use cw721::{
AllNftInfoResponse, ContractInfoResponse, NftInfoResponse, NumTokensResponse,
Expand All @@ -18,8 +19,8 @@ fn main() {
remove_schemas(&out_dir).unwrap();

export_schema(&schema_for!(InstantiateMsg), &out_dir);
export_schema_with_title(&schema_for!(ExecuteMsg<Extension>), &out_dir, "ExecuteMsg");
export_schema(&schema_for!(QueryMsg), &out_dir);
export_schema_with_title(&schema_for!(ExecuteMsg<Extension, Empty>), &out_dir, "ExecuteMsg");
export_schema(&schema_for!(QueryMsg<Cw2981QueryMsg>), &out_dir);
export_schema_with_title(
&schema_for!(AllNftInfoResponse<Extension>),
&out_dir,
Expand Down
29 changes: 16 additions & 13 deletions contracts/cw2981-royalties/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ pub type Extension = Option<Metadata>;

pub type MintExtension = Option<Extension>;

pub type Cw2981Contract<'a> = Cw721Contract<'a, Extension, Empty>;
pub type ExecuteMsg = cw721_base::ExecuteMsg<Extension>;
pub type Cw2981Contract<'a> = Cw721Contract<'a, Extension, Empty, Empty, Cw2981QueryMsg>;
pub type ExecuteMsg = cw721_base::ExecuteMsg<Extension, Empty>;
pub type QueryMsg = cw721_base::QueryMsg<Cw2981QueryMsg>;

#[cfg(not(feature = "library"))]
//#[cfg(not(feature = "library"))]
pub mod entry {
use super::*;

Expand Down Expand Up @@ -75,14 +76,16 @@ pub mod entry {
}

#[entry_point]
pub fn query(deps: Deps, env: Env, msg: Cw2981QueryMsg) -> StdResult<Binary> {
pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
Cw2981QueryMsg::RoyaltyInfo {
token_id,
sale_price,
} => to_binary(&query_royalties_info(deps, token_id, sale_price)?),
Cw2981QueryMsg::CheckRoyalties {} => to_binary(&check_royalties(deps)?),
_ => Cw2981Contract::default().query(deps, env, msg.into()),
QueryMsg::Extension { msg } => match msg {
Cw2981QueryMsg::RoyaltyInfo {
token_id,
sale_price,
} => to_binary(&query_royalties_info(deps, token_id, sale_price)?),
Cw2981QueryMsg::CheckRoyalties {} => to_binary(&check_royalties(deps)?),
}
_ => Cw2981Contract::default().query(deps, env, msg),
}
}
}
Expand Down Expand Up @@ -165,7 +168,7 @@ mod tests {
assert_eq!(res, expected);

// also check the longhand way
let query_msg = Cw2981QueryMsg::CheckRoyalties {};
let query_msg = QueryMsg::Extension { msg: Cw2981QueryMsg::CheckRoyalties {}};
let query_res: CheckRoyaltiesResponse =
from_binary(&entry::query(deps.as_ref(), mock_env(), query_msg).unwrap()).unwrap();
assert_eq!(query_res, expected);
Expand Down Expand Up @@ -208,10 +211,10 @@ mod tests {
assert_eq!(res, expected);

// also check the longhand way
let query_msg = Cw2981QueryMsg::RoyaltyInfo {
let query_msg = QueryMsg::Extension { msg: Cw2981QueryMsg::RoyaltyInfo {
token_id: token_id.to_string(),
sale_price: Uint128::new(100),
};
}};
let query_res: RoyaltiesInfoResponse =
from_binary(&entry::query(deps.as_ref(), mock_env(), query_msg).unwrap()).unwrap();
assert_eq!(query_res, expected);
Expand Down
103 changes: 5 additions & 98 deletions contracts/cw2981-royalties/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use cosmwasm_std::Uint128;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cw721_base::msg::QueryMsg as CW721QueryMsg;
use cw721::CustomMsg;

#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
#[serde(rename_all = "snake_case")]
Expand All @@ -25,106 +24,14 @@ pub enum Cw2981QueryMsg {
/// if royalties are implemented at token level
/// (i.e. always check on sale)
CheckRoyalties {},
/// Return the owner of the given token, error if token does not exist
/// Return type: OwnerOfResponse
OwnerOf {
token_id: String,
/// unset or false will filter out expired approvals, you must set to true to see them
include_expired: Option<bool>,
},
/// List all operators that can access all of the owner's tokens.
/// Return type: `OperatorsResponse`
AllOperators {
owner: String,
/// unset or false will filter out expired approvals, you must set to true to see them
include_expired: Option<bool>,
start_after: Option<String>,
limit: Option<u32>,
},
/// Total number of tokens issued
NumTokens {},

/// With MetaData Extension.
/// Returns top-level metadata about the contract: `ContractInfoResponse`
ContractInfo {},
/// With MetaData Extension.
/// Returns metadata about one particular token, based on *ERC721 Metadata JSON Schema*
/// but directly from the contract: `NftInfoResponse`
NftInfo { token_id: String },
/// With MetaData Extension.
/// Returns the result of both `NftInfo` and `OwnerOf` as one query as an optimization
/// for clients: `AllNftInfo`
AllNftInfo {
token_id: String,
/// unset or false will filter out expired approvals, you must set to true to see them
include_expired: Option<bool>,
},

/// With Enumerable extension.
/// Returns all tokens owned by the given address, [] if unset.
/// Return type: TokensResponse.
Tokens {
owner: String,
start_after: Option<String>,
limit: Option<u32>,
},
/// With Enumerable extension.
/// Requires pagination. Lists all token_ids controlled by the contract.
/// Return type: TokensResponse.
AllTokens {
start_after: Option<String>,
limit: Option<u32>,
},
}

impl From<Cw2981QueryMsg> for CW721QueryMsg {
fn from(msg: Cw2981QueryMsg) -> CW721QueryMsg {
match msg {
Cw2981QueryMsg::OwnerOf {
token_id,
include_expired,
} => CW721QueryMsg::OwnerOf {
token_id,
include_expired,
},
Cw2981QueryMsg::AllOperators {
owner,
include_expired,
start_after,
limit,
} => CW721QueryMsg::AllOperators {
owner,
include_expired,
start_after,
limit,
},
Cw2981QueryMsg::NumTokens {} => CW721QueryMsg::NumTokens {},
Cw2981QueryMsg::ContractInfo {} => CW721QueryMsg::ContractInfo {},
Cw2981QueryMsg::NftInfo { token_id } => CW721QueryMsg::NftInfo { token_id },
Cw2981QueryMsg::AllNftInfo {
token_id,
include_expired,
} => CW721QueryMsg::AllNftInfo {
token_id,
include_expired,
},
Cw2981QueryMsg::Tokens {
owner,
start_after,
limit,
} => CW721QueryMsg::Tokens {
owner,
start_after,
limit,
},
Cw2981QueryMsg::AllTokens { start_after, limit } => {
CW721QueryMsg::AllTokens { start_after, limit }
}
_ => panic!("cannot covert {:?} to CW721QueryMsg", msg),
}
}
impl Default for Cw2981QueryMsg {
fn default() -> Self { Cw2981QueryMsg::CheckRoyalties{} }
}

impl CustomMsg for Cw2981QueryMsg {}

#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
pub struct RoyaltiesInfoResponse {
pub address: String,
Expand Down

0 comments on commit 18915ed

Please sign in to comment.