Skip to content

Commit

Permalink
Remove name support from minter
Browse files Browse the repository at this point in the history
  • Loading branch information
MightOfOaks committed Sep 7, 2023
1 parent 517c1d9 commit 171c41e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 73 deletions.
78 changes: 28 additions & 50 deletions contracts/vip/minter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use sg_std::Response;

use crate::error::ContractError;
use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
use crate::state::{Config, CONFIG, NAME_QUEUE, NAME_UPDATE_HEIGHT, PAUSED};
use crate::state::{
increment_token_index, Config, CONFIG, PAUSED, TOKEN_INDEX, TOKEN_UPDATE_HEIGHT,
};

const CONTRACT_NAME: &str = "crates.io:stargaze-vip-minter";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down Expand Up @@ -44,7 +46,6 @@ pub fn instantiate(
deps.storage,
&Config {
vip_collection: deps.api.addr_validate(collection.as_str())?,
name_collection: deps.api.addr_validate(&msg.name_collection)?,
update_interval: msg.update_interval,
},
)?;
Expand Down Expand Up @@ -77,58 +78,46 @@ pub fn execute(
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::Mint { name } => execute_mint(deps, env, info, name),
ExecuteMsg::Update { name } => execute_update(deps, env, info, name),
ExecuteMsg::Mint {} => execute_mint(deps, env, info),
ExecuteMsg::Update { token_id } => execute_update(deps, env, info, token_id),
ExecuteMsg::Pause {} => execute_pause(deps, info),
ExecuteMsg::Resume {} => execute_resume(deps, info),
ExecuteMsg::UpdateConfig {
vip_collection,
name_collection,
update_interval,
} => execute_update_config(deps, info, vip_collection, name_collection, update_interval),
} => execute_update_config(deps, info, vip_collection, update_interval),
}
}

pub fn execute_mint(
deps: DepsMut,
mut deps: DepsMut,
env: Env,
info: MessageInfo,
name: String,
) -> Result<Response, ContractError> {
ensure!(
info.sender == associated_address(deps.as_ref(), name.clone())?,
ContractError::Unauthorized {}
);
ensure!(!PAUSED.load(deps.storage)?, ContractError::Paused {});

let Config { vip_collection, .. } = CONFIG.load(deps.storage)?;

let mint_msg = mint(
deps.as_ref(),
deps.branch(),
info.sender,
env.block.time,
name.clone(),
vip_collection,
None,
)?;

NAME_QUEUE.update(deps.storage, env.block.height, |names| -> StdResult<_> {
let mut names = names.unwrap_or_default();
names.push(name.clone());
Ok(names)
})?;

NAME_UPDATE_HEIGHT.update(deps.storage, name, |_| -> StdResult<_> {
let token_id = TOKEN_INDEX.load(deps.storage)?;
TOKEN_UPDATE_HEIGHT.update(deps.storage, token_id, |_| -> StdResult<_> {
Ok(env.block.height)
})?;
let event = Event::new("mint");
Ok(Response::new().add_message(mint_msg).add_event(event))
}

pub fn execute_update(
deps: DepsMut,
mut deps: DepsMut,
env: Env,
info: MessageInfo,
name: String,
token_id: u64,
) -> Result<Response, ContractError> {
ensure!(!PAUSED.load(deps.storage)?, ContractError::Paused {});
let Config {
Expand All @@ -137,7 +126,7 @@ pub fn execute_update(
..
} = CONFIG.load(deps.storage)?;

let last_update_height = NAME_UPDATE_HEIGHT.may_load(deps.storage, name.clone())?;
let last_update_height = TOKEN_UPDATE_HEIGHT.may_load(deps.storage, token_id.clone())?;
if let Some(last_update_height) = last_update_height {
if env.block.height - last_update_height < update_interval {
return Err(ContractError::UpdateIntervalNotPassed {});
Expand All @@ -147,29 +136,33 @@ pub fn execute_update(
}

let mint_msg = mint(
deps.as_ref(),
deps.branch(),
info.sender,
env.block.time,
name.clone(),
vip_collection,
Some(token_id),
)?;

NAME_UPDATE_HEIGHT.update(deps.storage, name, |_| -> StdResult<_> {
TOKEN_UPDATE_HEIGHT.update(deps.storage, token_id, |_| -> StdResult<_> {
Ok(env.block.height)
})?;
let event = Event::new("update");
Ok(Response::new().add_message(mint_msg).add_event(event))
}

pub fn mint(
deps: Deps,
deps: DepsMut,
sender: Addr,
block_time: Timestamp,
name: String,
vip_collection: Addr,
token_id: Option<u64>,
) -> Result<WasmMsg, ContractError> {
let token_id_to_mint = match token_id {
Some(id) => id.to_string(), // to be used for updates
None => increment_token_index(deps.storage)?.to_string(),
};

let msg = stargaze_vip_collection::ExecuteMsg::Mint {
token_id: name,
token_id: token_id_to_mint,
owner: sender.to_string(),
token_uri: None,
extension: stargaze_vip_collection::state::Metadata {
Expand All @@ -190,7 +183,6 @@ pub fn execute_update_config(
deps: DepsMut,
info: MessageInfo,
vip_collection: Option<String>,
name_collection: Option<String>,
update_interval: Option<u64>,
) -> Result<Response, ContractError> {
cw_ownable::assert_owner(deps.storage, &info.sender)
Expand All @@ -200,9 +192,6 @@ pub fn execute_update_config(
if let Some(vip_collection) = vip_collection {
config.vip_collection = deps.api.addr_validate(&vip_collection)?;
}
if let Some(name_collection) = name_collection {
config.name_collection = deps.api.addr_validate(&name_collection)?;
}
if let Some(update_interval) = update_interval {
// TODO: define a min and max for update_interval (and update the error)
if update_interval < 1 {
Expand All @@ -213,7 +202,6 @@ pub fn execute_update_config(
CONFIG.save(deps.storage, &config)?;
let event = Event::new("update_config")
.add_attribute("vip_collection", config.vip_collection)
.add_attribute("name_collection", config.name_collection)
.add_attribute("update_interval", config.update_interval.to_string());
Ok(Response::new().add_event(event))
}
Expand All @@ -240,16 +228,7 @@ pub fn execute_resume(deps: DepsMut, info: MessageInfo) -> Result<Response, Cont
Ok(Response::new().add_event(event))
}

pub fn associated_address(deps: Deps, name: String) -> Result<Addr, ContractError> {
let associated_addr: Addr = deps.querier.query_wasm_smart(
CONFIG.load(deps.storage)?.name_collection,
&sg_name::SgNameQueryMsg::AssociatedAddress { name },
)?;

Ok(associated_addr)
}

fn total_staked(deps: Deps, address: Addr) -> StdResult<Uint128> {
fn total_staked(deps: DepsMut, address: Addr) -> StdResult<Uint128> {
let total = deps
.querier
.query_all_delegations(address)?
Expand All @@ -264,8 +243,8 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::Config {} => to_binary(&CONFIG.load(deps.storage)?),
QueryMsg::IsPaused {} => to_binary(&PAUSED.load(deps.storage)?),
QueryMsg::NameUpdateHeight { name } => {
to_binary(&NAME_UPDATE_HEIGHT.load(deps.storage, name)?)
QueryMsg::TokenUpdateHeight { token_id } => {
to_binary(&TOKEN_UPDATE_HEIGHT.load(deps.storage, token_id)?)
}
}
}
Expand Down Expand Up @@ -303,7 +282,6 @@ mod tests {

let init_msg = InstantiateMsg {
collection_code_id,
name_collection: "name_collection".to_string(),
update_interval: 100,
};
let msg = WasmMsg::Instantiate {
Expand Down
10 changes: 3 additions & 7 deletions contracts/vip/minter/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,28 @@ use cosmwasm_schema::{cw_serde, QueryResponses};
#[cw_serde]
pub struct InstantiateMsg {
pub collection_code_id: u64,
pub name_collection: String,
pub update_interval: u64, // in blocks
}

#[cw_serde]
pub struct ConfigResponse {
pub vip_collection: String,
pub name_collection: String,
pub update_interval: u64,
}

#[cw_serde]
pub enum ExecuteMsg {
/// Mint a loyalty token for the given name
Mint { name: String },
Mint {},
/// Update the stake amount for the given name
Update { name: String },
Update { token_id: u64 },
/// So we can pause before migrating names, etc.
Pause {},
/// To resume paused operations
Resume {},
/// Update the minter config params
UpdateConfig {
vip_collection: Option<String>,
name_collection: Option<String>,
update_interval: Option<u64>,
},
}
Expand All @@ -44,7 +41,6 @@ pub enum SudoMsg {
// },
UpdateConfig {
vip_collection: Option<String>,
name_collection: Option<String>,
update_interval: Option<u64>,
},
}
Expand All @@ -57,5 +53,5 @@ pub enum QueryMsg {
#[returns(bool)]
IsPaused {},
#[returns(u64)]
NameUpdateHeight { name: String },
TokenUpdateHeight { token_id: u64 },
}
13 changes: 10 additions & 3 deletions contracts/vip/minter/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::Addr;
use cosmwasm_std::{Addr, StdResult, Storage};
use cw_storage_plus::{Item, Map};

#[cw_serde]
pub struct Config {
pub vip_collection: Addr,
pub name_collection: Addr,
pub update_interval: u64, // in blocks
}

Expand All @@ -16,4 +15,12 @@ pub const PAUSED: Item<bool> = Item::new("paused");
pub const NAME_QUEUE: Map<u64, Vec<String>> = Map::new("nq");

/// (name, block_height)
pub const NAME_UPDATE_HEIGHT: Map<String, u64> = Map::new("nuh");
pub const TOKEN_UPDATE_HEIGHT: Map<u64, u64> = Map::new("nuh");

pub const TOKEN_INDEX: Item<u64> = Item::new("token_index");

pub fn increment_token_index(store: &mut dyn Storage) -> StdResult<u64> {
let val = TOKEN_INDEX.may_load(store)?.unwrap_or_default() + 1;
TOKEN_INDEX.save(store, &val)?;
Ok(val)
}
15 changes: 2 additions & 13 deletions contracts/vip/minter/src/sudo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ use cosmwasm_std::entry_point;
use cosmwasm_std::{DepsMut, Env, Event};
use sg_std::Response;

use crate::{
contract::{associated_address, mint},
msg::SudoMsg,
state::{Config, CONFIG, NAME_QUEUE},
ContractError,
};
use crate::{msg::SudoMsg, state::CONFIG, ContractError};

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn sudo(deps: DepsMut, env: Env, msg: SudoMsg) -> Result<Response, ContractError> {
Expand All @@ -17,9 +12,8 @@ pub fn sudo(deps: DepsMut, env: Env, msg: SudoMsg) -> Result<Response, ContractE
SudoMsg::EndBlock {} => sudo_end_block(deps, env),
SudoMsg::UpdateConfig {
vip_collection,
name_collection,
update_interval,
} => sudo_execute_update_config(deps, vip_collection, name_collection, update_interval),
} => sudo_execute_update_config(deps, vip_collection, update_interval),
// SudoMsg::UpdateParams {
// fair_burn,
// trading_fee_percent,
Expand Down Expand Up @@ -72,16 +66,12 @@ pub fn sudo_end_block(_deps: DepsMut, _env: Env) -> Result<Response, ContractErr
pub fn sudo_execute_update_config(
deps: DepsMut,
vip_collection: Option<String>,
name_collection: Option<String>,
update_interval: Option<u64>,
) -> Result<Response, ContractError> {
let mut config = CONFIG.load(deps.storage)?;
if let Some(vip_collection) = vip_collection {
config.vip_collection = deps.api.addr_validate(&vip_collection)?;
}
if let Some(name_collection) = name_collection {
config.name_collection = deps.api.addr_validate(&name_collection)?;
}
if let Some(update_interval) = update_interval {
// TODO: define a min and max for update_interval (and update the error)
if update_interval < 1 {
Expand All @@ -92,7 +82,6 @@ pub fn sudo_execute_update_config(
CONFIG.save(deps.storage, &config)?;
let event = Event::new("sudo_update_config")
.add_attribute("vip_collection", config.vip_collection)
.add_attribute("name_collection", config.name_collection)
.add_attribute("update_interval", config.update_interval.to_string());
Ok(Response::new().add_event(event))
}

0 comments on commit 171c41e

Please sign in to comment.