Skip to content

Commit

Permalink
fix: 🐛 pallet-traits
Browse files Browse the repository at this point in the history
  • Loading branch information
yooml committed Sep 7, 2023
1 parent 045a683 commit 2fa28ad
Show file tree
Hide file tree
Showing 6 changed files with 3 additions and 1,273 deletions.
53 changes: 0 additions & 53 deletions pallets/lend-market/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ use frame_support::{
use frame_system::{EnsureRoot, EnsureSigned, EnsureSignedBy};
pub use node_primitives::*;
use orml_traits::{DataFeeder, DataProvider, DataProviderExtended};
use pallet_traits::{
DecimalProvider, ExchangeRateProvider, LiquidStakingCurrenciesProvider,
VaultTokenCurrenciesFilter, VaultTokenExchangeRateProvider,
};
use sp_core::H256;
use sp_runtime::{testing::Header, traits::IdentityLookup, AccountId32};
use sp_std::vec::Vec;
Expand Down Expand Up @@ -222,55 +218,6 @@ impl DataFeeder<CurrencyId, TimeStampedPrice, AccountId> for MockDataProvider {
}
}

pub struct LiquidStakingExchangeRateProvider;
impl ExchangeRateProvider<CurrencyId> for LiquidStakingExchangeRateProvider {
fn get_exchange_rate(_: &CurrencyId) -> Option<Rate> {
Some(Rate::saturating_from_rational(150, 100))
}
}

pub struct Decimal;
impl DecimalProvider<CurrencyId> for Decimal {
fn get_decimal(asset_id: &CurrencyId) -> Option<u8> {
match *asset_id {
KSM | VKSM => Some(12),
BNC => Some(12),
DOT_U => Some(12),
_ => Some(12),
}
}
}

pub struct LiquidStaking;
impl LiquidStakingCurrenciesProvider<CurrencyId> for LiquidStaking {
fn get_staking_currency() -> Option<CurrencyId> {
Some(KSM)
}
fn get_liquid_currency() -> Option<CurrencyId> {
Some(VKSM)
}
}

impl ExchangeRateProvider<CurrencyId> for LiquidStaking {
fn get_exchange_rate(_: &CurrencyId) -> Option<Rate> {
Some(Rate::saturating_from_rational(150, 100))
}
}

pub struct TokenExchangeRateProvider;
impl VaultTokenExchangeRateProvider<CurrencyId> for TokenExchangeRateProvider {
fn get_exchange_rate(_: &CurrencyId, _: Rate) -> Option<Rate> {
Some(Rate::saturating_from_rational(100, 150))
}
}

pub struct TokenCurrenciesFilter;
impl VaultTokenCurrenciesFilter<CurrencyId> for TokenCurrenciesFilter {
fn contains(_asset_id: &CurrencyId) -> bool {
return false;
}
}

pub struct VaultLoansRateProvider;
impl LoansMarketDataProvider<CurrencyId, Balance> for VaultLoansRateProvider {
fn get_full_interest_rate(_asset_id: CurrencyId) -> Option<Rate> {
Expand Down
14 changes: 0 additions & 14 deletions pallets/prices/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,6 @@ impl DataFeeder<CurrencyId, TimeStampedPrice, AccountId> for MockDataProvider {
}
}

pub struct LiquidStakingExchangeRateProvider;
impl ExchangeRateProvider<CurrencyId> for LiquidStakingExchangeRateProvider {
fn get_exchange_rate(_: &CurrencyId) -> Option<Rate> {
Some(Rate::saturating_from_rational(150, 100))
}
}

pub struct TokenExchangeRateProvider;
impl VaultTokenExchangeRateProvider<CurrencyId> for TokenExchangeRateProvider {
fn get_exchange_rate(_: &CurrencyId, _: Rate) -> Option<Rate> {
Some(Rate::saturating_from_rational(100, 150))
}
}

ord_parameter_types! {
pub const One: AccountId = 1;
}
Expand Down
22 changes: 2 additions & 20 deletions pallets/prices/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,25 +163,7 @@ fn reset_price_call_work() {
}

#[test]
fn get_liquid_price_work() {
new_test_ext().execute_with(|| {
assert_eq!(
Prices::get_price(&DOT),
Some((Price::from_inner(10_000_000_000 * PRICE_ONE), 0))
);

assert_eq!(
Prices::get_price(&VDOT),
LiquidStakingExchangeRateProvider::get_exchange_rate(&VDOT)
.unwrap()
.checked_mul_int(10_000_000_000 * PRICE_ONE)
.map(|i| (Price::from_inner(i), 0))
);
});
}

#[test]
fn get_ctoken_price_work() {
fn get_token_price_work() {
new_test_ext().execute_with(|| {
assert_eq!(
Prices::get_price(&DOT),
Expand All @@ -196,7 +178,7 @@ fn get_ctoken_price_work() {
}

#[test]
fn get_foreign_ctoken_price_work() {
fn get_foreign_token_price_work() {
new_test_ext().execute_with(|| {
assert_eq!(
Prices::get_price(&DOT),
Expand Down
218 changes: 1 addition & 217 deletions pallets/traits/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{dispatch::DispatchError, traits::tokens::Balance as BalanceT};
use num_bigint::{BigUint, ToBigUint};
use scale_info::TypeInfo;
use sp_runtime::{traits::Zero, RuntimeDebug};
use sp_std::prelude::*;

use primitives::{CurrencyId, DerivativeIndex, PriceDetail, Rate, Timestamp};
use primitives::{CurrencyId, PriceDetail};

pub mod loans;
pub mod ump;
// pub mod xcm;
pub use loans::*;

pub trait EmergencyCallFilter<Call> {
Expand All @@ -22,169 +14,11 @@ pub trait PriceFeeder {
fn get_price(asset_id: &CurrencyId) -> Option<PriceDetail>;
}

pub trait DecimalProvider<CurrencyId> {
fn get_decimal(asset_id: &CurrencyId) -> Option<u8>;
}

pub trait EmergencyPriceFeeder<CurrencyId, Price> {
fn set_emergency_price(asset_id: CurrencyId, price: Price);
fn reset_emergency_price(asset_id: CurrencyId);
}

pub trait ExchangeRateProvider<CurrencyId> {
fn get_exchange_rate(asset_id: &CurrencyId) -> Option<Rate>;
}

pub trait LiquidStakingConvert<Balance> {
fn staking_to_liquid(amount: Balance) -> Option<Balance>;
fn liquid_to_staking(liquid_amount: Balance) -> Option<Balance>;
}

pub trait LiquidStakingCurrenciesProvider<CurrencyId> {
fn get_staking_currency() -> Option<CurrencyId>;
fn get_liquid_currency() -> Option<CurrencyId>;
}

pub trait VaultTokenExchangeRateProvider<CurrencyId> {
fn get_exchange_rate(asset_id: &CurrencyId, init_rate: Rate) -> Option<Rate>;
}

pub trait LPVaultTokenExchangeRateProvider<CurrencyId> {
fn get_exchange_rate(lp_asset_id: &CurrencyId) -> Option<Rate>;
}

pub trait VaultTokenCurrenciesFilter<CurrencyId> {
fn contains(asset_id: &CurrencyId) -> bool;
}

pub trait LPVaultTokenCurrenciesFilter<CurrencyId> {
fn contains(lp_asset_id: &CurrencyId) -> bool;
}

#[derive(
Encode,
Decode,
Eq,
PartialEq,
Copy,
Clone,
RuntimeDebug,
PartialOrd,
Ord,
TypeInfo,
MaxEncodedLen,
)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct Pool<CurrencyId, Balance, BlockNumber> {
pub base_amount: Balance,
pub quote_amount: Balance,
pub base_amount_last: Balance,
pub quote_amount_last: Balance,
pub lp_token_id: CurrencyId,
pub block_timestamp_last: BlockNumber,
pub price_0_cumulative_last: Balance,
pub price_1_cumulative_last: Balance,
}

impl<CurrencyId, Balance: BalanceT, BlockNumber: BalanceT> Pool<CurrencyId, Balance, BlockNumber> {
pub fn new(lp_token_id: CurrencyId) -> Self {
Self {
base_amount: Zero::zero(),
quote_amount: Zero::zero(),
base_amount_last: Zero::zero(),
quote_amount_last: Zero::zero(),
lp_token_id,
block_timestamp_last: Zero::zero(),
price_0_cumulative_last: Zero::zero(),
price_1_cumulative_last: Zero::zero(),
}
}

pub fn is_empty(&self) -> bool {
self.base_amount.is_zero() && self.quote_amount.is_zero()
}
}

/// Exported traits from our AMM pallet. These functions are to be used
/// by the router to enable multi route token swaps
pub trait AMM<AccountId, CurrencyId, Balance, BlockNumber> {
/// Based on the path specified and the available pool balances
/// this will return the amounts outs when trading the specified
/// amount in
fn get_amounts_out(
amount_in: Balance,
path: Vec<CurrencyId>,
) -> Result<Vec<Balance>, DispatchError>;

/// Based on the path specified and the available pool balances
/// this will return the amounts in needed to produce the specified
/// amount out
fn get_amounts_in(
amount_out: Balance,
path: Vec<CurrencyId>,
) -> Result<Vec<Balance>, DispatchError>;

/// Handles a "swap" on the AMM side for "who".
/// This will move the `amount_in` funds to the AMM PalletId,
/// trade `pair.0` to `pair.1` and return a result with the amount
/// of currency that was sent back to the user.
fn swap(
who: &AccountId,
pair: (CurrencyId, CurrencyId),
amount_in: Balance,
) -> Result<(), DispatchError>;

/// Iterate keys of asset pair in AMM Pools
fn get_pools() -> Result<Vec<(CurrencyId, CurrencyId)>, DispatchError>;

/// Returns pool by lp_asset
fn get_pool_by_lp_asset(
asset_id: CurrencyId,
) -> Option<(CurrencyId, CurrencyId, Pool<CurrencyId, Balance, BlockNumber>)>;

/// Returns pool by asset pair
fn get_pool_by_asset_pair(
pair: (CurrencyId, CurrencyId),
) -> Option<Pool<CurrencyId, Balance, BlockNumber>>;
}

/// Exported traits from StableSwap pallet. These functions are to be used
/// by the router.
pub trait StableSwap<AccountId, CurrencyId, Balance> {
/// Based on the path specified and the available pool balances
/// this will return the amounts outs when trading the specified
/// amount in
fn get_amounts_out(
amount_in: Balance,
path: Vec<CurrencyId>,
) -> Result<Vec<Balance>, DispatchError>;

/// Based on the path specified and the available pool balances
/// this will return the amounts in needed to produce the specified
/// amount out
fn get_amounts_in(
amount_out: Balance,
path: Vec<CurrencyId>,
) -> Result<Vec<Balance>, DispatchError>;

/// Handles a "swap" on the AMM side for "who".
/// This will move the `amount_in` funds to the AMM PalletId,
/// trade `pair.0` to `pair.1` and return a result with the amount
/// of currency that was sent back to the user.
fn swap(
who: &AccountId,
pair: (CurrencyId, CurrencyId),
amount_in: Balance,
) -> Result<(), DispatchError>;

fn get_pools() -> Result<Vec<(CurrencyId, CurrencyId)>, DispatchError>;

fn get_reserves(
asset_in: CurrencyId,
asset_out: CurrencyId,
) -> Result<(Balance, Balance), DispatchError>;
}

pub trait ConvertToBigUint {
fn get_big_uint(&self) -> BigUint;
}
Expand All @@ -195,56 +29,6 @@ impl ConvertToBigUint for u128 {
}
}

/// Get relaychain validation data
// pub trait ValidationDataProvider {
// fn validation_data() -> Option<PersistedValidationData>;
// }

/// Distribute liquidstaking asset to multi-accounts
pub trait DistributionStrategy<Balance> {
fn get_bond_distributions(
bonded_amounts: Vec<(DerivativeIndex, Balance, Balance)>,
input: Balance,
cap: Balance,
min_nominator_bond: Balance,
) -> Vec<(DerivativeIndex, Balance)>;
fn get_unbond_distributions(
active_bonded_amounts: Vec<(DerivativeIndex, Balance)>,
input: Balance,
min_nominator_bond: Balance,
) -> Vec<(DerivativeIndex, Balance)>;
fn get_rebond_distributions(
unbonding_amounts: Vec<(DerivativeIndex, Balance)>,
input: Balance,
) -> Vec<(DerivativeIndex, Balance)>;
}

pub trait Streaming<AccountId, CurrencyId, Balance> {
fn create(
sender: AccountId,
recipient: AccountId,
deposit: Balance,
asset_id: CurrencyId,
start_time: Timestamp,
end_time: Timestamp,
cancellable: bool,
) -> Result<(), DispatchError>;
}

impl<AccountId, CurrencyId, Balance> Streaming<AccountId, CurrencyId, Balance> for () {
fn create(
_sender: AccountId,
_recipient: AccountId,
_deposit: Balance,
_asset_id: CurrencyId,
_start_time: Timestamp,
_end_time: Timestamp,
_cancellable: bool,
) -> Result<(), DispatchError> {
Ok(())
}
}

pub trait OnExchangeRateChange<CurrencyId> {
fn on_exchange_rate_change(currency_id: &CurrencyId);
}
Expand Down
Loading

0 comments on commit 2fa28ad

Please sign in to comment.