Skip to content
Snippets Groups Projects
Commit bade464b authored by wangjj9219's avatar wangjj9219 Committed by Xiliang Chen
Browse files

add remove_auction function (#68)

* add remove_auction function

* add Eq and PartialEq traits for AuctionId
parent ce13ee06
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, One, Saturating, SimpleArithmetic, Zero}, traits::{MaybeSerializeDeserialize, Member, One, SimpleArithmetic, Zero},
DispatchResult, DispatchResult,
}; };
...@@ -38,7 +38,7 @@ type AuctionIdLinkedItem<T> = LinkedItem<<T as Trait>::AuctionId>; ...@@ -38,7 +38,7 @@ type AuctionIdLinkedItem<T> = LinkedItem<<T as Trait>::AuctionId>;
decl_storage! { decl_storage! {
trait Store for Module<T: Trait> as Auction { trait Store for Module<T: Trait> as Auction {
pub Auctions get(fn auctions): map T::AuctionId => Option<AuctionInfo<T::AccountId, T::Balance, T::BlockNumber>>; pub Auctions get(fn auctions): map T::AuctionId => Option<AuctionInfo<T::AccountId, T::Balance, T::BlockNumber>>;
pub AuctionsCount get(fn auctions_count): T::AuctionId; pub AuctionsIndex get(fn auctions_index): T::AuctionId;
pub AuctionEndTime get(fn auction_end_time): map(T::BlockNumber, Option<T::AuctionId>) => Option<AuctionIdLinkedItem<T>>; pub AuctionEndTime get(fn auction_end_time): map(T::BlockNumber, Option<T::AuctionId>) => Option<AuctionIdLinkedItem<T>>;
} }
} }
...@@ -105,14 +105,11 @@ decl_error! { ...@@ -105,14 +105,11 @@ decl_error! {
impl<T: Trait> Module<T> { impl<T: Trait> Module<T> {
fn _on_finalize(now: T::BlockNumber) { fn _on_finalize(now: T::BlockNumber) {
let ended_auctions = <AuctionEndTimeList<T>>::take_all(&now); let ended_auctions = <AuctionEndTimeList<T>>::take_all(&now);
let mut count = Self::auctions_count();
ended_auctions.for_each(|auction_id| { ended_auctions.for_each(|auction_id| {
if let Some(auction) = <Auctions<T>>::take(&auction_id) { if let Some(auction) = <Auctions<T>>::take(&auction_id) {
T::Handler::on_auction_ended(auction_id, auction.bid.clone()); T::Handler::on_auction_ended(auction_id, auction.bid.clone());
count = count.saturating_sub(T::AuctionId::one());
} }
}); });
<AuctionsCount<T>>::put(count);
} }
} }
...@@ -141,8 +138,8 @@ impl<T: Trait> Auction<T::AccountId, T::BlockNumber> for Module<T> { ...@@ -141,8 +138,8 @@ impl<T: Trait> Auction<T::AccountId, T::BlockNumber> for Module<T> {
fn new_auction(start: T::BlockNumber, end: Option<T::BlockNumber>) -> Self::AuctionId { fn new_auction(start: T::BlockNumber, end: Option<T::BlockNumber>) -> Self::AuctionId {
let auction = AuctionInfo { bid: None, start, end }; let auction = AuctionInfo { bid: None, start, end };
let auction_id = Self::auctions_count(); let auction_id = Self::auctions_index();
<AuctionsCount<T>>::mutate(|n| *n += Self::AuctionId::from(1)); <AuctionsIndex<T>>::mutate(|n| *n += Self::AuctionId::one());
<Auctions<T>>::insert(auction_id, auction); <Auctions<T>>::insert(auction_id, auction);
if let Some(end_block) = end { if let Some(end_block) = end {
<AuctionEndTimeList<T>>::append(&end_block, auction_id); <AuctionEndTimeList<T>>::append(&end_block, auction_id);
...@@ -150,4 +147,12 @@ impl<T: Trait> Auction<T::AccountId, T::BlockNumber> for Module<T> { ...@@ -150,4 +147,12 @@ impl<T: Trait> Auction<T::AccountId, T::BlockNumber> for Module<T> {
auction_id auction_id
} }
fn remove_auction(id: Self::AuctionId) {
if let Some(auction) = <Auctions<T>>::take(&id) {
if let Some(end_block) = auction.end {
<AuctionEndTimeList<T>>::remove(&end_block, id);
}
}
}
} }
...@@ -71,18 +71,33 @@ fn bid_should_fail() { ...@@ -71,18 +71,33 @@ fn bid_should_fail() {
}); });
} }
#[test]
fn remove_auction_should_work() {
ExtBuilder::default().build().execute_with(|| {
assert_eq!(AuctionModule::new_auction(10, Some(100)), 0);
assert_eq!(AuctionModule::auctions_index(), 1);
assert_eq!(AuctionModule::auctions(0).is_some(), true);
assert_eq!(AuctionModule::auction_end_time((100, Some(0))).is_some(), true);
AuctionModule::remove_auction(0);
assert_eq!(AuctionModule::auctions(0), None);
assert_eq!(AuctionModule::auction_end_time((100, Some(0))), None);
});
}
#[test] #[test]
fn cleanup_auction_should_work() { fn cleanup_auction_should_work() {
ExtBuilder::default().build().execute_with(|| { ExtBuilder::default().build().execute_with(|| {
assert_eq!(AuctionModule::new_auction(10, Some(100)), 0); assert_eq!(AuctionModule::new_auction(10, Some(100)), 0);
assert_eq!(AuctionModule::auctions_count(), 1); assert_eq!(AuctionModule::auctions_index(), 1);
assert_eq!(AuctionModule::new_auction(10, Some(50)), 1); assert_eq!(AuctionModule::new_auction(10, Some(50)), 1);
assert_eq!(AuctionModule::auctions_count(), 2); assert_eq!(AuctionModule::auctions_index(), 2);
AuctionModule::on_finalize(1); assert_eq!(AuctionModule::auctions(0).is_some(), true);
assert_eq!(AuctionModule::auctions_count(), 2); assert_eq!(AuctionModule::auctions(1).is_some(), true);
AuctionModule::on_finalize(50); AuctionModule::on_finalize(50);
assert_eq!(AuctionModule::auctions_count(), 1); assert_eq!(AuctionModule::auctions(0).is_some(), true);
assert_eq!(AuctionModule::auctions(1).is_some(), false);
AuctionModule::on_finalize(100); AuctionModule::on_finalize(100);
assert_eq!(AuctionModule::auctions_count(), 0); assert_eq!(AuctionModule::auctions(0).is_some(), false);
assert_eq!(AuctionModule::auctions(1).is_some(), false);
}); });
} }
use codec::FullCodec; use codec::FullCodec;
use codec::{Decode, Encode}; use codec::{Decode, Encode};
use rstd::fmt::Debug; use rstd::{
cmp::{Eq, PartialEq},
fmt::Debug,
};
use sp_runtime::{ use sp_runtime::{
traits::{MaybeSerializeDeserialize, SimpleArithmetic}, traits::{MaybeSerializeDeserialize, SimpleArithmetic},
DispatchResult, RuntimeDebug, DispatchResult, RuntimeDebug,
...@@ -21,7 +24,7 @@ pub struct AuctionInfo<AccountId, Balance, BlockNumber> { ...@@ -21,7 +24,7 @@ pub struct AuctionInfo<AccountId, Balance, BlockNumber> {
/// Abstraction over a simple auction system. /// Abstraction over a simple auction system.
pub trait Auction<AccountId, BlockNumber> { pub trait Auction<AccountId, BlockNumber> {
/// The id of an AuctionInfo /// The id of an AuctionInfo
type AuctionId: FullCodec + Default + Copy + MaybeSerializeDeserialize + Debug; type AuctionId: FullCodec + Default + Copy + Eq + PartialEq + MaybeSerializeDeserialize + Debug;
/// The price to bid. /// The price to bid.
type Balance: SimpleArithmetic + FullCodec + Copy + MaybeSerializeDeserialize + Debug + Default; type Balance: SimpleArithmetic + FullCodec + Copy + MaybeSerializeDeserialize + Debug + Default;
...@@ -31,6 +34,8 @@ pub trait Auction<AccountId, BlockNumber> { ...@@ -31,6 +34,8 @@ pub trait Auction<AccountId, BlockNumber> {
fn update_auction(id: Self::AuctionId, info: AuctionInfo<AccountId, Self::Balance, BlockNumber>) -> DispatchResult; fn update_auction(id: Self::AuctionId, info: AuctionInfo<AccountId, Self::Balance, BlockNumber>) -> DispatchResult;
/// Create new auction with specific startblock and endblock, return the id of the auction /// Create new auction with specific startblock and endblock, return the id of the auction
fn new_auction(start: BlockNumber, end: Option<BlockNumber>) -> Self::AuctionId; fn new_auction(start: BlockNumber, end: Option<BlockNumber>) -> Self::AuctionId;
/// Remove auction by `id`
fn remove_auction(id: Self::AuctionId);
} }
/// The result of bid handling. /// The result of bid handling.
......
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