Skip to content

Commit

Permalink
Allow updating collection creator (#633)
Browse files Browse the repository at this point in the history
* allow to update collection creator

* cargo clippy --tests

* validate creator address
  • Loading branch information
taitruong authored Jan 18, 2024
1 parent df0ffef commit dc0ed6e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
5 changes: 5 additions & 0 deletions contracts/collections/sg721-base/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ where
return Err(ContractError::Unauthorized {});
}

if let Some(new_creator) = collection_msg.creator {
deps.api.addr_validate(&new_creator)?;
collection.creator = new_creator;
}

collection.description = collection_msg
.description
.unwrap_or_else(|| collection.description.to_string());
Expand Down
13 changes: 5 additions & 8 deletions packages/mint-hooks/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ fn merge_variants(metadata: TokenStream, left: TokenStream, right: TokenStream)

// parse the left enum
let mut left: DeriveInput = parse_macro_input!(left);
let Enum(DataEnum {
variants,
..
}) = &mut left.data else {
let Enum(DataEnum { variants, .. }) = &mut left.data else {
return syn::Error::new(left.ident.span(), "only enums can accept variants")
.to_compile_error()
.into();
Expand All @@ -31,16 +28,16 @@ fn merge_variants(metadata: TokenStream, left: TokenStream, right: TokenStream)
// parse the right enum
let right: DeriveInput = parse_macro_input!(right);
let Enum(DataEnum {
variants: to_add,
..
}) = right.data else {
variants: to_add, ..
}) = right.data
else {
return syn::Error::new(left.ident.span(), "only enums can provide variants")
.to_compile_error()
.into();
};

// insert variants from the right to the left
variants.extend(to_add.into_iter());
variants.extend(to_add);

quote! { #left }.into()
}
Expand Down
1 change: 1 addition & 0 deletions packages/sg721/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pub struct UpdateCollectionInfoMsg<T> {
pub external_link: Option<Option<String>>,
pub explicit_content: Option<bool>,
pub royalty_info: Option<Option<T>>,
pub creator: Option<String>,
}

#[cw_serde]
Expand Down
24 changes: 17 additions & 7 deletions test-suite/src/sg721_base/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ mod tests {
contract,
&Sg721ExecuteMsg::<Empty, Empty>::UpdateCollectionInfo {
collection_info: UpdateCollectionInfoMsg {
creator: None,
description: None,
image: None,
external_link: None,
Expand All @@ -290,6 +291,7 @@ mod tests {
);
assert!(res.is_err());
}

#[test]
fn update_collection_info() {
// customize params so external_link is None
Expand All @@ -313,13 +315,15 @@ mod tests {
let (mut app, contract) = custom_proper_instantiate(custom_create_minter_msg);

let creator = Addr::unchecked("creator".to_string());
let new_creator = Addr::unchecked("new_creator".to_string());

// succeeds
let res = app.execute_contract(
creator.clone(),
contract.clone(),
&Sg721ExecuteMsg::<Empty, Empty>::UpdateCollectionInfo {
collection_info: UpdateCollectionInfoMsg {
creator: None,
description: Some(params.info.description.clone()),
image: Some(params.info.image.clone()),
external_link: Some(params.info.external_link.clone()),
Expand All @@ -341,6 +345,7 @@ mod tests {
contract.clone(),
&Sg721ExecuteMsg::<Empty, Empty>::UpdateCollectionInfo {
collection_info: UpdateCollectionInfoMsg {
creator: None,
description: Some(params.info.description.clone()),
image: Some(params.info.image.clone()),
external_link: Some(params.info.external_link.clone()),
Expand Down Expand Up @@ -374,6 +379,7 @@ mod tests {
contract.clone(),
&Sg721ExecuteMsg::<Empty, Empty>::UpdateCollectionInfo {
collection_info: UpdateCollectionInfoMsg {
creator: None,
description: Some(params.info.description.clone()),
image: Some(params.info.image.clone()),
external_link: Some(params.info.external_link.clone()),
Expand Down Expand Up @@ -401,6 +407,7 @@ mod tests {
contract.clone(),
&Sg721ExecuteMsg::<Empty, Empty>::UpdateCollectionInfo {
collection_info: UpdateCollectionInfoMsg {
creator: None,
description: Some(params.info.description.clone()),
image: Some(params.info.image.clone()),
external_link: Some(params.info.external_link.clone()),
Expand All @@ -419,20 +426,21 @@ mod tests {
);

// raise royalty_info by 2% succeeds
let royalty_info: Option<RoyaltyInfoResponse> = Some(RoyaltyInfoResponse {
let royalty_info: RoyaltyInfoResponse = RoyaltyInfoResponse {
payment_address: creator.to_string(),
share: Decimal::percent(9),
});
};
let res = app.execute_contract(
creator.clone(),
contract.clone(),
&Sg721ExecuteMsg::<Empty, Empty>::UpdateCollectionInfo {
collection_info: UpdateCollectionInfoMsg {
creator: None,
description: Some(params.info.description.clone()),
image: Some(params.info.image.clone()),
external_link: Some(params.info.external_link.clone()),
explicit_content: None,
royalty_info: Some(royalty_info.clone()),
royalty_info: Some(Some(royalty_info.clone())),
},
},
&[],
Expand All @@ -443,14 +451,15 @@ mod tests {
.wrap()
.query_wasm_smart(contract.clone(), &QueryMsg::CollectionInfo {})
.unwrap();
assert_eq!(res.royalty_info.unwrap(), royalty_info.unwrap());
assert_eq!(res.royalty_info.unwrap(), royalty_info);

// update explicit content
// update explicit content with new creator
let res = app.execute_contract(
creator.clone(),
contract.clone(),
&Sg721ExecuteMsg::<Empty, Empty>::UpdateCollectionInfo {
collection_info: UpdateCollectionInfoMsg {
creator: Some(new_creator.to_string()),
description: Some(params.info.description.clone()),
image: Some(params.info.image.clone()),
external_link: Some(params.info.external_link.clone()),
Expand Down Expand Up @@ -479,7 +488,7 @@ mod tests {
assert!(res.is_err());
// freeze collection to prevent further updates
let res = app.execute_contract(
creator.clone(),
new_creator.clone(),
contract.clone(),
&Sg721ExecuteMsg::<Empty, Empty>::FreezeCollectionInfo {},
&[],
Expand All @@ -488,10 +497,11 @@ mod tests {

// trying to update collection after frozen should throw err
let res = app.execute_contract(
creator,
new_creator,
contract,
&Sg721ExecuteMsg::<Empty, Empty>::UpdateCollectionInfo {
collection_info: UpdateCollectionInfoMsg {
creator: None,
description: Some(params.info.description.clone()),
image: Some(params.info.image.clone()),
external_link: Some(params.info.external_link),
Expand Down

0 comments on commit dc0ed6e

Please sign in to comment.