diff --git a/pallets/orders/src/lib.rs b/pallets/orders/src/lib.rs index 7a80565..3b2208d 100644 --- a/pallets/orders/src/lib.rs +++ b/pallets/orders/src/lib.rs @@ -293,5 +293,9 @@ pub mod pallet { fn order(order_id: &OrderId) -> Option> { Orders::::get(order_id) } + + fn remove_order(order_id: &OrderId) { + Orders::::remove(order_id) + } } } diff --git a/pallets/processor/src/lib.rs b/pallets/processor/src/lib.rs index fdbf4d8..b21e718 100644 --- a/pallets/processor/src/lib.rs +++ b/pallets/processor/src/lib.rs @@ -65,7 +65,9 @@ pub mod pallet { #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] - pub enum Event {} + pub enum Event { + OrderFulfilled { order_id: OrderId, region_id: RegionId, seller: T::AccountId }, + } #[pallet::error] #[derive(PartialEq)] @@ -84,6 +86,8 @@ pub mod pallet { RecordUnavailable, /// Locked regions cannot be listed on sale. RegionLocked, + /// The caller is not the owner of the region. + NotOwner, } #[pallet::call] @@ -100,14 +104,23 @@ pub mod pallet { let region = T::Regions::region(®ion_id.into()).ok_or(Error::::UnknownRegion)?; ensure!(!region.locked, Error::::RegionLocked); + ensure!(region.owner == who, Error::::NotOwner); + let record = region.record.get().ok_or(Error::::RecordUnavailable)?; let order = T::Orders::order(&order_id).ok_or(Error::::UnknownOrder)?; Self::ensure_matching_requirements(region_id, record, order.requirements)?; - // TODO: process fulfilling. + // Transfer the region to the order creator + T::Regions::transfer(®ion_id.into(), &order.creator)?; + // Transfer the tokens collected by the order to the caller(ie the seller) + // FIXME: Price ??? + // T::Currency::transfer(&order.creator, ®ion.owner) + + // remove the order + T::Orders::remove_order(&order_id); - // TODO: event + Self::deposit_event(Event::OrderFulfilled { order_id, region_id, seller: who }); Ok(()) } diff --git a/primitives/order/src/lib.rs b/primitives/order/src/lib.rs index ddb0368..ff5b350 100644 --- a/primitives/order/src/lib.rs +++ b/primitives/order/src/lib.rs @@ -50,4 +50,7 @@ pub trait OrderInspect { /// /// If `None` the order was not found. fn order(order_id: &OrderId) -> Option>; + + /// Remove an order with the associated id. + fn remove_order(order_id: &OrderId); }