best_path/
types.rs

1#[cfg(not(feature = "std"))]
2use alloc::vec::Vec;
3#[cfg(feature = "scale")]
4use codec::{Decode, Encode};
5#[cfg(feature = "scale")]
6use scale_info::TypeInfo;
7
8/// Currency representation, eg. "BTC".
9pub trait Currency: Ord + Clone {}
10impl<T: Ord + Clone> Currency for T {}
11
12/// Numeric amount, must be representable by u128.
13pub trait Amount: Copy + TryInto<u128> + TryFrom<u128> {}
14impl<T: Copy + TryInto<u128> + TryFrom<u128>> Amount for T {}
15
16/// Oracle providing currency pair pricing.
17pub trait Provider: Ord + Clone {}
18impl<T: Ord + Clone> Provider for T {}
19
20/// Currency  pair, as provided by Provider.
21#[derive(Clone, Eq, PartialEq, Debug, Ord, PartialOrd)]
22#[cfg_attr(feature = "scale", derive(Encode, Decode, TypeInfo))]
23pub struct Pair<C: Currency> {
24    pub source: C,
25    pub target: C,
26}
27
28/// Currency pair per provider.
29#[derive(Clone, Eq, PartialEq, Debug, Ord, PartialOrd)]
30#[cfg_attr(feature = "scale", derive(Encode, Decode, TypeInfo))]
31pub struct ProviderPair<C: Currency, P: Provider> {
32    pub pair: Pair<C>,
33    pub provider: P,
34}
35
36/// Path for every ProviderPair. Consists of `steps` and overall cost
37#[derive(Clone, Eq, PartialEq, Debug)]
38#[cfg_attr(feature = "scale", derive(Encode, Decode, TypeInfo))]
39pub struct PricePath<C: Currency, A: Amount, P: Provider> {
40    pub total_cost: A,
41    pub steps: Vec<PathStep<C, A, P>>,
42}
43
44/// Conversion cost of source/target currency, per provider. Can be used as a building block for longer paths when there's no direct route.
45#[derive(Clone, Eq, PartialEq, Debug)]
46#[cfg_attr(feature = "scale", derive(Encode, Decode, TypeInfo))]
47pub struct PathStep<C: Currency, A: Amount, P: Provider> {
48    pub pair: Pair<C>,
49    pub provider: P,
50    pub cost: A,
51}
52
53#[derive(Debug)]
54pub enum CalculatorError {
55    NegativeCyclesError,
56    ConversionError,
57}