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
#![deny(warnings)]
#![cfg_attr(test, feature(proc_macro_hygiene))]
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
pub mod amount;
use codec::{EncodeLike, FullCodec};
use frame_support::{dispatch::DispatchResult, traits::Get};
use orml_traits::{MultiCurrency, MultiReservableCurrency};
use primitives::{CurrencyId as PrimitivesCurrencyId, TruncateFixedPointToInt};
use scale_info::TypeInfo;
use sp_core::U256;
use sp_runtime::{
traits::{AtLeast32BitUnsigned, CheckedDiv, MaybeSerializeDeserialize},
DispatchError, FixedPointNumber, FixedPointOperand,
};
use sp_std::{
convert::{TryFrom, TryInto},
fmt::Debug,
marker::PhantomData,
};
use traits::{LoansApi, OracleApi};
pub use amount::Amount;
pub use pallet::*;
mod types;
use types::*;
pub use types::{CurrencyConversion, CurrencyId, Rounding};
pub struct CurrencyConvert<T, Oracle, Loans>(PhantomData<(T, Oracle, Loans)>);
impl<T, Oracle, Loans> CurrencyConversion<Amount<T>, CurrencyId<T>> for CurrencyConvert<T, Oracle, Loans>
where
T: Config,
Oracle: OracleApi<Amount<T>, CurrencyId<T>>,
Loans: LoansApi<CurrencyId<T>, T::AccountId, Amount<T>>,
{
fn convert(amount: &Amount<T>, to: CurrencyId<T>) -> Result<Amount<T>, DispatchError> {
if amount.currency().is_lend_token() && to.is_lend_token() {
let to_underlying_id = Loans::underlying_id(to)?;
let from_underlying_amount = Loans::recompute_underlying_amount(amount)?;
let to_underlying_amount = Oracle::convert(&from_underlying_amount, to_underlying_id)?;
Loans::recompute_collateral_amount(&to_underlying_amount)
} else if amount.currency().is_lend_token() {
Oracle::convert(&Loans::recompute_underlying_amount(amount)?, to)
} else if to.is_lend_token() {
let underlying_id = Loans::underlying_id(to)?;
let underlying_amount = Oracle::convert(amount, underlying_id)?;
Loans::recompute_collateral_amount(&underlying_amount)
} else {
Oracle::convert(amount, to)
}
}
}
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
#[pallet::config]
pub trait Config:
frame_system::Config + orml_tokens::Config<Balance = BalanceOf<Self>, CurrencyId = PrimitivesCurrencyId>
{
type UnsignedFixedPoint: FixedPointNumber<Inner = BalanceOf<Self>>
+ TruncateFixedPointToInt
+ Encode
+ EncodeLike
+ Decode
+ MaybeSerializeDeserialize
+ TypeInfo
+ From<BalanceOf<Self>>
+ MaxEncodedLen;
type SignedInner: Debug
+ CheckedDiv
+ TryFrom<BalanceOf<Self>>
+ TryInto<BalanceOf<Self>>
+ MaybeSerializeDeserialize;
type SignedFixedPoint: FixedPointNumber<Inner = SignedInner<Self>>
+ TruncateFixedPointToInt
+ Encode
+ EncodeLike
+ Decode
+ MaybeSerializeDeserialize;
type Balance: AtLeast32BitUnsigned
+ FixedPointOperand
+ Into<U256>
+ TryFrom<U256>
+ TryFrom<i64> + TryInto<i64> + MaybeSerializeDeserialize
+ FullCodec
+ Copy
+ Default
+ Debug
+ TypeInfo
+ MaxEncodedLen;
#[pallet::constant]
type GetNativeCurrencyId: Get<CurrencyId<Self>>;
#[pallet::constant]
type GetRelayChainCurrencyId: Get<CurrencyId<Self>>;
#[pallet::constant]
type GetWrappedCurrencyId: Get<CurrencyId<Self>>;
type CurrencyConversion: types::CurrencyConversion<Amount<Self>, CurrencyId<Self>>;
}
#[pallet::error]
pub enum Error<T> {
TryIntoIntError,
InvalidCurrency,
}
#[pallet::pallet]
pub struct Pallet<T>(_);
}
pub mod getters {
use super::*;
pub fn get_relay_chain_currency_id<T: Config>() -> CurrencyId<T> {
<T as Config>::GetRelayChainCurrencyId::get()
}
pub fn get_native_currency_id<T: Config>() -> CurrencyId<T> {
<T as Config>::GetNativeCurrencyId::get()
}
pub fn get_wrapped_currency_id<T: Config>() -> CurrencyId<T> {
<T as Config>::GetWrappedCurrencyId::get()
}
}
pub fn get_free_balance<T: Config>(currency_id: T::CurrencyId, account: &T::AccountId) -> Amount<T> {
let amount = <orml_tokens::Pallet<T>>::free_balance(currency_id, account);
Amount::new(amount, currency_id)
}
pub fn get_reserved_balance<T: Config>(currency_id: T::CurrencyId, account: &T::AccountId) -> Amount<T> {
let amount = <orml_tokens::Pallet<T>>::reserved_balance(currency_id, account);
Amount::new(amount, currency_id)
}
pub trait OnSweep<AccountId, Balance> {
fn on_sweep(who: &AccountId, amount: Balance) -> DispatchResult;
}
impl<AccountId, Balance> OnSweep<AccountId, Balance> for () {
fn on_sweep(_: &AccountId, _: Balance) -> DispatchResult {
Ok(())
}
}
pub struct SweepFunds<T, GetAccountId>(PhantomData<(T, GetAccountId)>);
impl<T, GetAccountId> OnSweep<T::AccountId, Amount<T>> for SweepFunds<T, GetAccountId>
where
T: Config,
GetAccountId: Get<T::AccountId>,
{
fn on_sweep(who: &T::AccountId, amount: Amount<T>) -> DispatchResult {
amount.transfer(who, &GetAccountId::get())
}
}