Skip to content

Commit

Permalink
using controllers/mint package
Browse files Browse the repository at this point in the history
  • Loading branch information
humanalgorithm committed Aug 28, 2023
1 parent 3d9ed09 commit 46d482a
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 128 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion contracts/minters/open-edition-minter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ url = { workspace = true }
open-edition-factory = { workspace = true, features = ["library"] }
semver = { workspace = true }
burn-to-mint = { workspace = true }
base-minter = { workspace = true }
base-minter = { workspace = true }
sg-controllers = { workspace = true }
54 changes: 7 additions & 47 deletions contracts/minters/open-edition-minter/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
coin, to_binary, Addr, BankMsg, Binary, Coin, Decimal, Deps, DepsMut, Empty, Env, MessageInfo,
Order, Reply, ReplyOn, StdError, StdResult, Timestamp, Uint128, WasmMsg,
coin, to_binary, Addr, Binary, Coin, Decimal, Deps, DepsMut, Empty, Env, MessageInfo, Order,
Reply, ReplyOn, StdError, StdResult, Timestamp, Uint128, WasmMsg,
};
use cw2::set_contract_version;
use cw721::Cw721ReceiveMsg;
use cw_utils::{may_pay, maybe_addr, nonpayable, parse_reply_instantiate_data};
use cw_utils::{maybe_addr, nonpayable, parse_reply_instantiate_data};
use semver::Version;
use sg_std::math::U64Ext;
use sg_std::{StargazeMsgWrapper, NATIVE_DENOM};
Expand Down Expand Up @@ -266,21 +266,6 @@ pub fn execute_mint_to(
_execute_mint(deps, info, action, true, Some(recipient))
}

fn pay_mint(
info: MessageInfo,
mint_price_with_discounts: Coin,
config_denom: String,
) -> Result<Uint128, ContractError> {
let payment = may_pay(&info, &config_denom)?;
if payment != mint_price_with_discounts.amount {
return Err(ContractError::IncorrectPaymentAmount(
coin(payment.u128(), &config_denom),
mint_price_with_discounts,
));
}
Ok(payment)
}

fn pay_fairburn(
deps: &DepsMut,
info: MessageInfo,
Expand Down Expand Up @@ -317,32 +302,6 @@ fn pay_fairburn(
Ok((res, network_fee))
}

fn compute_seller_amount(
res: Response,
mint_price_with_discounts: Coin,
network_fee: Uint128,
config_extension: crate::state::ConfigExtension,
) -> Result<(Response, Uint128), ContractError> {
let mut res = res;

let seller_amount = {
// the net amount is mint price - network fee (mint free + dev fee)
let amount = mint_price_with_discounts.amount.checked_sub(network_fee)?;
let payment_address = config_extension.payment_address;
let seller = config_extension.admin;
// Sending 0 coins fails, so only send if amount is non-zero
if !amount.is_zero() {
let msg = BankMsg::Send {
to_address: payment_address.unwrap_or(seller).to_string(),
amount: vec![coin(amount.u128(), mint_price_with_discounts.denom)],
};
res = res.add_message(msg);
}
amount
};
Ok((res, seller_amount))
}

// Generalize checks and mint message creation
// mint -> _execute_mint(recipient: None, token_id: None)
// mint_to(recipient: "friend") -> _execute_mint(Some(recipient), token_id: None)
Expand Down Expand Up @@ -374,7 +333,7 @@ fn _execute_mint(
info.clone(),
config.allowed_burn_collections.clone(),
) {
pay_mint(
sg_controllers::pay_mint(
info.clone(),
mint_price_with_discounts.clone(),
config_denom,
Expand Down Expand Up @@ -448,11 +407,12 @@ fn _execute_mint(
config.allowed_burn_collections,
) {
true => (res, Uint128::new(0)),
false => compute_seller_amount(
false => sg_controllers::compute_seller_amount(
res,
mint_price_with_discounts.clone(),
network_fee,
config.extension,
config.extension.payment_address,
config.extension.admin,
)?,
};

Expand Down
3 changes: 2 additions & 1 deletion contracts/minters/vending-minter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ thiserror = { workspace = true }
url = { workspace = true }
vending-factory = { workspace = true, features = ["library"] }
semver = {workspace = true }
burn-to-mint = { workspace = true }
burn-to-mint = { workspace = true }
sg-controllers = { workspace = true }
51 changes: 7 additions & 44 deletions contracts/minters/vending-minter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ use crate::validation::{check_dynamic_per_address_limit, get_three_percent_of_to
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
coin, coins, to_binary, Addr, BankMsg, Binary, Coin, CosmosMsg, Deps, DepsMut, Empty, Env,
Event, MessageInfo, Order, Reply, ReplyOn, StdError, StdResult, Timestamp, Uint128, WasmMsg,
coin, coins, to_binary, Addr, Binary, Coin, CosmosMsg, Deps, DepsMut, Empty, Env, Event,
MessageInfo, Order, Reply, ReplyOn, StdError, StdResult, Timestamp, Uint128, WasmMsg,
};
use cw2::set_contract_version;
use cw721::Cw721ReceiveMsg;
use cw721_base::Extension;
use cw_utils::{may_pay, maybe_addr, nonpayable, parse_reply_instantiate_data};
use cw_utils::{maybe_addr, nonpayable, parse_reply_instantiate_data};
use rand_core::{RngCore, SeedableRng};
use rand_xoshiro::Xoshiro128PlusPlus;
use semver::Version;
Expand Down Expand Up @@ -596,44 +596,6 @@ pub fn execute_mint_for(
)
}

fn pay_mint(
info: MessageInfo,
mint_price_with_discounts: Coin,
config_denom: String,
) -> Result<Uint128, ContractError> {
let payment = may_pay(&info, &config_denom)?;
if payment != mint_price_with_discounts.amount {
return Err(ContractError::IncorrectPaymentAmount(
coin(payment.u128(), &config_denom),
mint_price_with_discounts,
));
}
Ok(payment)
}

fn compute_seller_amount(
mut res: Response,
mint_price_with_discounts: Coin,
network_fee: Uint128,
config_extension: crate::state::ConfigExtension,
) -> Result<(Response, Uint128), ContractError> {
let seller_amount = {
let amount = mint_price_with_discounts.amount - network_fee;
let payment_address = config_extension.payment_address;
let seller = config_extension.admin;
// Sending 0 coins fails, so only send if amount is non-zero
if !amount.is_zero() {
let msg = BankMsg::Send {
to_address: payment_address.unwrap_or(seller).to_string(),
amount: vec![coin(amount.u128(), mint_price_with_discounts.denom)],
};
res = res.add_message(msg);
}
amount
};
Ok((res, seller_amount))
}

// Generalize checks and mint message creation
// mint -> _execute_mint(recipient: None, token_id: None)
// mint_to(recipient: "friend") -> _execute_mint(Some(recipient), token_id: None)
Expand Down Expand Up @@ -682,7 +644,7 @@ fn _execute_mint(
info.clone(),
config.allowed_burn_collections.clone(),
) {
pay_mint(
sg_controllers::pay_mint(
info.clone(),
mint_price_with_discounts.clone(),
config_denom,
Expand Down Expand Up @@ -775,11 +737,12 @@ fn _execute_mint(
config.allowed_burn_collections,
) {
true => (res, Uint128::new(0)),
false => compute_seller_amount(
false => sg_controllers::compute_seller_amount(
res,
mint_price_with_discounts.clone(),
network_fee,
config.extension,
config.extension.payment_address,
config.extension.admin,
)?,
};

Expand Down
29 changes: 1 addition & 28 deletions packages/burn-to-mint/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,11 @@
use cosmwasm_std::{Addr, StdError};
pub mod msg;
#[cfg(not(feature = "library"))]
use cosmwasm_std::{to_binary, CosmosMsg, Env, MessageInfo, WasmMsg};
use cosmwasm_std::{to_binary, CosmosMsg, MessageInfo, WasmMsg};

use cw721::Cw721ReceiveMsg;
use serde::Serialize;
use sg_std::Response;

pub fn generate_burn_mint_response<T: Serialize>(
info: MessageInfo,
env: Env,
msg: Cw721ReceiveMsg,
execute_mint_msg: T,
) -> Result<Response, StdError> {
let mut res = Response::new();
let burn_msg = cw721::Cw721ExecuteMsg::Burn {
token_id: msg.token_id,
};
let cosmos_burn_msg = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: info.sender.to_string(),
msg: to_binary(&burn_msg)?,
funds: vec![],
});
res = res.add_message(cosmos_burn_msg);
let execute_msg = to_binary(&execute_mint_msg)?;
let cosmos_mint_msg = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: env.contract.address.to_string(),
msg: execute_msg,
funds: vec![],
});
let res = res.add_message(cosmos_mint_msg);
Ok(res)
}

pub fn generate_burn_msg(
info: MessageInfo,
msg: Cw721ReceiveMsg,
Expand Down
2 changes: 2 additions & 0 deletions packages/controllers/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod hooks;
mod init;
mod minter;

pub use hooks::{HookError, Hooks, HooksResponse};
pub use init::{Admin, ContractInstantiateMsg};
pub use minter::{compute_seller_amount, pay_mint};
47 changes: 47 additions & 0 deletions packages/controllers/src/minter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use cosmwasm_std::{coin, BankMsg, Coin, MessageInfo, Uint128};
use cosmwasm_std::{Addr, StdError};
use cw_utils::may_pay;
use sg_std::Response;

pub fn compute_seller_amount(
mut res: Response,
mint_price_with_discounts: Coin,
network_fee: Uint128,
payment_address: Option<Addr>,
admin: Addr,
) -> Result<(Response, Uint128), StdError> {
let seller_amount = {
// the net amount is mint price - network fee (mint free + dev fee)
let amount = mint_price_with_discounts.amount.checked_sub(network_fee)?;
let payment_address = payment_address;
let seller = admin;
// Sending 0 coins fails, so only send if amount is non-zero
if !amount.is_zero() {
let msg = BankMsg::Send {
to_address: payment_address.unwrap_or(seller).to_string(),
amount: vec![coin(amount.u128(), mint_price_with_discounts.denom)],
};
res = res.clone().add_message(msg);
}
amount
};
Ok((res, seller_amount))
}

pub fn pay_mint(
info: MessageInfo,
mint_price_with_discounts: Coin,
config_denom: String,
) -> Result<Uint128, StdError> {
let payment =
may_pay(&info, &config_denom).map_err(|e| StdError::GenericErr { msg: e.to_string() })?;
if payment != mint_price_with_discounts.amount {
let err_msg = format!(
"IncorrectPaymentAmount {0} != {1}",
coin(payment.u128(), &config_denom),
mint_price_with_discounts,
);
return Err(StdError::GenericErr { msg: err_msg });
}
Ok(payment)
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn check_mint_revenues_distribution() {
);
assert_eq!(
res.err().unwrap().source().unwrap().to_string(),
"IncorrectPaymentAmount 100ustars != 100000000ustars"
"Generic error: IncorrectPaymentAmount 100ustars != 100000000ustars"
);

// Invalid price
Expand All @@ -109,15 +109,15 @@ fn check_mint_revenues_distribution() {
);
assert_eq!(
res.err().unwrap().source().unwrap().to_string(),
"IncorrectPaymentAmount 200000000ustars != 100000000ustars"
"Generic error: IncorrectPaymentAmount 200000000ustars != 100000000ustars"
);

// Invalid price
let mint_msg = ExecuteMsg::Mint {};
let res = router.execute_contract(buyer.clone(), minter_addr.clone(), &mint_msg, &[]);
assert_eq!(
res.err().unwrap().source().unwrap().to_string(),
"IncorrectPaymentAmount 0ustars != 100000000ustars"
"Generic error: IncorrectPaymentAmount 0ustars != 100000000ustars"
);

// Invalid denom
Expand All @@ -139,7 +139,7 @@ fn check_mint_revenues_distribution() {
);
assert_eq!(
res.err().unwrap().source().unwrap().to_string(),
"Received unsupported denom 'invalid'"
"Generic error: Received unsupported denom 'invalid'"
);

for _i in 1..=2 {
Expand Down
6 changes: 3 additions & 3 deletions test-suite/src/vending_minter/tests/address_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,11 @@ fn mint_for_token_id_addr() {
)
.unwrap_err();
assert_eq!(
ContractError::IncorrectPaymentAmount(
format!(
"Generic error: IncorrectPaymentAmount {} != {}",
coin(ADMIN_MINT_PRICE + 1, NATIVE_DENOM.to_string()),
coin(ADMIN_MINT_PRICE, NATIVE_DENOM.to_string())
)
.to_string(),
),
err.source().unwrap().to_string()
);

Expand Down

0 comments on commit 46d482a

Please sign in to comment.