Skip to content

Commit

Permalink
Merge branch 'tasiov/sg-1051-names-renewal' into flat-rate-whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
jason-c-child authored Feb 8, 2024
2 parents c7e819b + 0d18873 commit 3b3e590
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 73 deletions.
3 changes: 0 additions & 3 deletions contracts/marketplace/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,6 @@ pub fn execute_process_renewals(
.querier
.query_wasm_smart::<NameMinterParams>(name_minter, &SgNameMinterQueryMsg::Params {})?;

let name_collection = NAME_COLLECTION.load(deps.storage)?;

let mut response = Response::new();

for renewable_ask in renewable_asks {
Expand All @@ -596,7 +594,6 @@ pub fn execute_process_renewals(
&env,
&sudo_params,
&name_minter_params,
&name_collection,
renewable_ask,
response,
)?;
Expand Down
90 changes: 34 additions & 56 deletions contracts/marketplace/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ use std::cmp::max;
use crate::{
execute::{finalize_sale, store_ask},
msg::{ExecuteMsg, QueryMsg},
state::{ask_key, asks, bid_key, bids, Ask, Bid, SudoParams, RENEWAL_QUEUE},
state::{bid_key, bids, Ask, Bid, SudoParams, RENEWAL_QUEUE},
ContractError,
};
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{
coins, ensure, to_json_binary, Addr, BankMsg, Deps, DepsMut, Env, Event, Order, QuerierWrapper,
QueryRequest, StdError, StdResult, Timestamp, Uint128, WasmMsg, WasmQuery,
};
use cw721::Cw721ExecuteMsg;
use cw_storage_plus::Bound;
use sg_name_common::{charge_fees, SECONDS_PER_YEAR};
use sg_name_minter::SudoParams as NameMinterParams;
Expand Down Expand Up @@ -155,13 +154,15 @@ pub fn renew_name(
renewal_price: Uint128,
mut response: Response,
) -> Result<Response, ContractError> {
// Take renewal payment
ask.renewal_fund -= renewal_price;
charge_fees(
&mut response,
sudo_params.trading_fee_percent,
renewal_price,
);
if !renewal_price.is_zero() {
// Take renewal payment
ask.renewal_fund -= renewal_price;
charge_fees(
&mut response,
sudo_params.trading_fee_percent,
renewal_price,
);
}

// Update renewal time
RENEWAL_QUEUE.remove(deps.storage, (ask.renewal_time.seconds(), ask.id));
Expand Down Expand Up @@ -217,33 +218,11 @@ fn sell_name(
Ok(response)
}

fn burn_name(
deps: DepsMut,
collection: &Addr,
ask: Ask,
mut response: Response,
) -> Result<Response, ContractError> {
response = response.add_message(WasmMsg::Execute {
contract_addr: collection.to_string(),
msg: to_json_binary(&Cw721ExecuteMsg::Burn {
token_id: ask.token_id.to_string(),
})?,
funds: vec![],
});

// Delete ask
RENEWAL_QUEUE.remove(deps.storage, (ask.renewal_time.seconds(), ask.id));
asks().remove(deps.storage, ask_key(&ask.token_id))?;

Ok(response)
}

pub fn process_renewal(
deps: DepsMut,
env: &Env,
sudo_params: &SudoParams,
name_minter_params: &NameMinterParams,
collection: &Addr,
mut ask: Ask,
mut response: Response,
) -> Result<Response, ContractError> {
Expand All @@ -264,34 +243,33 @@ pub fn process_renewal(
name_minter_params.base_price.u128(),
)?;

// If the renewal fund is sufficient, renew it
if ask.renewal_fund >= renewal_price {
process_renewal_event = process_renewal_event.add_attribute("action", "renew");
response = response.add_event(process_renewal_event);

return renew_name(deps, env, sudo_params, ask, renewal_price, response);
}

// Renewal fund is insufficient, send it back to the owner
if !ask.renewal_fund.is_zero() {
response = response.add_message(BankMsg::Send {
to_address: ask.seller.to_string(),
amount: coins(ask.renewal_fund.u128(), NATIVE_DENOM),
});
ask.renewal_fund = Uint128::zero();
}

if let Some(bid) = valid_bid {
// The renewal fund is insufficient, sell to the highest bidder
process_renewal_event = process_renewal_event.add_attribute("action", "sell");
response = response.add_event(process_renewal_event);

sell_name(deps, env, ask, bid, response)
// If the renewal fund is sufficient, renew it
if ask.renewal_fund >= renewal_price {
process_renewal_event = process_renewal_event.add_attribute("action", "renew");
response = response.add_event(process_renewal_event);

renew_name(deps, env, sudo_params, ask, renewal_price, response)
} else {
// Renewal fund is insufficient, send it back to the owner
if !ask.renewal_fund.is_zero() {
response = response.add_message(BankMsg::Send {
to_address: ask.seller.to_string(),
amount: coins(ask.renewal_fund.u128(), NATIVE_DENOM),
});
ask.renewal_fund = Uint128::zero();
}

// The renewal fund is insufficient, sell to the highest bidder
process_renewal_event = process_renewal_event.add_attribute("action", "sell");
response = response.add_event(process_renewal_event);

sell_name(deps, env, ask, bid, response)
}
} else {
// The renewal fund is insufficient, and there is no valid bid, burn it
process_renewal_event = process_renewal_event.add_attribute("action", "burn");
process_renewal_event = process_renewal_event.add_attribute("action", "renew");
response = response.add_event(process_renewal_event);

burn_name(deps, collection, ask, response)
renew_name(deps, env, sudo_params, ask, Uint128::zero(), response)
}
}
3 changes: 0 additions & 3 deletions contracts/marketplace/src/sudo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,12 @@ pub fn sudo_end_block(mut deps: DepsMut, env: Env) -> Result<Response, ContractE
.querier
.query_wasm_smart::<NameMinterParams>(name_minter, &SgNameMinterQueryMsg::Params {})?;

let name_collection = NAME_COLLECTION.load(deps.storage)?;

for renewable_ask in renewable_asks {
response = process_renewal(
deps.branch(),
&env,
&sudo_params,
&name_minter_params,
&name_collection,
renewable_ask,
response,
)?;
Expand Down
6 changes: 3 additions & 3 deletions contracts/name-minter/src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,7 @@ mod query {
let ask = result.unwrap().unwrap();

assert_eq!(ask.seller, USER.to_string());
assert_eq!(ask.renewal_fund, Uint128::zero());
assert_eq!(ask.renewal_fund, renewal_price.amount);

let expected_renewal_time = app.block_info().time.plus_seconds(SECONDS_PER_YEAR);
assert_eq!(ask.renewal_time, expected_renewal_time);
Expand Down Expand Up @@ -1444,7 +1444,7 @@ mod query {
}

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

mint_and_list(&mut app, NAME, USER, None).unwrap();
Expand Down Expand Up @@ -1497,7 +1497,7 @@ mod query {
},
);
assert!(result.is_ok());
assert!(result.unwrap().is_none());
assert!(result.unwrap().is_some());
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion scripts/migrate_mkt.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ MSG=$(cat <<EOF
EOF
)

starsd tx wasm migrate stars1ejc9sve7wcvg56acyynz3rn73dtfcg7n49efxpvvragwwy5fu7csskmwr5 3497 "$MSG" \
starsd tx wasm migrate stars1ejc9sve7wcvg56acyynz3rn73dtfcg7n49efxpvvragwwy5fu7csskmwr5 3500 "$MSG" \
--from hot-wallet --keyring-backend test \
--gas-prices 0.025ustars --gas auto --gas-adjustment 1.9 \
-b block -o json | jq .
12 changes: 5 additions & 7 deletions scripts/store_v2.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ starsd config node $NODE
starsd config chain-id $CHAIN_ID
starsd config output json

STORE

starsd tx wasm store artifacts/name_marketplace.wasm --from hot-wallet \
--keyring-backend test \
--gas-prices 0.025ustars --gas-adjustment 1.7 \
--gas auto -y -b block -o json | jq '.logs' | grep -A 1 code_id

starsd tx wasm store artifacts/sg721_name.wasm --from hot-wallet \
--keyring-backend test \
--gas-prices 0.025ustars --gas-adjustment 1.7 \
--gas auto -y -b block -o json | jq '.logs' | grep -A 1 code_id
# starsd tx wasm store artifacts/sg721_name.wasm --from hot-wallet \
# --keyring-backend test \
# --gas-prices 0.025ustars --gas-adjustment 1.7 \
# --gas auto -y -b block -o json | jq '.logs' | grep -A 1 code_id


0 comments on commit 3b3e590

Please sign in to comment.