use codec::{Decode, Encode, FullCodec, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_core::{RuntimeDebug, TypedGet};
use sp_std::fmt::Debug;
use super::{fungible, Balance, Preservation::Expendable};
pub trait Pay {
type Balance: Balance;
type Beneficiary;
type AssetKind;
type Id: FullCodec + MaxEncodedLen + TypeInfo + Clone + Eq + PartialEq + Debug + Copy;
fn pay(
who: &Self::Beneficiary,
asset_kind: Self::AssetKind,
amount: Self::Balance,
) -> Result<Self::Id, ()>;
fn check_payment(id: Self::Id) -> PaymentStatus;
#[cfg(feature = "runtime-benchmarks")]
fn ensure_successful(who: &Self::Beneficiary, amount: Self::Balance);
#[cfg(feature = "runtime-benchmarks")]
fn ensure_concluded(id: Self::Id);
}
#[derive(Encode, Decode, Eq, PartialEq, Clone, TypeInfo, MaxEncodedLen, RuntimeDebug)]
pub enum PaymentStatus {
InProgress,
Unknown,
Success,
Failure,
}
pub struct PayFromAccount<F, A>(sp_std::marker::PhantomData<(F, A)>);
impl<A: TypedGet, F: fungible::Mutate<A::Type>> Pay for PayFromAccount<F, A> {
type Balance = F::Balance;
type Beneficiary = A::Type;
type AssetKind = ();
type Id = ();
fn pay(
who: &Self::Beneficiary,
_: Self::AssetKind,
amount: Self::Balance,
) -> Result<Self::Id, ()> {
<F as fungible::Mutate<_>>::transfer(&A::get(), who, amount, Expendable).map_err(|_| ())?;
Ok(())
}
fn check_payment(_: ()) -> PaymentStatus {
PaymentStatus::Success
}
#[cfg(feature = "runtime-benchmarks")]
fn ensure_successful(_: &Self::Beneficiary, amount: Self::Balance) {
<F as fungible::Mutate<_>>::mint_into(&A::get(), amount).unwrap();
}
#[cfg(feature = "runtime-benchmarks")]
fn ensure_concluded(_: Self::Id) {}
}