Skip to content

Commit

Permalink
refactor utility functions and cargo schema
Browse files Browse the repository at this point in the history
  • Loading branch information
humanalgorithm committed Aug 27, 2023
1 parent 863301f commit 44fed17
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 61 deletions.
44 changes: 19 additions & 25 deletions contracts/minters/base-minter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,32 +131,26 @@ pub fn burn_and_mint(
Ok(mint_res.add_submessages(res.messages))
}

fn pay_mint_if_not_burn_collection(
fn pay_mint(
info: MessageInfo,
mint_price: Token,
factory_params: MinterParams<Option<Empty>>,
allowed_burn_collections: Option<Vec<Addr>>,
) -> Result<Uint128, ContractError> {
let mut res = Response::new();
match burn_to_mint::sender_is_allowed_burn_collection(info.clone(), allowed_burn_collections) {
true => Ok(0_u128.into()),
false => {
let funds_sent = must_pay(&info, NATIVE_DENOM)?;
// Create network fee msgs
let mint_fee_percent = factory_params.mint_fee_bps.bps_to_decimal();
let network_fee = match mint_price {
sg2::Token::Fungible(coin) => coin.amount * mint_fee_percent,
sg2::Token::NonFungible(_) => Uint128::new(0),
};
// TODO: NFTs don't have a fee
// For the base 1/1 minter, the entire mint price should be Fair Burned
if network_fee != funds_sent {
return Err(ContractError::InvalidMintPrice {});
}
checked_fair_burn(&info, network_fee.u128(), None, &mut res)?;
Ok(network_fee)
}
let funds_sent = must_pay(&info, NATIVE_DENOM)?;
// Create network fee msgs
let mint_fee_percent = factory_params.mint_fee_bps.bps_to_decimal();
let network_fee = match mint_price {
sg2::Token::Fungible(coin) => coin.amount * mint_fee_percent,
sg2::Token::NonFungible(_) => Uint128::new(0),

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L145 was not covered by tests
};
// TODO: NFTs don't have a fee
// For the base 1/1 minter, the entire mint price should be Fair Burned
if network_fee != funds_sent {
return Err(ContractError::InvalidMintPrice {});
}
checked_fair_burn(&info, network_fee.u128(), None, &mut res)?;
Ok(network_fee)
}

fn execute_mint_sender(
Expand Down Expand Up @@ -191,13 +185,13 @@ fn execute_mint_sender(
.query_wasm_smart(config.factory, &Sg2QueryMsg::Params {})?;
let factory_params = factory.params;

//TODO CHECK FOR ALLOWED CONTRACTS
let network_fee = pay_mint_if_not_burn_collection(
let network_fee = match burn_to_mint::sender_is_allowed_burn_collection(
info.clone(),
config.mint_price,
factory_params,
config.allowed_burn_collections,
)?;
) {
true => 0_u128.into(),
false => pay_mint(info.clone(), config.mint_price, factory_params)?,
};
// Create mint msgs
let mint_msg = Sg721ExecuteMsg::<Extension, Empty>::Mint {
token_id: increment_token_index(deps.storage)?.to_string(),
Expand Down
13 changes: 13 additions & 0 deletions contracts/minters/open-edition-minter/schema/instantiate_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
},
"additionalProperties": false,
"definitions": {
"Addr": {
"description": "A human readable address.\n\nIn Cosmos, this is typically bech32 encoded. But for multi-chain smart contracts no assumptions should be made other than being UTF-8 encoded and of reasonable length.\n\nThis type represents a validated address. It can be created in the following ways 1. Use `Addr::unchecked(input)` 2. Use `let checked: Addr = deps.api.addr_validate(input)?` 3. Use `let checked: Addr = deps.api.addr_humanize(canonical_addr)?` 4. Deserialize from JSON. This must only be done from JSON that was validated before such as a contract's state. `Addr` must not be used in messages sent by the user because this would result in unvalidated instances.\n\nThis type is immutable. If you really need to mutate it (Really? Are you sure?), create a mutable copy using `let mut mutable = Addr::to_string()` and operate on that `String` instance.",
"type": "string"
},
"Coin": {
"type": "object",
"required": [
Expand Down Expand Up @@ -117,6 +121,15 @@
"init_msg"
],
"properties": {
"allowed_burn_collections": {
"type": [
"array",
"null"
],
"items": {
"$ref": "#/definitions/Addr"
}
},
"collection_params": {
"$ref": "#/definitions/CollectionParams"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
"mint_price"
],
"properties": {
"allowed_burn_collections": {
"type": [
"array",
"null"
],
"items": {
"$ref": "#/definitions/Addr"
}
},
"collection_code_id": {
"type": "integer",
"format": "uint64",
Expand Down
35 changes: 17 additions & 18 deletions contracts/minters/open-edition-minter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,25 +266,19 @@ pub fn execute_mint_to(
_execute_mint(deps, info, action, true, Some(recipient))
}

fn pay_mint_if_not_burn_collection(
fn pay_mint(
info: MessageInfo,
mint_price_with_discounts: Coin,
config_denom: String,
allowed_burn_collections: Option<Vec<Addr>>,
) -> Result<Uint128, ContractError> {
match burn_to_mint::sender_is_allowed_burn_collection(info.clone(), allowed_burn_collections) {
true => Ok(Uint128::new(0)),
false => {
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)
}
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(
Expand Down Expand Up @@ -334,6 +328,7 @@ fn compute_seller_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 @@ -361,12 +356,16 @@ fn _execute_mint(
.map_err(|_| ContractError::IncorrectFungibility {})?;

Check warning on line 356 in contracts/minters/open-edition-minter/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/minters/open-edition-minter/src/contract.rs#L356

Added line #L356 was not covered by tests
// Exact payment only accepted

pay_mint_if_not_burn_collection(
if !burn_to_mint::sender_is_allowed_burn_collection(
info.clone(),
mint_price_with_discounts.clone(),
config_denom,
config.allowed_burn_collections.clone(),
)?;
) {
pay_mint(
info.clone(),
mint_price_with_discounts.clone(),
config_denom,
)?;
};

let factory: ParamsResponse = deps
.querier
Expand Down
13 changes: 13 additions & 0 deletions contracts/minters/vending-minter/schema/instantiate_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
},
"additionalProperties": false,
"definitions": {
"Addr": {
"description": "A human readable address.\n\nIn Cosmos, this is typically bech32 encoded. But for multi-chain smart contracts no assumptions should be made other than being UTF-8 encoded and of reasonable length.\n\nThis type represents a validated address. It can be created in the following ways 1. Use `Addr::unchecked(input)` 2. Use `let checked: Addr = deps.api.addr_validate(input)?` 3. Use `let checked: Addr = deps.api.addr_humanize(canonical_addr)?` 4. Deserialize from JSON. This must only be done from JSON that was validated before such as a contract's state. `Addr` must not be used in messages sent by the user because this would result in unvalidated instances.\n\nThis type is immutable. If you really need to mutate it (Really? Are you sure?), create a mutable copy using `let mut mutable = Addr::to_string()` and operate on that `String` instance.",
"type": "string"
},
"Coin": {
"type": "object",
"required": [
Expand Down Expand Up @@ -117,6 +121,15 @@
"init_msg"
],
"properties": {
"allowed_burn_collections": {
"type": [
"array",
"null"
],
"items": {
"$ref": "#/definitions/Addr"
}
},
"collection_params": {
"$ref": "#/definitions/CollectionParams"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
"mint_price"
],
"properties": {
"allowed_burn_collections": {
"type": [
"array",
"null"
],
"items": {
"$ref": "#/definitions/Addr"
}
},
"collection_code_id": {
"type": "integer",
"format": "uint64",
Expand Down
34 changes: 16 additions & 18 deletions contracts/minters/vending-minter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,25 +596,19 @@ pub fn execute_mint_for(
)
}

fn pay_mint_if_not_contract(
fn pay_mint(
info: MessageInfo,
mint_price_with_discounts: Coin,
config_denom: String,
allowed_burn_collections: Option<Vec<Addr>>,
) -> Result<Uint128, ContractError> {
match burn_to_mint::sender_is_allowed_burn_collection(info.clone(), allowed_burn_collections) {
true => Ok(Uint128::new(0)),
false => {
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)
}
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(
Expand Down Expand Up @@ -684,12 +678,16 @@ fn _execute_mint(
.denom()
.map_err(|_| ContractError::IncorrectFungibility {})?;

pay_mint_if_not_contract(
if !burn_to_mint::sender_is_allowed_burn_collection(
info.clone(),
mint_price_with_discounts.clone(),
config_denom,
config.allowed_burn_collections.clone(),
)?;
) {
pay_mint(
info.clone(),
mint_price_with_discounts.clone(),
config_denom,
)?;
};
let mut res = Response::new();

let factory: ParamsResponse = deps
Expand Down

0 comments on commit 44fed17

Please sign in to comment.