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
//! Trait implementations. Higher level file.

use cfg_traits::{investments::ForeignInvestment, swaps::SwapInfo, StatusNotificationHook};
use cfg_types::investments::CollectedAmount;
use frame_support::pallet_prelude::*;
use sp_std::marker::PhantomData;

use crate::{
	entities::{InvestmentInfo, RedemptionInfo},
	pallet::{Config, Error, ForeignInvestmentInfo, ForeignRedemptionInfo, Pallet},
	pool_currency_of,
	swaps::fulfilled_order,
	Action,
};

impl<T: Config> ForeignInvestment<T::AccountId> for Pallet<T> {
	type Amount = T::ForeignBalance;
	type CurrencyId = T::CurrencyId;
	type InvestmentId = T::InvestmentId;
	type TrancheAmount = T::TrancheBalance;

	fn increase_foreign_investment(
		who: &T::AccountId,
		investment_id: T::InvestmentId,
		foreign_amount: T::ForeignBalance,
		foreign_currency: T::CurrencyId,
	) -> DispatchResult {
		ForeignInvestmentInfo::<T>::mutate_exists(who, investment_id, |entry| {
			let info = entry.get_or_insert(InvestmentInfo::new(foreign_currency));
			info.ensure_same_foreign(foreign_currency)?;
			info.ensure_no_pending_cancel(investment_id)?;

			let (increased, pending) = info.increase(who, investment_id, foreign_amount)?;
			info.post_increase_swap(who, investment_id, increased, increased.into(), pending)?;

			remove_entry(info.is_completed(who, investment_id)?, entry)
		})
	}

	fn cancel_foreign_investment(
		who: &T::AccountId,
		investment_id: T::InvestmentId,
		foreign_currency: T::CurrencyId,
	) -> DispatchResult {
		ForeignInvestmentInfo::<T>::mutate_exists(who, investment_id, |entry| {
			let info = entry.as_mut().ok_or(Error::<T>::InfoNotFound)?;
			info.ensure_same_foreign(foreign_currency)?;
			info.ensure_no_pending_cancel(investment_id)?;

			let (cancelled, pending) = info.cancel(who, investment_id)?;
			info.post_cancel_swap(who, investment_id, cancelled, pending)?;

			remove_entry(info.is_completed(who, investment_id)?, entry)
		})
	}

	fn increase_foreign_redemption(
		who: &T::AccountId,
		investment_id: T::InvestmentId,
		tranche_tokens_amount: T::TrancheBalance,
		payout_foreign_currency: T::CurrencyId,
	) -> DispatchResult {
		ForeignRedemptionInfo::<T>::mutate_exists(who, investment_id, |entry| -> DispatchResult {
			let info = entry.get_or_insert(RedemptionInfo::new(payout_foreign_currency));
			info.ensure_same_foreign(payout_foreign_currency)?;
			info.increase_redemption(who, investment_id, tranche_tokens_amount)?;

			remove_entry(info.is_completed(who, investment_id)?, entry)
		})
	}

	fn cancel_foreign_redemption(
		who: &T::AccountId,
		investment_id: T::InvestmentId,
		payout_foreign_currency: T::CurrencyId,
	) -> Result<T::TrancheBalance, DispatchError> {
		ForeignRedemptionInfo::<T>::mutate_exists(who, investment_id, |entry| {
			let info = entry.as_mut().ok_or(Error::<T>::InfoNotFound)?;
			info.ensure_same_foreign(payout_foreign_currency)?;
			let cancelled = info.cancel_redeemption(who, investment_id)?;

			remove_entry(info.is_completed(who, investment_id)?, entry)?;
			Ok(cancelled)
		})
	}
}

impl<T: Config> StatusNotificationHook for Pallet<T> {
	type Error = DispatchError;
	type Id = T::OrderId;
	type Status = SwapInfo<T::SwapBalance, T::SwapBalance, T::CurrencyId, T::SwapRatio>;

	fn notify_status_change(order_id: T::OrderId, swap_info: Self::Status) -> DispatchResult {
		let (who, (investment_id, action)) = match fulfilled_order::<T>(&order_id, &swap_info) {
			Some(location) => location,
			None => return Ok(()), // notification not for FI
		};

		let pool_currency = pool_currency_of::<T>(investment_id)?;
		let swapped_amount_in = swap_info.swapped_in;
		let swapped_amount_out = swap_info.swapped_out;
		let pending_amount = swap_info.remaining.amount_out;

		match action {
			Action::Investment => {
				ForeignInvestmentInfo::<T>::mutate_exists(&who, investment_id, |entry| {
					let info = entry.as_mut().ok_or(Error::<T>::InfoNotFound)?;
					if pool_currency == swap_info.remaining.currency_in {
						info.post_increase_swap(
							&who,
							investment_id,
							swapped_amount_in.into(),
							swapped_amount_out.into(),
							pending_amount.into(),
						)
					} else {
						info.post_cancel_swap(
							&who,
							investment_id,
							swapped_amount_in.into(),
							pending_amount.into(),
						)?;

						remove_entry(info.is_completed(&who, investment_id)?, entry)
					}
				})
			}
			Action::Redemption => {
				ForeignRedemptionInfo::<T>::mutate_exists(&who, investment_id, |entry| {
					let info = entry.as_mut().ok_or(Error::<T>::InfoNotFound)?;
					info.post_swap(
						&who,
						investment_id,
						swapped_amount_in.into(),
						pending_amount.into(),
					)?;

					remove_entry(info.is_completed(&who, investment_id)?, entry)
				})
			}
		}
	}
}

pub struct CollectedInvestmentHook<T>(PhantomData<T>);
impl<T: Config> StatusNotificationHook for CollectedInvestmentHook<T> {
	type Error = DispatchError;
	type Id = (T::AccountId, T::InvestmentId);
	type Status = CollectedAmount<T::TrancheBalance, T::PoolBalance>;

	fn notify_status_change(
		(who, investment_id): (T::AccountId, T::InvestmentId),
		collected: CollectedAmount<T::TrancheBalance, T::PoolBalance>,
	) -> DispatchResult {
		ForeignInvestmentInfo::<T>::mutate_exists(&who, investment_id, |entry| {
			if let Some(info) = entry.as_mut() {
				info.ensure_no_pending_cancel(investment_id)?;
				info.post_collect(&who, investment_id, collected)?;

				remove_entry(info.is_completed(&who, investment_id)?, entry)?;
			}

			Ok(())
		})
	}
}

pub struct CollectedRedemptionHook<T>(PhantomData<T>);
impl<T: Config> StatusNotificationHook for CollectedRedemptionHook<T> {
	type Error = DispatchError;
	type Id = (T::AccountId, T::InvestmentId);
	type Status = CollectedAmount<T::PoolBalance, T::TrancheBalance>;

	fn notify_status_change(
		(who, investment_id): (T::AccountId, T::InvestmentId),
		collected: CollectedAmount<T::PoolBalance, T::TrancheBalance>,
	) -> DispatchResult {
		ForeignRedemptionInfo::<T>::mutate_exists(&who, investment_id, |entry| {
			if let Some(info) = entry.as_mut() {
				let (amount, pending) =
					info.post_collect_and_swap(&who, investment_id, collected)?;

				info.post_swap(&who, investment_id, amount, pending)?;

				remove_entry(info.is_completed(&who, investment_id)?, entry)?;
			}

			Ok(())
		})
	}
}

/// Avoiding boilerplate each time the entry needs to be removed
fn remove_entry<Entry>(condition: bool, entry: &mut Option<Entry>) -> DispatchResult {
	if condition {
		*entry = None;
	}

	Ok(())
}