emit TipClosed event on success tip payout (#5656)

* emit TipClosed event on success tip payout

* test for events

* bump version
This commit is contained in:
Xiliang Chen
2020-04-17 20:37:00 +12:00
committed by GitHub
parent a42d87483c
commit 7dcff4c1e2
3 changed files with 59 additions and 7 deletions
+1 -1
View File
@@ -127,7 +127,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 242,
spec_version: 243,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
};
+3 -2
View File
@@ -549,7 +549,7 @@ decl_module! {
// closed.
Reasons::<T>::remove(&tip.reason);
Tips::<T>::remove(hash);
Self::payout_tip(tip);
Self::payout_tip(hash, tip);
}
fn on_initialize(n: T::BlockNumber) -> Weight {
@@ -627,7 +627,7 @@ impl<T: Trait> Module<T> {
///
/// Up to three balance operations.
/// Plus `O(T)` (`T` is Tippers length).
fn payout_tip(tip: OpenTip<T::AccountId, BalanceOf<T>, T::BlockNumber, T::Hash>) {
fn payout_tip(hash: T::Hash, tip: OpenTip<T::AccountId, BalanceOf<T>, T::BlockNumber, T::Hash>) {
let mut tips = tip.tips;
Self::retain_active_tips(&mut tips);
tips.sort_by_key(|i| i.1);
@@ -647,6 +647,7 @@ impl<T: Trait> Module<T> {
}
// same as above: best-effort only.
let _ = T::Currency::transfer(&treasury, &tip.who, payout, KeepAlive);
Self::deposit_event(RawEvent::TipClosed(hash, tip.who, payout));
}
// Spend some money!
+55 -4
View File
@@ -21,7 +21,7 @@
use super::*;
use std::cell::RefCell;
use frame_support::{
assert_noop, assert_ok, impl_outer_origin, parameter_types, weights::Weight,
assert_noop, assert_ok, impl_outer_origin, impl_outer_event, parameter_types, weights::Weight,
traits::{Contains, OnInitialize}
};
use sp_core::H256;
@@ -35,6 +35,21 @@ impl_outer_origin! {
pub enum Origin for Test where system = frame_system {}
}
mod treasury {
// Re-export needed for `impl_outer_event!`.
pub use super::super::*;
}
impl_outer_event! {
pub enum Event for Test {
system<T>,
pallet_balances<T>,
treasury<T>,
}
}
#[derive(Clone, Eq, PartialEq)]
pub struct Test;
parameter_types! {
@@ -53,7 +68,7 @@ impl frame_system::Trait for Test {
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = ();
type Event = Event;
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type DbWeight = ();
@@ -70,7 +85,7 @@ parameter_types! {
}
impl pallet_balances::Trait for Test {
type Balance = u64;
type Event = ();
type Event = Event;
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
@@ -113,7 +128,7 @@ impl Trait for Test {
type TipFindersFee = TipFindersFee;
type TipReportDepositBase = TipReportDepositBase;
type TipReportDepositPerByte = TipReportDepositPerByte;
type Event = ();
type Event = Event;
type ProposalRejection = ();
type ProposalBond = ProposalBond;
type ProposalBondMinimum = ProposalBondMinimum;
@@ -206,15 +221,41 @@ fn report_awesome_from_beneficiary_and_tip_works() {
#[test]
fn close_tip_works() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
Balances::make_free_balance_be(&Treasury::account_id(), 101);
assert_eq!(Treasury::pot(), 100);
assert_ok!(Treasury::tip_new(Origin::signed(10), b"awesome.dot".to_vec(), 3, 10));
let h = tip_hash();
assert_eq!(
System::events().into_iter().map(|r| r.event)
.filter_map(|e| {
if let Event::treasury(inner) = e { Some(inner) } else { None }
})
.last()
.unwrap(),
RawEvent::NewTip(h),
);
assert_ok!(Treasury::tip(Origin::signed(11), h.clone(), 10));
assert_noop!(Treasury::close_tip(Origin::signed(0), h.into()), Error::<Test>::StillOpen);
assert_ok!(Treasury::tip(Origin::signed(12), h.clone(), 10));
assert_eq!(
System::events().into_iter().map(|r| r.event)
.filter_map(|e| {
if let Event::treasury(inner) = e { Some(inner) } else { None }
})
.last()
.unwrap(),
RawEvent::TipClosing(h),
);
assert_noop!(Treasury::close_tip(Origin::signed(0), h.into()), Error::<Test>::Premature);
System::set_block_number(2);
@@ -222,6 +263,16 @@ fn close_tip_works() {
assert_ok!(Treasury::close_tip(Origin::signed(0), h.into()));
assert_eq!(Balances::free_balance(3), 10);
assert_eq!(
System::events().into_iter().map(|r| r.event)
.filter_map(|e| {
if let Event::treasury(inner) = e { Some(inner) } else { None }
})
.last()
.unwrap(),
RawEvent::TipClosed(h, 3, 10),
);
assert_noop!(Treasury::close_tip(Origin::signed(100), h.into()), Error::<Test>::UnknownTip);
});
}