Skip to content

Commit b9f14a6

Browse files
committed
chore(permission0): accumulate distribution remainder
1 parent d881aab commit b9f14a6

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

pallets/permission0/src/permission/emission.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,14 @@ pub(crate) fn do_distribute_emission<T: Config>(
205205
let streams = streams.keys().filter_map(|id| {
206206
let acc =
207207
AccumulatedStreamAmounts::<T>::get((&contract.grantor, id, permission_id))?;
208+
209+
// You cannot remove the stream from the storage as
210+
// it's needed in the accumulation code
211+
AccumulatedStreamAmounts::<T>::set(
212+
(&contract.grantor, id, permission_id),
213+
Some(Zero::zero()),
214+
);
215+
208216
if acc.is_zero() {
209217
None
210218
} else {
@@ -226,10 +234,19 @@ pub(crate) fn do_distribute_emission<T: Config>(
226234
reason,
227235
);
228236

229-
AccumulatedStreamAmounts::<T>::set(
230-
(&contract.grantor, stream, permission_id),
231-
Some(imbalance.peek()),
232-
);
237+
let remainder = imbalance.peek();
238+
if !remainder.is_zero() {
239+
AccumulatedStreamAmounts::<T>::mutate(
240+
(&contract.grantor, stream, permission_id),
241+
|acc| {
242+
if let Some(acc_value) = acc {
243+
*acc_value = acc_value.saturating_add(remainder);
244+
} else {
245+
*acc = Some(remainder)
246+
}
247+
},
248+
);
249+
}
233250
}
234251
}
235252
EmissionAllocation::FixedAmount(amount) => {

pallets/permission0/tests/stream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ fn stream_accumulates_and_executes_at_threshold() {
188188

189189
let permission_id = assert_ok!(grant_emission_permission(
190190
miner,
191-
val,
191+
miner,
192192
pallet_permission0_api::EmissionAllocation::Streams(streams),
193193
vec![(val, u16::MAX)],
194194
pallet_permission0_api::DistributionControl::Automatic(total_incentives),

0 commit comments

Comments
 (0)