Skip to content

Commit

Permalink
Add custom errors, remove minmal template
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeHartnell committed Apr 27, 2023
1 parent 66bcb57 commit e8f1995
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 27 deletions.
1 change: 1 addition & 0 deletions contracts/cw721-template/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ cw721 = "0.17.0"
cw721-base = { version = "0.17.0", features = ["library"] }
schemars = "0.8.11"
serde = { version = "1.0.152", default-features = false, features = ["derive"] }
thiserror = "1.0.30"
6 changes: 0 additions & 6 deletions contracts/cw721-template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ Go to the folder in which you want to place it and run:
cargo generate --git https://github.com/CosmWasm/cw-nfts.git --name PROJECT_NAME
```

For cloning a minimal NFT contract without example code for extensions:

```sh
cargo generate --git https://github.com/CosmWasm/cw-nfts.git --name PROJECT_NAME -d minimal=true
```

**Older Versions**

Pass version as branch flag:
Expand Down
6 changes: 0 additions & 6 deletions contracts/cw721-template/cargo-generate.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,3 @@
# This is needed when files contains curly brackets as in `v4-cargo-cache-{{ arch }}-{{ checksum "Cargo.lock" }}`.
# The files will be copied 1:1 to the target project. To avoid shipping them completely add them to `.genignore`.
exclude = ["test_generate.sh", "cargo-generate.toml"]

[placeholders.minimal]
type = "bool"
prompt = """The full template includes more example code on how to extend a basic cw721 contract. This is useful
if you are new to writing CosmWasm NFT contracts. Would you like the minimal template instead?"""
default = false
39 changes: 24 additions & 15 deletions contracts/cw721-template/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
{% unless minimal %}use cosmwasm_schema::cw_serde;
{% endunless %}use cosmwasm_std::{% unless minimal %}{CustomMsg, {% endunless %}Empty{% unless minimal %}}{% endunless %};
pub use cw721_base::{ContractError, InstantiateMsg, MinterResponse};
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{CustomMsg, Empty, StdError};
pub use cw721_base::{InstantiateMsg, MinterResponse};
use thiserror::Error;

// Version info for migration
const CONTRACT_NAME: &str = "crates.io:{{project-name}}";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

{% if minimal %}pub type Cw721Contract<'a> = cw721_base::Cw721Contract<'a, Empty, Empty, Empty, Empty>;

pub type ExecuteMsg = cw721_base::ExecuteMsg<Empty, Empty>;
pub type QueryMsg = cw721_base::QueryMsg<Empty>;{% else %}// Implements extended on-chain metadata, by default cw721 NFTs only store a
// Implements extended on-chain metadata, by default cw721 NFTs only store a
// token_uri, which is a URL to off-chain metadata (same as ERC721).
#[cw_serde]
#[derive(Default)]
Expand Down Expand Up @@ -41,7 +39,18 @@ pub type ExecuteMsg = cw721_base::ExecuteMsg<MetadataExt, ExecuteExt>;
// The query message type for this contract.
// If you don't need the QueryExt extension, you can use the
// `Empty` type.
pub type QueryMsg = cw721_base::QueryMsg<QueryExt>;{% endif %}
pub type QueryMsg = cw721_base::QueryMsg<QueryExt>;

/// Custom errors for this contract, add additional errors here.
#[derive(Error, Debug, PartialEq)]
pub enum ContractError {
#[error("{0}")]
Std(#[from] StdError),

/// This inherits from cw721-base::ContractError to handle the base contract errors
#[error("{0}")]
Cw721Error(#[from] cw721_base::ContractError),
}

#[cfg(not(feature = "library"))]
pub mod entry {
Expand Down Expand Up @@ -71,8 +80,7 @@ pub mod entry {
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
{% if minimal %}// Use the default cw721-base implementation
Cw721Contract::default().execute(deps, env, info, msg){% else %}match msg {
match msg {
// Optionally override the default cw721-base behavior
// ExecuteMsg::Burn { token_id } => unimplemented!(),

Expand All @@ -83,14 +91,15 @@ pub mod entry {
},

// Use the default cw721-base implementation
_ => Cw721Contract::default().execute(deps, env, info, msg),
}{% endif %}
_ => Cw721Contract::default()
.execute(deps, env, info, msg)
.map_err(Into::into),
}
}

#[entry_point]
pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
{% if minimal %}// Use default cw721-base query implementation
Cw721Contract::default().query(deps, env, msg){% else %}match msg {
match msg {
// Optionally override a default cw721-base query
// QueryMsg::Minter {} => unimplemented!(),
QueryMsg::Extension { msg } => match msg {
Expand All @@ -99,7 +108,7 @@ pub mod entry {

// Use default cw721-base query implementation
_ => Cw721Contract::default().query(deps, env, msg),
}{% endif %}
}
}
}

Expand Down

0 comments on commit e8f1995

Please sign in to comment.