Lines
85.19 %
Functions
5.26 %
Branches
100 %
// Copyright 2025 Moonbeam foundation
// This file is part of Moonbeam.
// Moonbeam is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Moonbeam is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.
use frame_support::traits::fungible;
use frame_support::traits::{
fungible::NativeOrWithId,
tokens::{Pay, Preservation::Expendable},
};
use moonbeam_core_primitives::{AssetId, Balance};
use sp_core::{Get, U256};
use sp_runtime::DispatchError;
pub struct MultiAssetPaymaster<R, TreasuryAccount, NativeAsset>(
sp_std::marker::PhantomData<(R, TreasuryAccount, NativeAsset)>,
);
impl<R, TreasuryAccount, NativeAsset> Pay for MultiAssetPaymaster<R, TreasuryAccount, NativeAsset>
where
R: frame_system::Config + pallet_moonbeam_foreign_assets::Config,
TreasuryAccount: Get<R::AccountId>,
NativeAsset: fungible::Mutate<R::AccountId> + fungible::Inspect<R::AccountId>,
{
type Balance = Balance;
type Beneficiary = R::AccountId;
type AssetKind = NativeOrWithId<AssetId>;
type Id = ();
type Error = DispatchError;
fn pay(
who: &Self::Beneficiary,
asset_kind: Self::AssetKind,
amount: Self::Balance,
) -> Result<Self::Id, Self::Error> {
match asset_kind {
Self::AssetKind::Native => {
<NativeAsset as fungible::Mutate<_>>::transfer(
&TreasuryAccount::get(),
who,
amount
.try_into()
.map_err(|_| DispatchError::Other("failed to convert amount"))?,
Expendable,
)?;
Ok(())
}
Self::AssetKind::WithId(id) => pallet_moonbeam_foreign_assets::Pallet::<R>::transfer(
id,
TreasuryAccount::get(),
who.clone(),
U256::from(amount as u128),
)
.map_err(|_| Self::Error::Other("failed to transfer amount")),
fn check_payment(_id: Self::Id) -> frame_support::traits::tokens::PaymentStatus {
frame_support::traits::tokens::PaymentStatus::Success
#[cfg(feature = "runtime-benchmarks")]
fn ensure_successful(
_beneficiary: &Self::Beneficiary,
asset: Self::AssetKind,
) {
let treasury = TreasuryAccount::get();
match asset {
let _ = <NativeAsset as fungible::Mutate<_>>::mint_into(
&treasury,
(amount as u32).into(),
Self::AssetKind::WithId(id) => {
// Fund treasury account
pallet_moonbeam_foreign_assets::Pallet::<R>::mint_into(
treasury,
.expect("failed to mint asset into treasury account");
fn ensure_concluded(_: Self::Id) {}