Skip to content

Commit

Permalink
Start on the Cw721 trait, with one method, to demo
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Sep 21, 2021
1 parent b32b21e commit 19803f2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 36 deletions.
59 changes: 31 additions & 28 deletions contracts/cw721-base/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
use schemars::JsonSchema;
use serde::de::DeserializeOwned;
use serde::Serialize;

use cosmwasm_std::{
to_binary, Addr, Binary, BlockInfo, Deps, DepsMut, Empty, Env, MessageInfo, Order, Pair,
Response, StdError, StdResult, Storage,
to_binary, Addr, Binary, BlockInfo, Deps, DepsMut, Env, MessageInfo, Order, Pair, Response,
StdError, StdResult, Storage,
};

use cw0::maybe_addr;
use cw2::set_contract_version;
use cw721::{
AllNftInfoResponse, ApprovedForAllResponse, ContractInfoResponse, Cw721ReceiveMsg, Expiration,
NftInfoResponse, NumTokensResponse, OwnerOfResponse, TokensResponse,
AllNftInfoResponse, ApprovedForAllResponse, ContractInfoResponse, CustomMsg, Cw721,
Cw721ReceiveMsg, Expiration, NftInfoResponse, NumTokensResponse, OwnerOfResponse,
TokensResponse,
};
use cw_storage_plus::{Bound, IndexedMap, Item, Map, MultiIndex};

use crate::error::ContractError;
use crate::msg::{ExecuteMsg, InstantiateMsg, MintMsg, MinterResponse, QueryMsg};
use crate::state::{token_owner_idx, Approval, TokenIndexes, TokenInfo};

// TODO: move this somewhere else... ideally cosmwasm-std
pub trait CustomMsg: Clone + std::fmt::Debug + PartialEq + JsonSchema {}

impl CustomMsg for Empty {}

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:cw721-base";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down Expand Up @@ -94,6 +89,30 @@ where
}
}

impl<'a, T> Cw721<T> for Cw721Contract<'a, T>
where
T: Serialize + DeserializeOwned + Clone,
{
type Err = ContractError;

fn transfer_nft<C: CustomMsg>(
&self,
deps: DepsMut,
env: Env,
info: MessageInfo,
recipient: String,
token_id: String,
) -> Result<Response<C>, ContractError> {
self._transfer_nft(deps, &env, &info, &recipient, &token_id)?;

Ok(Response::new()
.add_attribute("action", "transfer_nft")
.add_attribute("sender", info.sender)
.add_attribute("recipient", recipient)
.add_attribute("token_id", token_id))
}
}

impl<'a, T> Cw721Contract<'a, T>
where
T: Serialize + DeserializeOwned + Clone,
Expand Down Expand Up @@ -143,7 +162,7 @@ where
ExecuteMsg::TransferNft {
recipient,
token_id,
} => self.execute_transfer_nft(deps, env, info, recipient, token_id),
} => self.transfer_nft(deps, env, info, recipient, token_id),
ExecuteMsg::SendNft {
contract,
token_id,
Expand All @@ -152,6 +171,7 @@ where
}
}

// Note: this is not in cw721
pub fn execute_mint<C: CustomMsg>(
&self,
deps: DepsMut,
Expand Down Expand Up @@ -188,23 +208,6 @@ where
.add_attribute("token_id", msg.token_id))
}

pub fn execute_transfer_nft<C: CustomMsg>(
&self,
deps: DepsMut,
env: Env,
info: MessageInfo,
recipient: String,
token_id: String,
) -> Result<Response<C>, ContractError> {
self._transfer_nft(deps, &env, &info, &recipient, &token_id)?;

Ok(Response::new()
.add_attribute("action", "transfer_nft")
.add_attribute("sender", info.sender)
.add_attribute("recipient", recipient)
.add_attribute("token_id", token_id))
}

pub fn execute_send_nft<C: CustomMsg>(
&self,
deps: DepsMut,
Expand Down
10 changes: 2 additions & 8 deletions packages/cw721/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod helpers;
mod msg;
mod query;
mod receiver;
mod traits;

pub use cw0::Expiration;

Expand All @@ -12,11 +13,4 @@ pub use crate::query::{
NftInfoResponse, NumTokensResponse, OwnerOfResponse, TokensResponse,
};
pub use crate::receiver::Cw721ReceiveMsg;

#[cfg(test)]
mod tests {
#[test]
fn it_works() {
// test me
}
}
pub use crate::traits::{CustomMsg, Cw721};
26 changes: 26 additions & 0 deletions packages/cw721/src/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use schemars::JsonSchema;
use serde::de::DeserializeOwned;
use serde::Serialize;

use cosmwasm_std::{DepsMut, Empty, Env, MessageInfo, Response};

// TODO: move this somewhere else... ideally cosmwasm-std
pub trait CustomMsg: Clone + std::fmt::Debug + PartialEq + JsonSchema {}

impl CustomMsg for Empty {}

pub trait Cw721<T>
where
T: Serialize + DeserializeOwned + Clone,
{
type Err: ToString;

fn transfer_nft<C: CustomMsg>(
&self,
deps: DepsMut,
env: Env,
info: MessageInfo,
recipient: String,
token_id: String,
) -> Result<Response<C>, Self::Err>;
}

0 comments on commit 19803f2

Please sign in to comment.