Skip to content

Commit

Permalink
refactor(oracle): Erased types from member methods
Browse files Browse the repository at this point in the history
  • Loading branch information
KirilMihaylov authored and Gancho Manev committed May 25, 2023
1 parent 101b7a7 commit f097002
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 54 deletions.
36 changes: 11 additions & 25 deletions contracts/oracle/src/contract/oracle/feed/mod.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,26 @@
use std::{iter, marker::PhantomData};
use std::marker::PhantomData;

use serde::de::DeserializeOwned;

use finance::{
currency::{self, AnyVisitorPair, Currency, SymbolOwned},
currency::{self, Currency, SymbolOwned},
price::base::BasePrice,
};
use leg_cmd::LegCmd;
use marketprice::{config::Config, market_price::PriceFeeds, SpotPrice};
use price_querier::FedPrices;
use sdk::cosmwasm_std::{Addr, Storage, Timestamp};
use swap::{SwapGroup, SwapTarget};

use crate::{
error::ContractError,
state::supported_pairs::{SupportedPairs, SwapLeg},
ContractError,
};

use self::{leg_cmd::LegCmd, price_querier::FedPrices};

mod leg_cmd;
mod price_querier;

pub type AllPricesIterCmd<'r, OracleBase> = LegCmd<OracleBase, FedPrices<'r>>;
pub type AllPricesIterScanFnItem<'r, 't, OracleBase> = Option<
Result<
BasePrice<SwapGroup, OracleBase>,
<&'t mut AllPricesIterCmd<'r, OracleBase> as AnyVisitorPair>::Error,
>,
>;
pub type AllPricesIterScanFn<'r, OracleBase> =
for<'t> fn(
&'t mut AllPricesIterCmd<'r, OracleBase>,
SwapLeg,
) -> Option<AllPricesIterScanFnItem<'r, 't, OracleBase>>;
pub type AllPricesIter<'r, I, OracleBase> = iter::Flatten<
iter::Scan<I, AllPricesIterCmd<'r, OracleBase>, AllPricesIterScanFn<'r, OracleBase>>,
>;
pub type AllPricesIterItem<OracleBase> = Result<BasePrice<SwapGroup, OracleBase>, ContractError>;

pub struct Feeds<OracleBase> {
feeds: PriceFeeds<'static>,
Expand Down Expand Up @@ -84,21 +70,21 @@ where
swap_pairs_df: I,
at: Timestamp,
total_feeders: usize,
) -> AllPricesIter<'r, I, OracleBase>
) -> impl Iterator<Item = AllPricesIterItem<OracleBase>> + 'r
where
'self_: 'r,
'storage: 'r,
I: Iterator<Item = SwapLeg>,
I: Iterator<Item = SwapLeg> + 'r,
{
let cmd: AllPricesIterCmd<'r, OracleBase> = LegCmd::new(
let cmd: LegCmd<OracleBase, FedPrices<'_>> = LegCmd::new(
FedPrices::new(storage, &self.feeds, at, total_feeders),
vec![],
);

swap_pairs_df
.scan::<_, _, AllPricesIterScanFn<'r, OracleBase>>(
.scan(
cmd,
|cmd: &mut AllPricesIterCmd<'r, OracleBase>, leg: SwapLeg| {
|cmd: &mut LegCmd<OracleBase, FedPrices>, leg: SwapLeg| {
Some(
currency::visit_any_on_tickers::<SwapGroup, SwapGroup, _>(
&leg.from,
Expand Down
16 changes: 8 additions & 8 deletions contracts/oracle/src/contract/oracle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,20 @@ use swap::SwapGroup;
use crate::{
contract::{
alarms::MarketAlarms,
oracle::{feed::AllPricesIter, feed::Feeds},
oracle::feed::{AllPricesIterItem, Feeds},
},
error::ContractError,
msg::{AlarmsStatusResponse, ExecuteAlarmMsg},
result::ContractResult,
state::{
config::Config,
supported_pairs::{SupportedPairs, SwapPairsDfIter},
},
ContractError,
state::{config::Config, supported_pairs::SupportedPairs},
};

use self::feeder::Feeders;

pub mod feed;
pub mod feeder;

pub(crate) type CalculateAllPricesIter<'r, BaseC> = AllPricesIter<'r, SwapPairsDfIter<'r>, BaseC>;
pub(crate) type CalculateAllPricesIterItem<OracleBase> = AllPricesIterItem<OracleBase>;

pub(crate) struct Oracle<'storage, S, OracleBase>
where
Expand Down Expand Up @@ -100,7 +97,10 @@ where
.calc_price(self.storage.deref(), &self.tree, currency, at, self.feeders)
}

fn calc_all_prices(&self, at: Timestamp) -> CalculateAllPricesIter<'_, OracleBase> {
fn calc_all_prices(
&self,
at: Timestamp,
) -> impl Iterator<Item = CalculateAllPricesIterItem<OracleBase>> + '_ {
self.feeds.all_prices_iter(
self.storage.deref(),
self.tree.swap_pairs_df(),
Expand Down
36 changes: 15 additions & 21 deletions contracts/oracle/src/state/supported_pairs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt::Debug, iter, marker::PhantomData};
use std::{fmt::Debug, marker::PhantomData};

use serde::{de::DeserializeOwned, Deserialize, Deserializer, Serialize, Serializer};

Expand Down Expand Up @@ -47,10 +47,6 @@ impl<'de> Deserialize<'de> for SwapLeg {

type Tree = tree::Tree<SwapTarget>;

pub type SwapPairsDfFilterMapFn<'r> = fn(NodeRef<'r, SwapTarget>) -> Option<SwapLeg>;
pub type SwapPairsDfIter<'r> =
iter::FilterMap<tree::TreeIter<'r, SwapTarget>, SwapPairsDfFilterMapFn<'r>>;

#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
pub struct SupportedPairs<B>
where
Expand Down Expand Up @@ -198,25 +194,23 @@ where
))
}

pub fn swap_pairs_df(&self) -> SwapPairsDfIter<'_> {
self.tree
.iter()
.filter_map::<SwapLeg, SwapPairsDfFilterMapFn<'_>>(|node| {
let parent = node.parent()?;
pub fn swap_pairs_df(&self) -> impl Iterator<Item = SwapLeg> + '_ {
self.tree.iter().filter_map(|node: NodeRef<SwapTarget>| {
let parent: NodeRef<SwapTarget> = node.parent()?;

let SwapTarget {
let SwapTarget {
pool_id,
target: child,
} = node.value().clone();

Some(SwapLeg {
from: child,
to: SwapTarget {
pool_id,
target: child,
} = node.value().clone();

Some(SwapLeg {
from: child,
to: SwapTarget {
pool_id,
target: parent.value().target.clone(),
},
})
target: parent.value().target.clone(),
},
})
})
}

pub fn query_swap_tree(self) -> Tree {
Expand Down

0 comments on commit f097002

Please sign in to comment.