Skip to content

Commit

Permalink
Merge pull request #262 from public-awesome/renewal-funding-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jason-c-child authored Jul 29, 2024
2 parents 30b608d + efb8662 commit b3582af
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 17 deletions.
20 changes: 10 additions & 10 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ members = ["packages/*", "contracts/*"]
resolver = "2"

[workspace.package]
version = "2.1.0"
version = "2.1.2"
edition = "2021"
homepage = "https://stargaze.zone"
repository = "https://github.com/public-awesome/names"
Expand Down
2 changes: 1 addition & 1 deletion contracts/marketplace/schema/name-marketplace.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"contract_name": "name-marketplace",
"contract_version": "2.1.0",
"contract_version": "2.1.2",
"idl_version": "1.0.0",
"instantiate": {
"$schema": "http://json-schema.org/draft-07/schema#",
Expand Down
6 changes: 6 additions & 0 deletions contracts/marketplace/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ pub enum ContractError {
#[error("InsufficientRenewalFunds: expected {expected}, actual {actual}")]
InsufficientRenewalFunds { expected: Coin, actual: Coin },

#[error("ExcededRenewalFund: expected {expected}, actual {actual}")]
ExceededRenewalFund { expected: Coin, actual: Coin },

#[error("InvalidRenewalPrice")]
InvalidRenewalPrice {},

#[error("Cannot remove ask with existing bids")]
ExistingBids {},

Expand Down
21 changes: 20 additions & 1 deletion contracts/marketplace/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use crate::error::ContractError;
use crate::helpers::{get_char_price, get_renewal_price_and_bid, process_renewal, renew_name};
use crate::hooks::{prepare_ask_hook, prepare_bid_hook, prepare_sale_hook};
use crate::msg::{ExecuteMsg, HookAction, InstantiateMsg};
use crate::query::query_ask_renew_price;
use crate::state::{
ask_key, asks, bid_key, bids, increment_asks, legacy_bids, Ask, Bid, SudoParams, IS_SETUP,
NAME_COLLECTION, NAME_MINTER, RENEWAL_QUEUE, SUDO_PARAMS,
};

use cosmwasm_std::{
coin, coins, ensure, ensure_eq, to_json_binary, Addr, BankMsg, Decimal, Deps, DepsMut, Empty,
Env, Event, MessageInfo, Order, StdError, StdResult, Storage, Uint128, WasmMsg,
Expand Down Expand Up @@ -462,6 +462,25 @@ pub fn execute_fund_renewal(
let payment = must_pay(&info, NATIVE_DENOM)?;

let mut ask = asks().load(deps.storage, ask_key(token_id))?;

// get the renewal price
let renewal_price =
query_ask_renew_price(deps.as_ref(), ask.renewal_time, (&token_id).to_string())?;

// make sure that we do not over fund the renewal
// based on the price we got back
if let Some(renewal_price_coin) = renewal_price.0.as_ref() {
ensure!(
ask.renewal_fund + payment <= renewal_price_coin.amount,
ContractError::ExceededRenewalFund {
expected: coin(renewal_price_coin.amount.u128(), NATIVE_DENOM),
actual: coin(ask.renewal_fund.u128() + payment.u128(), NATIVE_DENOM),
}
);
} else {
return Err(ContractError::InvalidRenewalPrice {});
}

ask.renewal_fund += payment;
asks().save(deps.storage, ask_key(token_id), &ask)?;

Expand Down
2 changes: 1 addition & 1 deletion contracts/name-minter/schema/name-minter.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"contract_name": "name-minter",
"contract_version": "2.1.0",
"contract_version": "2.1.2",
"idl_version": "1.0.0",
"instantiate": {
"$schema": "http://json-schema.org/draft-07/schema#",
Expand Down
83 changes: 81 additions & 2 deletions contracts/name-minter/src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,8 @@ mod query {
assert!(response.is_ok());
let renewal_price = response.unwrap().0.unwrap();

let fund_amount = coins(renewal_price.amount.u128() * 100_u128, NATIVE_DENOM);
// send set the fund_amount to the exact price
let fund_amount = coins(renewal_price.amount.u128(), NATIVE_DENOM);
app.sudo(CwSudoMsg::Bank({
BankSudo::Mint {
to_address: USER.to_string(),
Expand All @@ -1522,7 +1523,7 @@ mod query {
&MarketplaceExecuteMsg::FundRenewal {
token_id: NAME.to_string(),
},
&[coin(renewal_price.amount.u128() - 1u128, NATIVE_DENOM)],
&[coin(renewal_price.amount.u128(), NATIVE_DENOM)],
);
assert!(result.is_ok());

Expand All @@ -1546,6 +1547,84 @@ mod query {
assert!(result.unwrap().is_some());
}

#[test]
fn allow_partial_funded_renewal() {
let mut app = instantiate_contracts(None, None, None);

mint_and_list(&mut app, NAME, USER, None).unwrap();

update_block_time(&mut app, SECONDS_PER_YEAR - (60 * 60 * 24 * 30));

let response = app.wrap().query_wasm_smart::<(Option<Coin>, Option<Bid>)>(
MKT,
&MarketplaceQueryMsg::AskRenewPrice {
current_time: app.block_info().time,
token_id: NAME.to_string(),
},
);
assert!(response.is_ok());
let renewal_price = response.unwrap().0.unwrap();

// try to over-fund renewal
let fund_amount = coins(renewal_price.amount.u128(), NATIVE_DENOM);
app.sudo(CwSudoMsg::Bank({
BankSudo::Mint {
to_address: USER.to_string(),
amount: fund_amount,
}
}))
.map_err(|err| println!("{:?}", err))
.ok();
let result = app.execute_contract(
Addr::unchecked(USER),
Addr::unchecked(MKT),
&MarketplaceExecuteMsg::FundRenewal {
token_id: NAME.to_string(),
},
&[coin(renewal_price.amount.u128() - 100u128, NATIVE_DENOM)],
);
assert!(result.is_ok());
}

#[test]
fn reject_overfunded_renewal() {
let mut app = instantiate_contracts(None, None, None);

mint_and_list(&mut app, NAME, USER, None).unwrap();

update_block_time(&mut app, SECONDS_PER_YEAR - (60 * 60 * 24 * 30));

let response = app.wrap().query_wasm_smart::<(Option<Coin>, Option<Bid>)>(
MKT,
&MarketplaceQueryMsg::AskRenewPrice {
current_time: app.block_info().time,
token_id: NAME.to_string(),
},
);
assert!(response.is_ok());
let renewal_price = response.unwrap().0.unwrap();

// try to over-fund renewal
let fund_amount = coins(renewal_price.amount.u128(), NATIVE_DENOM);
app.sudo(CwSudoMsg::Bank({
BankSudo::Mint {
to_address: USER.to_string(),
amount: fund_amount,
}
}))
.map_err(|err| println!("{:?}", err))
.ok();
let result = app.execute_contract(
Addr::unchecked(USER),
Addr::unchecked(MKT),
&MarketplaceExecuteMsg::FundRenewal {
token_id: NAME.to_string(),
},
&[coin(renewal_price.amount.u128() + 100u128, NATIVE_DENOM)],
);
assert!(result.is_err());
}

#[test]
fn query_name() {
let mut app = instantiate_contracts(None, None, None);
Expand Down
2 changes: 1 addition & 1 deletion contracts/sg721-name/schema/sg721-name.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"contract_name": "sg721-name",
"contract_version": "2.1.0",
"contract_version": "2.1.2",
"idl_version": "1.0.0",
"instantiate": {
"$schema": "http://json-schema.org/draft-07/schema#",
Expand Down

0 comments on commit b3582af

Please sign in to comment.