Skip to content

Commit c1ece55

Browse files
authored
Merge pull request #111 from peara/cw2981-validate
Validate royalty percentage when minting cw2981
2 parents a662fac + c9deaad commit c1ece55

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

Diff for: Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: contracts/cw2981-royalties/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ cw721 = { workspace = true }
2626
cw721-base = { workspace = true, features = ["library"] }
2727
schemars = { workspace = true }
2828
serde = { workspace = true }
29+
thiserror = { workspace = true }

Diff for: contracts/cw2981-royalties/src/error.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use cosmwasm_std::StdError;
2+
use thiserror::Error;
3+
4+
#[derive(Debug, Error, PartialEq)]
5+
pub enum ContractError {
6+
#[error(transparent)]
7+
Std(#[from] StdError),
8+
9+
#[error(transparent)]
10+
Base(#[from] cw721_base::ContractError),
11+
12+
#[error("Royalty percentage must be between 0 and 100")]
13+
InvalidRoyaltyPercentage,
14+
}

Diff for: contracts/cw2981-royalties/src/lib.rs

+52-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod error;
12
pub mod msg;
23
pub mod query;
34

@@ -6,8 +7,9 @@ pub use query::{check_royalties, query_royalties_info};
67
use cosmwasm_schema::cw_serde;
78
use cosmwasm_std::{to_binary, Empty};
89
use cw721_base::Cw721Contract;
9-
pub use cw721_base::{ContractError, InstantiateMsg, MinterResponse};
10+
pub use cw721_base::{InstantiateMsg, MinterResponse};
1011

12+
use crate::error::ContractError;
1113
use crate::msg::Cw2981QueryMsg;
1214

1315
// Version info for migration
@@ -77,7 +79,25 @@ pub mod entry {
7779
info: MessageInfo,
7880
msg: ExecuteMsg,
7981
) -> Result<Response, ContractError> {
80-
Cw2981Contract::default().execute(deps, env, info, msg)
82+
if let ExecuteMsg::Mint {
83+
extension:
84+
Some(Metadata {
85+
royalty_percentage: Some(royalty_percentage),
86+
..
87+
}),
88+
..
89+
} = &msg
90+
{
91+
// validate royalty_percentage to be between 0 and 100
92+
// no need to check < 0 because royalty_percentage is u64
93+
if *royalty_percentage > 100 {
94+
return Err(ContractError::InvalidRoyaltyPercentage);
95+
}
96+
}
97+
98+
Cw2981Contract::default()
99+
.execute(deps, env, info, msg)
100+
.map_err(Into::into)
81101
}
82102

83103
#[entry_point]
@@ -140,6 +160,36 @@ mod tests {
140160
assert_eq!(res.extension, extension);
141161
}
142162

163+
#[test]
164+
fn validate_royalty_information() {
165+
let mut deps = mock_dependencies();
166+
let _contract = Cw2981Contract::default();
167+
168+
let info = mock_info(CREATOR, &[]);
169+
let init_msg = InstantiateMsg {
170+
name: "SpaceShips".to_string(),
171+
symbol: "SPACE".to_string(),
172+
minter: CREATOR.to_string(),
173+
};
174+
entry::instantiate(deps.as_mut(), mock_env(), info.clone(), init_msg).unwrap();
175+
176+
let token_id = "Enterprise";
177+
let exec_msg = ExecuteMsg::Mint {
178+
token_id: token_id.to_string(),
179+
owner: "john".to_string(),
180+
token_uri: Some("https://starships.example.com/Starship/Enterprise.json".into()),
181+
extension: Some(Metadata {
182+
description: Some("Spaceship with Warp Drive".into()),
183+
name: Some("Starship USS Enterprise".to_string()),
184+
royalty_percentage: Some(101),
185+
..Metadata::default()
186+
}),
187+
};
188+
// mint will return StdError
189+
let err = entry::execute(deps.as_mut(), mock_env(), info, exec_msg).unwrap_err();
190+
assert_eq!(err, ContractError::InvalidRoyaltyPercentage);
191+
}
192+
143193
#[test]
144194
fn check_royalties_response() {
145195
let mut deps = mock_dependencies();

0 commit comments

Comments
 (0)