Skip to content
Snippets Groups Projects
lib.rs 4.75 KiB
Newer Older
Bryan Chen's avatar
Bryan Chen committed
#![cfg_attr(not(feature = "std"), no_std)]

Xiliang Chen's avatar
Xiliang Chen committed
pub mod arithmetic;
pub mod auction;

pub use auction::{Auction, AuctionHandler, AuctionInfo, OnNewBidResult};
Shaopeng Wang's avatar
Shaopeng Wang committed
use codec::{Codec, FullCodec, HasCompact};
use rstd::{
Shaopeng Wang's avatar
Shaopeng Wang committed
	cmp::{Eq, PartialEq},
	convert::{TryFrom, TryInto},
	fmt::Debug,
	result,
};
Shaopeng Wang's avatar
Shaopeng Wang committed
use sr_primitives::traits::{MaybeSerializeDeserialize, SimpleArithmetic};

/// Abstraction over a fungible multi-currency system.
pub trait MultiCurrency<AccountId> {
	/// The currency identifier.
Shaopeng Wang's avatar
Shaopeng Wang committed
	type CurrencyId: FullCodec + HasCompact + Eq + PartialEq + Copy + MaybeSerializeDeserialize + Debug;
Shaopeng Wang's avatar
Shaopeng Wang committed

	/// The balance of an account.
	type Balance: SimpleArithmetic + FullCodec + Copy + MaybeSerializeDeserialize + Debug + Default;

	/// The error type.
	type Error: Into<&'static str>;

Shaopeng Wang's avatar
Shaopeng Wang committed
	// Public immutables

	/// The total amount of issuance of `currency_id`.
	fn total_issuance(currency_id: Self::CurrencyId) -> Self::Balance;
Shaopeng Wang's avatar
Shaopeng Wang committed

	/// The combined balance of `who` under `currency_id`.
	fn balance(currency_id: Self::CurrencyId, who: &AccountId) -> Self::Balance;

	// Public mutables

	/// Transfer some amount from one account to another.
	fn transfer(
		currency_id: Self::CurrencyId,
		from: &AccountId,
		to: &AccountId,
		amount: Self::Balance,
	) -> result::Result<(), Self::Error>;
Shaopeng Wang's avatar
Shaopeng Wang committed

	/// Add `amount` to the balance of `who` under `currency_id` and increase total issuance.
	fn deposit(
		currency_id: Self::CurrencyId,
		who: &AccountId,
		amount: Self::Balance,
	) -> result::Result<(), Self::Error>;
Shaopeng Wang's avatar
Shaopeng Wang committed

	/// Remove `amount` from the balance of `who` under `currency_id` and reduce total issuance.
	fn withdraw(
		currency_id: Self::CurrencyId,
		who: &AccountId,
		amount: Self::Balance,
	) -> result::Result<(), Self::Error>;
Shaopeng Wang's avatar
Shaopeng Wang committed

	/// Deduct the balance of `who` by up to `amount`.
	///
Shaopeng Wang's avatar
Shaopeng Wang committed
	/// As much funds up to `amount` will be deducted as possible.  If this is less than `amount`,then a non-zero
	/// value will be returned.
Shaopeng Wang's avatar
Shaopeng Wang committed
	fn slash(currency_id: Self::CurrencyId, who: &AccountId, amount: Self::Balance) -> Self::Balance;
}

/// Extended `MultiCurrency` with additional helper types and methods.
pub trait MultiCurrencyExtended<AccountId>: MultiCurrency<AccountId> {
	/// The type for balance related operations, typically signed int.
	type Amount: arithmetic::Signed
		+ TryInto<Self::Balance>
		+ TryFrom<Self::Balance>
		+ arithmetic::SimpleArithmetic
		+ Codec
		+ Copy
		+ MaybeSerializeDeserialize
		+ Debug
		+ Default;

	/// Add or remove abs(`by_amount`) from the balance of `who` under `currency_id`. If positive `by_amount`, do add, else do remove.
	fn update_balance(
		currency_id: Self::CurrencyId,
		by_amount: Self::Amount,
	) -> result::Result<(), Self::Error>;
}

/// Abstraction over a fungible (single) currency system.
pub trait BasicCurrency<AccountId> {
	/// The balance of an account.
	type Balance: SimpleArithmetic + FullCodec + Copy + MaybeSerializeDeserialize + Debug + Default;

	/// The error type.
	type Error: Into<&'static str>;

	// Public immutables

	/// The total amount of issuance.
	fn total_issuance() -> Self::Balance;

	/// The balance of `who`.
	fn balance(who: &AccountId) -> Self::Balance;

	// Public mutables

	/// Transfer some amount from one account to another.
	fn transfer(from: &AccountId, to: &AccountId, amount: Self::Balance) -> result::Result<(), Self::Error>;

	/// Add `amount` to the balance of `who` and increase total issuance.
	fn deposit(who: &AccountId, amount: Self::Balance) -> result::Result<(), Self::Error>;

	/// Remove `amount` from the balance of `who` and reduce total issuance.
	fn withdraw(who: &AccountId, amount: Self::Balance) -> result::Result<(), Self::Error>;

	/// Deduct the balance of `who` by up to `amount`.
	///
Shaopeng Wang's avatar
Shaopeng Wang committed
	/// As much funds up to `amount` will be deducted as possible. If this is less than `amount`,then a non-zero
	/// value will be returned.
	fn slash(who: &AccountId, amount: Self::Balance) -> Self::Balance;
}

/// Extended `BasicCurrency` with additional helper types and methods.
pub trait BasicCurrencyExtended<AccountId>: BasicCurrency<AccountId> {
	/// The signed type for balance related operations, typically signed int.
	type Amount: arithmetic::Signed
		+ TryInto<Self::Balance>
		+ TryFrom<Self::Balance>
		+ arithmetic::SimpleArithmetic
		+ Codec
		+ Copy
		+ MaybeSerializeDeserialize
		+ Debug
		+ Default;

	/// Add or remove abs(`by_amount`) from the balance of `who`. If positive `by_amount`, do add, else do remove.
	fn update_balance(who: &AccountId, by_amount: Self::Amount) -> result::Result<(), Self::Error>;
Xiliang Chen's avatar
Xiliang Chen committed

#[impl_trait_for_tuples::impl_for_tuples(30)]
pub trait OnNewData<Key, Value> {
	fn on_new_data(key: &Key, value: &Value);
}

pub trait DataProvider<Key, Value> {
	fn get(key: &Key) -> Option<Value>;
}

pub trait PriceProvider<CurrencyId, Price> {
	fn get_price(base: CurrencyId, quote: CurrencyId) -> Option<Price>;
}