diff --git a/pallets/slow-clap/Cargo.toml b/pallets/slow-clap/Cargo.toml index be4301f..0b9bc67 100644 --- a/pallets/slow-clap/Cargo.toml +++ b/pallets/slow-clap/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ghost-slow-clap" -version = "0.3.20" +version = "0.3.21" description = "Applause protocol for the EVM bridge" license.workspace = true authors.workspace = true diff --git a/pallets/slow-clap/src/lib.rs b/pallets/slow-clap/src/lib.rs index 01b8111..f30e3f2 100644 --- a/pallets/slow-clap/src/lib.rs +++ b/pallets/slow-clap/src/lib.rs @@ -573,11 +573,11 @@ impl Pallet { return Ok(()) } - let incoming_fee = T::NetworkDataHandler::get(&clap.network_id) + let commission = T::NetworkDataHandler::get(&clap.network_id) .map(|network_data| Perbill::from_parts(network_data.incoming_fee)) - .unwrap_or_default(); + .unwrap_or_default() + .mul_ceil(clap.amount); - let commission = incoming_fee.mul_ceil(clap.amount); let final_amount = clap.amount .checked_sub(&commission) .map(|value| T::Currency::minimum_balance() diff --git a/pallets/slow-clap/src/tests.rs b/pallets/slow-clap/src/tests.rs index a39b5a5..069454e 100644 --- a/pallets/slow-clap/src/tests.rs +++ b/pallets/slow-clap/src/tests.rs @@ -660,7 +660,70 @@ fn should_self_applause_if_enough_received_claps() { }); } -// TODO: check event +#[test] +fn should_emit_event_on_each_clap_and_on_applause() { + let (network_id, transaction_hash, unique_transaction_hash) = + generate_unique_hash(None, None, None, None); + let (_, receiver, amount) = get_mocked_metadata(); + + new_test_ext().execute_with(|| { + let commission = Perbill::from_parts(100_000_000); // 10% + let commission_amount = commission.mul_ceil(amount); + let amount_after_commission = amount.saturating_sub(commission_amount); + let _ = prepare_evm_network(Some(network_id), Some(100_000_000)); + let session_index = advance_session_and_get_index(); + let storage_key = (session_index, transaction_hash, unique_transaction_hash); + + assert_eq!(pallet::ApplausesForTransaction::::get(&storage_key), false); + assert_claps_info_correct(&storage_key, &session_index, 0); + assert_ok!(do_clap_from(session_index, network_id, 0, false)); + System::assert_last_event(RuntimeEvent::SlowClap( + crate::Event::Clapped { + receiver: receiver.clone(), + authority_id: 0, + network_id, + transaction_hash, + amount, + })); + + assert_eq!(pallet::ApplausesForTransaction::::get(&storage_key), false); + assert_claps_info_correct(&storage_key, &session_index, 1); + assert_ok!(do_clap_from(session_index, network_id, 1, false)); + let binding = System::events(); + let last_two_events = binding + .iter() + .rev() + .take(5) + .collect::>(); + assert_eq!(last_two_events[0].event, RuntimeEvent::SlowClap( + crate::Event::Applaused { + network_id, + receiver: receiver.clone(), + received_amount: amount_after_commission, + })); + assert_eq!(last_two_events[4].event, RuntimeEvent::SlowClap( + crate::Event::Clapped { + receiver: receiver.clone(), + authority_id: 1, + network_id, + transaction_hash, + amount, + })); + + assert_eq!(pallet::ApplausesForTransaction::::get(&storage_key), true); + assert_claps_info_correct(&storage_key, &session_index, 2); + assert_ok!(do_clap_from(session_index, network_id, 2, false)); + System::assert_last_event(RuntimeEvent::SlowClap( + crate::Event::Clapped { + receiver: receiver.clone(), + authority_id: 2, + network_id, + transaction_hash, + amount, + })); + }); +} + // TODO: multiple logs will create multiple records // TODO: errors should be checked as much as possible // TODO: offences generated as expected