-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathcontract.rs
1013 lines (910 loc) · 33.3 KB
/
contract.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use crate::error::ContractError;
use crate::msg::{
ConfigResponse, ExecuteMsg, MintCountResponse, MintTokensResponse, MintableNumTokensResponse,
QueryMsg, ReceiveNftMsg, StartTimeResponse,
};
use crate::state::{
Config, ConfigExtension, CONFIG, MINTABLE_NUM_TOKENS, MINTABLE_TOKEN_POSITIONS, MINTER_ADDRS,
RECEIVED_TOKENS, SG721_ADDRESS, STATUS,
};
use crate::validation::{check_dynamic_per_address_limit, get_three_percent_of_tokens};
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
coin, ensure, from_json, to_json_binary, Addr, Binary, Coin, CosmosMsg, Decimal, Deps, DepsMut,
Empty, Env, Event, MessageInfo, Order, Reply, ReplyOn, Response, StdError, StdResult, SubMsg,
Timestamp, Uint128, WasmMsg,
};
use cw2::set_contract_version;
use cw721::Cw721ReceiveMsg;
use cw721_base::Extension;
use cw_utils::{may_pay, nonpayable, parse_reply_instantiate_data};
use nois::{int_in_range, shuffle};
use semver::Version;
use sg1::{distribute_mint_fees, transfer_funds_to_launchpad_dao};
use sg4::{Status, StatusResponse, SudoMsg};
use sg721::{ExecuteMsg as Sg721ExecuteMsg, InstantiateMsg as Sg721InstantiateMsg};
use sg_utils::{GENESIS_MINT_START_TIME, NATIVE_DENOM};
use sha2::{Digest, Sha256};
use std::convert::TryInto;
use token_merge_factory::msg::QueryMsg as FactoryQueryMsg;
use token_merge_factory::msg::{MintToken, ParamsResponse, TokenMergeMinterCreateMsg};
use url::Url;
pub struct TokenPositionMapping {
pub position: u32,
pub token_id: u32,
}
// version info for migration info
const CONTRACT_NAME: &str = "crates.io:sg-minter";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
const INSTANTIATE_SG721_REPLY_ID: u64 = 1;
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: TokenMergeMinterCreateMsg,
) -> Result<Response, ContractError> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
let factory = info.sender.clone();
// Make sure the sender is the factory contract
// This will fail if the sender cannot parse a response from the factory contract
let factory_response: ParamsResponse = deps
.querier
.query_wasm_smart(factory.clone(), &FactoryQueryMsg::Params {})?;
let factory_params = factory_response.params;
// set default status so it can be queried without failing
STATUS.save(deps.storage, &Status::default())?;
if !check_dynamic_per_address_limit(
msg.init_msg.per_address_limit,
msg.init_msg.num_tokens,
factory_params.max_per_address_limit,
)? {
return Err(ContractError::InvalidPerAddressLimit {
max: display_max_mintable_tokens(
msg.init_msg.per_address_limit,
msg.init_msg.num_tokens,
factory_params.max_per_address_limit,
)?,
min: 1,
got: msg.init_msg.per_address_limit,
});
}
// sanitize base token uri
let mut base_token_uri = msg.init_msg.base_token_uri.trim().to_string();
// Token URI must be a valid URL (ipfs, https, etc.)
let parsed_token_uri =
Url::parse(&base_token_uri).map_err(|_| ContractError::InvalidBaseTokenURI {})?;
base_token_uri = parsed_token_uri.to_string();
let genesis_time = Timestamp::from_nanos(GENESIS_MINT_START_TIME);
// If start time is before genesis time return error
if msg.init_msg.start_time < genesis_time {
return Err(ContractError::BeforeGenesisTime {});
}
// If current time is beyond the provided start time return error
if env.block.time > msg.init_msg.start_time {
return Err(ContractError::InvalidStartTime(
msg.init_msg.start_time,
env.block.time,
));
}
// Use default start trading time if not provided
let mut collection_info = msg.collection_params.info.clone();
let offset = factory_params.max_trading_offset_secs;
let default_start_time_with_offset = msg.init_msg.start_time.plus_seconds(offset);
if let Some(start_trading_time) = msg.collection_params.info.start_trading_time {
// If trading start time > start_time + offset, return error
if start_trading_time > default_start_time_with_offset {
return Err(ContractError::InvalidStartTradingTime(
start_trading_time,
default_start_time_with_offset,
));
}
}
let start_trading_time = msg
.collection_params
.info
.start_trading_time
.or(Some(default_start_time_with_offset));
collection_info.start_trading_time = start_trading_time;
let config = Config {
factory: factory.clone(),
collection_code_id: msg.collection_params.code_id,
extension: ConfigExtension {
admin: deps
.api
.addr_validate(&msg.collection_params.info.creator)?,
base_token_uri,
num_tokens: msg.init_msg.num_tokens,
per_address_limit: msg.init_msg.per_address_limit,
start_time: msg.init_msg.start_time,
mint_tokens: msg.init_msg.mint_tokens,
},
};
CONFIG.save(deps.storage, &config)?;
MINTABLE_NUM_TOKENS.save(deps.storage, &msg.init_msg.num_tokens)?;
let token_ids = random_token_list(
&env,
deps.api
.addr_validate(&msg.collection_params.info.creator)?,
(1..=msg.init_msg.num_tokens).collect::<Vec<u32>>(),
)?;
// Save mintable token ids map
let mut token_position = 1;
for token_id in token_ids {
MINTABLE_TOKEN_POSITIONS.save(deps.storage, token_position, &token_id)?;
token_position += 1;
}
// Submessage to instantiate sg721 contract
let submsg = SubMsg {
msg: WasmMsg::Instantiate {
code_id: msg.collection_params.code_id,
msg: to_json_binary(&Sg721InstantiateMsg {
name: msg.collection_params.name.clone(),
symbol: msg.collection_params.symbol,
minter: env.contract.address.to_string(),
collection_info,
})?,
funds: info.funds,
admin: Some(config.extension.admin.to_string()),
label: format!("SG721-{}", msg.collection_params.name.trim()),
}
.into(),
id: INSTANTIATE_SG721_REPLY_ID,
gas_limit: None,
reply_on: ReplyOn::Success,
};
Ok(Response::new()
.add_attribute("action", "instantiate")
.add_attribute("contract_name", CONTRACT_NAME)
.add_attribute("contract_version", CONTRACT_VERSION)
.add_attribute("sender", factory)
.add_submessage(submsg))
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::ReceiveNft(Cw721ReceiveMsg {
sender,
token_id,
msg: raw_msg,
}) => {
let msg: ReceiveNftMsg = from_json(raw_msg)?;
match msg {
ReceiveNftMsg::DepositToken { recipient } => {
execute_receive_nft(deps, env, info, sender, token_id, recipient)
}
}
}
ExecuteMsg::Purge {} => execute_purge(deps, env, info),
ExecuteMsg::UpdateStartTime(time) => execute_update_start_time(deps, env, info, time),
ExecuteMsg::UpdateStartTradingTime(time) => {
execute_update_start_trading_time(deps, env, info, time)
}
ExecuteMsg::UpdatePerAddressLimit { per_address_limit } => {
execute_update_per_address_limit(deps, env, info, per_address_limit)
}
ExecuteMsg::MintTo { recipient } => execute_mint_to(deps, env, info, recipient),
ExecuteMsg::MintFor {
token_id,
recipient,
} => execute_mint_for(deps, env, info, token_id, recipient),
ExecuteMsg::Shuffle {} => execute_shuffle(deps, env, info),
ExecuteMsg::BurnRemaining {} => execute_burn_remaining(deps, env, info),
}
}
pub fn execute_receive_nft(
deps: DepsMut,
env: Env,
info: MessageInfo,
sender: String,
token_id: String,
recipient: Option<String>,
) -> Result<Response, ContractError> {
let config = CONFIG.load(deps.storage)?;
let mut action = "receive_and_burn_nft";
ensure!(
env.block.time > config.extension.start_time,
ContractError::BeforeMintStartTime {}
);
let recipient_addr = deps
.api
.addr_validate(&recipient.unwrap_or(sender.clone()))?;
let mint_count = mint_count(deps.as_ref(), recipient_addr.clone())?;
if mint_count >= config.extension.per_address_limit {
return Err(ContractError::MaxPerAddressLimitExceeded {});
}
// Check received token is from an expected collection
let valid_mint_token = config
.extension
.mint_tokens
.iter()
.find(|token| token.collection == info.sender);
ensure!(
valid_mint_token.is_some(),
ContractError::InvalidCollection {}
);
let already_received_amount = RECEIVED_TOKENS
.load(deps.storage, (&recipient_addr, info.sender.to_string()))
.unwrap_or(0);
ensure!(
already_received_amount < valid_mint_token.unwrap().amount,
ContractError::TooManyTokens {}
);
RECEIVED_TOKENS.save(
deps.storage,
(&recipient_addr, info.sender.to_string()),
&(already_received_amount + 1),
)?;
let mint_requirement_fulfilled = check_all_mint_tokens_received(
deps.as_ref(),
recipient_addr.clone(),
config.extension.mint_tokens,
)?;
// Create the burn message for the received token
let burn_msg = Sg721ExecuteMsg::<Extension, Empty>::Burn {
token_id: token_id.clone(),
};
let burn_cosmos_msg = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: info.sender.to_string(),
msg: to_json_binary(&burn_msg)?,
funds: vec![],
});
if !mint_requirement_fulfilled {
return Ok(Response::new()
.add_message(burn_cosmos_msg)
.add_attribute("action", action)
.add_attribute("sender", sender)
.add_attribute("collection", info.sender.to_string())
.add_attribute("token_id", token_id));
}
action = "mint_sender";
_execute_mint(
deps,
env,
info,
action,
false,
Some(recipient_addr),
None,
Some(burn_cosmos_msg),
)
}
// Purge frees data after a mint is sold out
// Anyone can purge
pub fn execute_purge(
deps: DepsMut,
env: Env,
info: MessageInfo,
) -> Result<Response, ContractError> {
nonpayable(&info)?;
// check mint sold out
let mintable_num_tokens = MINTABLE_NUM_TOKENS.load(deps.storage)?;
if mintable_num_tokens != 0 {
return Err(ContractError::NotSoldOut {});
}
let keys = MINTER_ADDRS
.keys(deps.storage, None, None, Order::Ascending)
.collect::<Vec<_>>();
for key in keys {
MINTER_ADDRS.remove(deps.storage, &key?);
}
Ok(Response::new()
.add_attribute("action", "purge")
.add_attribute("contract", env.contract.address.to_string())
.add_attribute("sender", info.sender))
}
// Anyone can pay to shuffle at any time
// Introduces another source of randomness to minting
// There's a fee because this action is expensive.
pub fn execute_shuffle(
deps: DepsMut,
env: Env,
info: MessageInfo,
) -> Result<Response, ContractError> {
let mut res = Response::new();
let config = CONFIG.load(deps.storage)?;
let factory: ParamsResponse = deps
.querier
.query_wasm_smart(config.factory, &FactoryQueryMsg::Params {})?;
let factory_params = factory.params;
transfer_funds_to_launchpad_dao(
&info,
factory_params.shuffle_fee.amount.u128(),
NATIVE_DENOM,
&mut res,
)?;
// Check not sold out
let mintable_num_tokens = MINTABLE_NUM_TOKENS.load(deps.storage)?;
if mintable_num_tokens == 0 {
return Err(ContractError::SoldOut {});
}
// get positions and token_ids, then randomize token_ids and reassign positions
let mut positions = vec![];
let mut token_ids = vec![];
for mapping in MINTABLE_TOKEN_POSITIONS.range(deps.storage, None, None, Order::Ascending) {
let (position, token_id) = mapping?;
positions.push(position);
token_ids.push(token_id);
}
let randomized_token_ids = random_token_list(&env, info.sender.clone(), token_ids.clone())?;
for (i, position) in positions.iter().enumerate() {
MINTABLE_TOKEN_POSITIONS.save(deps.storage, *position, &randomized_token_ids[i])?;
}
Ok(res
.add_attribute("action", "shuffle")
.add_attribute("sender", info.sender))
}
pub fn execute_mint_to(
deps: DepsMut,
env: Env,
info: MessageInfo,
recipient: String,
) -> Result<Response, ContractError> {
let recipient = deps.api.addr_validate(&recipient)?;
let config = CONFIG.load(deps.storage)?;
let action = "mint_to";
// Check only admin
if info.sender != config.extension.admin {
return Err(ContractError::Unauthorized(
"Sender is not an admin".to_owned(),
));
}
_execute_mint(deps, env, info, action, true, Some(recipient), None, None)
}
pub fn execute_mint_for(
deps: DepsMut,
env: Env,
info: MessageInfo,
token_id: u32,
recipient: String,
) -> Result<Response, ContractError> {
let recipient = deps.api.addr_validate(&recipient)?;
let config = CONFIG.load(deps.storage)?;
let action = "mint_for";
// Check only admin
if info.sender != config.extension.admin {
return Err(ContractError::Unauthorized(
"Sender is not an admin".to_owned(),
));
}
_execute_mint(
deps,
env,
info,
action,
true,
Some(recipient),
Some(token_id),
None,
)
}
fn check_all_mint_tokens_received(
deps: Deps,
sender: Addr,
mint_tokens: Vec<MintToken>,
) -> Result<bool, ContractError> {
for mint_token in mint_tokens {
let received_amount = RECEIVED_TOKENS
.load(deps.storage, (&sender, mint_token.collection.clone()))
.unwrap_or(0);
if received_amount < mint_token.amount {
return Ok(false);
}
}
Ok(true)
}
// Generalize checks and mint message creation
// ReceiveNFT -> _execute_mint(recipient: None, token_id: None)
// mint_to(recipient: "friend") -> _execute_mint(Some(recipient), token_id: None)
// mint_for(recipient: "friend2", token_id: 420) -> _execute_mint(recipient, token_id)
#[allow(clippy::too_many_arguments)]
fn _execute_mint(
deps: DepsMut,
env: Env,
info: MessageInfo,
action: &str,
is_admin: bool,
recipient: Option<Addr>,
token_id: Option<u32>,
burn_message: Option<CosmosMsg>,
) -> Result<Response, ContractError> {
let mut network_fee = Uint128::zero();
let mintable_num_tokens = MINTABLE_NUM_TOKENS.load(deps.storage)?;
if mintable_num_tokens == 0 {
return Err(ContractError::SoldOut {});
}
let config = CONFIG.load(deps.storage)?;
if let Some(token_id) = token_id {
if token_id == 0 || token_id > config.extension.num_tokens {
return Err(ContractError::InvalidTokenId {});
}
}
let sg721_address = SG721_ADDRESS.load(deps.storage)?;
let recipient_addr = match recipient {
Some(some_recipient) => some_recipient,
None => info.sender.clone(),
};
let factory: ParamsResponse = deps
.querier
.query_wasm_smart(config.factory, &FactoryQueryMsg::Params {})?;
let factory_params = factory.params;
if is_admin {
let airdrop_price: Coin = coin(
factory_params.airdrop_mint_price.amount.u128(),
factory_params.airdrop_mint_price.denom.clone(),
);
// Exact payment only accepted
let payment = may_pay(&info, &airdrop_price.denom)?;
if payment != airdrop_price.amount {
return Err(ContractError::IncorrectPaymentAmount(
coin(
payment.u128(),
factory_params.airdrop_mint_price.denom.clone(),
),
factory_params.airdrop_mint_price,
));
}
let airdrop_fee_bps = Decimal::bps(factory_params.airdrop_mint_fee_bps);
network_fee = airdrop_price.amount * airdrop_fee_bps;
}
let mut res = Response::new();
if !network_fee.is_zero() {
distribute_mint_fees(
coin(
network_fee.u128(),
factory_params.airdrop_mint_price.clone().denom,
),
&mut res,
false,
None,
)?;
}
let mintable_token_mapping = match token_id {
Some(token_id) => {
// set position to invalid value, iterate to find matching token_id
// if token_id not found, token_id is already sold, position is unchanged and throw err
// otherwise return position and token_id
let mut position = 0;
for res in MINTABLE_TOKEN_POSITIONS.range(deps.storage, None, None, Order::Ascending) {
let (pos, id) = res?;
if id == token_id {
position = pos;
break;
}
}
if position == 0 {
return Err(ContractError::TokenIdAlreadySold { token_id });
}
TokenPositionMapping { position, token_id }
}
None => random_mintable_token_mapping(deps.as_ref(), env, info.sender.clone())?,
};
// Create mint msgs
let mint_msg = Sg721ExecuteMsg::<Extension, Empty>::Mint {
token_id: mintable_token_mapping.token_id.to_string(),
owner: recipient_addr.to_string(),
token_uri: Some(format!(
"{}/{}",
config.extension.base_token_uri, mintable_token_mapping.token_id
)),
extension: None,
};
let msg = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: sg721_address.to_string(),
msg: to_json_binary(&mint_msg)?,
funds: vec![],
});
res = res.add_message(msg);
// Burn the final token received
if let Some(burn_message) = burn_message {
res = res.add_message(burn_message);
// Clear received tokens record for recipient
for mint_token in config.extension.mint_tokens.iter() {
RECEIVED_TOKENS.remove(
deps.storage,
(&recipient_addr, mint_token.collection.clone()),
);
}
}
// Remove mintable token position from map
MINTABLE_TOKEN_POSITIONS.remove(deps.storage, mintable_token_mapping.position);
let mintable_num_tokens = MINTABLE_NUM_TOKENS.load(deps.storage)?;
// Decrement mintable num tokens
MINTABLE_NUM_TOKENS.save(deps.storage, &(mintable_num_tokens - 1))?;
// Save the new mint count for the recipient's address
let new_mint_count = mint_count(deps.as_ref(), recipient_addr.clone())? + 1;
MINTER_ADDRS.save(deps.storage, &recipient_addr, &new_mint_count)?;
Ok(res
.add_attribute("action", action)
.add_attribute("recipient", recipient_addr)
.add_attribute("token_id", mintable_token_mapping.token_id.to_string())
.add_attribute(
"network_fee",
coin(network_fee.u128(), factory_params.airdrop_mint_price.denom).to_string(),
))
}
fn random_token_list(
env: &Env,
sender: Addr,
mut tokens: Vec<u32>,
) -> Result<Vec<u32>, ContractError> {
let tx_index = if let Some(tx) = &env.transaction {
tx.index
} else {
0
};
let sha256 = Sha256::digest(
format!("{}{}{}{}", sender, env.block.height, tokens.len(), tx_index).into_bytes(),
);
let randomness: [u8; 32] = sha256.to_vec().try_into().unwrap();
tokens = shuffle(randomness, tokens);
Ok(tokens)
}
// Does a baby shuffle, picking a token_id from the first or last 50 mintable positions.
fn random_mintable_token_mapping(
deps: Deps,
env: Env,
sender: Addr,
) -> Result<TokenPositionMapping, ContractError> {
let num_tokens = MINTABLE_NUM_TOKENS.load(deps.storage)?;
let tx_index = if let Some(tx) = &env.transaction {
tx.index
} else {
0
};
let sha256 = Sha256::digest(
format!("{}{}{}{}", sender, num_tokens, env.block.height, tx_index).into_bytes(),
);
let randomness: [u8; 32] = sha256.to_vec().try_into().unwrap();
let r = int_in_range(randomness, 0, 50);
let order = match r % 2 {
1 => Order::Descending,
_ => Order::Ascending,
};
let mut rem = 50;
if rem > num_tokens {
rem = num_tokens;
}
let n = r % rem;
let position = MINTABLE_TOKEN_POSITIONS
.keys(deps.storage, None, None, order)
.skip(n as usize)
.take(1)
.collect::<StdResult<Vec<_>>>()?[0];
let token_id = MINTABLE_TOKEN_POSITIONS.load(deps.storage, position)?;
Ok(TokenPositionMapping { position, token_id })
}
pub fn execute_update_start_time(
deps: DepsMut,
env: Env,
info: MessageInfo,
start_time: Timestamp,
) -> Result<Response, ContractError> {
nonpayable(&info)?;
let mut config = CONFIG.load(deps.storage)?;
if info.sender != config.extension.admin {
return Err(ContractError::Unauthorized(
"Sender is not an admin".to_owned(),
));
}
// If current time is after the stored start time return error
if env.block.time >= config.extension.start_time {
return Err(ContractError::AlreadyStarted {});
}
// If current time already passed the new start_time return error
if env.block.time > start_time {
return Err(ContractError::InvalidStartTime(start_time, env.block.time));
}
let genesis_start_time = Timestamp::from_nanos(GENESIS_MINT_START_TIME);
// If the new start_time is before genesis start time return error
if start_time < genesis_start_time {
return Err(ContractError::BeforeGenesisTime {});
}
config.extension.start_time = start_time;
CONFIG.save(deps.storage, &config)?;
Ok(Response::new()
.add_attribute("action", "update_start_time")
.add_attribute("sender", info.sender)
.add_attribute("start_time", start_time.to_string()))
}
pub fn execute_update_start_trading_time(
deps: DepsMut,
env: Env,
info: MessageInfo,
start_time: Option<Timestamp>,
) -> Result<Response, ContractError> {
nonpayable(&info)?;
let config = CONFIG.load(deps.storage)?;
let sg721_contract_addr = SG721_ADDRESS.load(deps.storage)?;
if info.sender != config.extension.admin {
return Err(ContractError::Unauthorized(
"Sender is not an admin".to_owned(),
));
}
// add custom rules here
let factory_params: ParamsResponse = deps
.querier
.query_wasm_smart(config.factory.clone(), &FactoryQueryMsg::Params {})?;
let default_start_time_with_offset = config
.extension
.start_time
.plus_seconds(factory_params.params.max_trading_offset_secs);
if let Some(start_trading_time) = start_time {
if env.block.time > start_trading_time {
return Err(ContractError::InvalidStartTradingTime(
env.block.time,
start_trading_time,
));
}
// If new start_trading_time > old start time + offset , return error
if start_trading_time > default_start_time_with_offset {
return Err(ContractError::InvalidStartTradingTime(
start_trading_time,
default_start_time_with_offset,
));
}
}
// execute sg721 contract
let msg = WasmMsg::Execute {
contract_addr: sg721_contract_addr.to_string(),
msg: to_json_binary(&Sg721ExecuteMsg::<Empty, Empty>::UpdateStartTradingTime(
start_time,
))?,
funds: vec![],
};
Ok(Response::new()
.add_attribute("action", "update_start_trading_time")
.add_attribute("sender", info.sender)
.add_message(msg))
}
pub fn execute_update_per_address_limit(
deps: DepsMut,
_env: Env,
info: MessageInfo,
per_address_limit: u32,
) -> Result<Response, ContractError> {
nonpayable(&info)?;
let mut config = CONFIG.load(deps.storage)?;
if info.sender != config.extension.admin {
return Err(ContractError::Unauthorized(
"Sender is not an admin".to_owned(),
));
}
let factory: ParamsResponse = deps
.querier
.query_wasm_smart(config.factory.clone(), &FactoryQueryMsg::Params {})?;
let factory_params = factory.params;
if per_address_limit == 0 || per_address_limit > factory_params.max_per_address_limit {
return Err(ContractError::InvalidPerAddressLimit {
max: factory_params.max_per_address_limit,
min: 1,
got: per_address_limit,
});
}
if !check_dynamic_per_address_limit(
per_address_limit,
config.extension.num_tokens,
factory_params.max_per_address_limit,
)? {
return Err(ContractError::InvalidPerAddressLimit {
max: display_max_mintable_tokens(
per_address_limit,
config.extension.num_tokens,
factory_params.max_per_address_limit,
)?,
min: 1,
got: per_address_limit,
});
}
config.extension.per_address_limit = per_address_limit;
CONFIG.save(deps.storage, &config)?;
Ok(Response::new()
.add_attribute("action", "update_per_address_limit")
.add_attribute("sender", info.sender)
.add_attribute("limit", per_address_limit.to_string()))
}
pub fn execute_burn_remaining(
deps: DepsMut,
env: Env,
info: MessageInfo,
) -> Result<Response, ContractError> {
nonpayable(&info)?;
let config = CONFIG.load(deps.storage)?;
// Check only admin
if info.sender != config.extension.admin {
return Err(ContractError::Unauthorized(
"Sender is not an admin".to_owned(),
));
}
// check mint not sold out
let mintable_num_tokens = MINTABLE_NUM_TOKENS.load(deps.storage)?;
if mintable_num_tokens == 0 {
return Err(ContractError::SoldOut {});
}
let keys = MINTABLE_TOKEN_POSITIONS
.keys(deps.storage, None, None, Order::Ascending)
.collect::<Vec<_>>();
let mut total: u32 = 0;
for key in keys {
total += 1;
MINTABLE_TOKEN_POSITIONS.remove(deps.storage, key?);
}
// Decrement mintable num tokens
MINTABLE_NUM_TOKENS.save(deps.storage, &(mintable_num_tokens - total))?;
let event = Event::new("burn-remaining")
.add_attribute("sender", info.sender)
.add_attribute("tokens_burned", total.to_string())
.add_attribute("minter", env.contract.address.to_string());
Ok(Response::new().add_event(event))
}
fn mint_count(deps: Deps, address: Addr) -> Result<u32, StdError> {
let mint_count = (MINTER_ADDRS.key(&address).may_load(deps.storage)?).unwrap_or(0);
Ok(mint_count)
}
pub fn display_max_mintable_tokens(
per_address_limit: u32,
num_tokens: u32,
max_per_address_limit: u32,
) -> Result<u32, ContractError> {
if per_address_limit > max_per_address_limit {
return Ok(max_per_address_limit);
}
if num_tokens < 100 {
return Ok(3_u32);
}
let three_percent = get_three_percent_of_tokens(num_tokens)?.u128();
Ok(three_percent as u32)
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn sudo(deps: DepsMut, _env: Env, msg: SudoMsg) -> Result<Response, ContractError> {
match msg {
SudoMsg::UpdateStatus {
is_verified,
is_blocked,
is_explicit,
} => update_status(deps, is_verified, is_blocked, is_explicit)
.map_err(|_| ContractError::UpdateStatus {}),
}
}
/// Only governance can update contract params
pub fn update_status(
deps: DepsMut,
is_verified: bool,
is_blocked: bool,
is_explicit: bool,
) -> StdResult<Response> {
let mut status = STATUS.load(deps.storage)?;
status.is_verified = is_verified;
status.is_blocked = is_blocked;
status.is_explicit = is_explicit;
STATUS.save(deps.storage, &status)?;
Ok(Response::new().add_attribute("action", "sudo_update_status"))
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::Config {} => to_json_binary(&query_config(deps)?),
QueryMsg::Status {} => to_json_binary(&query_status(deps)?),
QueryMsg::StartTime {} => to_json_binary(&query_start_time(deps)?),
QueryMsg::MintableNumTokens {} => to_json_binary(&query_mintable_num_tokens(deps)?),
QueryMsg::MintCount { address } => to_json_binary(&query_mint_count(deps, address)?),
QueryMsg::MintTokens {} => to_json_binary(&query_mint_tokens(deps)?),
QueryMsg::DepositedTokens { address } => {
to_json_binary(&query_deposited_tokens(deps, address)?)
}
}
}
fn query_config(deps: Deps) -> StdResult<ConfigResponse> {
let config = CONFIG.load(deps.storage)?;
let sg721_address = SG721_ADDRESS.load(deps.storage)?;
Ok(ConfigResponse {
admin: config.extension.admin.to_string(),
base_token_uri: config.extension.base_token_uri,
sg721_address: sg721_address.to_string(),
sg721_code_id: config.collection_code_id,
num_tokens: config.extension.num_tokens,
start_time: config.extension.start_time,
per_address_limit: config.extension.per_address_limit,
factory: config.factory.to_string(),
mint_tokens: config.extension.mint_tokens,
})
}
pub fn query_status(deps: Deps) -> StdResult<StatusResponse> {
let status = STATUS.load(deps.storage)?;
Ok(StatusResponse { status })
}
pub fn query_mint_tokens(deps: Deps) -> StdResult<Vec<MintToken>> {
let config = CONFIG.load(deps.storage)?;
Ok(config.extension.mint_tokens)
}
pub fn query_deposited_tokens(deps: Deps, address: String) -> StdResult<MintTokensResponse> {
let addr = deps.api.addr_validate(&address)?;
let received_tokens = RECEIVED_TOKENS
.prefix(&addr)
.range(deps.storage, None, None, Order::Ascending)
.map(|item| {
let (k, v) = item?;
Ok((k, v))
})
.collect::<StdResult<Vec<(String, u32)>>>()?
.iter()
.map(|(k, v)| MintToken {
collection: k.to_string(),
amount: *v,
})
.collect::<Vec<MintToken>>();
Ok(MintTokensResponse {
mint_tokens: received_tokens,
})
}
fn query_mint_count(deps: Deps, address: String) -> StdResult<MintCountResponse> {
let addr = deps.api.addr_validate(&address)?;
let mint_count = (MINTER_ADDRS.key(&addr).may_load(deps.storage)?).unwrap_or(0);
Ok(MintCountResponse {
address: addr.to_string(),
count: mint_count,
})
}
fn query_start_time(deps: Deps) -> StdResult<StartTimeResponse> {
let config = CONFIG.load(deps.storage)?;
Ok(StartTimeResponse {
start_time: config.extension.start_time.to_string(),
})
}
fn query_mintable_num_tokens(deps: Deps) -> StdResult<MintableNumTokensResponse> {
let count = MINTABLE_NUM_TOKENS.load(deps.storage)?;
Ok(MintableNumTokensResponse { count })
}
// Reply callback triggered from cw721 contract instantiation
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn reply(deps: DepsMut, _env: Env, msg: Reply) -> Result<Response, ContractError> {
if msg.id != INSTANTIATE_SG721_REPLY_ID {
return Err(ContractError::InvalidReplyID {});
}
let reply = parse_reply_instantiate_data(msg);
match reply {
Ok(res) => {
let sg721_address = res.contract_address;
SG721_ADDRESS.save(deps.storage, &Addr::unchecked(sg721_address.clone()))?;
Ok(Response::default()
.add_attribute("action", "instantiate_sg721_reply")
.add_attribute("sg721_address", sg721_address))
}
Err(_) => Err(ContractError::InstantiateSg721Error {}),
}
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: Empty) -> Result<Response, ContractError> {
let current_version = cw2::get_contract_version(deps.storage)?;
if current_version.contract != CONTRACT_NAME {
return Err(StdError::generic_err("Cannot upgrade to a different contract").into());
}
let version: Version = current_version
.version
.parse()
.map_err(|_| StdError::generic_err("Invalid contract version"))?;
let new_version: Version = CONTRACT_VERSION
.parse()
.map_err(|_| StdError::generic_err("Invalid contract version"))?;
if version > new_version {
return Err(StdError::generic_err("Cannot upgrade to a previous contract version").into());
}
// if same version return