use super::*;
use frame_support::{
sp_runtime::traits::Zero, traits::tokens::Balance as BalanceTrait, RuntimeDebug,
};
use sp_std::marker::PhantomData;
mod error;
mod internal;
mod lockable;
mod negative_imbalance;
mod node;
mod positive_imbalance;
mod reservable;
#[cfg(test)]
mod property_tests;
pub use error::Error;
pub use internal::TreeImpl;
pub use lockable::{LockId, LockableTree};
pub use negative_imbalance::NegativeImbalance;
pub use node::{ChildrenRefs, GasNode, GasNodeId, NodeLock};
pub use positive_imbalance::PositiveImbalance;
pub use reservable::ReservableTree;
pub type ConsumeResultOf<T> = Result<
Option<(
<T as Tree>::NegativeImbalance,
GasMultiplier<<T as Tree>::Funds, <T as Tree>::Balance>,
<T as Tree>::ExternalOrigin,
)>,
<T as Tree>::Error,
>;
pub type OriginNodeDataOf<T> = (
<T as Tree>::ExternalOrigin,
GasMultiplier<<T as Tree>::Funds, <T as Tree>::Balance>,
<T as Tree>::NodeId,
);
pub trait Tree {
type ExternalOrigin;
type NodeId: Clone;
type Balance: Clone;
type Funds: Clone;
type PositiveImbalance: Imbalance<Balance = Self::Balance>;
type NegativeImbalance: Imbalance<Balance = Self::Balance>;
type InternalError: Error;
type Error: From<Self::InternalError>;
fn total_supply() -> Self::Balance;
fn create(
origin: Self::ExternalOrigin,
multiplier: GasMultiplier<Self::Funds, Self::Balance>,
key: impl Into<Self::NodeId>,
amount: Self::Balance,
) -> Result<Self::PositiveImbalance, Self::Error>;
fn get_origin_node(key: impl Into<Self::NodeId>)
-> Result<OriginNodeDataOf<Self>, Self::Error>;
fn get_external(key: impl Into<Self::NodeId>) -> Result<Self::ExternalOrigin, Self::Error> {
Self::get_origin_node(key).map(|(external, _multiplier, _key)| external)
}
fn get_funds_multiplier(
key: impl Into<Self::NodeId>,
) -> Result<GasMultiplier<Self::Funds, Self::Balance>, Self::Error> {
Self::get_origin_node(key).map(|(_external, multiplier, _key)| multiplier)
}
fn get_origin_key(key: impl Into<Self::NodeId>) -> Result<Self::NodeId, Self::Error> {
Self::get_origin_node(key).map(|(_external, _multiplier, key)| key)
}
fn get_limit_node(
key: impl Into<Self::NodeId>,
) -> Result<(Self::Balance, Self::NodeId), Self::Error>;
fn get_limit_node_consumed(
key: impl Into<Self::NodeId>,
) -> Result<(Self::Balance, Self::NodeId), Self::Error>;
fn get_limit(key: impl Into<Self::NodeId>) -> Result<Self::Balance, Self::Error> {
Self::get_limit_node(key).map(|(balance, _key)| balance)
}
fn get_limit_consumed(key: impl Into<Self::NodeId>) -> Result<Self::Balance, Self::Error> {
Self::get_limit_node_consumed(key).map(|(balance, _key)| balance)
}
fn consume(key: impl Into<Self::NodeId>) -> ConsumeResultOf<Self>;
fn spend(
key: impl Into<Self::NodeId>,
amount: Self::Balance,
) -> Result<Self::NegativeImbalance, Self::Error>;
fn split_with_value(
key: impl Into<Self::NodeId>,
new_key: impl Into<Self::NodeId>,
amount: Self::Balance,
) -> Result<(), Self::Error>;
fn split(
key: impl Into<Self::NodeId>,
new_key: impl Into<Self::NodeId>,
) -> Result<(), Self::Error>;
fn cut(
key: impl Into<Self::NodeId>,
new_key: impl Into<Self::NodeId>,
amount: Self::Balance,
) -> Result<(), Self::Error>;
fn create_deposit(
key: impl Into<Self::NodeId>,
new_key: impl Into<Self::NodeId>,
amount: Self::Balance,
) -> Result<(), Self::Error>;
fn exists(key: impl Into<Self::NodeId>) -> bool;
fn exists_and_deposit(key: impl Into<Self::NodeId>) -> bool;
fn clear();
}
pub trait Provider {
type ExternalOrigin;
type NodeId;
type Balance;
type Funds;
type InternalError: Error;
type Error: From<Self::InternalError>;
type GasTree: LockableTree<
ExternalOrigin = Self::ExternalOrigin,
NodeId = Self::NodeId,
Balance = Self::Balance,
Funds = Self::Funds,
InternalError = Self::InternalError,
Error = Self::Error,
> + ReservableTree;
fn reset() {
Self::GasTree::clear();
}
}
pub trait Imbalance {
type Balance;
fn peek(&self) -> Self::Balance;
fn apply_to(&self, amount: &mut Option<Self::Balance>) -> Result<(), ImbalanceError>;
}
#[derive(Debug, PartialEq)]
pub struct ImbalanceError;