@@ -3,8 +3,8 @@ use std::env;
3
3
#[ cfg( not( feature = "library" ) ) ]
4
4
use cosmwasm_std:: entry_point;
5
5
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 ,
8
8
} ;
9
9
use cw2:: set_contract_version;
10
10
use cw721:: { AllNftInfoResponse , TokensResponse } ;
@@ -51,7 +51,7 @@ pub fn instantiate(
51
51
admin : Some ( String :: from ( info. sender ) ) ,
52
52
code_id : msg. collection_code_id ,
53
53
label : String :: from ( "vip-collection" ) ,
54
- msg : to_binary ( & cw721_base:: InstantiateMsg {
54
+ msg : to_json_binary ( & cw721_base:: InstantiateMsg {
55
55
name : "Stargaze VIP Collection" . to_string ( ) ,
56
56
symbol : "SGVIP" . to_string ( ) ,
57
57
minter : minter. to_string ( ) ,
@@ -76,7 +76,7 @@ pub fn execute(
76
76
) -> Result < Response , ContractError > {
77
77
match msg {
78
78
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 ) ,
80
80
ExecuteMsg :: Pause { } => execute_pause ( deps, info) ,
81
81
ExecuteMsg :: Resume { } => execute_resume ( deps, info) ,
82
82
ExecuteMsg :: UpdateTiers { tiers } => execute_update_tiers ( deps, info, tiers) ,
@@ -94,7 +94,7 @@ pub fn execute_mint(
94
94
let vip_collection = COLLECTION . load ( deps. storage ) ?;
95
95
96
96
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 ( ) ;
98
98
TOKEN_UPDATE_HEIGHT . update ( deps. storage , token_id, |_| -> StdResult < _ > {
99
99
Ok ( env. block . height )
100
100
} ) ?;
@@ -106,17 +106,26 @@ pub fn execute_update(
106
106
mut deps : DepsMut ,
107
107
env : Env ,
108
108
_info : MessageInfo ,
109
- token_id : u64 ,
109
+ address : String ,
110
110
) -> Result < Response , ContractError > {
111
111
ensure ! ( !PAUSED . load( deps. storage) ?, ContractError :: Paused { } ) ;
112
+ deps. api . addr_validate ( & address) ?;
113
+
112
114
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 { } ) ?;
113
117
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 ( ) ) ?;
115
119
if last_update_height. is_none ( ) {
116
120
return Err ( ContractError :: TokenNotFound { } ) ;
117
121
}
118
122
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
+ ) ?;
120
129
121
130
TOKEN_UPDATE_HEIGHT . update ( deps. storage , token_id, |_| -> StdResult < _ > {
122
131
Ok ( env. block . height )
@@ -167,7 +176,7 @@ pub fn mint(
167
176
168
177
Ok ( WasmMsg :: Execute {
169
178
contract_addr : vip_collection. to_string ( ) ,
170
- msg : to_binary ( & msg) ?,
179
+ msg : to_json_binary ( & msg) ?,
171
180
funds : vec ! [ ] ,
172
181
} )
173
182
}
@@ -176,12 +185,12 @@ pub fn update(
176
185
mut deps : DepsMut ,
177
186
block_time : Timestamp ,
178
187
vip_collection : Addr ,
179
- token_id : u64 ,
188
+ token_id : String ,
180
189
) -> Result < WasmMsg , ContractError > {
181
190
let all_nft_info_response: AllNftInfoResponse < Metadata > = deps. querier . query_wasm_smart (
182
191
vip_collection. clone ( ) ,
183
192
& cw721_base:: msg:: QueryMsg :: < AllNftInfoResponse < Metadata > > :: AllNftInfo {
184
- token_id : token_id. to_string ( ) ,
193
+ token_id : token_id. clone ( ) ,
185
194
include_expired : None ,
186
195
} ,
187
196
) ?;
@@ -198,7 +207,7 @@ pub fn update(
198
207
let token_uri = Some ( format ! ( "{}/{}" , base_uri, index) ) ;
199
208
200
209
let msg = stargaze_vip_collection:: ExecuteMsg :: Mint {
201
- token_id : token_id . to_string ( ) ,
210
+ token_id,
202
211
owner,
203
212
token_uri,
204
213
extension : stargaze_vip_collection:: state:: Metadata {
@@ -210,7 +219,7 @@ pub fn update(
210
219
211
220
Ok ( WasmMsg :: Execute {
212
221
contract_addr : vip_collection. to_string ( ) ,
213
- msg : to_binary ( & msg) ?,
222
+ msg : to_json_binary ( & msg) ?,
214
223
funds : vec ! [ ] ,
215
224
} )
216
225
}
@@ -293,37 +302,26 @@ fn check_tier_order(tiers: &[Uint128]) -> StdResult<()> {
293
302
#[ cfg_attr( not( feature = "library" ) , entry_point) ]
294
303
pub fn query ( deps : Deps , _env : Env , msg : QueryMsg ) -> StdResult < Binary > {
295
304
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 ) ?) ,
298
307
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) ?)
300
309
}
301
310
QueryMsg :: Tier { address } => {
302
311
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) ?;
311
313
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 {
314
316
tier : None ,
315
317
last_update_time : None ,
316
318
} ) ?) ;
317
319
}
318
- let token_id = tokens_response
319
- . tokens
320
- . first ( )
321
- . ok_or_else ( || StdError :: generic_err ( "No token found for address" ) ) ?;
322
320
323
321
let token_info: cw721:: NftInfoResponse < Metadata > = deps. querier . query_wasm_smart (
324
322
COLLECTION . load ( deps. storage ) ?,
325
323
& cw721:: Cw721QueryMsg :: NftInfo {
326
- token_id : token_id. to_string ( ) ,
324
+ token_id : token_id. unwrap ( ) ,
327
325
} ,
328
326
) ?;
329
327
let staked_amount = token_info. extension . staked_amount ;
@@ -334,17 +332,35 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
334
332
. position ( |& x| x >= staked_amount)
335
333
. unwrap_or ( tiers. len ( ) ) ;
336
334
337
- Ok ( to_binary ( & TierResponse {
335
+ Ok ( to_json_binary ( & TierResponse {
338
336
tier : Some ( index as u64 ) ,
339
337
last_update_time : Some ( last_update_time) ,
340
338
} ) ?)
341
339
}
342
340
QueryMsg :: Tiers { } => {
343
341
let tiers = TIERS . load ( deps. storage ) ?;
344
- Ok ( to_binary ( & tiers) ?)
342
+ Ok ( to_json_binary ( & tiers) ?)
345
343
}
346
344
}
347
345
}
348
346
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
+
349
365
#[ cfg( test) ]
350
366
mod tests { }
0 commit comments