Skip to content
Snippets Groups Projects
Commit 0526dd25 authored by Ermal Kaleci's avatar Ermal Kaleci Committed by Xiliang Chen
Browse files

update ended auctions cleanup (#67)

* update ended auctions cleanup

* update

* update
parent 18ffc4b9
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,7 @@ use frame_support::{decl_error, decl_event, decl_module, decl_storage, ensure, P ...@@ -4,7 +4,7 @@ use frame_support::{decl_error, decl_event, decl_module, decl_storage, ensure, P
use frame_system::{self as system, ensure_signed}; use frame_system::{self as system, ensure_signed};
use orml_utilities::{LinkedItem, LinkedList}; use orml_utilities::{LinkedItem, LinkedList};
use sp_runtime::{ use sp_runtime::{
traits::{MaybeSerializeDeserialize, Member, SimpleArithmetic, Zero}, traits::{MaybeSerializeDeserialize, Member, One, Saturating, SimpleArithmetic, Zero},
DispatchResult, DispatchResult,
}; };
...@@ -87,22 +87,7 @@ decl_module! { ...@@ -87,22 +87,7 @@ decl_module! {
} }
fn on_finalize(now: T::BlockNumber) { fn on_finalize(now: T::BlockNumber) {
let head_key: Option<T::AuctionId> = None; Self::_on_finalize(now);
if let Some(mut head_item) = <AuctionEndTime<T>>::get((now, head_key)) {
while let Some(auction_id) = head_item.next {
if let Some(auction) = Self::auctions(auction_id) {
T::Handler::on_auction_ended(auction_id, auction.bid);
<Auctions<T>>::remove(auction_id);
}
head_item = <AuctionEndTime<T>>::get((now, Some(auction_id))).unwrap_or_else(|| LinkedItem {
prev: None,
next: None,
});
<AuctionEndTime<T>>::remove((now, Some(auction_id)));
}
<AuctionEndTime<T>>::remove((now, head_key));
}
} }
} }
} }
...@@ -117,7 +102,19 @@ decl_error! { ...@@ -117,7 +102,19 @@ decl_error! {
} }
} }
impl<T: Trait> Module<T> {} impl<T: Trait> Module<T> {
fn _on_finalize(now: T::BlockNumber) {
let ended_auctions = <AuctionEndTimeList<T>>::take_all(&now);
let mut count = Self::auctions_count();
ended_auctions.for_each(|auction_id| {
if let Some(auction) = <Auctions<T>>::take(&auction_id) {
T::Handler::on_auction_ended(auction_id, auction.bid.clone());
count = count.saturating_sub(T::AuctionId::one());
}
});
<AuctionsCount<T>>::put(count);
}
}
impl<T: Trait> Auction<T::AccountId, T::BlockNumber> for Module<T> { impl<T: Trait> Auction<T::AccountId, T::BlockNumber> for Module<T> {
type AuctionId = T::AuctionId; type AuctionId = T::AuctionId;
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
use super::*; use super::*;
use frame_support::assert_ok; use frame_support::assert_ok;
use mock::{AuctionModule, ExtBuilder, Runtime, ALICE}; use mock::{AuctionModule, ExtBuilder, Runtime, ALICE};
use sp_runtime::traits::OnFinalize;
#[test] #[test]
fn new_auction_should_work() { fn new_auction_should_work() {
...@@ -69,3 +70,19 @@ fn bid_should_fail() { ...@@ -69,3 +70,19 @@ fn bid_should_fail() {
); );
}); });
} }
#[test]
fn cleanup_auction_should_work() {
ExtBuilder::default().build().execute_with(|| {
assert_eq!(AuctionModule::new_auction(10, Some(100)), 0);
assert_eq!(AuctionModule::auctions_count(), 1);
assert_eq!(AuctionModule::new_auction(10, Some(50)), 1);
assert_eq!(AuctionModule::auctions_count(), 2);
AuctionModule::on_finalize(1);
assert_eq!(AuctionModule::auctions_count(), 2);
AuctionModule::on_finalize(50);
assert_eq!(AuctionModule::auctions_count(), 1);
AuctionModule::on_finalize(100);
assert_eq!(AuctionModule::auctions_count(), 0);
});
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment