From a9924c4b2b388ac75e8d36e74d855a4e00892972 Mon Sep 17 00:00:00 2001 From: Agusrodri Date: Mon, 23 Dec 2024 16:44:00 -0800 Subject: [PATCH] add test for receiving a ump message --- solo-chains/runtime/dancelight/src/lib.rs | 12 +++++ .../xcm/test-xcm-send-upward.ts | 54 +++++++++++++------ .../interfaces/augment-api-runtime.ts | 15 +++++- 3 files changed, 65 insertions(+), 16 deletions(-) diff --git a/solo-chains/runtime/dancelight/src/lib.rs b/solo-chains/runtime/dancelight/src/lib.rs index 98b2255b7..cfc000daa 100644 --- a/solo-chains/runtime/dancelight/src/lib.rs +++ b/solo-chains/runtime/dancelight/src/lib.rs @@ -2257,6 +2257,18 @@ sp_api::impl_runtime_apis! { } } + impl xcm_runtime_apis::conversions::LocationToAccountApi for Runtime { + fn convert_location(location: VersionedLocation) -> Result< + AccountId, + xcm_runtime_apis::conversions::Error + > { + xcm_runtime_apis::conversions::LocationToAccountHelper::< + AccountId, + xcm_config::LocationConverter, + >::convert_location(location) + } + } + impl sp_api::Metadata for Runtime { fn metadata() -> OpaqueMetadata { OpaqueMetadata::new(Runtime::metadata().into()) diff --git a/test/suites/dev-tanssi-relay/xcm/test-xcm-send-upward.ts b/test/suites/dev-tanssi-relay/xcm/test-xcm-send-upward.ts index d68f35096..e656ea5ec 100644 --- a/test/suites/dev-tanssi-relay/xcm/test-xcm-send-upward.ts +++ b/test/suites/dev-tanssi-relay/xcm/test-xcm-send-upward.ts @@ -1,5 +1,5 @@ import { beforeAll, customDevRpcRequest, describeSuite, expect } from "@moonwall/cli"; -import { KeyringPair } from "@moonwall/util"; +import { KeyringPair, generateKeyringPair } from "@moonwall/util"; import { ApiPromise, Keyring } from "@polkadot/api"; import { u8aToHex } from "@polkadot/util"; import { jumpToSession } from "util/block"; @@ -12,6 +12,7 @@ describeSuite({ testCases: ({ context, it }) => { let polkadotJs: ApiPromise; let alice: KeyringPair; + let random: KeyringPair; let transferredBalance; beforeAll(async function () { @@ -20,14 +21,43 @@ describeSuite({ name: "Alice default", }); - transferredBalance = 10_000_000_000_000n; + random = generateKeyringPair("sr25519"); + + transferredBalance = 100_000_000_000_000_000n; + + const location = { + V3: { + parents: 0, + interior: { X1: { Parachain: 2000 } }, + }, + }; + + const locationToAccountResult = await polkadotJs.call.locationToAccountApi.convertLocation(location); + expect(locationToAccountResult.isOk); + + const convertedAddress = locationToAccountResult.asOk.toJSON(); + + let aliceNonce = (await polkadotJs.query.system.account(alice.address)).nonce.toNumber(); + + // Send some tokens to the sovereign account of para 2000 + const txSigned = polkadotJs.tx.balances.transferAllowDeath(convertedAddress, transferredBalance); + await context.createBlock(await txSigned.signAsync(alice, { nonce: aliceNonce++ }), { + allowFailures: false, + }); + + const balanceSigned = (await polkadotJs.query.system.account(convertedAddress)).data.free.toBigInt(); + expect(balanceSigned).to.eq(transferredBalance); }); it({ id: "T01", title: "Should succeed receiving tokens", test: async function () { - // XCM message sending reserved assets to alice + const balanceRandomBefore = ( + await polkadotJs.query.system.account(random.address) + ).data.free.toBigInt(); + expect(balanceRandomBefore).to.eq(0n); + const xcmMessage = new XcmFragment({ assets: [ { @@ -35,15 +65,14 @@ describeSuite({ parents: 0, interior: { Here: null }, }, - fungible: transferredBalance, + fungible: transferredBalance / 10n, }, ], - beneficiary: u8aToHex(alice.addressRaw), + beneficiary: u8aToHex(random.addressRaw), }) - .reserve_asset_deposited() - .clear_origin() + .withdraw_asset() .buy_execution() - .deposit_asset() + .deposit_asset_v3() .as_v3(); await customDevRpcRequest("mock_enableParaInherentCandidate", []); @@ -59,13 +88,8 @@ describeSuite({ // Create a block in which the XCM will be executed await context.createBlock(); - // Make sure the state has alice's to DOT tokens - const alice_dot_balance = (await context.polkadotJs().query.foreignAssets.account(1, alice.address)) - .unwrap() - .balance.toBigInt(); - expect(alice_dot_balance > 0n).to.be.true; - // we should expect to have received less than the amount transferred - expect(alice_dot_balance < transferredBalance).to.be.true; + const balanceRandomAfter = (await polkadotJs.query.system.account(random.address)).data.free.toBigInt(); + expect(Number(balanceRandomAfter)).to.be.greaterThan(0); }, }); }, diff --git a/typescript-api/src/dancelight/interfaces/augment-api-runtime.ts b/typescript-api/src/dancelight/interfaces/augment-api-runtime.ts index 460286f05..63f8de4ae 100644 --- a/typescript-api/src/dancelight/interfaces/augment-api-runtime.ts +++ b/typescript-api/src/dancelight/interfaces/augment-api-runtime.ts @@ -74,7 +74,8 @@ import type { RuntimeVersion } from "@polkadot/types/interfaces/state"; import type { ApplyExtrinsicResult } from "@polkadot/types/interfaces/system"; import type { TransactionSource, TransactionValidity } from "@polkadot/types/interfaces/txqueue"; import type { XcmPaymentApiError } from "@polkadot/types/interfaces/xcmPaymentApi"; -import type { XcmVersionedAssetId, XcmVersionedXcm } from "@polkadot/types/lookup"; +import type { Error } from "@polkadot/types/interfaces/xcmRuntimeApi"; +import type { XcmVersionedAssetId, XcmVersionedLocation, XcmVersionedXcm } from "@polkadot/types/lookup"; import type { IExtrinsic, Observable } from "@polkadot/types/types"; export type __AugmentedCall = AugmentedCall; @@ -230,6 +231,18 @@ declare module "@polkadot/api-base/types/calls" { /** Generic call */ [key: string]: DecoratedCallBase; }; + /** 0x9ffb505aa738d69c/1 */ + locationToAccountApi: { + /** Converts `Location` to `AccountId` */ + convertLocation: AugmentedCall< + ApiType, + ( + location: XcmVersionedLocation | { V2: any } | { V3: any } | { V4: any } | string | Uint8Array + ) => Observable> + >; + /** Generic call */ + [key: string]: DecoratedCallBase; + }; /** 0x37e397fc7c91f5e4/2 */ metadata: { /** Returns the metadata of a runtime */