1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// 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,
		amount: Self::Balance,
	) {
		let treasury = TreasuryAccount::get();
		match asset {
			Self::AssetKind::Native => {
				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(
					id,
					treasury,
					U256::from(amount as u128),
				)
				.expect("failed to mint asset into treasury account");
			}
		}
	}
	#[cfg(feature = "runtime-benchmarks")]
	fn ensure_concluded(_: Self::Id) {}
}