Skip to content

Commit

Permalink
Merge pull request #604 from public-awesome/shanev/burn-to-mint-refactor
Browse files Browse the repository at this point in the history
Refactor in idiomatic Rust
  • Loading branch information
shanev authored Aug 19, 2023
2 parents 79f13a8 + 9d6746f commit e81212c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
11 changes: 3 additions & 8 deletions contracts/factories/base-factory/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,10 @@ pub fn update_params<T, C>(
);
}
sg2::Token::NonFungible(collection) => {
let minter_response: Result<
cw721_base::msg::MinterResponse,
cosmwasm_std::StdError,
> = deps
let _: cw721_base::msg::MinterResponse = deps
.querier

Check warning on line 163 in contracts/factories/base-factory/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/factories/base-factory/src/contract.rs#L163

Added line #L163 was not covered by tests
.query_wasm_smart(collection, &sg721_base::QueryMsg::Minter {});
if minter_response.is_err() {
return Err(ContractError::InvalidCollectionAddress {});
}
.query_wasm_smart(collection, &sg721_base::QueryMsg::Minter {})
.map_err(|_| ContractError::InvalidCollectionAddress {})?;
}
}
params.min_mint_price = min_mint_price;
Expand Down
33 changes: 16 additions & 17 deletions contracts/minters/base-minter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use base_factory::state::Extension;
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
from_binary, to_binary, Addr, Binary, CosmosMsg, Deps, DepsMut, Empty, Env, MessageInfo, Reply,
StdResult, Timestamp, Uint128, WasmMsg,
ensure, from_binary, to_binary, Addr, Binary, CosmosMsg, Deps, DepsMut, Empty, Env,
MessageInfo, Reply, StdResult, Timestamp, Uint128, WasmMsg,
};

use cw2::set_contract_version;
Expand Down Expand Up @@ -115,38 +115,36 @@ pub fn execute(
ExecuteMsg::UpdateStartTradingTime(time) => {
execute_update_start_trading_time(deps, env, info, time)
}
ExecuteMsg::ReceiveNft(msg) => execute_burn_to_mint(deps, env, info, msg),
ExecuteMsg::ReceiveNft(msg) => execute_burn_to_mint(env, info, msg),
}
}

pub fn execute_burn_to_mint(
_deps: DepsMut,
env: Env,
info: MessageInfo,
msg: Cw721ReceiveMsg,
) -> Result<Response, ContractError> {
let mut res = Response::new();
let burn_msg = cw721::Cw721ExecuteMsg::Burn {
token_id: msg.token_id,
};
let cosmos_burn_msg = CosmosMsg::Wasm(WasmMsg::Execute {
let burn_msg = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: info.sender.to_string(),
msg: to_binary(&burn_msg)?,
msg: to_binary(&cw721::Cw721ExecuteMsg::Burn {
token_id: msg.token_id,
})?,
funds: vec![],
});
res = res.add_message(cosmos_burn_msg);

let token_uri_msg: TokenUriMsg = from_binary(&msg.msg)?;
let execute_mint_msg = ExecuteMsg::Mint {
token_uri: token_uri_msg.token_uri,
};
let cosmos_mint_msg = CosmosMsg::Wasm(WasmMsg::Execute {
let mint_msg = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: env.contract.address.to_string(),
msg: to_binary(&execute_mint_msg)?,
funds: vec![],
});
let res = res.add_message(cosmos_mint_msg);
Ok(res)

Ok(Response::new()
.add_attribute("action", "burn_to_mint")
.add_messages(vec![burn_msg, mint_msg]))
}

pub fn execute_mint_sender(
Expand All @@ -157,9 +155,10 @@ pub fn execute_mint_sender(
) -> Result<Response, ContractError> {
let config = CONFIG.load(deps.storage)?;

if matches!(config.mint_price, Token::NonFungible(_)) {
return Err(ContractError::InvalidMintPrice {});
}
ensure!(
config.mint_price.is_fungible(),
ContractError::InvalidMintPrice {}

Check warning on line 160 in contracts/minters/base-minter/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/minters/base-minter/src/contract.rs#L160

Added line #L160 was not covered by tests
);

_execute_mint_sender(deps, env, info, token_uri)
}
Expand Down

0 comments on commit e81212c

Please sign in to comment.