-
Notifications
You must be signed in to change notification settings - Fork 325
/
lib.rs
167 lines (135 loc) · 4.67 KB
/
lib.rs
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
#![cfg_attr(not(feature = "std"), no_std)]
use frame_support::{decl_error, decl_event, decl_module, decl_storage, ensure};
use frame_system::{self as system, ensure_signed};
use orml_traits::{MultiCurrency, MultiCurrencyExtended};
use sp_runtime::{traits::Zero, DispatchResult};
use support::EmergencyShutdown;
mod mock;
mod tests;
pub trait Trait: system::Trait + cdp_engine::Trait {
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
}
type CurrencyIdOf<T> = <<T as vaults::Trait>::Currency as MultiCurrency<<T as system::Trait>::AccountId>>::CurrencyId;
type AmountOf<T> = <<T as vaults::Trait>::Currency as MultiCurrencyExtended<<T as system::Trait>::AccountId>>::Amount;
decl_storage! {
trait Store for Module<T: Trait> as Honzon {
pub Authorization get(fn authorization): double_map hasher(blake2_256) T::AccountId, hasher(blake2_256) (CurrencyIdOf<T>, T::AccountId) => bool;
pub IsShutdown get(fn is_shutdown): bool;
}
}
decl_event!(
pub enum Event<T> where
<T as system::Trait>::AccountId,
CurrencyId = CurrencyIdOf<T>,
{
/// authorization (from, to, currency_id)
Authorization(AccountId, AccountId, CurrencyId),
/// cancel authorization (from, to, currency_id)
UnAuthorization(AccountId, AccountId, CurrencyId),
/// cancel all authorization
UnAuthorizationAll(AccountId),
}
);
decl_error! {
pub enum Error for Module<T: Trait> {
NoAuthorization,
TransferVaultFailed,
UpdatePositionFailed,
LiquidateFailed,
AlreadyShutdown,
MustAfterShutdown,
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
type Error = Error<T>;
fn deposit_event() = default;
pub fn liquidate(_origin, who: T::AccountId, currency_id: CurrencyIdOf<T>) {
ensure!(!Self::is_shutdown(), Error::<T>::AlreadyShutdown);
<cdp_engine::Module<T>>::liquidate_unsafe_cdp(who.clone(), currency_id).map_err(|_| Error::<T>::LiquidateFailed)?;
}
pub fn settle_cdp(_origin, who: T::AccountId, currency_id: CurrencyIdOf<T>) {
ensure!(Self::is_shutdown(), Error::<T>::MustAfterShutdown);
<cdp_engine::Module<T>>::settle_cdp_has_debit(who, currency_id)?;
}
pub fn update_vault(
origin,
currency_id: CurrencyIdOf<T>,
collateral: AmountOf<T>,
debit: T::DebitAmount,
) {
let who = ensure_signed(origin)?;
ensure!(!Self::is_shutdown(), Error::<T>::AlreadyShutdown);
<cdp_engine::Module<T>>::update_position(&who, currency_id, collateral, debit).map_err(|_| Error::<T>::UpdatePositionFailed)?;
}
pub fn withdraw_collateral(
origin,
currency_id: CurrencyIdOf<T>,
collateral: AmountOf<T>,
) {
let who = ensure_signed(origin)?;
ensure!(Self::is_shutdown(), Error::<T>::MustAfterShutdown);
<cdp_engine::Module<T>>::update_position(&who, currency_id, collateral, T::DebitAmount::zero())?;
}
pub fn transfer_vault_from(
origin,
currency_id: CurrencyIdOf<T>,
from: T::AccountId,
) {
let to = ensure_signed(origin)?;
ensure!(!Self::is_shutdown(), Error::<T>::AlreadyShutdown);
// check authorization if `from` can manipulate `to`
Self::check_authorization(&from, &to, currency_id)?;
<vaults::Module<T>>::transfer(from.clone(), to.clone(), currency_id).map_err(|_|
Error::<T>::TransferVaultFailed
)?;
}
/// `origin` allow `to` to manipulate the `currency_id` vault
pub fn authorize(
origin,
currency_id: CurrencyIdOf<T>,
to: T::AccountId,
) {
let from = ensure_signed(origin)?;
// update authorization
<Authorization<T>>::insert(&from, (currency_id, &to), true);
Self::deposit_event(RawEvent::Authorization(from, to, currency_id));
}
/// `origin` refuse `to` to manipulate the vault of `currency_id`
pub fn unauthorize(
origin,
currency_id: CurrencyIdOf<T>,
to: T::AccountId,
) {
let from = ensure_signed(origin)?;
// update authorization
<Authorization<T>>::remove(&from, (currency_id, &to));
Self::deposit_event(RawEvent::UnAuthorization(from, to, currency_id));
}
/// `origin` refuse anyone to manipulate its vault
pub fn unauthorize_all(origin) {
let from = ensure_signed(origin)?;
// update authorization
<Authorization<T>>::remove_prefix(&from);
Self::deposit_event(RawEvent::UnAuthorizationAll(from));
}
}
}
impl<T: Trait> Module<T> {
/// check if `from` allow `to` to manipulate its vault
pub fn check_authorization(from: &T::AccountId, to: &T::AccountId, currency_id: CurrencyIdOf<T>) -> DispatchResult {
ensure!(
from == to || Self::authorization(from, (currency_id, to)),
Error::<T>::NoAuthorization
);
Ok(())
}
pub fn emergency_shutdown() {
<IsShutdown>::put(true);
}
}
impl<T: Trait> EmergencyShutdown for Module<T> {
fn on_emergency_shutdown() {
Self::emergency_shutdown();
}
}