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
use cfg_traits::{
	self, data::DataRegistry, interest::InterestRate, IntoSeconds, Seconds, TimeAsSecs,
};
use cfg_types::adjustments::Adjustment;
use frame_support::{self, ensure, pallet_prelude::RuntimeDebug, RuntimeDebugNoBound};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_runtime::{
	traits::{EnsureAdd, EnsureFixedPointNumber, EnsureSub, Zero},
	ArithmeticError, DispatchError, DispatchResult, FixedPointNumber,
};
use sp_std::{cmp::min, collections::btree_map::BTreeMap};

use crate::{
	entities::interest::ActiveInterestRate,
	pallet::{Config, Error},
	PriceOf,
};

#[derive(Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeDebugNoBound, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
pub struct ExternalAmount<T: Config> {
	/// Quantity of different assets identified by the price_id
	pub quantity: T::Quantity,

	/// Price used to borrow/repay. it must be in the interval
	/// [price * (1 - max_price_variation), price * (1 + max_price_variation)],
	/// being price the Oracle price.
	pub settlement_price: T::Balance,
}

impl<T: Config> ExternalAmount<T> {
	pub fn new(quantity: T::Quantity, price: T::Balance) -> Self {
		Self {
			quantity,
			settlement_price: price,
		}
	}

	pub fn empty() -> Self {
		Self {
			quantity: T::Quantity::zero(),
			settlement_price: T::Balance::zero(),
		}
	}

	pub fn balance(&self) -> Result<T::Balance, ArithmeticError> {
		self.quantity.ensure_mul_int(self.settlement_price)
	}
}

/// Define the max borrow amount of a loan
#[derive(Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeDebug, MaxEncodedLen)]
pub enum MaxBorrowAmount<Quantity> {
	/// You can borrow until the pool reserve
	NoLimit,

	/// Maximum number of items associated with the loan of the pricing.
	Quantity(Quantity),
}

/// External pricing method
#[derive(Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeDebugNoBound, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
pub struct ExternalPricing<T: Config> {
	/// Id of an external price
	pub price_id: T::PriceId,

	/// Maximum amount that can be borrowed.
	pub max_borrow_amount: MaxBorrowAmount<T::Quantity>,

	/// Reference price used to calculate the interest
	/// It refers to the expected asset price.
	pub notional: T::Balance,

	/// Maximum variation between the settlement price chosen for
	/// borrow/repay and the current oracle price.
	/// See [`ExternalAmount::settlement_price`].
	pub max_price_variation: T::Rate,

	/// If the pricing is estimated with a linear pricing model.
	pub with_linear_pricing: bool,
}

impl<T: Config> ExternalPricing<T> {
	pub fn validate(&self) -> DispatchResult {
		Ok(())
	}
}

/// External pricing method with extra attributes for active loans
#[derive(Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeDebugNoBound, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
pub struct ExternalActivePricing<T: Config> {
	/// Basic external pricing info
	info: ExternalPricing<T>,

	/// Outstanding quantity that should be repaid.
	outstanding_quantity: T::Quantity,

	/// Current interest rate
	pub interest: ActiveInterestRate<T>,

	/// Settlement price used in the most recent borrow or repay transaction.
	latest_settlement_price: T::Balance,

	/// When `latest_settlement_price` was updated.
	settlement_price_updated: Seconds,
}

impl<T: Config> ExternalActivePricing<T> {
	pub fn activate(
		info: ExternalPricing<T>,
		interest_rate: InterestRate<T::Rate>,
		pool_id: T::PoolId,
		amount: ExternalAmount<T>,
		price_required: bool,
	) -> Result<Self, DispatchError> {
		let result = T::PriceRegistry::register_id(&info.price_id, &pool_id);
		if price_required {
			// Only if the price is required, we treat the error as an error.
			result?;
		}

		Ok(Self {
			info,
			outstanding_quantity: T::Quantity::zero(),
			interest: ActiveInterestRate::activate(interest_rate)?,
			latest_settlement_price: amount.settlement_price,
			settlement_price_updated: T::Time::now(),
		})
	}

	pub fn deactivate(
		self,
		pool_id: T::PoolId,
	) -> Result<(ExternalPricing<T>, InterestRate<T::Rate>), DispatchError> {
		T::PriceRegistry::unregister_id(&self.info.price_id, &pool_id)?;
		Ok((self.info, self.interest.deactivate()?))
	}

	pub fn price_id(&self) -> T::PriceId {
		self.info.price_id
	}

	pub fn has_registered_price(&self, pool_id: T::PoolId) -> bool {
		T::PriceRegistry::get(&self.info.price_id, &pool_id).is_ok()
	}

	pub fn last_updated(&self, pool_id: T::PoolId) -> Seconds {
		match T::PriceRegistry::get(&self.info.price_id, &pool_id) {
			Ok((_, timestamp)) => timestamp.into_seconds(),
			Err(_) => self.settlement_price_updated,
		}
	}

	fn maybe_with_linear_accrual_price(
		&self,
		maturity: Option<Seconds>,
		price: T::Balance,
		price_last_updated: Seconds,
	) -> Result<T::Balance, DispatchError> {
		if let (Some(maturity), true) = (maturity, self.info.with_linear_pricing) {
			if min(price_last_updated, maturity) == maturity {
				// We can not have 2 'xs' with different 'y' in a rect.
				// That only happens at maturity
				return Ok(self.info.notional);
			}

			return Ok(cfg_utils::math::y_coord_in_rect(
				(min(price_last_updated, maturity), price),
				(maturity, self.info.notional),
				min(T::Time::now(), maturity),
			)?);
		}

		Ok(price)
	}

	pub fn current_price(
		&self,
		pool_id: T::PoolId,
		maturity: Option<Seconds>,
	) -> Result<T::Balance, DispatchError> {
		self.current_price_inner(
			maturity,
			T::PriceRegistry::get(&self.info.price_id, &pool_id).ok(),
		)
	}

	fn current_price_inner(
		&self,
		maturity: Option<Seconds>,
		oracle: Option<PriceOf<T>>,
	) -> Result<T::Balance, DispatchError> {
		if let Some((oracle_price, oracle_provided_at)) = oracle {
			self.maybe_with_linear_accrual_price(
				maturity,
				oracle_price,
				oracle_provided_at.into_seconds(),
			)
		} else {
			self.maybe_with_linear_accrual_price(
				maturity,
				self.latest_settlement_price,
				self.settlement_price_updated,
			)
		}
	}

	pub fn outstanding_notional_principal(&self) -> Result<T::Balance, DispatchError> {
		Ok(self
			.outstanding_quantity
			.ensure_mul_int(self.info.notional)?)
	}

	pub fn outstanding_priced_principal(
		&self,
		pool_id: T::PoolId,
		maturity: Option<Seconds>,
	) -> Result<T::Balance, DispatchError> {
		let price = self.current_price(pool_id, maturity)?;
		Ok(self.outstanding_quantity.ensure_mul_int(price)?)
	}

	pub fn outstanding_interest(&self) -> Result<T::Balance, DispatchError> {
		let debt = self.interest.current_debt()?;
		Ok(debt.ensure_sub(self.outstanding_notional_principal()?)?)
	}

	pub fn present_value(
		&self,
		pool_id: T::PoolId,
		maturity: Option<Seconds>,
	) -> Result<T::Balance, DispatchError> {
		self.outstanding_priced_principal(pool_id, maturity)
	}

	pub fn present_value_cached(
		&self,
		cache: &BTreeMap<T::PriceId, PriceOf<T>>,
		maturity: Option<Seconds>,
	) -> Result<T::Balance, DispatchError> {
		let price = self.current_price_inner(maturity, cache.get(&self.info.price_id).copied())?;
		Ok(self.outstanding_quantity.ensure_mul_int(price)?)
	}

	fn validate_amount(
		&self,
		amount: &ExternalAmount<T>,
		pool_id: T::PoolId,
	) -> Result<(), DispatchError> {
		match T::PriceRegistry::get(&self.info.price_id, &pool_id) {
			Ok(data) => {
				let price = data.0;
				let delta = if amount.settlement_price > price {
					amount.settlement_price.ensure_sub(price)?
				} else {
					price.ensure_sub(amount.settlement_price)?
				};
				let variation = T::Rate::checked_from_rational(delta, price)
					.ok_or(ArithmeticError::Overflow)?;

				// We bypass any price if quantity is zero,
				// because it does not take effect in the computation.
				ensure!(
					variation <= self.info.max_price_variation || amount.quantity.is_zero(),
					Error::<T>::SettlementPriceExceedsVariation
				);

				Ok(())
			}
			Err(_) => Ok(()),
		}
	}

	pub fn max_borrow_amount(
		&self,
		amount: ExternalAmount<T>,
		pool_id: T::PoolId,
	) -> Result<T::Balance, DispatchError> {
		self.validate_amount(&amount, pool_id)?;

		match self.info.max_borrow_amount {
			MaxBorrowAmount::Quantity(quantity) => {
				let available = quantity.ensure_sub(self.outstanding_quantity)?;
				Ok(available.ensure_mul_int(amount.settlement_price)?)
			}
			MaxBorrowAmount::NoLimit => Ok(amount.balance()?),
		}
	}

	pub fn max_repay_principal(
		&self,
		amount: ExternalAmount<T>,
		pool_id: T::PoolId,
	) -> Result<T::Balance, DispatchError> {
		self.validate_amount(&amount, pool_id)?;

		Ok(self
			.outstanding_quantity
			.ensure_mul_int(amount.settlement_price)?)
	}

	pub fn adjust(
		&mut self,
		amount_adj: Adjustment<ExternalAmount<T>>,
		interest: T::Balance,
	) -> DispatchResult {
		self.outstanding_quantity = amount_adj
			.clone()
			.map(|amount| amount.quantity)
			.ensure_add(self.outstanding_quantity)?;

		let interest_adj = amount_adj.clone().try_map(|amount| {
			amount
				.quantity
				.ensure_mul_int(self.info.notional)?
				.ensure_add(interest)
		})?;

		self.interest.adjust_debt(interest_adj)?;
		self.latest_settlement_price = amount_adj.abs().settlement_price;
		self.settlement_price_updated = T::Time::now();

		Ok(())
	}
}

/// Adds `with_linear_pricing` to ExternalPricing struct for migration to v4
pub mod v3 {
	use cfg_traits::Seconds;
	use parity_scale_codec::{Decode, Encode};

	use crate::{
		entities::{
			interest::ActiveInterestRate,
			pricing::{external::MaxBorrowAmount, internal, internal::InternalActivePricing},
		},
		Config,
	};

	#[derive(Encode, Decode)]
	pub enum Pricing<T: Config> {
		Internal(internal::InternalPricing<T>),
		External(ExternalPricing<T>),
	}

	impl<T: Config> Pricing<T> {
		pub fn migrate(self, with_linear_pricing: bool) -> crate::entities::pricing::Pricing<T> {
			match self {
				Pricing::Internal(i) => crate::entities::pricing::Pricing::Internal(i),
				Pricing::External(e) => {
					crate::entities::pricing::Pricing::External(e.migrate(with_linear_pricing))
				}
			}
		}
	}

	#[derive(Encode, Decode)]
	pub struct ExternalPricing<T: Config> {
		pub price_id: T::PriceId,
		pub max_borrow_amount: MaxBorrowAmount<T::Quantity>,
		pub notional: T::Balance,
		pub max_price_variation: T::Rate,
	}

	#[derive(Encode, Decode)]
	pub enum ActivePricing<T: Config> {
		Internal(InternalActivePricing<T>),
		External(ExternalActivePricing<T>),
	}

	impl<T: Config> ActivePricing<T> {
		pub fn migrate(
			self,
			with_linear_pricing: bool,
		) -> crate::entities::pricing::ActivePricing<T> {
			match self {
				ActivePricing::Internal(i) => crate::entities::pricing::ActivePricing::Internal(i),
				ActivePricing::External(e) => crate::entities::pricing::ActivePricing::External(
					e.migrate(with_linear_pricing),
				),
			}
		}
	}

	#[derive(Encode, Decode)]
	pub struct ExternalActivePricing<T: Config> {
		info: ExternalPricing<T>,
		outstanding_quantity: T::Quantity,
		pub interest: ActiveInterestRate<T>,
		latest_settlement_price: T::Balance,
		settlement_price_updated: Seconds,
	}

	impl<T: Config> ExternalActivePricing<T> {
		pub fn migrate(self, with_linear_pricing: bool) -> super::ExternalActivePricing<T> {
			super::ExternalActivePricing {
				info: self.info.migrate(with_linear_pricing),
				outstanding_quantity: self.outstanding_quantity,
				interest: self.interest,
				latest_settlement_price: self.latest_settlement_price,
				settlement_price_updated: self.settlement_price_updated,
			}
		}
	}

	impl<T: Config> ExternalPricing<T> {
		pub fn migrate(self, with_linear_pricing: bool) -> super::ExternalPricing<T> {
			super::ExternalPricing {
				price_id: self.price_id,
				max_borrow_amount: self.max_borrow_amount,
				notional: self.notional,
				max_price_variation: self.max_price_variation,
				with_linear_pricing,
			}
		}
	}
}