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
//! This is the only module to handle
//! - OrderBook trait
//! - OrderIdToSwapId storage
//! - Swap events

use cfg_traits::swaps::{OrderInfo, OrderRatio, Swap, SwapInfo, TokenSwaps};
use sp_runtime::{
	traits::{EnsureAdd, Zero},
	DispatchError, DispatchResult,
};

use crate::{Config, Event, OrderIdToSwapId, Pallet, SwapId, SwapOf};

pub fn create_swap<T: Config>(
	who: &T::AccountId,
	swap_id: SwapId<T>,
	swap: SwapOf<T>,
) -> Result<Option<T::OrderId>, DispatchError> {
	if swap.amount_out == Zero::zero() {
		return Ok(None);
	}

	Pallet::<T>::deposit_event(Event::SwapCreatedOrUpdated {
		who: who.clone(),
		swap_id,
		swap: swap.clone(),
	});

	let order_id = T::OrderBook::place_order(
		who.clone(),
		swap.currency_in,
		swap.currency_out,
		swap.amount_out,
		OrderRatio::Market,
	)?;

	OrderIdToSwapId::<T>::insert(order_id, (who.clone(), swap_id));

	Ok(Some(order_id))
}

pub fn increase_swap<T: Config>(
	who: &T::AccountId,
	swap_id: SwapId<T>,
	order_id: &T::OrderId,
	amount: T::SwapBalance,
) -> DispatchResult {
	if amount == Zero::zero() {
		return Ok(());
	}

	match T::OrderBook::get_order_details(*order_id) {
		Some(info) => {
			let new_amount = info.swap.amount_out.ensure_add(amount)?;

			Pallet::<T>::deposit_event(Event::SwapCreatedOrUpdated {
				who: who.clone(),
				swap_id,
				swap: Swap {
					amount_out: new_amount,
					..info.swap
				},
			});

			T::OrderBook::update_order(*order_id, new_amount, info.ratio)
		}
		None => Err(DispatchError::Other(
			"increase_swap() is always called over an existent order, qed",
		)),
	}
}

pub fn create_or_increase_swap<T: Config>(
	who: &T::AccountId,
	swap_id: SwapId<T>,
	order_id: &Option<T::OrderId>,
	swap: SwapOf<T>,
) -> Result<Option<T::OrderId>, DispatchError> {
	match order_id {
		None => create_swap::<T>(who, swap_id, swap),
		Some(order_id) => {
			increase_swap::<T>(who, swap_id, order_id, swap.amount_out)?;
			Ok(Some(*order_id))
		}
	}
}

pub fn cancel_swap<T: Config>(
	who: &T::AccountId,
	swap_id: SwapId<T>,
	order_id: &T::OrderId,
) -> Result<T::SwapBalance, DispatchError> {
	match T::OrderBook::get_order_details(*order_id) {
		Some(info) => {
			Pallet::<T>::deposit_event(Event::SwapCancelled {
				who: who.clone(),
				swap_id,
				swap: info.swap.clone(),
			});

			T::OrderBook::cancel_order(*order_id)?;

			OrderIdToSwapId::<T>::remove(order_id);

			Ok(info.swap.amount_out)
		}
		None => Err(DispatchError::Other(
			"cancel_swap() is always called over an existent order, qed",
		)),
	}
}

pub fn get_swap<T: Config>(
	order_id: &T::OrderId,
) -> Option<OrderInfo<T::SwapBalance, T::CurrencyId, T::SwapRatio>> {
	T::OrderBook::get_order_details(*order_id)
}

pub fn fulfilled_order<T: Config>(
	order_id: &T::OrderId,
	swap_info: &SwapInfo<T::SwapBalance, T::SwapBalance, T::CurrencyId, T::SwapRatio>,
) -> Option<(T::AccountId, SwapId<T>)> {
	let swap_id = OrderIdToSwapId::<T>::get(order_id);

	if let Some((who, (investment_id, action))) = swap_id.clone() {
		if swap_info.remaining.amount_out.is_zero() {
			OrderIdToSwapId::<T>::remove(order_id);
		}

		Pallet::<T>::deposit_event(Event::SwapFullfilled {
			who: who.clone(),
			swap_id: (investment_id, action),
			remaining: swap_info.remaining.clone(),
			swapped_in: swap_info.swapped_in,
			swapped_out: swap_info.swapped_out,
		});
	}

	swap_id
}