-
Notifications
You must be signed in to change notification settings - Fork 5
/
lib.rs
executable file
·1189 lines (1051 loc) · 44 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
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
// (c) 2021 Alexander Gryaznov (agryaznov.com)
//
//! Candle Auction implemented with Ink! smartcontract
#![cfg_attr(not(feature = "std"), no_std)]
use ink_lang as ink;
// randomness source
mod entropy;
#[ink::contract]
mod candle_auction {
use ink_env::{
call::{build_call, utils::ReturnType, ExecutionInput, Selector},
transfer,
};
use ink_storage::collections::HashMap as StorageHashMap;
use ink_storage::Vec as StorageVec;
use scale::{Decode, Encode};
// use parity_scale_codec::Decode
#[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
/// Error types
pub enum Error {
/// Returned if bidding while auction isn't in active status
AuctionNotActive,
/// Placed bid_new isn't outbidding current winning nid_quo
/// (bid_new, bid_quo) returned for info
NotOutBidding(Balance, Balance),
/// Problems with winning_data observed
WinningDataCorrupted,
}
/// Auction statuses
/// logic inspired by
/// [Parachain Auction](https://github.com/paritytech/polkadot/blob/master/runtime/common/src/traits.rs#L160)
#[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(::scale_info::TypeInfo))]
pub enum Status {
/// An auction has not started yet.
NotStarted,
/// We are in the starting period of the auction, collecting initial bids.
OpeningPeriod,
/// We are in the ending period of the auction, where we are taking snapshots of the winning
/// bids. Snapshots are taken currently on per-block basis, but this logic could be later evolve
/// to take snapshots of on arbitrary length (in blocks)
EndingPeriod(BlockNumber),
/// Candle was blown
Ended,
/// We have completed the bidding process and are waiting for the Random Function to return some acceptable
/// randomness to select the winner. The number represents how many blocks we have been waiting.
RfDelay(BlockNumber),
}
/// Auction subject: what are we bidding for?
#[derive(scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum Subject {
NFTs,
Domain(Hash),
}
/// Event emitted when a bid is accepted.
#[ink(event)]
pub struct Bid {
#[ink(topic)]
from: AccountId,
bid: Balance,
}
/// Event emitted when Winning block is detected.
#[ink(event)]
pub struct WinningOffset {
offset: BlockNumber,
}
/// Event emitted when a winner is detected.
#[ink(event)]
pub struct Winner {
account: AccountId,
bid: Balance,
}
/// Event emitted when the auction winner is rewarded.
#[ink(event)]
pub struct Reward {
#[ink(topic)]
to: AccountId,
contract: AccountId,
subject: Subject,
}
/// Defines the storage of the contract.
#[ink(storage)]
pub struct CandleAuction {
/// Contract owner
owner: AccountId,
/// Stores a single `bool` value on the storage.
// value: bool,
start_block: BlockNumber,
/// The number of blocks of Opening period.
/// We assume this period starts in start_block (default val is the next block after the Auction has been created)
opening_period: BlockNumber,
/// The number of blocks of Ending period, over which an auction may be retroactively ended.
/// We assume this period starts right after Opening perid ends.
ending_period: BlockNumber,
/// Bidders balances storage.
/// Current user's balance = her top bid
balances: StorageHashMap<AccountId, Balance>,
/// *winning* <bidder> = current top bidder.
/// Not to be confused with *winner* = bidder who finally won.
winning: Option<AccountId>,
// Winner (with bid) who finally won Candle auction
winner: Option<(AccountId, Balance)>,
/// Finalization flag (needed because winner detected by candle could be None)
/// Once auction is finalized, that means candle went out and the winner has been detected
finalized: bool,
/// WinningData = storage of winners per sample (block)
/// it's a vector of optional (AccountId, Balance) tuples representing winner in block (sample) along with her bid
/// 0-indexed value is winner for OpeningPeriod
/// i-indexed value is winner for sample (block) #i of EndingPeriod
winning_data: StorageVec<Option<(AccountId, Balance)>>,
/// ERC721 contract
/// rewarding contract address (NFT or DNS)
reward_contract_address: AccountId,
/// What we are bidding for?
/// 0 = NFT <-- default
/// 1 = DNS
/// 2..255 = reserved for further reward methods
subject: u8,
/// Domain name (in case we bid for it)
domain: Hash,
}
impl CandleAuction {
/// Auction constructor.
/// Initializes the start_block to next block (if not set).
/// If start_block is set, checks it is in the future (to prevent backdating).
#[ink(constructor)]
pub fn new(
start_block: Option<BlockNumber>,
opening_period: BlockNumber,
ending_period: BlockNumber,
subject: u8,
domain: Hash,
reward_contract_address: AccountId,
) -> Self {
if subject > 1 {
panic!("Only subjects [0,1] are supported so far!")
}
let now = Self::env().block_number();
let start_in = start_block.unwrap_or(now + 1);
// Security check versus backdating
assert!(
start_in > now,
"Auction is allowed to be scheduled to future blocks only!"
);
let mut winning_data = StorageVec::<Option<(AccountId, Balance)>>::new();
(0..ending_period + 1).for_each(|_| winning_data.push(None));
Self {
owner: Self::env().caller(),
start_block: start_in,
opening_period,
ending_period,
balances: StorageHashMap::new(),
winning: None,
winner: None,
finalized: false,
winning_data,
reward_contract_address,
subject,
domain,
}
}
/// Auction status.
fn status(&self, block: BlockNumber) -> Status {
let opening_period_last_block = self.start_block + self.opening_period - 1;
let ending_period_last_block = opening_period_last_block + self.ending_period;
if block >= self.start_block {
if block > opening_period_last_block {
if block > ending_period_last_block {
if !self.finalized {
Status::RfDelay(block - ending_period_last_block - 1)
} else {
Status::Ended
}
} else {
// number of slot = number of block inside ending period
Status::EndingPeriod(block - opening_period_last_block)
}
} else {
Status::OpeningPeriod
}
} else {
Status::NotStarted
}
}
/// Handle bid.
fn handle_bid(
&mut self,
bidder: AccountId,
bid: Balance,
block: BlockNumber,
) -> Result<(), Error> {
// fail unless auction is active
let auction_status = self.status(block);
let offset = match auction_status {
Status::OpeningPeriod => 0,
Status::EndingPeriod(o) => o,
_ => return Err(Error::AuctionNotActive),
};
// do not accept bids lesser that current top bid
if let Some(winning) = self.winning {
let winning_balance = *self.balances.get(&winning).unwrap_or(&0);
if bid < winning_balance {
return Err(Error::NotOutBidding(bid, winning_balance));
}
}
// return previous bid amount back
// TODO: compare gas consumption with incremental bids variant
if let Some(old_balance) = self.balances.take(&bidder) {
transfer::<Environment>(bidder, old_balance).unwrap();
}
// finally, accept bid
self.balances.insert(bidder, bid);
self.winning = Some(bidder);
// and update winning_data
// for retrospective candle-fashioned winning bidder detection
match self.winning_data.set(offset, Some((bidder, bid))) {
Err(ink_storage::collections::vec::IndexOutOfBounds) => {
Err(Error::WinningDataCorrupted)
}
Ok(_) => {
self.env().emit_event(Bid {
from: bidder,
bid: bid,
});
Ok(())
}
}
}
/// Pay back.
/// Winner gets her reward.
/// Loosers get their balances back.
/// Contract owner gets winner`s balance (winning bid).
///
/// NOTE that the following situation is possible:
/// - `Status::Ended` but auction.winner is still `None`
/// as no one has called `find_winner()` yet
/// To avoid winner get back both
fn pay_back(&mut self, reward: fn(&Self, to: AccountId) -> (), to: AccountId) {
// should be executed only on Ended auction
assert_eq!(
self.get_status(),
Status::Ended,
"Auction is not Ended, no payback is possible!"
);
// we cannot payback no one until the winner is detected
// otherwise, the winner could take his money back
// in advance and break the auction
let (winner, _) = self
.get_winner()
.expect("Winner is not detected, no payback is possible!");
// winner gets her reward
if to == winner {
// reward winner with specified reward method call
reward(&self, to);
}
// whoever calls this should get his balance paid back
if let Some(bal) = self.balances.take(&to) {
// zero-balance check: bal 0 is possible, but nothing to pay back
if bal > 0 {
// and pay
transfer::<Environment>(to, bal).unwrap();
}
}
}
/// Cross contract invocation method
/// common for both rewarding methods
fn invoke_contract<Args>(&self, contract: AccountId, input: ExecutionInput<Args>)
where
Args: Encode,
{
let params = build_call::<Environment>()
.callee(contract)
.exec_input(input)
.returns::<ReturnType<Result<(), Error>>>();
match params.fire() {
Ok(_v) => {}
Err(e) => {
match e {
ink_env::Error::CodeNotFound | ink_env::Error::NotCallable => {
// Our recipient wasn't a smart contract, so there's nothing more for
// us to do
let msg = ink_prelude::format!(
"Recipient at {:#04X?} from is not a smart contract ({:?})",
self.reward_contract_address,
e
);
panic!("{}", msg)
}
_ => {
// We got some sort of error from the call to our recipient smart
// contract, and as such we must revert this call
let msg = ink_prelude::format!(
"Got error \"{:?}\" while trying to call {:?}",
e,
self.reward_contract_address,
);
panic!("{}", msg)
}
}
}
}
}
/// Pluggable reward logic: OPTION-1.
/// Reward with NFT(s) (ERC721).
/// Contract rewards an auction winner by giving her approval to transfer
/// ERC721 tokens on behalf of the auction contract.
///
/// DESIGN DECISION: we call ERC721 set_approval_for_all() instead of approve() for
/// 1. the sake of simplicity, no need to specify TokenID
/// as we need to send this token to the contract anyway, _after_ instantiation
/// but still _before_ auctions starts
/// 2. this allows to set auction for collection of tokens instead of just for one thing
///
/// Cross conract call to ERC721 set_approval_for_all() method
/// which is expected to have the selector: 0xFEEDBABE
fn give_nft(&self, to: AccountId) {
let selector = Selector::new([0xFE, 0xED, 0xBA, 0xBE]);
let input = ExecutionInput::new(selector).push_arg(to).push_arg(true);
self.invoke_contract(self.reward_contract_address, input);
self.env().emit_event(Reward {
to: to,
subject: Subject::NFTs,
contract: self.reward_contract_address,
});
}
/// Pluggable reward logic: OPTION-2.
/// Reward with domain name.
/// Contract rewards an auction winner by transferring her auctioned
/// domain name using the dns contract.
///
/// Cross conract call to ERC721 set_approval_for_all() method,
/// which is expected to have the selector: 0xFEEDDEED
fn give_domain(&self, to: AccountId) {
let selector = Selector::new([0xFE, 0xED, 0xDE, 0xED]);
let input = ExecutionInput::new(selector)
.push_arg(self.domain)
.push_arg(to);
self.invoke_contract(self.reward_contract_address, input);
self.env().emit_event(Reward {
to: to,
subject: Subject::Domain(self.domain),
contract: self.reward_contract_address,
});
}
/// Retrospective RANDOM `candle blowing`:
/// `seed` buffer is used for additional hash randomization.
/// Returns a record from `winning_data` determined randomly by imitated `candle blow`
fn blow_candle(&self, seed: &[u8]) -> Option<(AccountId, Balance)> {
let opening_period_last_block = self.start_block + self.opening_period - 1;
let ending_period_last_block = opening_period_last_block + self.ending_period;
// Here is where we use Random func.
// ink_env::random() uses `T::Randomness::random()`
// which in `substrate-contracts-node` is implemented for `pallet_collective_flip`
// so that 81 blocks needed back in history to securely calcutate the seed
// see also https://github.com/paritytech/ink/issues/868
let (raw_offset, known_since): (Hash, BlockNumber) =
crate::entropy::random::<Environment>(seed);
let mut win_data: Option<(AccountId, Balance)> = None;
// The returned seed should only be used to distinguish commitments made before the returned block number
// https://docs.substrate.io/rustdocs/latest/frame_support/traits/trait.Randomness.html#tymethod.random
if ending_period_last_block <= known_since {
// Our random seed was known only after the auction ended. Good to use.
// (Inspired by:
// https://github.com/paritytech/polkadot/blob/v0.9.13-rc1/runtime/common/src/auctions.rs#L526)
let raw_offset_block_number = <BlockNumber>::decode(&mut raw_offset.as_ref())
.expect("secure hashes should always be bigger than the block number; qed");
// detect the block when 'the candle went out' in Ending Period
let offset = raw_offset_block_number % self.ending_period + 1;
// emit Winning Offset event
self.env().emit_event(WinningOffset { offset: offset });
// Detect winning slot.
// Starting from the `candle-determined` block,
// iterate backwards until a block with some bids found
// 0 index refers to winner in the Opening period
for i in (0..offset + 1).rev() {
if let Some(Some((w, b))) = self.winning_data.get(i) {
win_data = Some((*w, *b));
break;
}
}
return win_data;
}
let msg = ink_prelude::format!(
"Random seed known_since is to early: block#{:?}!",
known_since
);
win_data.expect(&msg);
win_data
}
/// Helper to determine the Candle auction winner:
fn detect_winner(&mut self, seed: &[u8]) -> Option<(AccountId, Balance)> {
if let Some(winner) = self.winner {
return Some(winner);
}
match self.get_status() {
Status::RfDelay(blocks) => {
// RfDelay status means candle hasn't go out yet, we haven't decide winner.
//
// no sense to try to `blow_candle` before RF_DELAY blocks passed (as Randomness is not mature yet)
// also, no sense to detect winner if there is no winning candidate
if (blocks >= crate::entropy::RF_DELAY) && (self.winning.is_some()) {
// Determine winner by random "candle blowing"
self.winner = self.blow_candle(seed);
if let Some((winner, bid)) = self.winner {
// we have a winner!
// decrement winner`s balance to won bid amount
self.balances.entry(winner).and_modify(|b| *b -= bid);
// increment auction owner's balance to won bid
self.balances
.entry(self.owner)
.and_modify(|b| *b += bid)
.or_insert(bid);
// emit Winner event
self.env().emit_event(Winner {
account: winner,
bid: bid,
});
}
// finalize auction
// this is needed for the case when
// candle-detected winner is None, which is fair enough to be a result
// e.g. when there were no bids at all before and in decisive round
self.finalized = true;
self.winner
} else {
None
}
}
_ => self.winner, // is None at this point
}
}
/// Message to get the auction subject.
#[ink(message)]
pub fn get_subject(&self) -> Subject {
match self.subject {
0 => Subject::NFTs,
1 => Subject::Domain(self.domain),
_ => panic!("Current Subject is not supported!"),
}
}
/// Message to get the rewarding contract address.
#[ink(message)]
pub fn get_contract(&self) -> AccountId {
self.reward_contract_address
}
/// Message to get the status of the auction given the current block number.
#[ink(message)]
pub fn get_status(&self) -> Status {
let now = self.env().block_number();
self.status(now)
}
/// Message to determine winner by candle.
/// Gets random block in Ending period,
/// then gets the highest bidder in that block
#[ink(message)]
pub fn find_winner(&mut self) -> Option<(AccountId, Balance)> {
if self.winner.is_none() {
// additional random source (seed) = caller address used as seed
self.detect_winner(self.env().caller().as_ref());
}
self.winner
}
/// Message to get current `winning` account along with her bid
/// Not to be confused with `winner`, which is final auction winner
#[ink(message)]
pub fn get_winning(&self) -> Option<(AccountId, Balance)> {
if let Some(winning) = self.winning {
let bid = self.balances.get(&winning).unwrap();
Some((winning, *bid))
} else {
None
}
}
/// Message to return winner.
/// Winner would be None until someone invokes `find_winner()`
#[ink(message)]
pub fn get_winner(&self) -> Option<(AccountId, Balance)> {
self.winner
}
/// Message to place a bid.
/// An account can bid by sending the bid amount to the contract.
#[ink(message, payable)]
pub fn bid(&mut self) {
let now = self.env().block_number();
let bidder = Self::env().caller();
let bid = self.env().transferred_balance();
match self.handle_bid(bidder, bid, now) {
Err(Error::AuctionNotActive) => {
panic!("Auction isn't active!")
}
Err(Error::NotOutBidding(bid_new, bid_quo)) => {
panic!("You can't outbid {} with {}", bid_quo, bid_new)
}
Err(Error::WinningDataCorrupted) => {
panic!("Auction's winning data corrupted!")
}
Ok(()) => {}
}
}
/// Message to claim the payout.
#[ink(message)]
pub fn payout(&mut self) {
const REWARD_METHODS: [fn(&CandleAuction, to: AccountId); 2] =
[CandleAuction::give_nft, CandleAuction::give_domain];
let caller = self.env().caller();
// invoke reward method
self.pay_back(REWARD_METHODS[usize::from(self.subject)], caller);
}
}
/// Tests
#[cfg(not(feature = "ink-experimental-engine"))]
#[cfg(test)]
mod tests {
use super::*;
use ink_env::balance as contract_balance;
use ink_env::test::get_account_balance as user_balance;
use ink_env::Clear;
use ink_lang as ink;
const DEFAULT_CALLEE_HASH: [u8; 32] = [0x06; 32];
fn accounts() -> ink_env::test::DefaultAccounts<Environment> {
ink_env::test::default_accounts::<Environment>().unwrap()
}
fn run_to_block(n: BlockNumber) {
let mut block = ink_env::block_number::<Environment>();
while block < n {
match ink_env::test::advance_block::<Environment>() {
Err(_) => {
panic!("Cannot add blocks to test chain!")
}
Ok(_) => block = ink_env::block_number::<Environment>(),
}
}
}
fn set_sender(sender: AccountId, amount: Balance) {
ink_env::test::push_execution_context::<Environment>(
sender,
ink_env::account_id::<Environment>(),
1000000,
amount,
ink_env::test::CallData::new(ink_env::call::Selector::new([0x00; 4])), /* dummy */
);
}
fn set_balance(account_id: AccountId, balance: Balance) {
ink_env::test::set_account_balance::<ink_env::DefaultEnvironment>(account_id, balance)
.expect("Cannot set account balance");
}
fn get_balance(account_id: AccountId) -> Balance {
ink_env::test::get_account_balance::<ink_env::DefaultEnvironment>(account_id)
.expect("Cannot set account balance")
}
fn contract_id() -> AccountId {
ink_env::test::get_current_contract_account_id::<Environment>()
.expect("Cannot get contract id")
}
fn create_auction(
start_at: Option<BlockNumber>,
opening_period: BlockNumber,
ending_period: BlockNumber,
subject: u8,
) -> CandleAuction {
CandleAuction::new(
start_at,
opening_period,
ending_period,
subject,
Hash::clear(),
AccountId::from(DEFAULT_CALLEE_HASH),
)
}
#[ink::test]
fn new_works() {
let auction = create_auction(Some(10), 5, 10, 0);
assert_eq!(auction.start_block, 10);
assert_eq!(auction.get_status(), Status::NotStarted);
}
#[ink::test]
fn new_default_start_block_works() {
run_to_block(12);
let auction = create_auction(None, 5, 10, 0);
assert_eq!(auction.start_block, 13);
assert_eq!(auction.get_status(), Status::NotStarted);
}
#[ink::test]
fn dns_auction_new_works() {
let auction_with_domain = CandleAuction::new(
Some(10),
5,
10,
1,
Hash::from([0x99; 32]),
AccountId::from(DEFAULT_CALLEE_HASH),
);
assert_eq!(auction_with_domain.start_block, 10);
assert_eq!(auction_with_domain.domain, Hash::from([0x99; 32]));
assert_eq!(auction_with_domain.get_status(), Status::NotStarted);
let auction_no_domain = create_auction(Some(10), 5, 10, 1);
assert_eq!(auction_no_domain.domain, Hash::clear());
}
#[ink::test]
#[should_panic(expected = "Auction is allowed to be scheduled to future blocks only!")]
fn cannot_init_backdated_auction() {
run_to_block(27);
create_auction(Some(1), 10, 20, 0);
}
#[ink::test]
#[should_panic(expected = "Auction isn't active!")]
fn cannot_bid_until_started() {
// given
// default account (Alice)
// when
// auction starts at block #5
let mut auction = create_auction(Some(5), 5, 10, 0);
// and Alice tries to make a bid before block #5
auction.bid();
// then
// contract should just panic after this line
}
#[ink::test]
fn auction_statuses_returned_correctly() {
// an auction with the following picture:
// [1][2][3][4][5][6][7][8][9][10][11][12][13]
// | opening | ending |
let mut auction = create_auction(Some(2), 4, 7, 0);
let alice = accounts().alice;
assert_eq!(auction.get_status(), Status::NotStarted);
run_to_block(1);
assert_eq!(auction.get_status(), Status::NotStarted);
run_to_block(2);
assert_eq!(auction.get_status(), Status::OpeningPeriod);
run_to_block(5);
assert_eq!(auction.get_status(), Status::OpeningPeriod);
run_to_block(6);
assert_eq!(auction.get_status(), Status::EndingPeriod(1));
set_sender(alice, 100);
auction.bid();
run_to_block(12);
assert_eq!(auction.get_status(), Status::EndingPeriod(7));
run_to_block(13);
assert_eq!(auction.get_status(), Status::RfDelay(0));
run_to_block(57);
assert_eq!(auction.get_status(), Status::RfDelay(57 - 13));
run_to_block(94);
assert_eq!(auction.get_status(), Status::RfDelay(81));
auction.find_winner();
assert_eq!(auction.get_status(), Status::Ended);
}
#[ink::test]
fn winner_gets_change_back() {
// given
// Charlie
let charlie = accounts().charlie;
set_sender(charlie, 1000);
// He setups
// an auction with the following structure:
// [1][2][3][4][5][6][7][8][9][10][11][12][13]
// | opening | ending |
let mut auction = create_auction(Some(2), 4, 7, 0);
// this is needed becase for some reason in tests payables don't add up to contract balance
set_balance(contract_id(), 1000);
// and Alice
let alice = accounts().alice;
// when
// she bids in opening period
run_to_block(3);
// Alice bids 100
set_sender(alice, 100);
auction.bid();
// and then she overbids herself
run_to_block(12);
// Alice bids 201 by adding 101 to her bid
set_sender(alice, 101);
auction.bid();
// and auction ends
run_to_block(13 + crate::entropy::RF_DELAY);
// and candle is blown
auction.find_winner();
// then
if Some((alice, 100)) == auction.get_winner() {
// if Alice wins with bid 100 (not 101)
// (we can't check that Alice gets her 1 change back
// on `payout()` invocation, because the whole `reward()` will fail
// as cross-contract calls are not available here in off-chain tests
// TODO: put this into integration test)
// auction.payout()
// then
// Charlie as auction owner gets only 100 paid out to him
set_sender(charlie, 0);
auction.payout();
// and `change` 1 is left to Alice balance
// (she will get it back along with her reward)
let change = auction.balances.take(&alice).unwrap();
assert_eq!(change, 1);
}
}
#[ink::test]
#[should_panic(expected = "Ended")]
fn not_ended_no_payout() {
// given
// Alice and Bob
let alice = accounts().alice;
let bob = accounts().bob;
// and an auction
let mut auction = create_auction(Some(1), 10, 20, 0);
run_to_block(27);
// Alice bids
set_sender(alice, 100);
auction.bid();
// then
// as auction is still not ended
// there is no winner yet
// candle is still burning
// and hence payout is not possible
// Bob calls for payout
run_to_block(33);
set_sender(bob, 100);
auction.payout();
// contract panics here
}
#[ink::test]
#[should_panic(expected = "Winner is not detected, no payback is possible!")]
fn no_winner_no_payout() {
// given
// Alice
let alice = accounts().alice;
// and an auction
let mut auction = create_auction(Some(1), 10, 20, 0);
// Alice bids at last block of the Ending period
run_to_block(30);
set_sender(alice, 100);
auction.bid();
// auction is Ended
run_to_block(31 + crate::entropy::RF_DELAY);
auction.find_winner();
// then
// if candle "went out" before that bid
if auction.winner.is_none() {
// as winner is not detected
// hence the payout is not possible
// Alice calls for payout
auction.payout();
// contract panics here
} else {
// this one is to make the test pass
// even if candle went out at last block
panic!("Winner is not detected, no payback is possible!")
}
}
#[ink::test]
#[should_panic(expected = "Auction isn't active!")]
fn cannot_bid_when_ended() {
// given
// default account (Alice)
// and auction starts at block #1 and ended after block #15
let mut auction = create_auction(None, 5, 10, 0);
// when
// Auction is ended, RfDelay
run_to_block(16);
// and Alice tries to make a bid before block #5
auction.bid();
// then
// contract should just panic after this line
}
#[ink::test]
fn bidding_works() {
// given
// Bob
let bob = accounts().bob;
// and the auction
let mut auction = create_auction(None, 5, 10, 0);
// when
// Push block to 1 to make auction started
run_to_block(1);
// Bob bids 100
set_sender(bob, 100);
assert_eq!(auction.bid(), ());
run_to_block(2);
// then
// bid is accepted
assert_eq!(auction.balances.get(&bob), Some(&100));
// and Bob is currently winning
assert_eq!(auction.winning, Some(bob));
// TODO: report problem: neither caller nor callee balances are changed with called payables
// and his balance decreased by the bid amount
// assert_eq!(get_balance(bob),25);
// then
// Bob bids 125
set_sender(bob, 125);
// TODO: report problem to ink_env::test: neither caller nor callee balances are changed with called payables
set_balance(contract_id(), 101);
auction.bid();
run_to_block(5);
// new bid is accepted: balance is updated
assert_eq!(auction.balances.get(&bob), Some(&125));
// and Bob is still winning
assert_eq!(auction.winning, Some(bob));
// and contract paid back the first bid
assert_eq!(get_balance(contract_id()), 1);
}
#[ink::test]
fn winning_data_constructed_correctly() {
// given
// an auction with the following structure:
// [1][2][3][4][5][6][7][8][9][10][11][12][13]
// | opening | ending |
let mut auction = create_auction(Some(2), 4, 7, 0);
// this is needed becase for some reason in tests payables don't add up to contract balance
set_balance(contract_id(), 1000);
// Alice and Bob
let alice = accounts().alice;
let bob = accounts().bob;
// when
// there is no bids
// then
// winning_data initialized with Nones
assert_eq!(auction.winning_data, [None; 8].iter().map(|o| *o).collect());
// when
// there are bids in opening period
run_to_block(3);
// Alice bids 100
set_sender(alice, 100);
auction.bid();
run_to_block(5);
// Bob bids 101
set_sender(bob, 101);
auction.bid();
// then
// the top of these bids goes to index 0
assert_eq!(
auction.winning_data,
[Some((bob, 101)), None, None, None, None, None, None, None]
.iter()
.map(|o| *o)
.collect()
);
// when
// bids added in Ending Period
run_to_block(7);
// Alice bids 102
set_sender(alice, 102);
auction.bid();
run_to_block(9);
// Bob bids 103
set_sender(bob, 103);
auction.bid();
run_to_block(11);
// Alice bids 104
set_sender(alice, 104);
auction.bid();
// then
// bids are accounted for correclty
assert_eq!(
auction.winning_data,
[
Some((bob, 101)),
None,
Some((alice, 102)),
None,
Some((bob, 103)),
None,
Some((alice, 104)),
None
]
.iter()
.map(|o| *o)
.collect()
);
}
#[ink::test]
fn no_winner_until_ended() {
// given
// Alice and Bob
let alice = accounts().alice;
let bob = accounts().bob;
// and an auction
let mut auction = create_auction(None, 5, 10, 0);
// when
// auction starts
run_to_block(1);
// Alice bids 100
set_sender(alice, 100);
auction.bid();
run_to_block(15);
// Bob bids 101
set_sender(bob, 101);
auction.bid();
// then