Skip to content

Commit

Permalink
feat(sponsorship): add more details to sponsorship events (#798)
Browse files Browse the repository at this point in the history
  • Loading branch information
aliXsed committed Nov 15, 2023
1 parent dede3e5 commit 77e6009
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 66 deletions.
69 changes: 56 additions & 13 deletions pallets/sponsorship/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,42 @@ pub mod pallet {
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// Event emitted when a new pot is created.
PotCreated { pot: T::PotId },
PotCreated {
pot: T::PotId,
sponsor: T::AccountId,
sponsorship_type: T::SponsorshipType,
fee_quota: BalanceOf<T>,
reserve_quota: BalanceOf<T>,
},
/// Event emitted when a pot is removed.
PotRemoved { pot: T::PotId },
/// Event emitted when a pot is updated.
PotUpdated { pot: T::PotId },
PotUpdated {
pot: T::PotId,
fee_quota: BalanceOf<T>,
reserve_quota: BalanceOf<T>,
},
/// Event emitted when a pot is updated.
PotSponsorshipTypeUpdated { pot: T::PotId },
PotSponsorshipTypeUpdated {
pot: T::PotId,
sponsorship_type: T::SponsorshipType,
},
/// Event emitted when user/users are registered indicating the list of them
UsersRegistered { pot: T::PotId, users: Vec<T::AccountId> },
UsersRegistered {
pot: T::PotId,
users: Vec<T::AccountId>,
fee_quota: BalanceOf<T>,
reserve_quota: BalanceOf<T>,
},
/// Event emitted when user/users are removed indicating the list of them
UsersRemoved { pot: T::PotId, users: Vec<T::AccountId> },
/// Event emitted when fee_quota or reserve_quota or both are updated for the given list
UsersLimitsUpdated { pot: T::PotId, users: Vec<T::AccountId> },
UsersLimitsUpdated {
pot: T::PotId,
users: Vec<T::AccountId>,
fee_quota: BalanceOf<T>,
reserve_quota: BalanceOf<T>,
},
/// Event emitted when a sponsor_me call has been successful indicating the reserved amount
Sponsored { paid: BalanceOf<T>, repaid: BalanceOf<T> },
/// Event emitted when the transaction fee is paid showing the payer and the amount
Expand Down Expand Up @@ -222,14 +245,20 @@ pub mod pallet {
<Pot<T>>::insert(
pot,
PotDetailsOf::<T> {
sponsor: who,
sponsorship_type,
sponsor: who.clone(),
sponsorship_type: sponsorship_type.clone(),
fee_quota: LimitedBalance::with_limit(fee_quota),
reserve_quota: LimitedBalance::with_limit(reserve_quota),
},
);

Self::deposit_event(Event::PotCreated { pot });
Self::deposit_event(Event::PotCreated {
pot,
sponsor: who,
sponsorship_type,
fee_quota,
reserve_quota,
});
Ok(())
}

Expand Down Expand Up @@ -287,7 +316,12 @@ pub mod pallet {
},
);
}
Self::deposit_event(Event::UsersRegistered { pot, users });
Self::deposit_event(Event::UsersRegistered {
pot,
users,
fee_quota: common_fee_quota,
reserve_quota: common_reserve_quota,
});
Ok(())
}

Expand Down Expand Up @@ -403,7 +437,11 @@ pub mod pallet {
.map_err(|_| Error::<T>::CannotUpdateReserveLimit)?;

<Pot<T>>::insert(pot, pot_details);
Self::deposit_event(Event::PotUpdated { pot });
Self::deposit_event(Event::PotUpdated {
pot,
fee_quota: new_fee_quota,
reserve_quota: new_reserve_quota,
});
Ok(())
}

Expand Down Expand Up @@ -437,7 +475,12 @@ pub mod pallet {
}

<Pot<T>>::insert(pot, pot_details);
Self::deposit_event(Event::UsersLimitsUpdated { pot, users });
Self::deposit_event(Event::UsersLimitsUpdated {
pot,
users,
fee_quota: new_fee_quota,
reserve_quota: new_reserve_quota,
});
Ok(())
}

Expand All @@ -455,11 +498,11 @@ pub mod pallet {
Pot::<T>::try_mutate(pot, |maybe_pot_details| -> DispatchResult {
let pot_details = maybe_pot_details.as_mut().ok_or(Error::<T>::PotNotExist)?;
ensure!(pot_details.sponsor == who, Error::<T>::NoPermission);
pot_details.sponsorship_type = sponsorship_type;
pot_details.sponsorship_type = sponsorship_type.clone();
Ok(())
})?;

Self::deposit_event(Event::PotSponsorshipTypeUpdated { pot });
Self::deposit_event(Event::PotSponsorshipTypeUpdated { pot, sponsorship_type });
Ok(())
}
}
Expand Down
43 changes: 40 additions & 3 deletions pallets/sponsorship/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,16 @@ fn creator_of_pot_becomes_sponsor() {
pot_details.reserve_quota.limit()
));
assert_eq!(Pot::<Test>::get(pot), Some(pot_details));
System::assert_last_event(Event::PotCreated { pot }.into());
System::assert_last_event(
Event::PotCreated {
pot,
sponsor: 1,
sponsorship_type: SponsorshipType::Uniques,
fee_quota: 5,
reserve_quota: 7,
}
.into(),
);
});
}

Expand Down Expand Up @@ -239,7 +248,14 @@ fn sponsors_can_always_increase_pot_limits() {
unused_pot_details.fee_quota.limit() + 1,
unused_pot_details.reserve_quota.limit() + 1
));
System::assert_last_event(Event::PotUpdated { pot: unused_pot }.into());
System::assert_last_event(
Event::PotUpdated {
pot: unused_pot,
fee_quota: 6,
reserve_quota: 8,
}
.into(),
);
let updated_pot = Pot::<Test>::get(unused_pot).unwrap();
assert_eq!(updated_pot.fee_quota.limit(), unused_pot_details.fee_quota.limit() + 1);
assert_eq!(
Expand All @@ -264,7 +280,14 @@ fn sponsors_can_always_increase_pot_limits() {
fully_used_pot_details.fee_quota.limit() + 1,
fully_used_pot_details.reserve_quota.limit() + 1
));
System::assert_last_event(Event::PotUpdated { pot: fully_used_pot }.into());
System::assert_last_event(
Event::PotUpdated {
pot: fully_used_pot,
fee_quota: 6,
reserve_quota: 8,
}
.into(),
);
let updated_pot = Pot::<Test>::get(fully_used_pot).unwrap();
assert_eq!(
updated_pot.fee_quota.limit(),
Expand Down Expand Up @@ -353,6 +376,8 @@ fn sponsors_can_decrease_pot_limits_only_when_available_margin_allows() {
System::assert_last_event(
Event::PotUpdated {
pot: partially_used_pot,
fee_quota: 4,
reserve_quota: 3,
}
.into(),
);
Expand Down Expand Up @@ -446,6 +471,8 @@ fn sponsors_can_register_new_users() {
Event::UsersRegistered {
pot,
users: vec![user_1, user_2],
fee_quota: common_fee_quota,
reserve_quota: common_reserve_quota,
}
.into(),
);
Expand Down Expand Up @@ -474,6 +501,8 @@ fn sponsors_can_register_new_users() {
Event::UsersRegistered {
pot,
users: vec![user_3],
fee_quota: user_3_fee_quota,
reserve_quota: user_3_reserve_quota,
}
.into(),
);
Expand Down Expand Up @@ -684,6 +713,8 @@ fn only_sponsors_have_permission_to_update_users_limits() {
Event::UsersLimitsUpdated {
pot,
users: vec![user_1, user_2],
fee_quota: common_fee_quota + 1,
reserve_quota: common_reserve_quota + 1,
}
.into(),
);
Expand Down Expand Up @@ -836,6 +867,8 @@ fn sponsors_can_always_set_user_limits_to_an_amount_equal_or_greater_than_before
Event::UsersLimitsUpdated {
pot,
users: vec![user_1, user_2],
fee_quota: common_fee_quota + 1,
reserve_quota: common_reserve_quota + 1,
}
.into(),
);
Expand Down Expand Up @@ -933,6 +966,8 @@ fn sponsors_can_reduce_user_limits_when_available_margin_allows() {
Event::UsersLimitsUpdated {
pot,
users: vec![user_1],
fee_quota: lowest_fee_limit,
reserve_quota: lowest_reserve_limit,
}
.into(),
);
Expand All @@ -949,6 +984,8 @@ fn sponsors_can_reduce_user_limits_when_available_margin_allows() {
Event::UsersLimitsUpdated {
pot,
users: vec![user_2],
fee_quota: lowest_fee_limit - 1,
reserve_quota: lowest_reserve_limit - 1,
}
.into(),
);
Expand Down
Loading

0 comments on commit 77e6009

Please sign in to comment.