use crate::functions::{CurrencyIdOf, GetDataLimit, TransactorOf, XcmTransactorWrapper};
use fp_evm::PrecompileHandle;
use frame_support::dispatch::{GetDispatchInfo, PostDispatchInfo};
use precompile_utils::prelude::*;
use sp_core::{H160, U256};
use sp_runtime::traits::Dispatchable;
use sp_std::{convert::TryFrom, marker::PhantomData};
use xcm::latest::Location;
use xcm_primitives::AccountIdToCurrencyId;
pub struct XcmTransactorPrecompileV2<Runtime>(PhantomData<Runtime>);
#[precompile_utils::precompile]
impl<Runtime> XcmTransactorPrecompileV2<Runtime>
where
Runtime: pallet_xcm_transactor::Config + pallet_evm::Config + frame_system::Config,
Runtime::RuntimeCall: Dispatchable<PostInfo = PostDispatchInfo> + GetDispatchInfo,
Runtime::RuntimeCall: From<pallet_xcm_transactor::Call<Runtime>>,
<Runtime::RuntimeCall as Dispatchable>::RuntimeOrigin: From<Option<Runtime::AccountId>>,
TransactorOf<Runtime>: TryFrom<u8>,
Runtime::AccountId: Into<H160>,
Runtime: AccountIdToCurrencyId<Runtime::AccountId, CurrencyIdOf<Runtime>>,
{
#[precompile::public("indexToAccount(uint16)")]
#[precompile::view]
fn index_to_account(handle: &mut impl PrecompileHandle, index: u16) -> EvmResult<Address> {
XcmTransactorWrapper::<Runtime>::account_index(handle, index)
}
#[precompile::public("transactInfoWithSigned((uint8,bytes[]))")]
#[precompile::view]
fn transact_info_with_signed(
handle: &mut impl PrecompileHandle,
location: Location,
) -> EvmResult<(u64, u64, u64)> {
XcmTransactorWrapper::<Runtime>::transact_info_with_signed(handle, location)
}
#[precompile::public("feePerSecond((uint8,bytes[]))")]
#[precompile::view]
fn fee_per_second(handle: &mut impl PrecompileHandle, location: Location) -> EvmResult<U256> {
XcmTransactorWrapper::<Runtime>::fee_per_second(handle, location)
}
#[precompile::public(
"transactThroughDerivativeMultilocation(\
uint8,\
uint16,\
(uint8,bytes[]),\
uint64,bytes,\
uint256,\
uint64)"
)]
fn transact_through_derivative_multilocation(
handle: &mut impl PrecompileHandle,
transactor: u8,
index: u16,
fee_asset: Location,
weight: u64,
inner_call: BoundedBytes<GetDataLimit>,
fee_amount: Convert<U256, u128>,
overall_weight: u64,
) -> EvmResult {
XcmTransactorWrapper::<Runtime>::transact_through_derivative_multilocation_fee_weight(
handle,
transactor,
index,
fee_asset,
weight,
inner_call,
fee_amount.converted(),
overall_weight,
)
}
#[precompile::public(
"transactThroughDerivative(\
uint8,\
uint16,\
address,\
uint64,\
bytes,\
uint256,\
uint64)"
)]
fn transact_through_derivative(
handle: &mut impl PrecompileHandle,
transactor: u8,
index: u16,
fee_asset: Address,
weight: u64,
inner_call: BoundedBytes<GetDataLimit>,
fee_amount: Convert<U256, u128>,
overall_weight: u64,
) -> EvmResult {
XcmTransactorWrapper::<Runtime>::transact_through_derivative_fee_weight(
handle,
transactor,
index,
fee_asset,
weight,
inner_call,
fee_amount.converted(),
overall_weight,
)
}
#[precompile::public(
"transactThroughSignedMultilocation(\
(uint8,bytes[]),\
(uint8,bytes[]),\
uint64,\
bytes,\
uint256,\
uint64)"
)]
fn transact_through_signed_multilocation(
handle: &mut impl PrecompileHandle,
dest: Location,
fee_asset: Location,
weight: u64,
call: BoundedBytes<GetDataLimit>,
fee_amount: Convert<U256, u128>,
overall_weight: u64,
) -> EvmResult {
XcmTransactorWrapper::<Runtime>::transact_through_signed_multilocation_fee_weight(
handle,
dest,
fee_asset,
weight,
call,
fee_amount.converted(),
overall_weight,
)
}
#[precompile::public(
"transactThroughSigned((uint8,bytes[]),address,uint64,bytes,uint256,uint64)"
)]
fn transact_through_signed(
handle: &mut impl PrecompileHandle,
dest: Location,
fee_asset: Address,
weight: u64,
call: BoundedBytes<GetDataLimit>,
fee_amount: Convert<U256, u128>,
overall_weight: u64,
) -> EvmResult {
XcmTransactorWrapper::<Runtime>::transact_through_signed_fee_weight(
handle,
dest,
fee_asset,
weight,
call,
fee_amount.converted(),
overall_weight,
)
}
#[precompile::public("encodeUtilityAsDerivative(uint8,uint16,bytes)")]
#[precompile::public("encode_utility_as_derivative(uint8,uint16,bytes)")]
#[precompile::view]
fn encode_utility_as_derivative(
handle: &mut impl PrecompileHandle,
transactor: u8,
index: u16,
inner_call: BoundedBytes<GetDataLimit>,
) -> EvmResult<UnboundedBytes> {
XcmTransactorWrapper::<Runtime>::encode_utility_as_derivative(
handle, transactor, index, inner_call,
)
}
}