Skip to content

Commit

Permalink
Add more TS tests for pallet-xcm precompile (#3107)
Browse files Browse the repository at this point in the history
* add tests for native fee payment

* fix sudo

* add tests for transferAssetsUsingTypeAndThen()

* fix test name

* try using evm foreign assets in pallet-xcm precompile

* fix: pallet-xcm-precompile shall allow subcalls

* increase recursion_limit to 1 in pallet-xcm-precompile

* fix formatting

* test(): correctly check balance of evm native assets

* fix tests

* clean code

* refactor test

---------

Co-authored-by: Rodrigo Quelhas <[email protected]>
Co-authored-by: Rodrigo Quelhas <[email protected]>
Co-authored-by: Gonza Montiel <[email protected]>
  • Loading branch information
4 people committed Jan 17, 2025
1 parent 77c99af commit 06c3db2
Show file tree
Hide file tree
Showing 6 changed files with 499 additions and 127 deletions.
16 changes: 9 additions & 7 deletions runtime/moonbase/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

use super::moonbase_weights;
use crate::{
asset_config::ForeignAssetInstance,
xcm_config::{AssetType, XcmExecutorConfig},
OpenTechCommitteeInstance, TreasuryCouncilInstance,
asset_config::ForeignAssetInstance, xcm_config::XcmExecutorConfig, OpenTechCommitteeInstance,
TreasuryCouncilInstance,
};
use crate::{AccountId, AssetId, AssetManager, Balances, Erc20XcmBridge, Runtime, H160};
use crate::{AccountId, AssetId, Balances, Erc20XcmBridge, EvmForeignAssets, Runtime, H160};
use frame_support::parameter_types;
use moonkit_xcm_primitives::{
location_matcher::{Erc20PalletMatcher, ForeignAssetMatcher, SingleAddressMatcher},
Expand Down Expand Up @@ -61,7 +60,6 @@ use pallet_precompile_benchmarks::WeightInfo;
use precompile_foreign_asset_migrator::ForeignAssetMigratorPrecompile;
use precompile_utils::precompile_set::*;
use sp_std::prelude::*;
use xcm_primitives::AsAssetType;

parameter_types! {
pub P256VerifyWeight: frame_support::weights::Weight =
Expand Down Expand Up @@ -113,7 +111,7 @@ type EthereumPrecompilesChecks = (AcceptDelegateCall, CallableByContract, Callab

// Pallet-xcm precompile types.
// Type that converts AssetId into Location
type AssetIdToLocationManager = AsAssetType<AssetId, AssetType, AssetManager>;
type AssetIdToLocationManager = EvmForeignAssets;

// The pallet-balances address is identified by ERC20_BALANCES_PRECOMPILE const
type SingleAddressMatch = SingleAddressMatcher<AccountId, ERC20_BALANCES_PRECOMPILE, Balances>;
Expand Down Expand Up @@ -280,7 +278,11 @@ type MoonbasePrecompilesAt<R> = (
PrecompileAt<
AddressU64<2074>,
PalletXcmPrecompile<R, (SingleAddressMatch, ForeignAssetMatch, Erc20Match)>,
(CallableByContract, CallableByPrecompile),
(
CallableByContract,
CallableByPrecompile,
SubcallWithMaxNesting<1>,
),
>,
PrecompileAt<AddressU64<2075>, ForeignAssetMigratorPrecompile<R>, ()>,
);
Expand Down
6 changes: 5 additions & 1 deletion runtime/moonbeam/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,11 @@ type MoonbeamPrecompilesAt<R> = (
PrecompileAt<
AddressU64<2074>,
PalletXcmPrecompile<R, (SingleAddressMatch, ForeignAssetMatch, Erc20Match)>,
(CallableByContract, CallableByPrecompile),
(
CallableByContract,
CallableByPrecompile,
SubcallWithMaxNesting<1>,
),
>,
);

Expand Down
6 changes: 5 additions & 1 deletion runtime/moonriver/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,11 @@ type MoonriverPrecompilesAt<R> = (
PrecompileAt<
AddressU64<2074>,
PalletXcmPrecompile<R, (SingleAddressMatch, ForeignAssetMatch, Erc20Match)>,
(CallableByContract, CallableByPrecompile),
(
CallableByContract,
CallableByPrecompile,
SubcallWithMaxNesting<1>,
),
>,
);

Expand Down
11 changes: 7 additions & 4 deletions test/helpers/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,20 +479,23 @@ export async function registerAndFundAsset(
context: any,
asset: TestAsset,
amount: bigint,
address: `0x${string}`
address: `0x${string}`,
addToWeightTrader = true
) {
const { registeredAssetId } = await registerForeignAsset(
const result = await registerForeignAsset(
context,
BigInt(asset.id),
asset.location,
asset.metadata
);

await addAssetToWeightTrader(asset.location, asset.relativePrice || 0n, context);
if (addToWeightTrader) {
await addAssetToWeightTrader(asset.location, asset.relativePrice || 0n, context);
}

await mockAssetBalance(context, amount, BigInt(asset.id), alith, address);

return registeredAssetId;
return result;
}

// Mock balance for old foreign assets
Expand Down
Loading

0 comments on commit 06c3db2

Please sign in to comment.