Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add execute msg to update minter #729

Merged
merged 3 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 96 additions & 2 deletions contracts/cw20-base/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ pub fn execute(
marketing,
} => execute_update_marketing(deps, env, info, project, description, marketing),
ExecuteMsg::UploadLogo(logo) => execute_upload_logo(deps, env, info, logo),
ExecuteMsg::UpdateMinter { new_minter } => {
execute_update_minter(deps, env, info, new_minter)
}
}
}

Expand Down Expand Up @@ -304,8 +307,17 @@ pub fn execute_mint(
return Err(ContractError::InvalidZeroAmount {});
}

let mut config = TOKEN_INFO.load(deps.storage)?;
if config.mint.is_none() || config.mint.as_ref().unwrap().minter != info.sender {
let mut config = TOKEN_INFO
maurolacy marked this conversation as resolved.
Show resolved Hide resolved
.may_load(deps.storage)?
.ok_or(ContractError::Unauthorized {})?;

if config
maurolacy marked this conversation as resolved.
Show resolved Hide resolved
.mint
.as_ref()
.ok_or(ContractError::Unauthorized {})?
.minter
!= info.sender
{
return Err(ContractError::Unauthorized {});
}

Expand Down Expand Up @@ -377,6 +389,35 @@ pub fn execute_send(
Ok(res)
}

pub fn execute_update_minter(
maurolacy marked this conversation as resolved.
Show resolved Hide resolved
deps: DepsMut,
_env: Env,
info: MessageInfo,
new_minter: String,
) -> Result<Response, ContractError> {
let mut config = TOKEN_INFO
.may_load(deps.storage)?
.ok_or(ContractError::Unauthorized {})?;

let mint = config.mint.as_ref().ok_or(ContractError::Unauthorized {})?;
if mint.minter != info.sender {
return Err(ContractError::Unauthorized {});
}

let minter = deps.api.addr_validate(&new_minter)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: You can move this validation before loading the config, to save some cycles in case of error.

let minter_data = MinterData {
minter,
cap: mint.cap,
};
config.mint = Some(minter_data);

TOKEN_INFO.save(deps.storage, &config)?;

Ok(Response::default()
.add_attribute("action", "update_minter")
.add_attribute("new_minter", new_minter))
}

pub fn execute_update_marketing(
deps: DepsMut,
_env: Env,
Expand Down Expand Up @@ -871,6 +912,59 @@ mod tests {
assert_eq!(err, ContractError::Unauthorized {});
}

#[test]
fn minter_can_update_minter_but_not_cap() {
let mut deps = mock_dependencies();
let minter = String::from("minter");
let cap = Some(Uint128::from(3000000u128));
do_instantiate_with_minter(
deps.as_mut(),
&String::from("genesis"),
Uint128::new(1234),
&minter,
cap,
);

let new_minter = "new_minter";
let msg = ExecuteMsg::UpdateMinter {
new_minter: new_minter.to_string(),
};

let info = mock_info(&minter, &[]);
let env = mock_env();
let res = execute(deps.as_mut(), env.clone(), info, msg);
assert!(res.is_ok());
let query_minter_msg = QueryMsg::Minter {};
let res = query(deps.as_ref(), env, query_minter_msg);
let mint: MinterResponse = from_binary(&res.unwrap()).unwrap();

// Minter cannot update cap.
assert!(mint.cap == cap);
assert!(mint.minter == new_minter)
}

#[test]
fn others_cannot_update_minter() {
let mut deps = mock_dependencies();
let minter = String::from("minter");
do_instantiate_with_minter(
deps.as_mut(),
&String::from("genesis"),
Uint128::new(1234),
&minter,
None,
);

let msg = ExecuteMsg::UpdateMinter {
new_minter: String::from("new_minter"),
};

let info = mock_info("not the minter", &[]);
let env = mock_env();
let err = execute(deps.as_mut(), env, info, msg).unwrap_err();
assert_eq!(err, ContractError::Unauthorized {});
}

blue-note marked this conversation as resolved.
Show resolved Hide resolved
#[test]
fn no_one_mints_if_minter_unset() {
let mut deps = mock_dependencies();
Expand Down
2 changes: 2 additions & 0 deletions packages/cw20/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub enum Cw20ExecuteMsg {
/// Only with the "mintable" extension. If authorized, creates amount new tokens
/// and adds to the recipient balance.
Mint { recipient: String, amount: Uint128 },
/// Only with the "mintable" extension. The current minter may set a new minter.
UpdateMinter { new_minter: String },
maurolacy marked this conversation as resolved.
Show resolved Hide resolved
/// Only with the "marketing" extension. If authorized, updates marketing metadata.
/// Setting None/null for any of these will leave it unchanged.
/// Setting Some("") will clear this field on the contract storage
Expand Down