Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test 🧹: Update tests using old Foreign Assets #3195

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 0 additions & 98 deletions test/helpers/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,104 +279,6 @@ export async function addAssetToWeightTrader(asset: any, relativePrice: bigint,
}
}

// This registers an old foreign asset via the asset-manager pallet.
// DEPRECATED: Please don't use for new tests
export async function registerOldForeignAsset(
context: DevModeContext,
asset: any,
metadata: AssetMetadata,
unitsPerSecond?: number,
numAssetsWeightHint?: number
) {
const { result } = await context.createBlock(
context
.polkadotJs()
.tx.sudo.sudo(
context.polkadotJs().tx.assetManager.registerForeignAsset(asset, metadata, new BN(1), true)
)
);

const polkadotJs = await ApiPromise.create({
provider: new WsProvider(`ws://localhost:${process.env.MOONWALL_RPC_PORT}/`),
...runtimeApi,
});

const WEIGHT_REF_TIME_PER_SECOND = 1_000_000_000_000;
const weight = {
refTime: WEIGHT_REF_TIME_PER_SECOND,
proofSize: 0,
};

const nativeAmountPerSecond = await context
.polkadotJs()
.call.transactionPaymentApi.queryWeightToFee(weight);

const relativePriceDecimals = new BN(18);
const relativePrice = nativeAmountPerSecond
.mul(new BN(10).pow(relativePriceDecimals))
.div(unitsPerSecond ? new BN(unitsPerSecond) : new BN(1));

const assetV4 = patchLocationV4recursively(asset.Xcm);
const { result: result2 } = await context.createBlock(
context
.polkadotJs()
.tx.sudo.sudo(context.polkadotJs().tx.xcmWeightTrader.addAsset(assetV4, relativePrice)),
{
expectEvents: [context.polkadotJs().events.xcmWeightTrader.SupportedAssetAdded as any],
allowFailures: false,
}
);

// If no unitspersecond is provided, we add the asset to the supported assets
// and force-set the relative price to 0
if (unitsPerSecond == null) {
const module = xxhashAsU8a(new TextEncoder().encode("XcmWeightTrader"), 128);
const method = xxhashAsU8a(new TextEncoder().encode("SupportedAssets"), 128);

const assetLocationU8a = context
.polkadotJs()
.createType("StagingXcmV4Location", assetV4)
.toU8a();

const blake2concatStagingXcmV4Location = new Uint8Array([
...blake2AsU8a(assetLocationU8a, 128),
...assetLocationU8a,
]);

const overallAssetKey = new Uint8Array([
...module,
...method,
...blake2concatStagingXcmV4Location,
]);

await context.createBlock(
context.polkadotJs().tx.sudo.sudo(
context.polkadotJs().tx.system.setStorage([
[
u8aToHex(overallAssetKey),
"0x0100000000000000000000000000000000", // (enabled bool, 0 u128)
],
])
)
);
}

const registeredAssetId = result!.events
.find(({ event: { section } }) => section.toString() === "assetManager")!
.event.data[0].toHex()
.replace(/,/g, "");

// check asset in storage
const registeredAsset = (
(await context.polkadotJs().query.assets.asset(registeredAssetId)) as any
).unwrap();
return {
registeredAssetId,
events: result2!.events,
registeredAsset,
};
}

/**
*
* This registers a foreign asset via the moonbeam - foreign - assets pallet.
Expand Down
146 changes: 86 additions & 60 deletions test/suites/dev/moonbase/test-maintenance/test-maintenance-filter3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,37 @@ import "@moonbeam-network/api-augment";
import {
beforeAll,
beforeEach,
customDevRpcRequest,
describeSuite,
expect,
execOpenTechCommitteeProposal,
} from "@moonwall/cli";
import { ALITH_ADDRESS } from "@moonwall/util";
import { BN } from "@polkadot/util";
import { addAssetToWeightTrader } from "../../../../helpers";
import {
expectSystemEvent,
fundAccount,
getPalletIndex,
injectHrmpMessage,
type RawXcmMessage,
sovereignAccountOfSibling,
XcmFragment,
} from "../../../../helpers";

describeSuite({
id: "D012003",
title: "Maintenance Mode - Filter2",
foundationMethods: "dev",
testCases: ({ context, it, log }) => {
let assetId: string;
const assetId = 1;
const foreignParaId = 2000;
const assetLocation = {
parents: 1,
interior: {
X3: [{ Parachain: foreignParaId }, { PalletInstance: 1 }, { GeneralIndex: 1 }],
},
};

beforeAll(async () => {
const assetMetadata = {
name: "FOREIGN",
symbol: "FOREIGN",
decimals: new BN(12),
isFroze: false,
};

const sourceLocation = {
Xcm: { parents: 1, interior: { X1: { Parachain: foreignParaId } } },
};

// registerForeignAsset
const { result } = await context.createBlock(
context
.polkadotJs()
.tx.sudo.sudo(
context
.polkadotJs()
.tx.assetManager.registerForeignAsset(sourceLocation, assetMetadata, new BN(1), true)
)
);

const events = result?.events.find(
({ event: { section } }) => section.toString() === "assetManager"
);

if (!events) {
throw new Error("Events Not Found!");
}

assetId = events.event.data[0].toHex().replace(/,/g, "");

// set relative price in xcmWeightTrader
await addAssetToWeightTrader(sourceLocation, 0n, context);
const paraAddress = sovereignAccountOfSibling(context, foreignParaId) as `0x${string}`;
await fundAccount(paraAddress, 1_000_000_000_000_000_000_000n, context);
});

beforeEach(async () => {
Expand All @@ -67,36 +46,83 @@ describeSuite({
id: "T01",
title: "should queue XCM messages until resuming operations",
test: async () => {
// Send RPC call to inject XCMP message
// You can provide a message, but if you don't a downward transfer is the default
await customDevRpcRequest("xcm_injectHrmpMessage", [foreignParaId, []]);
const createForeignAssetCall = context
.polkadotJs()
.tx.evmForeignAssets.createForeignAsset(assetId, assetLocation, 18, "TEST", "TEST");

// Create a block in which the XCM should be executed
await context.createBlock();
const { blockRes } = await sendCallAsParaUnchecked(
createForeignAssetCall,
foreignParaId,
context,
1_000_000_000_000_000_000n
);

// Make sure the state does not have ALITH's foreign asset tokens
let alithBalance = (await context
.polkadotJs()
.query.assets.account(assetId, ALITH_ADDRESS)) as any;
// Alith balance is 0
expect(alithBalance.isNone).to.eq(true);
// Check asset has not been created yet
const assetInfo = await context.polkadotJs().query.evmForeignAssets.assetsById(assetId);
expect(assetInfo.isNone).to.eq(true);

// turn maintenance off
// Turn maintenance off
await execOpenTechCommitteeProposal(
context,
context.polkadotJs().tx.maintenanceMode.resumeNormalOperation()
);

// Create a block in which the XCM will be executed
await context.createBlock();

// Make sure the state has ALITH's to foreign assets tokens
alithBalance = (
await context.polkadotJs().query.assets.account(assetId, ALITH_ADDRESS)
).unwrap();

expect(alithBalance.balance.toBigInt()).to.eq(10000000000000n);
// Expect the asset to be created
const currentBlock = await context.polkadotJs().rpc.chain.getBlock();
await expectSystemEvent(
currentBlock.block.hash.toString(),
"evmForeignAssets",
"ForeignAssetCreated",
context
);
},
});
},
});

const sendCallAsParaUnchecked = async (call, paraId, context, fungible) => {
const encodedCall = call.method.toHex();
const balancesPalletIndex = await getPalletIndex("Balances", context);

const xcmMessage = new XcmFragment({
assets: [
{
multilocation: {
parents: 0,
interior: {
X1: { PalletInstance: balancesPalletIndex },
},
},
fungible: fungible,
},
],
weight_limit: {
refTime: 40_000_000_000n,
proofSize: 150_000n,
},
})
.withdraw_asset()
.buy_execution()
.push_any({
Transact: {
originKind: "Xcm",
requireWeightAtMost: {
refTime: 20_089_165_000n,
proofSize: 80_000n,
},
call: {
encoded: encodedCall,
},
},
})
.as_v4();

await injectHrmpMessage(context, paraId, {
type: "XcmVersionedXcm",
payload: xcmMessage,
} as RawXcmMessage);

const blockRes = await context.createBlock();

return { blockRes };
};
Loading