-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathexistence_optional.rs
More file actions
55 lines (46 loc) · 1.82 KB
/
Copy pathexistence_optional.rs
File metadata and controls
55 lines (46 loc) · 1.82 KB
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
//! An implementation that does not require pot account existence and can potentially kill the
//! pot account by withdrawing all the funds from it.
use frame_support::{
sp_runtime::{traits::Convert, DispatchError},
traits::{Currency, ExistenceRequirement, Get, Imbalance, WithdrawReasons},
};
use super::{Config, CurrencySwap, Pallet};
/// A marker type for the implementation that does not require pot accounts existence.
pub enum Marker {}
impl<T: Config<I>, I: 'static>
primitives_currency_swap::CurrencySwap<T::AccountIdFrom, T::AccountIdTo>
for CurrencySwap<Pallet<T, I>, Marker>
{
type From = T::CurrencyFrom;
type To = T::CurrencyTo;
type Error = DispatchError;
fn swap(
incoming_imbalance: <Self::From as Currency<T::AccountIdFrom>>::NegativeImbalance,
) -> Result<
<Self::To as Currency<T::AccountIdTo>>::NegativeImbalance,
primitives_currency_swap::ErrorFor<Self, T::AccountIdFrom, T::AccountIdTo>,
> {
let amount = incoming_imbalance.peek();
let outgoing_imbalance = match T::CurrencyTo::withdraw(
&T::PotTo::get(),
T::BalanceConverter::convert(amount),
WithdrawReasons::TRANSFER,
ExistenceRequirement::AllowDeath,
) {
Ok(imbalance) => imbalance,
Err(error) => {
return Err(primitives_currency_swap::Error {
cause: error,
incoming_imbalance,
})
}
};
T::CurrencyFrom::resolve_creating(&T::PotFrom::get(), incoming_imbalance);
Ok(outgoing_imbalance)
}
fn estimate_swapped_balance(
balance: <Self::From as Currency<T::AccountIdFrom>>::Balance,
) -> <Self::To as Currency<T::AccountIdTo>>::Balance {
T::BalanceConverter::convert(balance)
}
}