Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Allow retract_tip on tip_new
  • Loading branch information
shawntabrizi committed Jun 25, 2020
commit 6498495e1e4c76b97ca98e9e5d22addd67acd37c
48 changes: 35 additions & 13 deletions frame/treasury/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,17 @@ pub struct OpenTip<
reason: Hash,
/// The account to be tipped.
who: AccountId,
/// The account who began this tip and the amount held on deposit.
finder: Option<(AccountId, Balance)>,
/// The account who began this tip.
finder: AccountId,
/// The amount held on deposit for this tip.
deposit: Balance,
/// The block number at which this tip will close if `Some`. If `None`, then no closing is
/// scheduled.
closes: Option<BlockNumber>,
/// The members who have voted for this tip. Sorted by AccountId.
tips: Vec<(AccountId, Balance)>,
/// Whether this tip should result in the finder taking a fee.
finders_fee: bool,
}

decl_storage! {
Expand Down Expand Up @@ -428,8 +432,15 @@ decl_module! {
T::Currency::reserve(&finder, deposit)?;

Reasons::<T>::insert(&reason_hash, &reason);
let finder = Some((finder, deposit));
let tip = OpenTip { reason: reason_hash, who, finder, closes: None, tips: vec![] };
let tip = OpenTip {
reason: reason_hash,
who,
finder,
deposit,
closes: None,
tips: vec![],
finders_fee: true
};
Tips::<T>::insert(&hash, tip);
Self::deposit_event(RawEvent::NewTip(hash));
}
Expand Down Expand Up @@ -457,12 +468,13 @@ decl_module! {
fn retract_tip(origin, hash: T::Hash) {
let who = ensure_signed(origin)?;
let tip = Tips::<T>::get(&hash).ok_or(Error::<T>::UnknownTip)?;
let (finder, deposit) = tip.finder.ok_or(Error::<T>::NotFinder)?;
ensure!(finder == who, Error::<T>::NotFinder);
ensure!(tip.finder == who, Error::<T>::NotFinder);

Reasons::<T>::remove(&tip.reason);
Tips::<T>::remove(&hash);
let _ = T::Currency::unreserve(&who, deposit);
if !tip.deposit.is_zero() {
let _ = T::Currency::unreserve(&who, tip.deposit);
}
Self::deposit_event(RawEvent::TipRetracted(hash));
}

Expand Down Expand Up @@ -501,8 +513,16 @@ decl_module! {

Reasons::<T>::insert(&reason_hash, &reason);
Self::deposit_event(RawEvent::NewTip(hash.clone()));
let tips = vec![(tipper, tip_value)];
let tip = OpenTip { reason: reason_hash, who, finder: None, closes: None, tips };
let tips = vec![(tipper.clone(), tip_value)];
let tip = OpenTip {
reason: reason_hash,
who,
finder: tipper,
deposit: Zero::zero(),
closes: None,
tips,
finders_fee: false,
};
Tips::<T>::insert(&hash, tip);
}

Expand Down Expand Up @@ -667,15 +687,17 @@ impl<T: Trait> Module<T> {
let treasury = Self::account_id();
let max_payout = Self::pot();
let mut payout = tips[tips.len() / 2].1.min(max_payout);
if let Some((finder, deposit)) = tip.finder {
let _ = T::Currency::unreserve(&finder, deposit);
if finder != tip.who {
if !tip.deposit.is_zero() {
let _ = T::Currency::unreserve(&tip.finder, tip.deposit);
}
if tip.finders_fee {
if tip.finder != tip.who {
// pay out the finder's fee.
let finders_fee = T::TipFindersFee::get() * payout;
payout -= finders_fee;
// this should go through given we checked it's at most the free balance, but still
// we only make a best-effort.
let _ = T::Currency::transfer(&treasury, &finder, finders_fee, KeepAlive);
let _ = T::Currency::transfer(&treasury, &tip.finder, finders_fee, KeepAlive);
}
}
// same as above: best-effort only.
Expand Down
12 changes: 12 additions & 0 deletions frame/treasury/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ fn close_tip_works() {
#[test]
fn retract_tip_works() {
new_test_ext().execute_with(|| {
// with report awesome
Balances::make_free_balance_be(&Treasury::account_id(), 101);
assert_ok!(Treasury::report_awesome(Origin::signed(0), b"awesome.dot".to_vec(), 3));
let h = tip_hash();
Expand All @@ -303,6 +304,17 @@ fn retract_tip_works() {
assert_ok!(Treasury::retract_tip(Origin::signed(0), h.clone()));
System::set_block_number(2);
assert_noop!(Treasury::close_tip(Origin::signed(0), h.into()), Error::<Test>::UnknownTip);

// with tip new
Balances::make_free_balance_be(&Treasury::account_id(), 101);
assert_ok!(Treasury::tip_new(Origin::signed(10), b"awesome.dot".to_vec(), 3, 10));
let h = tip_hash();
assert_ok!(Treasury::tip(Origin::signed(11), h.clone(), 10));
assert_ok!(Treasury::tip(Origin::signed(12), h.clone(), 10));
assert_noop!(Treasury::retract_tip(Origin::signed(0), h.clone()), Error::<Test>::NotFinder);
assert_ok!(Treasury::retract_tip(Origin::signed(10), h.clone()));
System::set_block_number(2);
assert_noop!(Treasury::close_tip(Origin::signed(10), h.into()), Error::<Test>::UnknownTip);
});
}

Expand Down