From 865836fc814ff24d7d1f030732bdd9ce8a8e8ebb Mon Sep 17 00:00:00 2001 From: cuteolaf Date: Thu, 30 May 2024 03:15:26 +0000 Subject: [PATCH] implement extrinsics --- pallets/orders/src/lib.rs | 62 +++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/pallets/orders/src/lib.rs b/pallets/orders/src/lib.rs index e73f82f..a529f71 100644 --- a/pallets/orders/src/lib.rs +++ b/pallets/orders/src/lib.rs @@ -39,9 +39,11 @@ pub mod pallet { use super::*; use frame_support::{ pallet_prelude::*, + sp_runtime::Saturating, traits::{ fungible::{Inspect, Mutate}, tokens::Balance, + Get, }, }; use frame_system::pallet_prelude::*; @@ -92,6 +94,11 @@ pub mod pallet { #[pallet::getter(fn orders)] pub type Orders = StorageMap<_, Blake2_128Concat, OrderId, Order>; + /// Last order id + #[pallet::storage] + #[pallet::getter(fn nextOrderId)] + pub type NextOrderId = StorageValue<_, OrderId, ValueQuery>; + /// Crowdfunding contributions. #[pallet::storage] #[pallet::getter(fn contributions)] @@ -116,11 +123,25 @@ pub mod pallet { #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] - pub enum Event {} + pub enum Event { + /// An order was created + OrderCreated { order_id: OrderId }, + /// An order was removed + OrderRemoved { order_id: OrderId }, + /// A contribution was made to the order specificed by `order_id` + Contributed { order_id: OrderId, who: T::AccountId, amount: BalanceOf }, + } #[pallet::error] #[derive(PartialEq)] - pub enum Error {} + pub enum Error { + /// Invalid order id + InvalidOrderId, + /// The caller is not the creator of the given order + NotAllowed, + /// The contribution amount is too small + InvalidAmount, + } #[pallet::call] impl Pallet { @@ -141,11 +162,13 @@ pub mod pallet { let who = Self::ensure_signed_or_para(origin)?; // TODO: charge order creation cost + // Where shall we send the order creation cost to? To Treasury? - // TODO: add the order to the `Orders` storage map. (The order id is incremental) - - // TODO: Emit event + let order_id = NextOrderId::::get(); + Orders::::insert(order_id, Order { creator: who, para_id, requirements }); + NextOrderId::::put(order_id + 1); + Self::deposit_event(Event::OrderCreated { order_id }); Ok(()) } @@ -158,15 +181,16 @@ pub mod pallet { /// - `requirements`: Region requirements of the order. #[pallet::call_index(1)] #[pallet::weight(10_000)] // TODO - pub fn cancel_order(origin: OriginFor, order: OrderId) -> DispatchResult { + pub fn cancel_order(origin: OriginFor, order_id: OrderId) -> DispatchResult { let who = Self::ensure_signed_or_para(origin)?; - // TODO: ensure order creator + let order = Orders::::get(order_id).ok_or(Error::::InvalidOrderId)?; + ensure!(order.creator == who, Error::::NotAllowed); + // TODO: Shall we also check if the caller is the sovereign account of para_id? - // TODO: remove the order from the `Orders` storage map. - - // TODO: emit event + Orders::::remove(order_id); + Self::deposit_event(Event::OrderRemoved { order_id }); Ok(()) } @@ -181,18 +205,26 @@ pub mod pallet { #[pallet::weight(10_000)] // TODO pub fn contribute( origin: OriginFor, - order: OrderId, + order_id: OrderId, amount: BalanceOf, ) -> DispatchResult { let who = ensure_signed(origin)?; - // TODO: ensure order exists + let order = Orders::::get(order_id).ok_or(Error::::InvalidOrderId)?; + + ensure!(amount >= T::MinimumContribution::get(), Error::::InvalidAmount); + + let mut contribution: BalanceOf = Contributions::::get(order_id, who.clone()); + + contribution = contribution.saturating_add(amount); + Contributions::::insert(order_id, who.clone(), contribution); - // TODO: Update `Contributions` + let mut total_contributions = TotalContributions::::get(order_id); - // TODO: Update `TotalContributions` + total_contributions = total_contributions.saturating_add(amount); + TotalContributions::::insert(order_id, total_contributions); - // TODO: emit event + Self::deposit_event(Event::Contributed { order_id, who, amount }); Ok(()) }