use crate::{PayRewardFromAccount, RewardsAccountParams};
use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_runtime::{
traits::{Get, IdentifyAccount, Zero},
DispatchError, DispatchResult,
};
#[derive(Clone, Debug)]
pub enum ExplicitOrAccountParams<AccountId, LaneId: Decode + Encode> {
Explicit(AccountId),
Params(RewardsAccountParams<LaneId>),
}
impl<AccountId, LaneId: Decode + Encode> From<RewardsAccountParams<LaneId>>
for ExplicitOrAccountParams<AccountId, LaneId>
{
fn from(params: RewardsAccountParams<LaneId>) -> Self {
ExplicitOrAccountParams::Params(params)
}
}
impl<AccountId: Decode + Encode, LaneId: Decode + Encode> IdentifyAccount
for ExplicitOrAccountParams<AccountId, LaneId>
{
type AccountId = AccountId;
fn into_account(self) -> Self::AccountId {
match self {
ExplicitOrAccountParams::Explicit(account_id) => account_id,
ExplicitOrAccountParams::Params(params) => {
PayRewardFromAccount::<(), AccountId, LaneId, ()>::rewards_account(params)
},
}
}
}
#[derive(
Copy,
Clone,
Debug,
Decode,
DecodeWithMemTracking,
Encode,
Eq,
PartialEq,
TypeInfo,
MaxEncodedLen,
)]
pub struct Registration<BlockNumber, Balance> {
pub valid_till: BlockNumber,
pub stake: Balance,
}
pub trait StakeAndSlash<AccountId, BlockNumber, Balance> {
type RequiredStake: Get<Balance>;
type RequiredRegistrationLease: Get<BlockNumber>;
fn reserve(relayer: &AccountId, amount: Balance) -> DispatchResult;
fn unreserve(relayer: &AccountId, amount: Balance) -> Balance;
fn repatriate_reserved(
relayer: &AccountId,
beneficiary: &AccountId,
amount: Balance,
) -> Result<Balance, DispatchError>;
}
impl<AccountId, BlockNumber, Balance> StakeAndSlash<AccountId, BlockNumber, Balance> for ()
where
Balance: Default + Zero,
BlockNumber: Default,
{
type RequiredStake = ();
type RequiredRegistrationLease = ();
fn reserve(_relayer: &AccountId, _amount: Balance) -> DispatchResult {
Ok(())
}
fn unreserve(_relayer: &AccountId, _amount: Balance) -> Balance {
Zero::zero()
}
fn repatriate_reserved(
_relayer: &AccountId,
_beneficiary: &AccountId,
_amount: Balance,
) -> Result<Balance, DispatchError> {
Ok(Zero::zero())
}
}