From 650b124fd81f4a438c212cb010cc0a730bac5c2d Mon Sep 17 00:00:00 2001
From: Bolaji Ahmad <56865496+bolajahmad@users.noreply.github.com>
Date: Tue, 28 May 2024 15:44:58 +0100
Subject: [PATCH] Improve On_demand_assigner events (#4339)

title: Improving `on_demand_assigner` emitted events

doc:
  - audience: Rutime User
description: OnDemandOrderPlaced event that is useful for indexers to
save data related to on demand orders. Check [discussion
here](https://substrate.stackexchange.com/questions/11366/ondemandassignmentprovider-ondemandorderplaced-event-was-removed/11389#11389).

Closes #4254

crates: [ 'runtime-parachain]

---------

Co-authored-by: Maciej <maciej.zyszkiewicz@parity.io>
---
 .../parachains/src/assigner_on_demand/mod.rs  | 38 ++++++++++++-------
 prdoc/pr_4339.prdoc                           | 13 +++++++
 2 files changed, 37 insertions(+), 14 deletions(-)
 create mode 100644 prdoc/pr_4339.prdoc

diff --git a/polkadot/runtime/parachains/src/assigner_on_demand/mod.rs b/polkadot/runtime/parachains/src/assigner_on_demand/mod.rs
index 37788a67ea0..795759b3b39 100644
--- a/polkadot/runtime/parachains/src/assigner_on_demand/mod.rs
+++ b/polkadot/runtime/parachains/src/assigner_on_demand/mod.rs
@@ -173,7 +173,7 @@ impl QueueStatusType {
 	fn consume_index(&mut self, removed_index: QueueIndex) {
 		if removed_index != self.smallest_index {
 			self.freed_indices.push(removed_index.reverse());
-			return
+			return;
 		}
 		let mut index = self.smallest_index.0.overflowing_add(1).0;
 		// Even more to advance?
@@ -368,10 +368,10 @@ pub mod pallet {
 	#[pallet::event]
 	#[pallet::generate_deposit(pub(super) fn deposit_event)]
 	pub enum Event<T: Config> {
-		/// An order was placed at some spot price amount.
-		OnDemandOrderPlaced { para_id: ParaId, spot_price: BalanceOf<T> },
-		/// The value of the spot traffic multiplier changed.
-		SpotTrafficSet { traffic: FixedU128 },
+		/// An order was placed at some spot price amount by orderer ordered_by
+		OnDemandOrderPlaced { para_id: ParaId, spot_price: BalanceOf<T>, ordered_by: T::AccountId },
+		/// The value of the spot price has likely changed
+		SpotPriceSet { spot_price: BalanceOf<T> },
 	}
 
 	#[pallet::error]
@@ -410,12 +410,11 @@ pub mod pallet {
 		///
 		/// Errors:
 		/// - `InsufficientBalance`: from the Currency implementation
-		/// - `InvalidParaId`
 		/// - `QueueFull`
 		/// - `SpotPriceHigherThanMaxAmount`
 		///
 		/// Events:
-		/// - `SpotOrderPlaced`
+		/// - `OnDemandOrderPlaced`
 		#[pallet::call_index(0)]
 		#[pallet::weight(<T as Config>::WeightInfo::place_order_allow_death(QueueStatus::<T>::get().size()))]
 		pub fn place_order_allow_death(
@@ -437,12 +436,11 @@ pub mod pallet {
 		///
 		/// Errors:
 		/// - `InsufficientBalance`: from the Currency implementation
-		/// - `InvalidParaId`
 		/// - `QueueFull`
 		/// - `SpotPriceHigherThanMaxAmount`
 		///
 		/// Events:
-		/// - `SpotOrderPlaced`
+		/// - `OnDemandOrderPlaced`
 		#[pallet::call_index(1)]
 		#[pallet::weight(<T as Config>::WeightInfo::place_order_keep_alive(QueueStatus::<T>::get().size()))]
 		pub fn place_order_keep_alive(
@@ -539,12 +537,11 @@ where
 	///
 	/// Errors:
 	/// - `InsufficientBalance`: from the Currency implementation
-	/// - `InvalidParaId`
 	/// - `QueueFull`
 	/// - `SpotPriceHigherThanMaxAmount`
 	///
 	/// Events:
-	/// - `SpotOrderPlaced`
+	/// - `OnDemandOrderPlaced`
 	fn do_place_order(
 		sender: <T as frame_system::Config>::AccountId,
 		max_amount: BalanceOf<T>,
@@ -578,6 +575,12 @@ where
 				Error::<T>::QueueFull
 			);
 			Pallet::<T>::add_on_demand_order(queue_status, para_id, QueuePushDirection::Back);
+			Pallet::<T>::deposit_event(Event::<T>::OnDemandOrderPlaced {
+				para_id,
+				spot_price,
+				ordered_by: sender,
+			});
+
 			Ok(())
 		})
 	}
@@ -599,7 +602,14 @@ where
 				// Only update storage on change
 				if new_traffic != old_traffic {
 					queue_status.traffic = new_traffic;
-					Pallet::<T>::deposit_event(Event::<T>::SpotTrafficSet { traffic: new_traffic });
+
+					// calculate the new spot price
+					let spot_price: BalanceOf<T> = new_traffic.saturating_mul_int(
+						config.scheduler_params.on_demand_base_fee.saturated_into::<BalanceOf<T>>(),
+					);
+
+					// emit the event for updated new price
+					Pallet::<T>::deposit_event(Event::<T>::SpotPriceSet { spot_price });
 				}
 			},
 			Err(err) => {
@@ -721,7 +731,7 @@ where
 			"Decreased affinity for a para that has not been served on a core?"
 		);
 		if affinity != Some(0) {
-			return
+			return;
 		}
 		// No affinity more for entries on this core, free any entries:
 		//
@@ -754,7 +764,7 @@ where
 				} else {
 					*maybe_affinity = None;
 				}
-				return Some(new_count)
+				return Some(new_count);
 			} else {
 				None
 			}
diff --git a/prdoc/pr_4339.prdoc b/prdoc/pr_4339.prdoc
new file mode 100644
index 00000000000..634ccfa1a33
--- /dev/null
+++ b/prdoc/pr_4339.prdoc
@@ -0,0 +1,13 @@
+# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
+# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json
+
+title: Improving on_demand_assigner emitted events
+
+doc:
+  - audience: Runtime User
+    description: |
+      Registering OnDemandOrderPlaced event that is useful for indexers to save data related to on demand orders. Adds SpotPriceSet as a new event to monitor on-demand spot prices. It updates whenever the price changes due to traffic.
+
+crates:
+  - name: polkadot-runtime-parachains
+    bump: minor
\ No newline at end of file
-- 
GitLab