1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
// Copyright 2023 Centrifuge Foundation (centrifuge.io).
//
// This file is part of the Centrifuge chain project.
// Centrifuge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version (see http://www.gnu.org/licenses).
// Centrifuge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
//! # Orderbook Pallet
//!
//! The Orderbook pallet allows orders for currency swaps to be placed and
//! fulfilled.
//!
//! This pallet was made using the ZeitGeist Orderbook pallet as a reference;
//! with much of the code being copied or adapted from that pallet.
//! The ZeitGeist Orderbook pallet can be found here:
//! <https://github.com/zeitgeistpm/zeitgeist/tree/main/zrml/orderbook-v1>

#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(test)]
pub(crate) mod mock;

#[cfg(test)]
mod tests;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

pub mod weights;

pub use pallet::*;
pub use weights::WeightInfo;

#[frame_support::pallet]
pub mod pallet {
	use cfg_primitives::conversion::convert_balance_decimals;
	use cfg_traits::{
		swaps::{OrderInfo, OrderRatio, Swap, SwapInfo, TokenSwaps},
		StatusNotificationHook, ValueProvider,
	};
	use frame_support::{
		pallet_prelude::{DispatchResult, Member, StorageDoubleMap, StorageValue, *},
		traits::{
			fungibles::{Inspect as AssetInspect, InspectHold, Mutate, MutateHold},
			tokens::{AssetId, Precision, Preservation},
		},
		Twox64Concat,
	};
	use frame_system::pallet_prelude::{OriginFor, *};
	use orml_traits::asset_registry::{self, Inspect as _};
	use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
	use scale_info::TypeInfo;
	use sp_arithmetic::traits::CheckedSub;
	use sp_runtime::{
		traits::{
			AtLeast32BitUnsigned, EnsureAdd, EnsureAddAssign, EnsureDiv, EnsureFixedPointNumber,
			EnsureMul, EnsureSub, EnsureSubAssign, MaybeSerializeDeserialize, One, Zero,
		},
		FixedPointNumber, FixedPointOperand, TokenError,
	};
	use sp_std::cmp::{min, Ordering};

	use super::*;

	/// The current storage version.
	const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);

	pub type BalanceOf<T> =
		<<T as Config>::Currency as AssetInspect<<T as frame_system::Config>::AccountId>>::Balance;

	#[pallet::pallet]
	#[pallet::storage_version(STORAGE_VERSION)]
	pub struct Pallet<T>(_);

	#[pallet::config]
	pub trait Config: frame_system::Config {
		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

		/// Asset registry for foreign currencies we can take orders for.
		type AssetRegistry: asset_registry::Inspect<
			AssetId = Self::CurrencyId,
			Balance = BalanceOf<Self>,
		>;

		/// CurrencyId that an order can be made for
		type CurrencyId: AssetId
			+ Parameter
			+ Default
			+ Member
			+ Copy
			+ MaybeSerializeDeserialize
			+ Ord;

		/// Type used for OrderId. OrderIdNonce ensures each
		/// OrderId is unique. OrderIdNonce incremented with each new order.
		type OrderIdNonce: Parameter
			+ Member
			+ AtLeast32BitUnsigned
			+ Default
			+ Copy
			+ EnsureAdd
			+ MaybeSerializeDeserialize
			+ MaxEncodedLen;

		/// Balance type for incoming values
		type BalanceIn: Member
			+ Parameter
			+ FixedPointOperand
			+ AtLeast32BitUnsigned
			+ EnsureMul
			+ EnsureDiv
			+ MaxEncodedLen
			+ Into<BalanceOf<Self>>
			+ From<BalanceOf<Self>>;

		/// Balance type for outgoing values
		type BalanceOut: Member
			+ Parameter
			+ FixedPointOperand
			+ AtLeast32BitUnsigned
			+ EnsureMul
			+ EnsureDiv
			+ MaxEncodedLen
			+ Into<BalanceOf<Self>>
			+ From<BalanceOf<Self>>;

		/// Type for currency orders can be made for
		type Currency: AssetInspect<Self::AccountId, AssetId = Self::CurrencyId>
			+ InspectHold<Self::AccountId, Reason = ()>
			+ MutateHold<Self::AccountId>
			+ Mutate<Self::AccountId>;

		/// Type for conversion ratios.
		/// It will be factor applied to `currency_out` amount to obtain
		/// `currency_in`
		type Ratio: Parameter
			+ Member
			+ FixedPointNumber
			+ EnsureMul
			+ EnsureDiv
			+ MaybeSerializeDeserialize
			+ MaxEncodedLen;

		/// The default minimum fulfillment amount for orders.
		///
		/// NOTE: The amount is expected to be denominated in native currency.
		/// When applying to a swap order, it will be re-denominated into the
		/// target currency.
		#[pallet::constant]
		type MinFulfillmentAmountNative: Get<Self::BalanceOut>;

		#[pallet::constant]
		type NativeDecimals: Get<u32>;

		/// The hook which acts upon a (partially) fulfilled order
		type FulfilledOrderHook: StatusNotificationHook<
			Id = Self::OrderIdNonce,
			Status = SwapInfo<Self::BalanceIn, Self::BalanceOut, Self::CurrencyId, Self::Ratio>,
			Error = DispatchError,
		>;

		/// Type for a market conversion ratio feeder
		type FeederId: Parameter + Member + Ord + MaxEncodedLen;

		/// A way to obtain conversion ratios for market pairs
		type RatioProvider: ValueProvider<
			Self::FeederId,
			(Self::CurrencyId, Self::CurrencyId),
			Value = Self::Ratio,
		>;

		/// The admin origin of this pallet
		type AdminOrigin: EnsureOrigin<Self::RuntimeOrigin>;

		/// Type for pallet weights
		type Weights: WeightInfo;
	}

	/// Order Storage item.
	/// Contains fields relevant to order information
	#[derive(
		Clone, RuntimeDebugNoBound, Encode, Decode, Eq, PartialEq, MaxEncodedLen, TypeInfo,
	)]
	#[scale_info(skip_type_params(T))]
	pub struct Order<T: Config> {
		/// Unique Id for this order
		pub order_id: T::OrderIdNonce,

		/// Associated account to this order
		pub placing_account: T::AccountId,

		/// Currency id expected to receive
		pub currency_in: T::CurrencyId,

		/// Currency id expected to give
		pub currency_out: T::CurrencyId,

		/// Amount in `currency_in` obtained by swaping `amount_out`
		pub amount_in: T::BalanceIn,

		/// How many tokens of `currency_out` available to sell
		pub amount_out: T::BalanceOut,

		/// Initial value of amount out, used for tracking amount fulfilled
		pub amount_out_initial: T::BalanceOut,

		/// Price given for this order,
		pub ratio: OrderRatio<T::Ratio>,
	}

	/// Map of Orders to look up orders by their order id.
	#[pallet::storage]
	pub type Orders<T: Config> = StorageMap<
		_,
		Twox64Concat,
		T::OrderIdNonce,
		Order<T>,
		ResultQuery<Error<T>::OrderNotFound>,
	>;

	/// Map of orders for a particular user
	/// Used to query orders for a particular user using the
	/// account id of the user as prefix
	#[pallet::storage]
	pub type UserOrders<T: Config> = StorageDoubleMap<
		_,
		Twox64Concat,
		T::AccountId,
		Twox64Concat,
		T::OrderIdNonce,
		(),
		ResultQuery<Error<T>::OrderNotFound>,
	>;

	/// Stores OrderIdNonce for orders placed
	/// Given that OrderIdNonce is to ensure that all orders have a unique ID,
	/// we can use just one OrderIdNonce, which means that we only have one val
	/// in storage, and we don't have to insert new map values upon a new
	/// account/currency order creation.
	#[pallet::storage]
	pub type OrderIdNonceStore<T: Config> = StorageValue<_, T::OrderIdNonce, ValueQuery>;

	/// Stores the market feeder id used to set with market conversion ratios
	#[pallet::storage]
	pub type MarketFeederId<T: Config> =
		StorageValue<_, T::FeederId, ResultQuery<Error<T>::MarketFeederNotFound>>;

	#[pallet::event]
	#[pallet::generate_deposit(pub(super) fn deposit_event)]
	pub enum Event<T: Config> {
		/// Event emitted when an order is created.
		OrderCreated {
			order_id: T::OrderIdNonce,
			creator_account: T::AccountId,
			currency_in: T::CurrencyId,
			currency_out: T::CurrencyId,
			amount_out: T::BalanceOut,
			min_fulfillment_amount_out: T::BalanceOut,
			ratio: OrderRatio<T::Ratio>,
		},
		/// Event emitted when an order is cancelled.
		OrderCancelled {
			account: T::AccountId,
			order_id: T::OrderIdNonce,
		},
		/// Event emitted when an order is updated.
		OrderUpdated {
			order_id: T::OrderIdNonce,
			account: T::AccountId,
			amount_out: T::BalanceOut,
			ratio: OrderRatio<T::Ratio>,
			min_fulfillment_amount_out: T::BalanceOut,
		},
		/// Event emitted when an order is fulfilled.
		/// Can be for either partial or total fulfillment.
		/// Contains amount fulfilled, and whether fulfillment was partial or
		/// full.
		OrderFulfillment {
			order_id: T::OrderIdNonce,
			placing_account: T::AccountId,
			fulfilling_account: T::AccountId,
			partial_fulfillment: bool,
			fulfillment_amount: T::BalanceOut,
			currency_in: T::CurrencyId,
			currency_out: T::CurrencyId,
			ratio: T::Ratio,
		},
		/// Event emitted when a valid trading pair is removed.
		FeederChanged { feeder_id: T::FeederId },
	}

	#[pallet::error]
	#[derive(PartialEq)]
	pub enum Error<T> {
		/// Error when order is placed attempting to exchange currencies of the
		/// same type.
		SameCurrencyIds,
		/// Error when an account cannot reserve or transfer the amount.
		BelowMinFulfillmentAmount,
		/// Error when an order is placed with a currency that is not in the
		/// `AssetRegistry`.
		InvalidCurrencyId,
		/// Error when an operation is attempted on an order id that is not in
		/// storage.
		OrderNotFound,
		/// Error when a user attempts an action on an order they are not
		/// authorised to perform, such as cancelling another accounts order.
		Unauthorised,
		/// Error when the provided amount to fulfill is too large for the
		/// order.
		FulfillAmountTooLarge,
		/// There is not feeder set for market conversion ratios
		MarketFeederNotFound,
		/// Expected a market ratio for the given pair of currencies.
		MarketRatioNotFound,
	}

	#[pallet::call]
	impl<T: Config> Pallet<T> {
		/// Create an order with the default min fulfillment amount.
		#[pallet::call_index(0)]
		#[pallet::weight(T::Weights::place_order())]
		pub fn place_order(
			origin: OriginFor<T>,
			currency_in: T::CurrencyId,
			currency_out: T::CurrencyId,
			amount_out: T::BalanceOut,
			ratio: OrderRatio<T::Ratio>,
		) -> DispatchResult {
			let account_id = ensure_signed(origin)?;

			Self::inner_place_order(
				account_id,
				currency_in,
				currency_out,
				amount_out,
				ratio,
				Self::min_fulfillment_amount(currency_out)?,
			)?;

			Ok(())
		}

		/// Update an existing order
		#[pallet::call_index(1)]
		#[pallet::weight(T::Weights::update_order())]
		pub fn update_order(
			origin: OriginFor<T>,
			order_id: T::OrderIdNonce,
			amount_out: T::BalanceOut,
			ratio: OrderRatio<T::Ratio>,
		) -> DispatchResult {
			let account_id = ensure_signed(origin)?;
			let order = Orders::<T>::get(order_id)?;

			ensure!(
				account_id == order.placing_account,
				Error::<T>::Unauthorised
			);

			Self::inner_update_order(
				order.clone(),
				amount_out,
				ratio,
				Self::min_fulfillment_amount(order.currency_out)?,
			)
		}

		///  Cancel an existing order that had been created by calling account.
		#[pallet::call_index(2)]
		#[pallet::weight(T::Weights::cancel_order())]
		pub fn cancel_order(origin: OriginFor<T>, order_id: T::OrderIdNonce) -> DispatchResult {
			let account_id = ensure_signed(origin)?;
			let order = <Orders<T>>::get(order_id)?;

			ensure!(
				account_id == order.placing_account,
				Error::<T>::Unauthorised
			);

			<Self as TokenSwaps<T::AccountId>>::cancel_order(order_id)
		}

		/// Fill an existing order with the given amount.
		/// The `amount_out` is the amount the originator of this call is
		/// willing to buy for
		#[pallet::call_index(3)]
		#[pallet::weight(T::Weights::fill_order())]
		pub fn fill_order(
			origin: OriginFor<T>,
			order_id: T::OrderIdNonce,
			amount_out: T::BalanceOut,
		) -> DispatchResult {
			let account_id = ensure_signed(origin)?;
			let order = <Orders<T>>::get(order_id)?;

			Self::fulfill_order_with_amount(order, amount_out, account_id)
		}

		/// Set the market feeder for set market ratios.
		/// The origin must be the admin origin.
		#[pallet::call_index(4)]
		#[pallet::weight(T::Weights::set_market_feeder())]
		pub fn set_market_feeder(origin: OriginFor<T>, feeder_id: T::FeederId) -> DispatchResult {
			T::AdminOrigin::ensure_origin(origin)?;

			MarketFeederId::<T>::put(feeder_id.clone());

			Self::deposit_event(Event::<T>::FeederChanged { feeder_id });

			Ok(())
		}
	}

	impl<T: Config> Pallet<T> {
		fn inner_place_order(
			account: T::AccountId,
			currency_in: T::CurrencyId,
			currency_out: T::CurrencyId,
			amount_out: T::BalanceOut,
			ratio: OrderRatio<T::Ratio>,
			min_fulfillment_amount_out: T::BalanceOut,
		) -> Result<T::OrderIdNonce, DispatchError> {
			let order_id = OrderIdNonceStore::<T>::try_mutate(|n| {
				n.ensure_add_assign(One::one())?;
				Ok::<_, DispatchError>(*n)
			})?;

			ensure!(
				amount_out >= min_fulfillment_amount_out,
				Error::<T>::BelowMinFulfillmentAmount
			);

			ensure!(currency_in != currency_out, Error::<T>::SameCurrencyIds);

			T::Currency::hold(currency_out, &(), &account, amount_out.into())?;

			let new_order = Order {
				order_id,
				placing_account: account.clone(),
				currency_in,
				currency_out,
				amount_out,
				ratio,
				amount_out_initial: amount_out,
				amount_in: Zero::zero(),
			};

			Orders::<T>::insert(order_id, new_order.clone());
			UserOrders::<T>::insert(&account, order_id, ());

			Self::deposit_event(Event::OrderCreated {
				creator_account: account,
				ratio,
				order_id,
				amount_out,
				currency_in,
				currency_out,
				min_fulfillment_amount_out,
			});

			Ok(order_id)
		}

		fn inner_update_order(
			mut order: Order<T>,
			amount_out: T::BalanceOut,
			ratio: OrderRatio<T::Ratio>,
			min_fulfillment_amount_out: T::BalanceOut,
		) -> DispatchResult {
			ensure!(
				amount_out >= min_fulfillment_amount_out,
				Error::<T>::BelowMinFulfillmentAmount
			);

			match amount_out.cmp(&order.amount_out) {
				Ordering::Greater => {
					let amount_diff = amount_out.ensure_sub(order.amount_out)?;
					order.amount_out_initial.ensure_add_assign(amount_diff)?;
					T::Currency::hold(
						order.currency_out,
						&(),
						&order.placing_account,
						amount_diff.into(),
					)?;
				}
				Ordering::Less => {
					let amount_diff = order.amount_out.ensure_sub(amount_out)?;
					order.amount_out_initial.ensure_sub_assign(amount_diff)?;

					T::Currency::release(
						order.currency_out,
						&(),
						&order.placing_account,
						amount_diff.into(),
						Precision::Exact,
					)?;
				}
				Ordering::Equal => (),
			}

			order.amount_out = amount_out;
			order.ratio = ratio;

			Orders::<T>::insert(order.order_id, order.clone());

			Self::deposit_event(Event::OrderUpdated {
				account: order.placing_account,
				order_id: order.order_id,
				amount_out,
				ratio,
				min_fulfillment_amount_out,
			});

			Ok(())
		}

		/// Remove an order from storage
		pub fn remove_order(order_id: T::OrderIdNonce) -> DispatchResult {
			let order = <Orders<T>>::get(order_id)?;

			Orders::<T>::remove(order.order_id);
			UserOrders::<T>::remove(&order.placing_account, order.order_id);

			Ok(())
		}

		fn fulfill_order_with_amount(
			order: Order<T>,
			amount_out: T::BalanceOut,
			fulfilling_account: T::AccountId,
		) -> DispatchResult {
			let min_fulfillment_amount_out = min(
				order.amount_out,
				Self::min_fulfillment_amount(order.currency_out)?,
			);

			ensure!(
				amount_out >= min_fulfillment_amount_out,
				Error::<T>::BelowMinFulfillmentAmount,
			);

			let ratio = match order.ratio {
				OrderRatio::Market => Self::market_ratio(order.currency_out, order.currency_in)?,
				OrderRatio::Custom(ratio) => ratio,
			};

			let amount_in =
				Self::convert_with_ratio(order.currency_out, order.currency_in, ratio, amount_out)?;

			let remaining_amount_out = order
				.amount_out
				.checked_sub(&amount_out)
				.ok_or(Error::<T>::FulfillAmountTooLarge)?;

			let partial_fulfillment = !remaining_amount_out.is_zero();
			if partial_fulfillment {
				let mut updated_order = order.clone();
				updated_order.amount_out = remaining_amount_out;
				updated_order.amount_in = order.amount_in.ensure_add(amount_in)?;

				Orders::<T>::insert(updated_order.order_id, updated_order.clone());
			} else {
				Self::remove_order(order.order_id)?;
			}

			T::Currency::release(
				order.currency_out,
				&(),
				&order.placing_account,
				amount_out.into(),
				Precision::Exact,
			)?;

			if T::Currency::balance(order.currency_out, &order.placing_account) < amount_out.into()
			{
				Err(DispatchError::Token(TokenError::FundsUnavailable))?
			}

			if T::Currency::balance(order.currency_in, &fulfilling_account) < amount_in.into() {
				Err(DispatchError::Token(TokenError::FundsUnavailable))?
			}

			T::Currency::transfer(
				order.currency_out,
				&order.placing_account,
				&fulfilling_account,
				amount_out.into(),
				Preservation::Expendable,
			)?;
			T::Currency::transfer(
				order.currency_in,
				&fulfilling_account,
				&order.placing_account,
				amount_in.into(),
				Preservation::Expendable,
			)?;

			T::FulfilledOrderHook::notify_status_change(
				order.order_id,
				SwapInfo {
					remaining: Swap {
						amount_out: remaining_amount_out,
						currency_in: order.currency_in,
						currency_out: order.currency_out,
					},
					swapped_in: amount_in,
					swapped_out: amount_out,
					ratio,
				},
			)?;

			Self::deposit_event(Event::OrderFulfillment {
				order_id: order.order_id,
				placing_account: order.placing_account,
				fulfilling_account,
				partial_fulfillment,
				currency_in: order.currency_in,
				currency_out: order.currency_out,
				fulfillment_amount: amount_out,
				ratio,
			});

			Ok(())
		}

		pub fn market_ratio(
			currency_from: T::CurrencyId,
			currency_to: T::CurrencyId,
		) -> Result<T::Ratio, DispatchError> {
			let feeder = MarketFeederId::<T>::get()?;

			T::RatioProvider::get(&feeder, &(currency_from, currency_to))?
				.ok_or(Error::<T>::MarketRatioNotFound.into())
		}

		/// `ratio` is the value you multiply `amount_from` to obtain
		/// `amount_to`
		pub fn convert_with_ratio(
			currency_from: T::CurrencyId,
			currency_to: T::CurrencyId,
			ratio: T::Ratio,
			amount_from: T::BalanceOut,
		) -> Result<T::BalanceIn, DispatchError> {
			let from_decimals = T::AssetRegistry::metadata(&currency_from)
				.ok_or(Error::<T>::InvalidCurrencyId)?
				.decimals;

			let to_decimals = T::AssetRegistry::metadata(&currency_to)
				.ok_or(Error::<T>::InvalidCurrencyId)?
				.decimals;

			let amount_in = ratio.ensure_mul_int(amount_from)?;
			Ok(convert_balance_decimals(from_decimals, to_decimals, amount_in.into())?.into())
		}

		pub fn min_fulfillment_amount(
			currency: T::CurrencyId,
		) -> Result<T::BalanceOut, DispatchError> {
			let to_decimals = T::AssetRegistry::metadata(&currency)
				.ok_or(Error::<T>::InvalidCurrencyId)?
				.decimals;

			Ok(convert_balance_decimals(
				T::NativeDecimals::get(),
				to_decimals,
				T::MinFulfillmentAmountNative::get().into(),
			)?
			.into())
		}
	}

	impl<T: Config> TokenSwaps<T::AccountId> for Pallet<T> {
		type BalanceIn = T::BalanceIn;
		type BalanceOut = T::BalanceOut;
		type CurrencyId = T::CurrencyId;
		type OrderId = T::OrderIdNonce;
		type Ratio = T::Ratio;

		fn place_order(
			account: T::AccountId,
			currency_in: T::CurrencyId,
			currency_out: T::CurrencyId,
			amount_out: T::BalanceOut,
			ratio: OrderRatio<T::Ratio>,
		) -> Result<Self::OrderId, DispatchError> {
			Self::inner_place_order(
				account,
				currency_in,
				currency_out,
				amount_out,
				ratio,
				T::BalanceOut::zero(),
			)
		}

		fn cancel_order(order: Self::OrderId) -> DispatchResult {
			let order = <Orders<T>>::get(order)?;
			let account_id = order.placing_account.clone();

			T::Currency::release(
				order.currency_out,
				&(),
				&order.placing_account,
				order.amount_out.into(),
				Precision::Exact,
			)?;

			Self::remove_order(order.order_id)?;
			Self::deposit_event(Event::OrderCancelled {
				account: account_id,
				order_id: order.order_id,
			});

			Ok(())
		}

		fn update_order(
			order_id: Self::OrderId,
			amount_out: T::BalanceOut,
			ratio: OrderRatio<T::Ratio>,
		) -> DispatchResult {
			let order = Orders::<T>::get(order_id)?;

			Self::inner_update_order(order, amount_out, ratio, T::BalanceOut::zero())
		}

		fn get_order_details(
			order: Self::OrderId,
		) -> Option<OrderInfo<Self::BalanceOut, Self::CurrencyId, Self::Ratio>> {
			Orders::<T>::get(order)
				.map(|order| OrderInfo {
					swap: Swap {
						currency_in: order.currency_in,
						currency_out: order.currency_out,
						amount_out: order.amount_out,
					},
					ratio: order.ratio,
				})
				.ok()
		}

		fn fill_order(
			account: T::AccountId,
			order_id: Self::OrderId,
			buy_amount: T::BalanceOut,
		) -> DispatchResult {
			let order = <Orders<T>>::get(order_id)?;

			Self::fulfill_order_with_amount(order, buy_amount, account)
		}

		fn convert_by_market(
			currency_in: Self::CurrencyId,
			currency_out: Self::CurrencyId,
			amount_out: T::BalanceOut,
		) -> Result<T::BalanceIn, DispatchError> {
			if currency_in == currency_out {
				let amount: BalanceOf<T> = amount_out.into();
				return Ok(amount.into());
			}

			let ratio = Self::market_ratio(currency_out, currency_in)?;
			Self::convert_with_ratio(currency_out, currency_in, ratio, amount_out)
		}

		fn market_ratio(
			currency_in: Self::CurrencyId,
			currency_out: Self::CurrencyId,
		) -> Result<Self::Ratio, DispatchError> {
			Self::market_ratio(currency_out, currency_in)
		}
	}

	#[cfg(feature = "runtime-benchmarks")]
	impl<T: Config> ValueProvider<(), (T::CurrencyId, T::CurrencyId)> for Pallet<T> {
		type Value = T::Ratio;

		fn get(
			_: &(),
			(currency_out, currency_in): &(T::CurrencyId, T::CurrencyId),
		) -> Result<Option<Self::Value>, DispatchError> {
			Self::market_ratio(*currency_out, *currency_in).map(Some)
		}

		fn set(_: &(), pair: &(T::CurrencyId, T::CurrencyId), value: Self::Value) {
			let feeder = MarketFeederId::<T>::get().unwrap();
			T::RatioProvider::set(&feeder, &pair, value);
		}
	}
}