Skip to content

Commit e461ad9

Browse files
committed
Perform updates with an address instead of token_id
1 parent a72a170 commit e461ad9

File tree

5 files changed

+57
-44
lines changed

5 files changed

+57
-44
lines changed

contracts/fair-burn/src/contract.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use cosmwasm_std::{
2-
ensure, to_binary, Addr, BankMsg, Binary, Coin, Deps, DepsMut, Empty, Env, Event, MessageInfo,
3-
StdResult,
4-
};
1+
use cosmwasm_std::{ensure, to_json_binary, Addr, BankMsg, Binary, Coin, Deps, DepsMut, Empty, Env, Event, MessageInfo, StdResult};
52
use cw2::{get_contract_version, set_contract_version};
63
use cw_utils::{maybe_addr, NativeBalance};
74
use sg_std::{create_fund_fairburn_pool_msg, Response, NATIVE_DENOM};
@@ -153,8 +150,8 @@ pub fn execute_fair_burn(
153150
#[cfg_attr(not(feature = "library"), entry_point)]
154151
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
155152
match msg {
156-
QueryMsg::ContractVersion {} => to_binary(&get_contract_version(deps.storage)?),
157-
QueryMsg::Config {} => to_binary(&CONFIG.load(deps.storage)?),
153+
QueryMsg::ContractVersion {} => to_json_binary(&get_contract_version(deps.storage)?),
154+
QueryMsg::Config {} => to_json_binary(&CONFIG.load(deps.storage)?),
158155
}
159156
}
160157

contracts/fair-burn/src/helpers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use cosmwasm_std::{coin, to_binary, Addr, Coin, Decimal, Uint128, WasmMsg};
1+
use cosmwasm_std::{coin, to_json_binary, Addr, Coin, Decimal, Uint128, WasmMsg};
22
use sg_std::Response;
33

44
use crate::{msg::ExecuteMsg, state::Config};
@@ -38,7 +38,7 @@ pub fn append_fair_burn_msg(
3838
) -> Response {
3939
response.add_message(WasmMsg::Execute {
4040
contract_addr: fair_burn_addr.to_string(),
41-
msg: to_binary(&ExecuteMsg::FairBurn {
41+
msg: to_json_binary(&ExecuteMsg::FairBurn {
4242
recipient: recipient.map(|r| r.to_string()),
4343
})
4444
.unwrap(),

contracts/vip/minter/src/contract.rs

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::env;
33
#[cfg(not(feature = "library"))]
44
use cosmwasm_std::entry_point;
55
use cosmwasm_std::{
6-
ensure, instantiate2_address, to_binary, Addr, Binary, CodeInfoResponse, Deps, DepsMut, Env,
7-
Event, MessageInfo, Response, StdError, StdResult, Timestamp, Uint128, WasmMsg,
6+
ensure, instantiate2_address, to_json_binary, Addr, Binary, CodeInfoResponse, Deps, DepsMut,
7+
Env, Event, MessageInfo, Response, StdError, StdResult, Timestamp, Uint128, WasmMsg,
88
};
99
use cw2::set_contract_version;
1010
use cw721::{AllNftInfoResponse, TokensResponse};
@@ -51,7 +51,7 @@ pub fn instantiate(
5151
admin: Some(String::from(info.sender)),
5252
code_id: msg.collection_code_id,
5353
label: String::from("vip-collection"),
54-
msg: to_binary(&cw721_base::InstantiateMsg {
54+
msg: to_json_binary(&cw721_base::InstantiateMsg {
5555
name: "Stargaze VIP Collection".to_string(),
5656
symbol: "SGVIP".to_string(),
5757
minter: minter.to_string(),
@@ -76,7 +76,7 @@ pub fn execute(
7676
) -> Result<Response, ContractError> {
7777
match msg {
7878
ExecuteMsg::Mint {} => execute_mint(deps, env, info),
79-
ExecuteMsg::Update { token_id } => execute_update(deps, env, info, token_id),
79+
ExecuteMsg::Update { address } => execute_update(deps, env, info, address),
8080
ExecuteMsg::Pause {} => execute_pause(deps, info),
8181
ExecuteMsg::Resume {} => execute_resume(deps, info),
8282
ExecuteMsg::UpdateTiers { tiers } => execute_update_tiers(deps, info, tiers),
@@ -94,7 +94,7 @@ pub fn execute_mint(
9494
let vip_collection = COLLECTION.load(deps.storage)?;
9595

9696
let mint_msg = mint(deps.branch(), info.sender, env.block.time, vip_collection)?;
97-
let token_id = TOKEN_INDEX.load(deps.storage)?;
97+
let token_id = TOKEN_INDEX.load(deps.storage)?.to_string();
9898
TOKEN_UPDATE_HEIGHT.update(deps.storage, token_id, |_| -> StdResult<_> {
9999
Ok(env.block.height)
100100
})?;
@@ -106,17 +106,26 @@ pub fn execute_update(
106106
mut deps: DepsMut,
107107
env: Env,
108108
_info: MessageInfo,
109-
token_id: u64,
109+
address: String,
110110
) -> Result<Response, ContractError> {
111111
ensure!(!PAUSED.load(deps.storage)?, ContractError::Paused {});
112+
deps.api.addr_validate(&address)?;
113+
112114
let vip_collection = COLLECTION.load(deps.storage)?;
115+
let token_id = fetch_token_id_for_address(deps.as_ref(), address.to_string())?
116+
.ok_or(ContractError::TokenNotFound {})?;
113117

114-
let last_update_height = TOKEN_UPDATE_HEIGHT.may_load(deps.storage, token_id)?;
118+
let last_update_height = TOKEN_UPDATE_HEIGHT.may_load(deps.storage, token_id.clone())?;
115119
if last_update_height.is_none() {
116120
return Err(ContractError::TokenNotFound {});
117121
}
118122

119-
let mint_msg = update(deps.branch(), env.block.time, vip_collection, token_id)?;
123+
let mint_msg = update(
124+
deps.branch(),
125+
env.block.time,
126+
vip_collection,
127+
token_id.clone(),
128+
)?;
120129

121130
TOKEN_UPDATE_HEIGHT.update(deps.storage, token_id, |_| -> StdResult<_> {
122131
Ok(env.block.height)
@@ -167,7 +176,7 @@ pub fn mint(
167176

168177
Ok(WasmMsg::Execute {
169178
contract_addr: vip_collection.to_string(),
170-
msg: to_binary(&msg)?,
179+
msg: to_json_binary(&msg)?,
171180
funds: vec![],
172181
})
173182
}
@@ -176,12 +185,12 @@ pub fn update(
176185
mut deps: DepsMut,
177186
block_time: Timestamp,
178187
vip_collection: Addr,
179-
token_id: u64,
188+
token_id: String,
180189
) -> Result<WasmMsg, ContractError> {
181190
let all_nft_info_response: AllNftInfoResponse<Metadata> = deps.querier.query_wasm_smart(
182191
vip_collection.clone(),
183192
&cw721_base::msg::QueryMsg::<AllNftInfoResponse<Metadata>>::AllNftInfo {
184-
token_id: token_id.to_string(),
193+
token_id: token_id.clone(),
185194
include_expired: None,
186195
},
187196
)?;
@@ -198,7 +207,7 @@ pub fn update(
198207
let token_uri = Some(format!("{}/{}", base_uri, index));
199208

200209
let msg = stargaze_vip_collection::ExecuteMsg::Mint {
201-
token_id: token_id.to_string(),
210+
token_id,
202211
owner,
203212
token_uri,
204213
extension: stargaze_vip_collection::state::Metadata {
@@ -210,7 +219,7 @@ pub fn update(
210219

211220
Ok(WasmMsg::Execute {
212221
contract_addr: vip_collection.to_string(),
213-
msg: to_binary(&msg)?,
222+
msg: to_json_binary(&msg)?,
214223
funds: vec![],
215224
})
216225
}
@@ -293,37 +302,26 @@ fn check_tier_order(tiers: &[Uint128]) -> StdResult<()> {
293302
#[cfg_attr(not(feature = "library"), entry_point)]
294303
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
295304
match msg {
296-
QueryMsg::Collection {} => to_binary(&COLLECTION.load(deps.storage)?.to_string()),
297-
QueryMsg::IsPaused {} => to_binary(&PAUSED.load(deps.storage)?),
305+
QueryMsg::Collection {} => to_json_binary(&COLLECTION.load(deps.storage)?.to_string()),
306+
QueryMsg::IsPaused {} => to_json_binary(&PAUSED.load(deps.storage)?),
298307
QueryMsg::TokenUpdateHeight { token_id } => {
299-
to_binary(&TOKEN_UPDATE_HEIGHT.load(deps.storage, token_id)?)
308+
to_json_binary(&TOKEN_UPDATE_HEIGHT.load(deps.storage, token_id)?)
300309
}
301310
QueryMsg::Tier { address } => {
302311
let tiers = TIERS.load(deps.storage)?;
303-
let tokens_response: cw721::TokensResponse = deps.querier.query_wasm_smart(
304-
COLLECTION.load(deps.storage)?,
305-
&cw721::Cw721QueryMsg::Tokens {
306-
owner: address,
307-
start_after: None,
308-
limit: None,
309-
},
310-
)?;
312+
let token_id = fetch_token_id_for_address(deps, address)?;
311313

312-
if tokens_response.tokens.is_empty() {
313-
return Ok(to_binary(&TierResponse {
314+
if token_id.is_none() {
315+
return Ok(to_json_binary(&TierResponse {
314316
tier: None,
315317
last_update_time: None,
316318
})?);
317319
}
318-
let token_id = tokens_response
319-
.tokens
320-
.first()
321-
.ok_or_else(|| StdError::generic_err("No token found for address"))?;
322320

323321
let token_info: cw721::NftInfoResponse<Metadata> = deps.querier.query_wasm_smart(
324322
COLLECTION.load(deps.storage)?,
325323
&cw721::Cw721QueryMsg::NftInfo {
326-
token_id: token_id.to_string(),
324+
token_id: token_id.unwrap(),
327325
},
328326
)?;
329327
let staked_amount = token_info.extension.staked_amount;
@@ -334,17 +332,35 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
334332
.position(|&x| x >= staked_amount)
335333
.unwrap_or(tiers.len());
336334

337-
Ok(to_binary(&TierResponse {
335+
Ok(to_json_binary(&TierResponse {
338336
tier: Some(index as u64),
339337
last_update_time: Some(last_update_time),
340338
})?)
341339
}
342340
QueryMsg::Tiers {} => {
343341
let tiers = TIERS.load(deps.storage)?;
344-
Ok(to_binary(&tiers)?)
342+
Ok(to_json_binary(&tiers)?)
345343
}
346344
}
347345
}
348346

347+
pub fn fetch_token_id_for_address(deps: Deps, address: String) -> StdResult<Option<String>> {
348+
let tokens_response: cw721::TokensResponse = deps.querier.query_wasm_smart(
349+
COLLECTION.load(deps.storage)?,
350+
&cw721::Cw721QueryMsg::Tokens {
351+
owner: address,
352+
start_after: None,
353+
limit: None,
354+
},
355+
)?;
356+
357+
if tokens_response.tokens.is_empty() {
358+
return Ok(None);
359+
}
360+
let token_id = tokens_response.tokens.first().map(|id| id.to_string());
361+
362+
Ok(token_id)
363+
}
364+
349365
#[cfg(test)]
350366
mod tests {}

contracts/vip/minter/src/msg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub enum ExecuteMsg {
1414
Mint {},
1515
/// Update the stake amount for the given name
1616
Update {
17-
token_id: u64,
17+
address: String,
1818
},
1919
/// So we can pause before migrating names, etc.
2020
Pause {},
@@ -50,7 +50,7 @@ pub enum QueryMsg {
5050
#[returns(bool)]
5151
IsPaused {},
5252
#[returns(u64)]
53-
TokenUpdateHeight { token_id: u64 },
53+
TokenUpdateHeight { token_id: String },
5454
#[returns(TierResponse)]
5555
Tier { address: String },
5656
#[returns(Vec<Uint128>)]

contracts/vip/minter/src/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub const PAUSED: Item<bool> = Item::new("paused");
77
pub const BASE_URI: Item<String> = Item::new("base_uri");
88

99
/// (name, block_height)
10-
pub const TOKEN_UPDATE_HEIGHT: Map<u64, u64> = Map::new("tuh");
10+
pub const TOKEN_UPDATE_HEIGHT: Map<String, u64> = Map::new("tuh");
1111

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

0 commit comments

Comments
 (0)