Skip to content

Commit

Permalink
Merge pull request #397 from nolus-protocol/redesign-pricedto
Browse files Browse the repository at this point in the history
refactor(currency): PriceDTO changed focus - only at Oracle feeds
  • Loading branch information
gmanev7 authored Sep 19, 2024
2 parents a80009e + 32bde1a commit 01b5b39
Show file tree
Hide file tree
Showing 59 changed files with 776 additions and 773 deletions.
12 changes: 5 additions & 7 deletions platform/contracts/treasury/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use access_control::SingleUserAccess;
use admin_contract::msg::{
ProtocolQueryResponse, ProtocolsQueryResponse, QueryMsg as ProtocolsRegistry,
};
use currency::NativePlatform;
use currency::platform::PlatformGroup;
use finance::{duration::Duration, percent::Percent};
use lpp_platform::StableCurrencyGroup;
use platform::{batch::Batch, message::Response as MessageResponse, response};
use sdk::{
cosmwasm_ext::Response as CwResponse,
Expand Down Expand Up @@ -113,11 +112,10 @@ fn try_build_reward<'q>(
.map(|protocol| {
PoolImpl::new(
lpp_platform::new_stub(protocol.contracts.lpp, querier, env),
oracle_platform::new_unchecked_stable_quote_stub::<
NativePlatform,
_,
StableCurrencyGroup,
>(protocol.contracts.oracle, querier),
oracle_platform::new_unchecked_stable_quote_stub::<PlatformGroup, _>(
protocol.contracts.oracle,
querier,
),
)
})
.collect();
Expand Down
14 changes: 7 additions & 7 deletions platform/contracts/treasury/src/pool/impl_.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use currency::NativePlatform;
use currency::platform::{PlatformGroup, Stable};
use finance::{duration::Duration, interest, percent::Percent};
use lpp_platform::{CoinStable, Lpp as LppTrait, Stable, StableCurrencyGroup};
use lpp_platform::{CoinStable, Lpp as LppTrait};
use oracle_platform::{convert, Oracle, OracleRef};
use platform::message::Response as MessageResponse;

Expand All @@ -17,7 +17,7 @@ pub struct Pool<Lpp, StableOracle> {
impl<Lpp, StableOracle> Pool<Lpp, StableOracle>
where
Lpp: LppTrait,
StableOracle: Oracle<NativePlatform, QuoteC = Stable, QuoteG = StableCurrencyGroup>
StableOracle: Oracle<PlatformGroup, QuoteC = Stable, QuoteG = PlatformGroup>
+ AsRef<OracleRef<StableOracle::QuoteC, StableOracle::QuoteG>>,
{
pub fn new(lpp: Lpp, oracle: StableOracle) -> Result<Self, ContractError> {
Expand All @@ -34,7 +34,7 @@ where
impl<Lpp, StableOracle> PoolTrait for Pool<Lpp, StableOracle>
where
Lpp: LppTrait,
StableOracle: Oracle<NativePlatform, QuoteC = Stable, QuoteG = StableCurrencyGroup>,
StableOracle: Oracle<PlatformGroup, QuoteC = Stable, QuoteG = PlatformGroup>,
{
fn balance(&self) -> CoinStable {
self.balance
Expand All @@ -47,7 +47,7 @@ where
) -> Result<MessageResponse, ContractError> {
let reward_in_stable = interest::interest(apr, self.balance, period);

convert::from_quote::<_, _, _, _, NativePlatform>(&self.oracle, reward_in_stable)
convert::from_quote::<_, _, _, _, PlatformGroup>(&self.oracle, reward_in_stable)
.map_err(ContractError::ConvertRewardsToNLS)
.and_then(|rewards| {
self.lpp
Expand All @@ -59,7 +59,7 @@ where

#[cfg(test)]
mod test {
use currency::NlsPlatform;
use currency::platform::Nls;
use finance::{coin::Coin, duration::Duration, fraction::Fraction, percent::Percent, price};
use lpp_platform::{test::DummyLpp, CoinStable};
use oracle_platform::{test::DummyOracle, Oracle};
Expand All @@ -85,7 +85,7 @@ mod test {
let bar0_apr = Percent::from_percent(20);
let lpp0_tvl: CoinStable = 15_000.into();

let lpp = DummyLpp::with_balance(lpp0_tvl, Coin::<NlsPlatform>::default());
let lpp = DummyLpp::with_balance(lpp0_tvl, Coin::<Nls>::default());
let oracle = DummyOracle::failing();

let pool = PoolImpl::new(lpp, oracle).unwrap();
Expand Down
7 changes: 3 additions & 4 deletions platform/packages/currency/src/from_symbol_any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ mod test {
from_symbol_any::GroupVisit,
test::{
Expect, ExpectPair, ExpectUnknownCurrency, SubGroup, SubGroupTestC10, SubGroupTestC6,
SuperGroup, SuperGroupTestC1, SuperGroupTestC2, SuperGroupTestC3, SuperGroupTestC5,
SuperGroup, SuperGroupTestC1, SuperGroupTestC2, SuperGroupTestC3, SuperGroupTestC4,
},
CurrencyDef, Group, MemberOf, Tickers,
};
Expand Down Expand Up @@ -287,14 +287,13 @@ mod test {
visit_any_currencies_ok::<SuperGroup, SuperGroupTestC2, SubGroupTestC10>();
visit_any_currencies_ok::<SuperGroup, SubGroupTestC10, SuperGroupTestC2>();

// visit_any_currencies_nok::<SubGroup, SubGroupTestC10, SubGroupTestC10>();
visit_any_currencies_nok::<SuperGroup, SubGroupTestC10, SubGroupTestC10>();

visit_any_currencies_nok::<SuperGroup, SuperGroupTestC1, SuperGroupTestC3>();
visit_any_currencies_nok::<SuperGroup, SuperGroupTestC3, SuperGroupTestC1>();

visit_any_currencies_nok::<SuperGroup, SuperGroupTestC1, SuperGroupTestC5>();
visit_any_currencies_nok::<SuperGroup, SuperGroupTestC5, SuperGroupTestC1>();
visit_any_currencies_nok::<SuperGroup, SuperGroupTestC2, SuperGroupTestC4>();
visit_any_currencies_nok::<SuperGroup, SuperGroupTestC4, SuperGroupTestC2>();
}

fn visit_any_currencies_ok<VisitedG, CDef1, CDef2>()
Expand Down
2 changes: 1 addition & 1 deletion platform/packages/currency/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ where
Self: MemberOf<Self::TopG>,
{
const DESCR: &'static str;
type TopG: Group;
type TopG: Group<TopG = Self::TopG>;

// Visit this group directly by a visitor
fn maybe_visit<M, V>(matcher: &M, visitor: V) -> MaybeAnyVisitResult<Self, V>
Expand Down
3 changes: 1 addition & 2 deletions platform/packages/currency/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub use crate::{
},
group::{Group, MaybeAnyVisitResult, MemberOf},
matcher::{Matcher, TypeMatcher},
nls::{Native as NativePlatform, NlsPlatform},
pairs::{MaybePairsVisitorResult, PairsGroup, PairsVisitor},
symbol::{BankSymbols, DexSymbols, Symbol, Tickers},
};
Expand All @@ -23,8 +22,8 @@ mod from_symbol_any;
mod group;
mod matcher;
pub mod never;
mod nls;
mod pairs;
pub mod platform;
mod symbol;
#[cfg(any(test, feature = "testing"))]
pub mod test;
Expand Down
76 changes: 0 additions & 76 deletions platform/packages/currency/src/nls.rs

This file was deleted.

112 changes: 112 additions & 0 deletions platform/packages/currency/src/platform.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
use serde::{Deserialize, Serialize};

use sdk::schemars::{self, JsonSchema};

use crate::{
AnyVisitor, CurrencyDTO, CurrencyDef, Definition, Group, Matcher, MaybeAnyVisitResult,
MaybePairsVisitorResult, MemberOf, PairsGroup, PairsVisitor,
};

#[derive(
Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Serialize, Deserialize, JsonSchema,
)]
pub struct Stable(CurrencyDTO<PlatformGroup>);

impl CurrencyDef for Stable {
type Group = PlatformGroup;

fn definition() -> &'static Self {
const INSTANCE: &Stable = &Stable(CurrencyDTO::new(&Definition::new(
"STABLE",
"N/A_N/A_N/A",
"N/A_N/A_N/A",
0,
)));
INSTANCE
}

fn dto(&self) -> &CurrencyDTO<Self::Group> {
&self.0
}
}
impl PairsGroup for Stable {
type CommonGroup = PlatformGroup;

fn maybe_visit<M, V>(_matcher: &M, _visitor: V) -> MaybePairsVisitorResult<V>
where
M: Matcher,
V: PairsVisitor<Pivot = Self>,
{
unreachable!("The 'Stable' platform currency used in pairs resolution!")
}
}

#[derive(
Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Serialize, Deserialize, JsonSchema,
)]
/// A 'platform'-only 'dex-independent' representation of Nls.
///
/// Intended to be used *only* until the TODO below gets done, and *only* in dex-independent usecases:
/// - LP rewards
/// - Relayers' tips
pub struct Nls(CurrencyDTO<PlatformGroup>);

impl CurrencyDef for Nls {
type Group = PlatformGroup;

fn definition() -> &'static Self {
const INSTANCE: &Nls = &Nls(CurrencyDTO::new(&Definition::new(
"NLS",
"unls",
// TODO Define trait PlatformCurrency as a super trait of Currency and
// merge NlsPlatform and Nls
"N/A_N/A_N/A",
6,
)));

INSTANCE
}

fn dto(&self) -> &CurrencyDTO<Self::Group> {
&self.0
}
}

impl PairsGroup for Nls {
type CommonGroup = PlatformGroup;

fn maybe_visit<M, V>(_matcher: &M, _visitor: V) -> MaybePairsVisitorResult<V>
where
M: Matcher,
V: PairsVisitor,
{
unreachable!("The 'Nls' platform currency used in pairs resolution!")
}
}

#[derive(Debug, Copy, Clone, Ord, PartialEq, PartialOrd, Eq, Deserialize)]
pub struct PlatformGroup;
impl Group for PlatformGroup {
const DESCR: &'static str = "platform currencies";
type TopG = Self;

fn maybe_visit<M, V>(matcher: &M, visitor: V) -> MaybeAnyVisitResult<Self, V>
where
M: Matcher,
V: AnyVisitor<Self>,
{
Self::maybe_visit_member(matcher, visitor)
}

fn maybe_visit_member<M, V>(matcher: &M, visitor: V) -> MaybeAnyVisitResult<Self::TopG, V>
where
M: Matcher,
V: AnyVisitor<Self::TopG>,
{
crate::maybe_visit_member::<_, Nls, Self::TopG, _>(matcher, visitor)
.or_else(|v| MaybeAnyVisitResult::Ok(v.on::<Stable>(Stable::definition().dto())))
// we accept ANY currency to allow any stable@protocol to be a member
}
}

impl MemberOf<Self> for PlatformGroup {}
20 changes: 19 additions & 1 deletion platform/packages/currency/src/test/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,14 @@ impl PairsGroup for SuperGroupTestC1 {
use crate::maybe_visit_buddy as maybe_visit;
maybe_visit::<SuperGroupTestC2, _, _>(matcher, visitor)
.or_else(|v| maybe_visit::<SuperGroupTestC4, _, _>(matcher, v))
.or_else(|v| maybe_visit::<SuperGroupTestC5, _, _>(matcher, v))
.or_else(|v| maybe_visit::<SubGroupTestC10, _, _>(matcher, v))
}
}
impl InPoolWith<SuperGroup> for SuperGroupTestC1 {}
impl InPoolWith<SuperGroupTestC2> for SuperGroupTestC1 {}
impl InPoolWith<SuperGroupTestC4> for SuperGroupTestC1 {}
impl InPoolWith<SuperGroupTestC5> for SuperGroupTestC1 {}

//Pool pairs: 1:2, 1:4, 2:3, 4:5, 2:6, 2:10, 5:10, 6:10
impl PairsGroup for SuperGroupTestC2 {
Expand Down Expand Up @@ -100,10 +103,14 @@ impl PairsGroup for SuperGroupTestC3 {
{
use crate::maybe_visit_buddy as maybe_visit;
maybe_visit::<SuperGroupTestC2, _, _>(matcher, visitor)
.or_else(|v| maybe_visit::<SuperGroupTestC4, _, _>(matcher, v))
.or_else(|v| maybe_visit::<SuperGroupTestC5, _, _>(matcher, v))
.or_else(|v| maybe_visit::<SubGroupTestC10, _, _>(matcher, v))
}
}
impl InPoolWith<SuperGroup> for SuperGroupTestC3 {}
impl InPoolWith<SuperGroupTestC2> for SuperGroupTestC3 {}
impl InPoolWith<SuperGroupTestC5> for SuperGroupTestC3 {}

//Pool pairs: 1:2, 1:4, 2:3, 4:5, 2:6, 2:10, 5:10, 6:10
impl PairsGroup for SuperGroupTestC4 {
Expand All @@ -117,10 +124,12 @@ impl PairsGroup for SuperGroupTestC4 {
use crate::maybe_visit_buddy as maybe_visit;
maybe_visit::<SuperGroupTestC1, _, _>(matcher, visitor)
.or_else(|v| maybe_visit::<SuperGroupTestC5, _, _>(matcher, v))
.or_else(|v| maybe_visit::<SubGroupTestC10, _, _>(matcher, v))
}
}
impl InPoolWith<SuperGroup> for SuperGroupTestC4 {}
impl InPoolWith<SuperGroupTestC1> for SuperGroupTestC4 {}
impl InPoolWith<SuperGroupTestC3> for SuperGroupTestC4 {}
impl InPoolWith<SuperGroupTestC5> for SuperGroupTestC4 {}

//Pool pairs: 1:2, 1:4, 2:3, 4:5, 2:6, 2:10, 5:10, 6:10
Expand All @@ -133,12 +142,18 @@ impl PairsGroup for SuperGroupTestC5 {
V: PairsVisitor<Pivot = Self>,
{
use crate::maybe_visit_buddy as maybe_visit;
maybe_visit::<SuperGroupTestC4, _, _>(matcher, visitor)
maybe_visit::<SuperGroupTestC1, _, _>(matcher, visitor)
.or_else(|v| maybe_visit::<SuperGroupTestC3, _, _>(matcher, v))
.or_else(|v| maybe_visit::<SuperGroupTestC4, _, _>(matcher, v))
.or_else(|v| maybe_visit::<SuperGroupTestC5, _, _>(matcher, v))
.or_else(|v| maybe_visit::<SubGroupTestC10, _, _>(matcher, v))
}
}
impl InPoolWith<SuperGroup> for SuperGroupTestC5 {}
impl InPoolWith<SuperGroupTestC1> for SuperGroupTestC5 {}
impl InPoolWith<SuperGroupTestC3> for SuperGroupTestC5 {}
impl InPoolWith<SuperGroupTestC4> for SuperGroupTestC5 {}
impl InPoolWith<SuperGroupTestC5> for SuperGroupTestC5 {} // Note Self is InPoolWith<Self>, defined so to allow 'same-currency' 'unit tests on PriceDTO
impl InPoolWith<SubGroupTestC10> for SuperGroupTestC5 {}

#[derive(Debug, Copy, Clone, Ord, PartialEq, PartialOrd, Eq, Deserialize)]
Expand Down Expand Up @@ -214,7 +229,10 @@ impl PairsGroup for SubGroupTestC10 {
}
}
impl InPoolWith<SuperGroup> for SubGroupTestC10 {}
impl InPoolWith<SuperGroupTestC1> for SubGroupTestC10 {}
impl InPoolWith<SuperGroupTestC2> for SubGroupTestC10 {}
impl InPoolWith<SuperGroupTestC3> for SubGroupTestC10 {}
impl InPoolWith<SuperGroupTestC4> for SubGroupTestC10 {}
impl InPoolWith<SuperGroupTestC5> for SubGroupTestC10 {}
impl InPoolWith<SubGroupTestC6> for SubGroupTestC10 {}

Expand Down
Loading

0 comments on commit 01b5b39

Please sign in to comment.