-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlib.rs
555 lines (480 loc) · 18.7 KB
/
lib.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
#![cfg_attr(not(feature = "std"), no_std, no_main)]
#[openbrush::implementation(Ownable, PSP37, PSP37Metadata, PSP37Mintable)]
#[openbrush::contract]
pub mod dropspace_sale {
use ink::prelude::vec;
use ink::primitives::AccountId as Address;
use ink_prelude::format;
use ink_prelude::string::String as PreludeString;
use openbrush::{
contracts::{
ownable,
psp37::{
self,
extensions::metadata,
Id,
},
},
modifiers,
storage::Mapping,
//updated code Balance define
traits::{Storage, String},
};
#[derive(Default, Storage)]
#[ink(storage)]
pub struct Contract {
#[storage_field]
psp37: psp37::Data,
denied_ids: Mapping<Id, ()>,
#[storage_field]
ownable: ownable::Data,
#[storage_field]
metadata: metadata::Data,
base_uri: PreludeString,
mint_per_tx: u128,
mint_price: u128,
mint_fee: u128,
withdraw_wallet: Option<Address>,
dev_wallet: Option<Address>,
sale_time: u64,
//added code
supply_limit: u128,
}
impl Contract {
#[ink(constructor)]
pub fn new(
name: PreludeString,
symbol: PreludeString,
base_uri: PreludeString,
supply_limit: u128,
mint_per_tx: u128,
mint_price: u128,
mint_fee: u128,
withdraw_wallet: Option<Address>,
dev_wallet: Option<Address>,
sale_time: u64,
) -> Self {
let mut _instance = Self {
base_uri,
supply_limit,
mint_per_tx,
mint_price,
mint_fee,
withdraw_wallet,
dev_wallet,
sale_time,
psp37: Default::default(), // Initialize psp37 field
denied_ids: Default::default(), // Initialize denied_ids field
ownable: Default::default(), // Initialize ownable field
metadata: Default::default(), // Initialize metadata field
};
let id = Id::U128(1);
ownable::Internal::_init_with_owner(&mut _instance, Self::env().caller());
let _ = metadata::Internal::_set_attribute(
&mut _instance,
&id,
&String::from("name"),
&String::from(name),
);
let _ = metadata::Internal::_set_attribute(
&mut _instance,
&id,
&String::from("symbol"),
&String::from(symbol),
);
_instance
}
fn mint_token(&mut self, amount: u128) -> Result<(), PSP37Error> {
let current_supply: u128 = psp37::PSP37::total_supply(self, Some(Id::U128(1)));
if current_supply.saturating_add(amount) > self.supply_limit {
return Err(PSP37Error::Custom(String::from(
"DropspaceSale::mint: Supply limit reached",
)));
}
psp37::Internal::_mint_to(self, Self::env().caller(), vec![(Id::U128(1), amount)])?;
Ok(())
}
#[ink(message)]
#[modifiers(only_owner)]
pub fn reserve(&mut self, amount: u128) -> Result<(), PSP37Error> {
let __ = self.mint_token(amount)?;
Ok(())
}
#[ink(message, payable)]
pub fn buy(&mut self, amount: u128) -> Result<(), PSP37Error> {
let total_price = amount.saturating_mul(self.mint_price.saturating_add(self.mint_fee));
let current_supply: u128 = psp37::PSP37::total_supply(self, Some(Id::U128(1)));
if self.env().block_timestamp() < self.sale_time {
return Err(PSP37Error::Custom(String::from(
"DropspaceSale::buy: Sale hasn't started yet",
)));
}
if current_supply.saturating_add(amount) > self.supply_limit {
return Err(PSP37Error::Custom(String::from(
"DropspaceSale::buy: Supply limit reached",
)));
}
if amount > self.mint_per_tx {
return Err(PSP37Error::Custom(String::from(
"DropspaceSale::buy: Can't exceed amount of mints per tx",
)));
}
if self.env().transferred_value() < total_price {
return Err(PSP37Error::Custom(String::from(
"DropspaceSale::buy: Wrong amount paid.",
)));
}
let __ = self.mint_token(amount)?;
if let Some(withdraw_wallet) = self.withdraw_wallet {
if self.mint_price > 0 {
self.env()
.transfer(withdraw_wallet, amount.saturating_mul(self.mint_price))
.map_err(|_| {
PSP37Error::Custom(String::from("Transfer to owner wallet failed"))
})?;
}
} else {
return Err(PSP37Error::Custom(String::from("Owner wallet not set")));
}
if let Some(dev_wallet) = self.dev_wallet {
if self.mint_fee > 0 {
self.env()
.transfer(dev_wallet, amount.saturating_mul(self.mint_fee))
.map_err(|_| {
PSP37Error::Custom(String::from("Transfer to dev wallet failed"))
})?;
}
} else {
return Err(PSP37Error::Custom(String::from("Developer wallet not set")));
}
Ok(())
}
#[ink(message)]
#[modifiers(only_owner)]
pub fn set_base_uri(&mut self, uri: PreludeString) -> Result<(), PSP37Error> {
self.base_uri = uri;
Ok(())
}
#[ink(message)]
#[modifiers(only_owner)]
pub fn set_mint_per_tx(&mut self, mint_per_tx: u128) -> Result<(), PSP37Error> {
self.mint_per_tx = mint_per_tx;
Ok(())
}
#[ink(message)]
#[modifiers(only_owner)]
pub fn set_mint_price(&mut self, mint_price: u128) -> Result<(), PSP37Error> {
self.mint_price = mint_price;
Ok(())
}
#[ink(message)]
#[modifiers(only_owner)]
pub fn set_sale_time(&mut self, sale_time: u64) -> Result<(), PSP37Error> {
self.sale_time = sale_time;
Ok(())
}
#[ink(message)]
#[modifiers(only_owner)]
pub fn toggle_sale_active(&mut self) -> Result<(), PSP37Error> {
if self.sale_time() != 0 {
self.sale_time = 0;
} else {
self.sale_time = u64::MAX;
}
Ok(())
}
#[ink(message)]
#[modifiers(only_owner)]
pub fn set_supply_limit(&mut self, supply_limit: u128) -> Result<(), PSP37Error> {
let current_supply: u128 = psp37::PSP37::total_supply(self, Some(Id::U128(1)));
if current_supply > supply_limit {
return Err(PSP37Error::Custom(String::from(
"DropspaceSale::set_total_supply: Supply limit is lesser than current supply",
)));
}
self.supply_limit = supply_limit;
Ok(())
}
#[ink(message)]
#[modifiers(only_owner)]
pub fn set_withdraw_wallet(
&mut self,
withdraw_wallet: Option<Address>,
) -> Result<(), PSP37Error> {
self.withdraw_wallet = withdraw_wallet;
Ok(())
}
#[ink(message)]
pub fn token_uri(&self) -> Result<PreludeString, PSP37Error> {
let base_uri = self.base_uri.clone();
// Ok(format!("{base_uri}{token_id}")) // OLD
Ok(format!("{base_uri}")) // NEW
}
#[ink(message)]
pub fn supply_limit(&self) -> u128 {
self.supply_limit
}
#[ink(message)]
pub fn mint_per_tx(&self) -> u128 {
self.mint_per_tx
}
#[ink(message)]
pub fn get_account_balance(&self) -> u128 {
self.env().balance()
}
#[ink(message)]
pub fn mint_price(&self) -> u128 {
self.mint_price
}
#[ink(message)]
pub fn mint_fee(&self) -> u128 {
self.mint_fee
}
#[ink(message)]
pub fn dev_wallet(&self) -> Option<Address> {
self.dev_wallet
}
#[ink(message)]
pub fn withdraw_wallet(&self) -> Option<Address> {
self.withdraw_wallet
}
#[ink(message)]
pub fn sale_time(&self) -> u64 {
self.sale_time
}
#[ink(message)]
pub fn sale_active(&self) -> bool {
self.sale_time <= self.env().block_timestamp()
}
#[ink(message)]
pub fn base_uri(&self) -> PreludeString {
self.base_uri.clone()
}
#[ink(message)]
#[modifiers(only_owner)]
pub fn withdraw(&mut self) -> Result<(), PSP37Error> {
let contract_balance = self.get_account_balance();
if contract_balance > 0 {
match self.env().transfer(Self::env().caller(), contract_balance) {
Ok(_) => Ok(()),
Err(_) => Err(PSP37Error::Custom(String::from("Withdrawal failed"))),
}
} else {
Err(PSP37Error::Custom(String::from("No funds to withdraw")))
}
}
#[ink(message)]
#[modifiers(only_owner)]
pub fn transfer_ownership(
&mut self,
new_owner: Option<AccountId>,
) -> Result<(), PSP37Error> {
let _ = ownable::OwnableImpl::transfer_ownership(self, new_owner);
Ok(())
}
}
}
#[cfg(test)]
mod tests {
#[rustfmt::skip]
use super::*;
use dropspace_sale::Contract;
use ink::{env::DefaultEnvironment as Environment, primitives::AccountId};
use openbrush::contracts::ownable::Ownable;
use openbrush::contracts::psp37::extensions::metadata::psp37metadata_external::PSP37Metadata;
use openbrush::contracts::psp37::{psp37, Id, PSP37Error};
use openbrush::traits::{Balance, StorageAsMut};
use openbrush::{
modifiers,
storage::Mapping,
traits::{Storage, String},
};
fn default_accounts() -> ink::env::test::DefaultAccounts<ink::env::DefaultEnvironment> {
ink::env::test::default_accounts::<ink::env::DefaultEnvironment>()
}
struct ContractParam {
name: String,
symbol: String,
base_uri: String,
supply_limit: u128,
mint_per_tx: u128,
mint_price: u128,
mint_fee: u128,
withdraw_wallet: Option<AccountId>,
dev_wallet: Option<AccountId>,
sale_time: u64,
}
impl Default for ContractParam {
fn default() -> ContractParam {
ContractParam {
name: "Test".to_string(),
symbol: "TST".to_string(),
base_uri: "https://example.com/token/".to_string(),
supply_limit: 100000,
mint_per_tx: 10,
mint_price: 1000,
mint_fee: 10,
withdraw_wallet: None,
dev_wallet: None,
sale_time: 0,
}
}
}
fn get_contract(args: &ContractParam) -> Contract {
return Contract::new(
args.name.to_string(),
args.symbol.to_string(),
args.base_uri.to_string(),
args.supply_limit,
args.mint_per_tx,
args.mint_price,
args.mint_fee,
args.withdraw_wallet,
args.dev_wallet,
args.sale_time
);
}
#[ink::test]
fn new_works() {
let accounts = default_accounts();
let params = ContractParam {
withdraw_wallet: Some(accounts.django),
dev_wallet: Some(accounts.alice),
..Default::default()
};
let contract = get_contract(¶ms);
assert_eq!(contract.supply_limit(), params.supply_limit);
assert_eq!(contract.mint_per_tx(), params.mint_per_tx);
assert_eq!(contract.mint_price(), params.mint_price);
assert_eq!(contract.mint_fee(), params.mint_fee);
assert_eq!(contract.dev_wallet(), params.dev_wallet);
assert_eq!(contract.withdraw_wallet(), params.withdraw_wallet);
assert_eq!(contract.sale_time(), params.sale_time);
assert_eq!(contract.sale_active(), true);
assert_eq!(
PSP37Metadata::get_attribute(&contract, Id::U128(1), String::from("name")),
Some(params.name)
);
assert_eq!(
PSP37Metadata::get_attribute(&contract, Id::U128(1), String::from("symbol")),
Some(params.symbol)
);
assert_eq!(contract.base_uri(), params.base_uri);
}
#[ink::test]
fn buy_works() {
let accounts = default_accounts();
let params = ContractParam {
withdraw_wallet: Some(accounts.django),
dev_wallet: Some(accounts.alice),
..Default::default()
};
let mut contract = get_contract(¶ms);
// Buying a token should succeed
// assert_eq!(contract.buy(1), Ok(()));
assert_eq!(ink::env::pay_with_call!(contract.buy(1), (params.mint_price + params.mint_fee) * 1), Ok(()));
assert_eq!(
ink::env::pay_with_call!(contract.buy(1), (params.mint_price + params.mint_fee - 1) * 1),
Err(PSP37Error::Custom(String::from(
"DropspaceSale::buy: Wrong amount paid.",
)))
);
}
#[ink::test]
fn reserve_works() {
let accounts = default_accounts();
let params = ContractParam {
withdraw_wallet: Some(accounts.django),
dev_wallet: Some(accounts.alice),
..Default::default()
};
let mut contract = get_contract(¶ms);
// Reserving token should succeed
assert_eq!(contract.reserve(2), Ok(()));
}
#[ink::test]
fn sale_active_works() {
let accounts = default_accounts();
let params = ContractParam {
withdraw_wallet: Some(accounts.django),
dev_wallet: Some(accounts.alice),
..Default::default()
};
let mut contract = get_contract(¶ms);
// Set the block timestamp to simulate sale time passing
ink::env::test::set_block_timestamp::<ink::env::DefaultEnvironment>(12345678);
assert_eq!(contract.sale_active(), true);
let __ = contract.set_sale_time(12345679);
// After the sale time, sale should be active
assert_eq!(contract.sale_active(), false);
let __ = contract.set_sale_time(0);
assert_eq!(contract.sale_active(), true);
}
#[ink::test]
fn toggle_sale_active_works() {
let accounts = default_accounts();
let params = ContractParam {
withdraw_wallet: Some(accounts.django),
dev_wallet: Some(accounts.alice),
..Default::default()
};
// Set owner
ink::env::test::set_caller::<ink::env::DefaultEnvironment>(accounts.charlie);
let mut contract = get_contract(¶ms);
// Ensure that only the owner can toggle sale active
ink::env::test::set_caller::<ink::env::DefaultEnvironment>(accounts.bob);
assert_eq!(
contract.toggle_sale_active(),
Err(PSP37Error::Custom(String::from("O::CallerIsNotOwner")))
);
assert_eq!(contract.sale_active(), true);
ink::env::test::set_caller::<ink::env::DefaultEnvironment>(accounts.charlie);
assert_eq!(contract.toggle_sale_active(), Ok(()));
assert_eq!(contract.sale_active(), false);
// Simulate the owner calling the function
ink::env::test::set_caller::<ink::env::DefaultEnvironment>(accounts.charlie);
// Toggle sale active, which should set the sale time to 0
assert_eq!(contract.toggle_sale_active(), Ok(()));
assert_eq!(contract.sale_time(), 0);
assert_eq!(contract.sale_active(), true);
// Toggle sale active again, which should set the sale time to u64::MAX
assert_eq!(contract.toggle_sale_active(), Ok(()));
assert_eq!(contract.sale_time(), u64::MAX);
assert_eq!(contract.sale_active(), false);
}
#[ink::test]
fn withdraw_works() {
let accounts = default_accounts();
let params = ContractParam {
withdraw_wallet: Some(accounts.django),
dev_wallet: Some(accounts.alice),
..Default::default()
};
let mut contract = get_contract(¶ms);
// Simulate buying a token
ink::env::test::set_account_balance::<ink::env::DefaultEnvironment>(accounts.django, 0);
ink::env::test::set_account_balance::<ink::env::DefaultEnvironment>(accounts.alice, 0);
ink::env::test::set_account_balance::<ink::env::DefaultEnvironment>(
accounts.bob,
100_000_000,
);
assert_eq!(contract.toggle_sale_active(), Ok(()));
ink::env::test::set_caller::<ink::env::DefaultEnvironment>(accounts.bob);
assert_eq!(
// ink::env::pay_with_call!(contract.buy(vec![(Id::U8(1), 100), (Id::U8(2), 200)]), 2020),
ink::env::pay_with_call!(contract.buy(100), (params.mint_fee + params.mint_price) * 100),
Ok(())
);
// Check that owner's balance has increased by 10000 units
let withdraw_wallet_balance: u128 =
ink::env::test::get_account_balance::<ink::env::DefaultEnvironment>(params.withdraw_wallet.unwrap())
.unwrap_or_default();
assert_eq!(
withdraw_wallet_balance,
params.mint_price * 100,
);
// Simulate the owner calling the withdraw function
ink::env::test::set_caller::<ink::env::DefaultEnvironment>(accounts.alice);
assert_eq!(contract.withdraw(), Ok(()));
}
}