Skip to content

Commit 5ac4880

Browse files
committed
add dividends emission event
1 parent 4fdcdbc commit 5ac4880

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

pallets/subtensor/src/macros/events.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,5 +467,15 @@ mod events {
467467
/// Claim type
468468
root_claim_type: RootClaimTypeEnum,
469469
},
470+
471+
/// Subnet lease dividends have been distributed.
472+
SubnetLeaseDividendsDistributed {
473+
/// The lease ID
474+
lease_id: LeaseId,
475+
/// The contributor
476+
contributor: T::AccountId,
477+
/// The amount of alpha distributed
478+
alpha: AlphaCurrency,
479+
},
470480
}
471481
}

pallets/subtensor/src/subnets/leasing.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ impl<T: Config> Pallet<T> {
302302
.saturating_mul(U64F64::from(total_contributors_cut_alpha.to_u64()))
303303
.ceil()
304304
.saturating_to_num::<u64>();
305+
305306
Self::transfer_stake_within_subnet(
306307
&lease.coldkey,
307308
&lease.hotkey,
@@ -311,6 +312,12 @@ impl<T: Config> Pallet<T> {
311312
alpha_for_contributor.into(),
312313
)?;
313314
alpha_distributed = alpha_distributed.saturating_add(alpha_for_contributor.into());
315+
316+
Self::deposit_event(Event::SubnetLeaseDividendsDistributed {
317+
lease_id,
318+
contributor,
319+
alpha: alpha_for_contributor.into(),
320+
});
314321
}
315322

316323
// Distribute the leftover alpha to the beneficiary
@@ -324,6 +331,11 @@ impl<T: Config> Pallet<T> {
324331
lease.netuid,
325332
beneficiary_cut_alpha.into(),
326333
)?;
334+
Self::deposit_event(Event::SubnetLeaseDividendsDistributed {
335+
lease_id,
336+
contributor: lease.beneficiary.clone(),
337+
alpha: beneficiary_cut_alpha.into(),
338+
});
327339

328340
// Reset the accumulated dividends
329341
AccumulatedLeaseDividends::<T>::insert(lease_id, AlphaCurrency::ZERO);

pallets/subtensor/src/tests/leasing.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,20 +537,23 @@ fn test_distribute_lease_network_dividends_multiple_contributors_works() {
537537
let distributed_alpha =
538538
accumulated_dividends + emissions_share.mul_ceil(owner_cut_alpha.to_u64()).into();
539539
assert_ne!(distributed_alpha, AlphaCurrency::ZERO);
540+
540541
let contributor1_alpha_delta = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
541542
&lease.hotkey,
542543
&contributions[0].0,
543544
lease.netuid,
544545
)
545546
.saturating_sub(contributor1_alpha_before);
546547
assert_ne!(contributor1_alpha_delta, AlphaCurrency::ZERO);
548+
547549
let contributor2_alpha_delta = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
548550
&lease.hotkey,
549551
&contributions[1].0,
550552
lease.netuid,
551553
)
552554
.saturating_sub(contributor2_alpha_before);
553555
assert_ne!(contributor2_alpha_delta, AlphaCurrency::ZERO);
556+
554557
let beneficiary_alpha_delta = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
555558
&lease.hotkey,
556559
&beneficiary,
@@ -571,18 +574,42 @@ fn test_distribute_lease_network_dividends_multiple_contributors_works() {
571574
.ceil()
572575
.to_num::<u64>();
573576
assert_eq!(contributor1_alpha_delta, expected_contributor1_alpha.into());
577+
assert_eq!(
578+
System::events().get(2).expect("Event not found").event,
579+
RuntimeEvent::SubtensorModule(Event::SubnetLeaseDividendsDistributed {
580+
lease_id,
581+
contributor: contributions[0].0.into(),
582+
alpha: expected_contributor1_alpha.into(),
583+
},)
584+
);
574585

575586
let expected_contributor2_alpha =
576587
SubnetLeaseShares::<Test>::get(lease_id, contributions[1].0)
577588
.saturating_mul(U64F64::from(distributed_alpha.to_u64()))
578589
.ceil()
579590
.to_num::<u64>();
580591
assert_eq!(contributor2_alpha_delta, expected_contributor2_alpha.into());
592+
assert_eq!(
593+
System::events().get(5).expect("Event not found").event,
594+
RuntimeEvent::SubtensorModule(Event::SubnetLeaseDividendsDistributed {
595+
lease_id,
596+
contributor: contributions[1].0.into(),
597+
alpha: expected_contributor2_alpha.into(),
598+
},)
599+
);
581600

582601
// The beneficiary should have received the remaining dividends
583602
let expected_beneficiary_alpha = distributed_alpha.to_u64()
584603
- (expected_contributor1_alpha + expected_contributor2_alpha);
585604
assert_eq!(beneficiary_alpha_delta, expected_beneficiary_alpha.into());
605+
assert_eq!(
606+
System::events().get(8).expect("Event not found").event,
607+
RuntimeEvent::SubtensorModule(Event::SubnetLeaseDividendsDistributed {
608+
lease_id,
609+
contributor: beneficiary.into(),
610+
alpha: expected_beneficiary_alpha.into(),
611+
},)
612+
);
586613

587614
// Ensure nothing was accumulated for later distribution
588615
assert_eq!(
@@ -644,6 +671,13 @@ fn test_distribute_lease_network_dividends_only_beneficiary_works() {
644671
)
645672
.saturating_sub(beneficiary_alpha_before);
646673
assert_eq!(beneficiary_alpha_delta, distributed_alpha.into());
674+
assert_last_event::<Test>(RuntimeEvent::SubtensorModule(
675+
Event::SubnetLeaseDividendsDistributed {
676+
lease_id,
677+
contributor: beneficiary.into(),
678+
alpha: distributed_alpha,
679+
},
680+
));
647681

648682
// Ensure nothing was accumulated for later distribution
649683
assert_eq!(

0 commit comments

Comments
 (0)