difftreelog
Merge pull request #915 from UniqueNetwork/test/additional-xcm-tests
in: master
Test/additional xcm tests
4 files changed
tests/src/util/playgrounds/unique.dev.tsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// SPDX-License-Identifier: Apache-2.034import {stringToU8a} from '@polkadot/util';5import {encodeAddress, mnemonicGenerate} from '@polkadot/util-crypto';6import {UniqueHelper, MoonbeamHelper, ChainHelperBase, AcalaHelper, RelayHelper, WestmintHelper, AstarHelper} from './unique';7import {ApiPromise, Keyring, WsProvider} from '@polkadot/api';8import * as defs from '../../interfaces/definitions';9import {IKeyringPair} from '@polkadot/types/types';10import {EventRecord} from '@polkadot/types/interfaces';11import {ICrossAccountId, IPovInfo, TSigner} from './types';12import {FrameSystemEventRecord} from '@polkadot/types/lookup';13import {VoidFn} from '@polkadot/api/types';14import {Pallets} from '..';15import {spawnSync} from 'child_process';1617export class SilentLogger {18 log(_msg: any, _level: any): void { }19 level = {20 ERROR: 'ERROR' as const,21 WARNING: 'WARNING' as const,22 INFO: 'INFO' as const,23 };24}2526export class SilentConsole {27 // TODO: Remove, this is temporary: Filter unneeded API output28 // (Jaco promised it will be removed in the next version)29 consoleErr: any;30 consoleLog: any;31 consoleWarn: any;3233 constructor() {34 this.consoleErr = console.error;35 this.consoleLog = console.log;36 this.consoleWarn = console.warn;37 }3839 enable() {40 const outFn = (printer: any) => (...args: any[]) => {41 for (const arg of args) {42 if (typeof arg !== 'string')43 continue;44 if (arg.includes('1000:: Normal connection closure') || arg.includes('Not decorating unknown runtime apis:') || arg.includes('RPC methods not decorated:') || arg === 'Normal connection closure')45 return;46 }47 printer(...args);48 };4950 console.error = outFn(this.consoleErr.bind(console));51 console.log = outFn(this.consoleLog.bind(console));52 console.warn = outFn(this.consoleWarn.bind(console));53 }5455 disable() {56 console.error = this.consoleErr;57 console.log = this.consoleLog;58 console.warn = this.consoleWarn;59 }60}6162export class DevUniqueHelper extends UniqueHelper {63 /**64 * Arrange methods for tests65 */66 arrange: ArrangeGroup;67 wait: WaitGroup;68 admin: AdminGroup;69 session: SessionGroup;70 testUtils: TestUtilGroup;7172 constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {73 options.helperBase = options.helperBase ?? DevUniqueHelper;7475 super(logger, options);76 this.arrange = new ArrangeGroup(this);77 this.wait = new WaitGroup(this);78 this.admin = new AdminGroup(this);79 this.testUtils = new TestUtilGroup(this);80 this.session = new SessionGroup(this);81 }8283 async connect(wsEndpoint: string, _listeners?: any): Promise<void> {84 const wsProvider = new WsProvider(wsEndpoint);85 this.api = new ApiPromise({86 provider: wsProvider,87 signedExtensions: {88 ContractHelpers: {89 extrinsic: {},90 payload: {},91 },92 CheckMaintenance: {93 extrinsic: {},94 payload: {},95 },96 DisableIdentityCalls: {97 extrinsic: {},98 payload: {},99 },100 FakeTransactionFinalizer: {101 extrinsic: {},102 payload: {},103 },104 },105 rpc: {106 unique: defs.unique.rpc,107 appPromotion: defs.appPromotion.rpc,108 povinfo: defs.povinfo.rpc,109 eth: {110 feeHistory: {111 description: 'Dummy',112 params: [],113 type: 'u8',114 },115 maxPriorityFeePerGas: {116 description: 'Dummy',117 params: [],118 type: 'u8',119 },120 },121 },122 });123 await this.api.isReadyOrError;124 this.network = await UniqueHelper.detectNetwork(this.api);125 this.wsEndpoint = wsEndpoint;126 }127}128129export class DevRelayHelper extends RelayHelper {130 wait: WaitGroup;131132 constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {133 options.helperBase = options.helperBase ?? DevRelayHelper;134135 super(logger, options);136 this.wait = new WaitGroup(this);137 }138}139140export class DevWestmintHelper extends WestmintHelper {141 wait: WaitGroup;142143 constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {144 options.helperBase = options.helperBase ?? DevWestmintHelper;145146 super(logger, options);147 this.wait = new WaitGroup(this);148 }149}150151export class DevStatemineHelper extends DevWestmintHelper {}152153export class DevStatemintHelper extends DevWestmintHelper {}154155export class DevMoonbeamHelper extends MoonbeamHelper {156 account: MoonbeamAccountGroup;157 wait: WaitGroup;158159 constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {160 options.helperBase = options.helperBase ?? DevMoonbeamHelper;161 options.notePreimagePallet = options.notePreimagePallet ?? 'preimage';162163 super(logger, options);164 this.account = new MoonbeamAccountGroup(this);165 this.wait = new WaitGroup(this);166 }167}168169export class DevMoonriverHelper extends DevMoonbeamHelper {170 constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {171 options.notePreimagePallet = options.notePreimagePallet ?? 'preimage';172 super(logger, options);173 }174}175176export class DevAstarHelper extends AstarHelper {177 wait: WaitGroup;178179 constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {180 options.helperBase = options.helperBase ?? DevAstarHelper;181182 super(logger, options);183 this.wait = new WaitGroup(this);184 }185}186187export class DevShidenHelper extends AstarHelper {188 wait: WaitGroup;189190 constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {191 options.helperBase = options.helperBase ?? DevShidenHelper;192193 super(logger, options);194 this.wait = new WaitGroup(this);195 }196}197198export class DevAcalaHelper extends AcalaHelper {199 wait: WaitGroup;200201 constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {202 options.helperBase = options.helperBase ?? DevAcalaHelper;203204 super(logger, options);205 this.wait = new WaitGroup(this);206 }207}208209export class DevKaruraHelper extends DevAcalaHelper {}210211export class ArrangeGroup {212 helper: DevUniqueHelper;213214 scheduledIdSlider = 0;215216 constructor(helper: DevUniqueHelper) {217 this.helper = helper;218 }219220 /**221 * Generates accounts with the specified UNQ token balance222 * @param balances balances for generated accounts. Each balance will be multiplied by the token nominal.223 * @param donor donor account for balances224 * @returns array of newly created accounts225 * @example const [acc1, acc2, acc3] = await createAccounts([0n, 10n, 20n], donor);226 */227 createAccounts = async (balances: bigint[], donor: IKeyringPair): Promise<IKeyringPair[]> => {228 let nonce = await this.helper.chain.getNonce(donor.address);229 const wait = new WaitGroup(this.helper);230 const ss58Format = this.helper.chain.getChainProperties().ss58Format;231 const tokenNominal = this.helper.balance.getOneTokenNominal();232 const transactions = [];233 const accounts: IKeyringPair[] = [];234 for (const balance of balances) {235 const recipient = this.helper.util.fromSeed(mnemonicGenerate(), ss58Format);236 accounts.push(recipient);237 if (balance !== 0n) {238 const tx = this.helper.constructApiCall('api.tx.balances.transfer', [{Id: recipient.address}, balance * tokenNominal]);239 transactions.push(this.helper.signTransaction(donor, tx, {nonce, era: 0}, 'account generation'));240 nonce++;241 }242 }243244 await Promise.all(transactions).catch(_e => {});245246 //#region TODO remove this region, when nonce problem will be solved247 const checkBalances = async () => {248 let isSuccess = true;249 for (let i = 0; i < balances.length; i++) {250 const balance = await this.helper.balance.getSubstrate(accounts[i].address);251 if (balance !== balances[i] * tokenNominal) {252 isSuccess = false;253 break;254 }255 }256 return isSuccess;257 };258259 let accountsCreated = false;260 const maxBlocksChecked = await this.helper.arrange.isDevNode() ? 50 : 5;261 // checkBalances retry up to 5-50 blocks262 for (let index = 0; index < maxBlocksChecked; index++) {263 accountsCreated = await checkBalances();264 if(accountsCreated) break;265 await wait.newBlocks(1);266 }267268 if (!accountsCreated) throw Error('Accounts generation failed');269 //#endregion270271 return accounts;272 };273274 // TODO combine this method and createAccounts into one275 createCrowd = async (accountsToCreate: number, withBalance: bigint, donor: IKeyringPair): Promise<IKeyringPair[]> => {276 const createAsManyAsCan = async () => {277 let transactions: any = [];278 const accounts: IKeyringPair[] = [];279 let nonce = await this.helper.chain.getNonce(donor.address);280 const tokenNominal = this.helper.balance.getOneTokenNominal();281 const ss58Format = this.helper.chain.getChainProperties().ss58Format;282 for (let i = 0; i < accountsToCreate; i++) {283 if (i === 500) { // if there are too many accounts to create284 await Promise.allSettled(transactions); // wait while first 500 (should be 100 for devnode) tx will be settled285 transactions = []; //286 nonce = await this.helper.chain.getNonce(donor.address); // update nonce287 }288 const recipient = this.helper.util.fromSeed(mnemonicGenerate(), ss58Format);289 accounts.push(recipient);290 if (withBalance !== 0n) {291 const tx = this.helper.constructApiCall('api.tx.balances.transfer', [{Id: recipient.address}, withBalance * tokenNominal]);292 transactions.push(this.helper.signTransaction(donor, tx, {nonce}, 'account generation'));293 nonce++;294 }295 }296297 const fullfilledAccounts = [];298 await Promise.allSettled(transactions);299 for (const account of accounts) {300 const accountBalance = await this.helper.balance.getSubstrate(account.address);301 if (accountBalance === withBalance * tokenNominal) {302 fullfilledAccounts.push(account);303 }304 }305 return fullfilledAccounts;306 };307308309 const crowd: IKeyringPair[] = [];310 // do up to 5 retries311 for (let index = 0; index < 5 && accountsToCreate !== 0; index++) {312 const asManyAsCan = await createAsManyAsCan();313 crowd.push(...asManyAsCan);314 accountsToCreate -= asManyAsCan.length;315 }316317 if (accountsToCreate !== 0) throw Error(`Crowd generation failed: ${accountsToCreate} accounts left`);318319 return crowd;320 };321322 isDevNode = async () => {323 let blockNumber = (await this.helper.callRpc('api.query.system.number')).toJSON();324 if (blockNumber == 0) {325 await this.helper.wait.newBlocks(1);326 blockNumber = (await this.helper.callRpc('api.query.system.number')).toJSON();327 }328 const block2 = await this.helper.callRpc('api.rpc.chain.getBlock', [await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber])]);329 const block1 = await this.helper.callRpc('api.rpc.chain.getBlock', [await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber - 1])]);330 const findCreationDate = (block: any) => {331 const humanBlock = block.toHuman();332 let date;333 humanBlock.block.extrinsics.forEach((ext: any) => {334 if(ext.method.section === 'timestamp') {335 date = Number(ext.method.args.now.replaceAll(',', ''));336 }337 });338 return date;339 };340 const block1date = await findCreationDate(block1);341 const block2date = await findCreationDate(block2);342 if(block2date! - block1date! < 9000) return true;343 };344345 async calculcateFee(payer: ICrossAccountId, promise: () => Promise<any>): Promise<bigint> {346 const address = payer.Substrate ? payer.Substrate : this.helper.address.ethToSubstrate(payer.Ethereum!);347 let balance = await this.helper.balance.getSubstrate(address);348349 await promise();350351 balance -= await this.helper.balance.getSubstrate(address);352353 return balance;354 }355356 async calculatePoVInfo(txs: any[]): Promise<IPovInfo> {357 const rawPovInfo = await this.helper.callRpc('api.rpc.povinfo.estimateExtrinsicPoV', [txs]);358359 const kvJson: {[key: string]: string} = {};360361 for (const kv of rawPovInfo.keyValues) {362 kvJson[kv.key.toHex()] = kv.value.toHex();363 }364365 const kvStr = JSON.stringify(kvJson);366367 const chainql = spawnSync(368 'chainql',369 [370 `--tla-code=data=${kvStr}`,371 '-e', `function(data) cql.dump(cql.chain("${this.helper.getEndpoint()}").latest._meta, data, {omit_empty:true})`,372 ],373 );374375 if (!chainql.stdout) {376 throw Error('unable to get an output from the `chainql`');377 }378379 return {380 proofSize: rawPovInfo.proofSize.toNumber(),381 compactProofSize: rawPovInfo.compactProofSize.toNumber(),382 compressedProofSize: rawPovInfo.compressedProofSize.toNumber(),383 results: rawPovInfo.results,384 kv: JSON.parse(chainql.stdout.toString()),385 };386 }387388 calculatePalletAddress(palletId: any) {389 const address = stringToU8a(('modl' + palletId).padEnd(32, '\0'));390 return encodeAddress(address, this.helper.chain.getChainProperties().ss58Format);391 }392393 makeScheduledIds(num: number): string[] {394 function makeId(slider: number) {395 const scheduledIdSize = 64;396 const hexId = slider.toString(16);397 const prefixSize = scheduledIdSize - hexId.length;398399 const scheduledId = '0x' + '0'.repeat(prefixSize) + hexId;400401 return scheduledId;402 }403404 const ids = [];405 for (let i = 0; i < num; i++) {406 ids.push(makeId(this.scheduledIdSlider));407 this.scheduledIdSlider += 1;408 }409410 return ids;411 }412413 makeScheduledId(): string {414 return (this.makeScheduledIds(1))[0];415 }416417 async captureEvents(eventSection: string, eventMethod: string): Promise<EventCapture> {418 const capture = new EventCapture(this.helper, eventSection, eventMethod);419 await capture.startCapture();420421 return capture;422 }423}424425class MoonbeamAccountGroup {426 helper: MoonbeamHelper;427428 keyring: Keyring;429 _alithAccount: IKeyringPair;430 _baltatharAccount: IKeyringPair;431 _dorothyAccount: IKeyringPair;432433 constructor(helper: MoonbeamHelper) {434 this.helper = helper;435436 this.keyring = new Keyring({type: 'ethereum'});437 const alithPrivateKey = '0x5fb92d6e98884f76de468fa3f6278f8807c48bebc13595d45af5bdc4da702133';438 const baltatharPrivateKey = '0x8075991ce870b93a8870eca0c0f91913d12f47948ca0fd25b49c6fa7cdbeee8b';439 const dorothyPrivateKey = '0x39539ab1876910bbf3a223d84a29e28f1cb4e2e456503e7e91ed39b2e7223d68';440441 this._alithAccount = this.keyring.addFromUri(alithPrivateKey, undefined, 'ethereum');442 this._baltatharAccount = this.keyring.addFromUri(baltatharPrivateKey, undefined, 'ethereum');443 this._dorothyAccount = this.keyring.addFromUri(dorothyPrivateKey, undefined, 'ethereum');444 }445446 alithAccount() {447 return this._alithAccount;448 }449450 baltatharAccount() {451 return this._baltatharAccount;452 }453454 dorothyAccount() {455 return this._dorothyAccount;456 }457458 create() {459 return this.keyring.addFromUri(mnemonicGenerate());460 }461}462463class WaitGroup {464 helper: ChainHelperBase;465466 constructor(helper: ChainHelperBase) {467 this.helper = helper;468 }469470 sleep(milliseconds: number) {471 return new Promise((resolve) => setTimeout(resolve, milliseconds));472 }473474 private async waitWithTimeout(promise: Promise<any>, timeout: number) {475 let isBlock = false;476 promise.then(() => isBlock = true).catch(() => isBlock = true);477 let totalTime = 0;478 const step = 100;479 while(!isBlock) {480 await this.sleep(step);481 totalTime += step;482 if(totalTime >= timeout) throw Error('Blocks production failed');483 }484 return promise;485 }486487 /**488 * Launch some async operation, or throw an error after some time. Note that it will still continue executing after the timeout.489 * @param promise async operation to race against the timeout490 * @param timeoutMS time after which to time out491 * @param timeoutError error message to throw492 * @returns promise of the same type the operation had493 */494 withTimeout<T>(495 promise: Promise<T>,496 timeoutMS = 30000,497 timeoutError = 'The operation has timed out!',498 ): Promise<T> {499 const timeout = new Promise<never>((_, reject) => {500 setTimeout(() => {501 reject(new Error(timeoutError));502 }, timeoutMS);503 });504505 return Promise.race<T>([promise, timeout]).catch(e => {throw new Error(e);});506 }507508 /**509 * Wait for specified number of blocks510 * @param blocksCount number of blocks to wait511 * @returns512 */513 async newBlocks(blocksCount = 1, timeout?: number): Promise<void> {514 timeout = timeout ?? blocksCount * 60_000;515 // eslint-disable-next-line no-async-promise-executor516 const promise = new Promise<void>(async (resolve) => {517 const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(() => {518 if (blocksCount > 0) {519 blocksCount--;520 } else {521 unsubscribe();522 resolve();523 }524 });525 });526 await this.waitWithTimeout(promise, timeout);527 return promise;528 }529530 /**531 * Wait for the specified number of sessions to pass.532 * Only applicable if the Session pallet is turned on.533 * @param sessionCount number of sessions to wait534 * @param blockTimeout time in ms until panicking that the chain has stopped producing blocks535 * @returns536 */537 async newSessions(sessionCount = 1, blockTimeout = 60000): Promise<void> {538 console.log(`Waiting for ${sessionCount} new session${sessionCount>1's'''}.`539 + ' This might take a while -- check SessionPeriod in pallet_session::Config for session time.');540541 const expectedSessionIndex = await (this.helper as DevUniqueHelper).session.getIndex() + sessionCount;542 let currentSessionIndex = -1;543544 while (currentSessionIndex < expectedSessionIndex) {545 // eslint-disable-next-line no-async-promise-executor546 currentSessionIndex = await this.withTimeout(new Promise(async (resolve) => {547 await this.newBlocks(1);548 const res = await (this.helper as DevUniqueHelper).session.getIndex();549 resolve(res);550 }), blockTimeout, 'The chain has stopped producing blocks!');551 }552 }553554 async forParachainBlockNumber(blockNumber: bigint | number, timeout?: number) {555 timeout = timeout ?? 30 * 60 * 1000;556 // eslint-disable-next-line no-async-promise-executor557 const promise = new Promise<void>(async (resolve) => {558 const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads((data: any) => {559 if (data.number.toNumber() >= blockNumber) {560 unsubscribe();561 resolve();562 }563 });564 });565 await this.waitWithTimeout(promise, timeout);566 return promise;567 }568569 async forRelayBlockNumber(blockNumber: bigint | number, timeout?: number) {570 timeout = timeout ?? 30 * 60 * 1000;571 // eslint-disable-next-line no-async-promise-executor572 const promise = new Promise<void>(async (resolve) => {573 const unsubscribe = await this.helper.getApi().query.parachainSystem.validationData((data: any) => {574 if (data.value.relayParentNumber.toNumber() >= blockNumber) {575 // @ts-ignore576 unsubscribe();577 resolve();578 }579 });580 });581 await this.waitWithTimeout(promise, timeout);582 return promise;583 }584585 noScheduledTasks() {586 const api = this.helper.getApi();587588 // eslint-disable-next-line no-async-promise-executor589 const promise = new Promise<void>(async resolve => {590 const unsubscribe = await api.rpc.chain.subscribeNewHeads(async () => {591 const areThereScheduledTasks = await api.query.scheduler.lookup.entries();592593 if(areThereScheduledTasks.length == 0) {594 unsubscribe();595 resolve();596 }597 });598 });599600 return promise;601 }602603 event(maxBlocksToWait: number, eventSection: string, eventMethod: string) {604 // eslint-disable-next-line no-async-promise-executor605 const promise = new Promise<EventRecord | null>(async (resolve) => {606 const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(async header => {607 const blockNumber = header.number.toHuman();608 const blockHash = header.hash;609 const eventIdStr = `${eventSection}.${eventMethod}`;610 const waitLimitStr = `wait blocks remaining: ${maxBlocksToWait}`;611612 this.helper.logger.log(`[Block #${blockNumber}] Waiting for event \`${eventIdStr}\` (${waitLimitStr})`);613614 const apiAt = await this.helper.getApi().at(blockHash);615 const eventRecords = (await apiAt.query.system.events()) as any;616617 const neededEvent = eventRecords.toArray().find((r: FrameSystemEventRecord) => {618 return r.event.section == eventSection && r.event.method == eventMethod;619 });620621 if (neededEvent) {622 unsubscribe();623 resolve(neededEvent);624 } else if (maxBlocksToWait > 0) {625 maxBlocksToWait--;626 } else {627 this.helper.logger.log(`Event \`${eventIdStr}\` is NOT found`);628 unsubscribe();629 resolve(null);630 }631 });632 });633 return promise;634 }635}636637class SessionGroup {638 helper: ChainHelperBase;639640 constructor(helper: ChainHelperBase) {641 this.helper = helper;642 }643644 //todo:collator documentation645 async getIndex(): Promise<number> {646 return (await this.helper.callRpc('api.query.session.currentIndex')).toNumber();647 }648649 newSessions(sessionCount = 1, blockTimeout = 24000): Promise<void> {650 return (this.helper as DevUniqueHelper).wait.newSessions(sessionCount, blockTimeout);651 }652653 setOwnKeys(signer: TSigner, key: string) {654 return this.helper.executeExtrinsic(655 signer,656 'api.tx.session.setKeys',657 [key, '0x0'],658 true,659 );660 }661662 setOwnKeysFromAddress(signer: TSigner) {663 return this.setOwnKeys(signer, '0x' + Buffer.from(signer.addressRaw).toString('hex'));664 }665}666667class TestUtilGroup {668 helper: DevUniqueHelper;669670 constructor(helper: DevUniqueHelper) {671 this.helper = helper;672 }673674 async enable() {675 if (this.helper.fetchMissingPalletNames([Pallets.TestUtils]).length != 0) {676 return;677 }678679 const signer = this.helper.util.fromSeed('//Alice');680 await this.helper.getSudo<DevUniqueHelper>().executeExtrinsic(signer, 'api.tx.testUtils.enable', [], true);681 }682683 async setTestValue(signer: TSigner, testVal: number) {684 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.setTestValue', [testVal], true);685 }686687 async incTestValue(signer: TSigner) {688 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.incTestValue', [], true);689 }690691 async setTestValueAndRollback(signer: TSigner, testVal: number) {692 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.setTestValueAndRollback', [testVal], true);693 }694695 async testValue(blockIdx?: number) {696 const api = blockIdx697 ? await this.helper.getApi().at(await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockIdx]))698 : this.helper.getApi();699700 return (await api.query.testUtils.testValue()).toJSON();701 }702703 async justTakeFee(signer: TSigner) {704 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.justTakeFee', [], true);705 }706707 async selfCancelingInc(signer: TSigner, scheduledId: string, maxTestVal: number) {708 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.selfCancelingInc', [scheduledId, maxTestVal], true);709 }710}711712class EventCapture {713 helper: DevUniqueHelper;714 eventSection: string;715 eventMethod: string;716 events: EventRecord[] = [];717 unsubscribe: VoidFn | null = null;718719 constructor(720 helper: DevUniqueHelper,721 eventSection: string,722 eventMethod: string,723 ) {724 this.helper = helper;725 this.eventSection = eventSection;726 this.eventMethod = eventMethod;727 }728729 async startCapture() {730 this.stopCapture();731 this.unsubscribe = (await this.helper.getApi().query.system.events((eventRecords: FrameSystemEventRecord[]) => {732 const newEvents = eventRecords.filter(r => {733 return r.event.section == this.eventSection && r.event.method == this.eventMethod;734 });735736 this.events.push(...newEvents);737 })) as any;738 }739740 stopCapture() {741 if (this.unsubscribe !== null) {742 this.unsubscribe();743 }744 }745746 extractCapturedEvents() {747 return this.events;748 }749}750751class AdminGroup {752 helper: UniqueHelper;753754 constructor(helper: UniqueHelper) {755 this.helper = helper;756 }757758 async payoutStakers(signer: IKeyringPair, stakersToPayout: number): Promise<{staker: string, stake: bigint, payout: bigint}[]> {759 const payoutResult = await this.helper.executeExtrinsic(signer, 'api.tx.appPromotion.payoutStakers', [stakersToPayout], true);760 return payoutResult.result.events.filter(e => e.event.method === 'StakingRecalculation').map(e => {761 return {762 staker: e.event.data[0].toString(),763 stake: e.event.data[1].toBigInt(),764 payout: e.event.data[2].toBigInt(),765 };766 });767 }768}tests/src/util/playgrounds/unique.tsdiffbeforeafterboth--- a/tests/src/util/playgrounds/unique.ts
+++ b/tests/src/util/playgrounds/unique.ts
@@ -2446,6 +2446,10 @@
return this.ethBalanceGroup.getEthereum(address);
}
+ async setBalanceSubstrate(signer: TSigner, address: TSubstrateAccount, amount: bigint, reservedAmount = 0n) {
+ await this.helper.executeExtrinsic(signer, 'api.tx.balances.setBalance', [address, amount, reservedAmount], true);
+ }
+
/**
* Transfer tokens to substrate address
* @param signer keyring of signer
@@ -3017,6 +3021,18 @@
await this.teleportAssets(signer, destination, beneficiary, assets, feeAssetItem);
}
+
+ async send(signer: IKeyringPair, destination: any, message: any) {
+ await this.helper.executeExtrinsic(
+ signer,
+ `api.tx.${this.palletName}.send`,
+ [
+ destination,
+ message,
+ ],
+ true,
+ );
+ }
}
class XTokensGroup<T extends ChainHelperBase> extends HelperGroup<T> {
@@ -3280,6 +3296,7 @@
assetRegistry: AcalaAssetRegistryGroup;
xTokens: XTokensGroup<AcalaHelper>;
tokens: TokensGroup<AcalaHelper>;
+ xcm: XcmGroup<AcalaHelper>;
constructor(logger?: ILogger, options: {[key: string]: any} = {}) {
super(logger, options.helperBase ?? AcalaHelper);
@@ -3288,6 +3305,7 @@
this.assetRegistry = new AcalaAssetRegistryGroup(this);
this.xTokens = new XTokensGroup(this);
this.tokens = new TokensGroup(this);
+ this.xcm = new XcmGroup(this, 'polkadotXcm');
}
getSudo<T extends AcalaHelper>() {
tests/src/xcm/xcmQuartz.test.tsdiffbeforeafterboth--- a/tests/src/xcm/xcmQuartz.test.ts
+++ b/tests/src/xcm/xcmQuartz.test.ts
@@ -19,6 +19,7 @@
import config from '../config';
import {XcmV2TraitsError} from '../interfaces';
import {itSub, expect, describeXCM, usingPlaygrounds, usingKaruraPlaygrounds, usingRelayPlaygrounds, usingMoonriverPlaygrounds, usingStateminePlaygrounds, usingShidenPlaygrounds} from '../util';
+import {DevUniqueHelper} from '../util/playgrounds/unique.dev';
const QUARTZ_CHAIN = 2095;
const STATEMINE_CHAIN = 1000;
@@ -641,63 +642,277 @@
console.log('[Karura -> Quartz] transaction fees on Quartz: %s QTZ', helper.util.bigIntToDecimals(qtzFees));
expect(qtzFees == 0n).to.be.true;
});
+
+ itSub('Karura can send only up to its balance', async ({helper}) => {
+ // set Karura's sovereign account's balance
+ const karuraBalance = 10000n * (10n ** QTZ_DECIMALS);
+ const karuraSovereignAccount = helper.address.paraSiblingSovereignAccount(KARURA_CHAIN);
+ await helper.getSudo().balance.setBalanceSubstrate(alice, karuraSovereignAccount, karuraBalance);
+
+ const moreThanKaruraHas = karuraBalance * 2n;
+
+ let targetAccountBalance = 0n;
+ const [targetAccount] = await helper.arrange.createAccounts([targetAccountBalance], alice);
+
+ const quartzMultilocation = {
+ V1: {
+ parents: 1,
+ interior: {
+ X1: {Parachain: QUARTZ_CHAIN},
+ },
+ },
+ };
+
+ const maliciousXcmProgram = helper.arrange.makeXcmProgramWithdrawDeposit(
+ targetAccount.addressRaw,
+ {
+ Concrete: {
+ parents: 0,
+ interior: 'Here',
+ },
+ },
+ moreThanKaruraHas,
+ );
+
+ // Try to trick Quartz
+ await usingKaruraPlaygrounds(karuraUrl, async (helper) => {
+ await helper.getSudo().xcm.send(alice, quartzMultilocation, maliciousXcmProgram);
+ });
+
+ const maxWaitBlocks = 3;
+
+ const xcmpQueueFailEvent = await helper.wait.eventOutcome<XcmV2TraitsError>(
+ maxWaitBlocks,
+ 'xcmpQueue',
+ 'Fail',
+ );
+
+ expect(
+ xcmpQueueFailEvent != null,
+ '\'xcmpQueue.FailEvent\' event is expected',
+ ).to.be.true;
+
+ expect(
+ xcmpQueueFailEvent!.isFailedToTransactAsset,
+ 'The XCM error should be \'FailedToTransactAsset\'',
+ ).to.be.true;
+
+ targetAccountBalance = await helper.balance.getSubstrate(targetAccount.address);
+ expect(targetAccountBalance).to.be.equal(0n);
+
+ // But Karura still can send the correct amount
+ const validTransferAmount = karuraBalance / 2n;
+ const validXcmProgram = helper.arrange.makeXcmProgramWithdrawDeposit(
+ targetAccount.addressRaw,
+ {
+ Concrete: {
+ parents: 0,
+ interior: 'Here',
+ },
+ },
+ validTransferAmount,
+ );
+
+ await usingKaruraPlaygrounds(karuraUrl, async (helper) => {
+ await helper.getSudo().xcm.send(alice, quartzMultilocation, validXcmProgram);
+ });
+
+ await helper.wait.newBlocks(maxWaitBlocks);
+
+ targetAccountBalance = await helper.balance.getSubstrate(targetAccount.address);
+ expect(targetAccountBalance).to.be.equal(validTransferAmount);
+ });
+
+ itSub('Should not accept reserve transfer of QTZ from Karura', async ({helper}) => {
+ const testAmount = 10_000n * (10n ** QTZ_DECIMALS);
+ const [targetAccount] = await helper.arrange.createAccounts([0n], alice);
+
+ const quartzMultilocation = {
+ V1: {
+ parents: 1,
+ interior: {
+ X1: {
+ Parachain: QUARTZ_CHAIN,
+ },
+ },
+ },
+ };
+
+ const maliciousXcmProgram = helper.arrange.makeXcmProgramReserveAssetDeposited(
+ targetAccount.addressRaw,
+ {
+ Concrete: {
+ parents: 1,
+ interior: {
+ X1: {
+ Parachain: QUARTZ_CHAIN,
+ },
+ },
+ },
+ },
+ testAmount,
+ );
+
+ await usingKaruraPlaygrounds(karuraUrl, async (helper) => {
+ await helper.getSudo().xcm.send(alice, quartzMultilocation, maliciousXcmProgram);
+ });
+
+ const maxWaitBlocks = 3;
+
+ const xcmpQueueFailEvent = await helper.wait.eventOutcome<XcmV2TraitsError>(
+ maxWaitBlocks,
+ 'xcmpQueue',
+ 'Fail',
+ );
+
+ expect(
+ xcmpQueueFailEvent != null,
+ '\'xcmpQueue.FailEvent\' event is expected',
+ ).to.be.true;
+
+ expect(
+ xcmpQueueFailEvent!.isUntrustedReserveLocation,
+ 'The XCM error should be \'isUntrustedReserveLocation\'',
+ ).to.be.true;
+
+ const accountBalance = await helper.balance.getSubstrate(targetAccount.address);
+ expect(accountBalance).to.be.equal(0n);
+ });
});
-// These tests are relevant only when the foreign asset pallet is disabled
+// These tests are relevant only when
+// the the corresponding foreign assets are not registered
describeXCM('[XCM] Integration test: Quartz rejects non-native tokens', () => {
let alice: IKeyringPair;
+ let alith: IKeyringPair;
+
+ const testAmount = 100_000_000_000n;
+ let quartzParachainJunction;
+ let quartzAccountJunction;
+ let quartzParachainMultilocation: any;
+ let quartzAccountMultilocation: any;
+ let quartzCombinedMultilocation: any;
+
before(async () => {
await usingPlaygrounds(async (helper, privateKey) => {
alice = await privateKey('//Alice');
- // Set the default version to wrap the first message to other chains.
- await helper.getSudo().xcm.setSafeXcmVersion(alice, SAFE_XCM_VERSION);
- });
- });
+ quartzParachainJunction = {Parachain: QUARTZ_CHAIN};
+ quartzAccountJunction = {
+ AccountId32: {
+ network: 'Any',
+ id: alice.addressRaw,
+ },
+ };
- itSub('Quartz rejects KAR tokens from Karura', async ({helper}) => {
- await usingKaruraPlaygrounds(karuraUrl, async (helper) => {
- const destination = {
+ quartzParachainMultilocation = {
V1: {
parents: 1,
interior: {
- X2: [
- {Parachain: QUARTZ_CHAIN},
- {
- AccountId32: {
- network: 'Any',
- id: alice.addressRaw,
- },
- },
- ],
+ X1: quartzParachainJunction,
+ },
+ },
+ };
+
+ quartzAccountMultilocation = {
+ V1: {
+ parents: 0,
+ interior: {
+ X1: quartzAccountJunction,
},
},
};
- const id = {
- Token: 'KAR',
+ quartzCombinedMultilocation = {
+ V1: {
+ parents: 1,
+ interior: {
+ X2: [quartzParachainJunction, quartzAccountJunction],
+ },
+ },
};
- await helper.xTokens.transfer(alice, id, 100_000_000_000n, destination, 'Unlimited');
+ // Set the default version to wrap the first message to other chains.
+ await helper.getSudo().xcm.setSafeXcmVersion(alice, SAFE_XCM_VERSION);
+ });
+
+ // eslint-disable-next-line require-await
+ await usingMoonriverPlaygrounds(moonriverUrl, async (helper) => {
+ alith = helper.account.alithAccount();
});
+ });
+ const expectFailedToTransact = async (network: string, helper: DevUniqueHelper) => {
const maxWaitBlocks = 3;
- const xcmpQueueFailEvent = await helper.wait.event(maxWaitBlocks, 'xcmpQueue', 'Fail');
+ const xcmpQueueFailEvent = await helper.wait.eventOutcome<XcmV2TraitsError>(
+ maxWaitBlocks,
+ 'xcmpQueue',
+ 'Fail',
+ );
expect(
xcmpQueueFailEvent != null,
- '[Karura] xcmpQueue.FailEvent event is expected',
+ `[reject ${network} tokens] 'xcmpQueue.FailEvent' event is expected`,
).to.be.true;
- const event = xcmpQueueFailEvent!.event;
- const outcome = event.data[1] as XcmV2TraitsError;
-
expect(
- outcome.isFailedToTransactAsset,
- '[Karura] The XCM error should be `FailedToTransactAsset`',
+ xcmpQueueFailEvent!.isFailedToTransactAsset,
+ `[reject ${network} tokens] The XCM error should be 'FailedToTransactAsset'`,
).to.be.true;
+ };
+
+ itSub('Quartz rejects KAR tokens from Karura', async ({helper}) => {
+ await usingKaruraPlaygrounds(karuraUrl, async (helper) => {
+ const id = {
+ Token: 'KAR',
+ };
+ const destination = quartzCombinedMultilocation;
+ await helper.xTokens.transfer(alice, id, testAmount, destination, 'Unlimited');
+ });
+
+ await expectFailedToTransact('KAR', helper);
+ });
+
+ itSub('Quartz rejects MOVR tokens from Moonriver', async ({helper}) => {
+ await usingMoonriverPlaygrounds(moonriverUrl, async (helper) => {
+ const id = 'SelfReserve';
+ const destination = quartzCombinedMultilocation;
+ await helper.xTokens.transfer(alith, id, testAmount, destination, 'Unlimited');
+ });
+
+ await expectFailedToTransact('MOVR', helper);
+ });
+
+ itSub('Quartz rejects SDN tokens from Shiden', async ({helper}) => {
+ await usingShidenPlaygrounds(shidenUrl, async (helper) => {
+ const destinationParachain = quartzParachainMultilocation;
+ const beneficiary = quartzAccountMultilocation;
+ const assets = {
+ V1: [{
+ id: {
+ Concrete: {
+ parents: 0,
+ interior: 'Here',
+ },
+ },
+ fun: {
+ Fungible: testAmount,
+ },
+ }],
+ };
+ const feeAssetItem = 0;
+
+ await helper.executeExtrinsic(alice, 'api.tx.polkadotXcm.reserveWithdrawAssets', [
+ destinationParachain,
+ beneficiary,
+ assets,
+ feeAssetItem,
+ ]);
+ });
+
+ await expectFailedToTransact('SDN', helper);
});
});
@@ -981,6 +1196,16 @@
console.log('[Moonriver -> Quartz] transaction fees on Quartz: %s QTZ', helper.util.bigIntToDecimals(qtzFees));
expect(qtzFees == 0n).to.be.true;
});
+
+ // eslint-disable-next-line require-await
+ itSub.skip('Moonriver can send only up to its balance', async ({helper}) => {
+ throw Error('Not yet implemented');
+ });
+
+ // eslint-disable-next-line require-await
+ itSub.skip('Should not accept reserve transfer of QTZ from Moonriver', async ({helper}) => {
+ throw Error('Not yet implemented');
+ });
});
describeXCM('[XCM] Integration test: Exchanging tokens with Shiden', () => {
@@ -1193,4 +1418,140 @@
console.log(`QTZ Balance on Quartz after XCM is: ${balanceQTZ}`);
expect(balanceQTZ).to.eq(balanceAfterQuartzToShidenXCM + qtzFromShidenTransfered);
});
+
+ itSub('Shiden can send only up to its balance', async ({helper}) => {
+ // set Shiden's sovereign account's balance
+ const shidenBalance = 10000n * (10n ** QTZ_DECIMALS);
+ const shidenSovereignAccount = helper.address.paraSiblingSovereignAccount(SHIDEN_CHAIN);
+ await helper.getSudo().balance.setBalanceSubstrate(alice, shidenSovereignAccount, shidenBalance);
+
+ const moreThanShidenHas = shidenBalance * 2n;
+
+ let targetAccountBalance = 0n;
+ const [targetAccount] = await helper.arrange.createAccounts([targetAccountBalance], alice);
+
+ const quartzMultilocation = {
+ V1: {
+ parents: 1,
+ interior: {
+ X1: {Parachain: QUARTZ_CHAIN},
+ },
+ },
+ };
+
+ const maliciousXcmProgram = helper.arrange.makeXcmProgramWithdrawDeposit(
+ targetAccount.addressRaw,
+ {
+ Concrete: {
+ parents: 0,
+ interior: 'Here',
+ },
+ },
+ moreThanShidenHas,
+ );
+
+ // Try to trick Quartz
+ await usingShidenPlaygrounds(shidenUrl, async (helper) => {
+ await helper.getSudo().xcm.send(alice, quartzMultilocation, maliciousXcmProgram);
+ });
+
+ const maxWaitBlocks = 3;
+
+ const xcmpQueueFailEvent = await helper.wait.eventOutcome<XcmV2TraitsError>(
+ maxWaitBlocks,
+ 'xcmpQueue',
+ 'Fail',
+ );
+
+ expect(
+ xcmpQueueFailEvent != null,
+ '\'xcmpQueue.FailEvent\' event is expected',
+ ).to.be.true;
+
+ expect(
+ xcmpQueueFailEvent!.isFailedToTransactAsset,
+ 'The XCM error should be \'FailedToTransactAsset\'',
+ ).to.be.true;
+
+ targetAccountBalance = await helper.balance.getSubstrate(targetAccount.address);
+ expect(targetAccountBalance).to.be.equal(0n);
+
+ // But Shiden still can send the correct amount
+ const validTransferAmount = shidenBalance / 2n;
+ const validXcmProgram = helper.arrange.makeXcmProgramWithdrawDeposit(
+ targetAccount.addressRaw,
+ {
+ Concrete: {
+ parents: 0,
+ interior: 'Here',
+ },
+ },
+ validTransferAmount,
+ );
+
+ await usingShidenPlaygrounds(shidenUrl, async (helper) => {
+ await helper.getSudo().xcm.send(alice, quartzMultilocation, validXcmProgram);
+ });
+
+ await helper.wait.newBlocks(maxWaitBlocks);
+
+ targetAccountBalance = await helper.balance.getSubstrate(targetAccount.address);
+ expect(targetAccountBalance).to.be.equal(validTransferAmount);
+ });
+
+ itSub('Should not accept reserve transfer of QTZ from Shiden', async ({helper}) => {
+ const testAmount = 10_000n * (10n ** QTZ_DECIMALS);
+ const [targetAccount] = await helper.arrange.createAccounts([0n], alice);
+
+ const quartzMultilocation = {
+ V1: {
+ parents: 1,
+ interior: {
+ X1: {
+ Parachain: QUARTZ_CHAIN,
+ },
+ },
+ },
+ };
+
+ const maliciousXcmProgram = helper.arrange.makeXcmProgramReserveAssetDeposited(
+ targetAccount.addressRaw,
+ {
+ Concrete: {
+ parents: 1,
+ interior: {
+ X1: {
+ Parachain: QUARTZ_CHAIN,
+ },
+ },
+ },
+ },
+ testAmount,
+ );
+
+ await usingShidenPlaygrounds(shidenUrl, async (helper) => {
+ await helper.getSudo().xcm.send(alice, quartzMultilocation, maliciousXcmProgram);
+ });
+
+ const maxWaitBlocks = 3;
+
+ const xcmpQueueFailEvent = await helper.wait.eventOutcome<XcmV2TraitsError>(
+ maxWaitBlocks,
+ 'xcmpQueue',
+ 'Fail',
+ );
+
+ expect(
+ xcmpQueueFailEvent != null,
+ '\'xcmpQueue.FailEvent\' event is expected',
+ ).to.be.true;
+
+ expect(
+ xcmpQueueFailEvent!.isUntrustedReserveLocation,
+ 'The XCM error should be \'isUntrustedReserveLocation\'',
+ ).to.be.true;
+
+ const accountBalance = await helper.balance.getSubstrate(targetAccount.address);
+ expect(accountBalance).to.be.equal(0n);
+ });
});
tests/src/xcm/xcmUnique.test.tsdiffbeforeafterboth--- a/tests/src/xcm/xcmUnique.test.ts
+++ b/tests/src/xcm/xcmUnique.test.ts
@@ -19,6 +19,7 @@
import config from '../config';
import {XcmV2TraitsError} from '../interfaces';
import {itSub, expect, describeXCM, usingPlaygrounds, usingAcalaPlaygrounds, usingRelayPlaygrounds, usingMoonbeamPlaygrounds, usingStatemintPlaygrounds, usingAstarPlaygrounds} from '../util';
+import {DevUniqueHelper} from '../util/playgrounds/unique.dev';
const UNIQUE_CHAIN = 2037;
const STATEMINT_CHAIN = 1000;
@@ -643,63 +644,277 @@
console.log('[Acala -> Unique] transaction fees on Unique: %s UNQ', helper.util.bigIntToDecimals(unqFees));
expect(unqFees == 0n).to.be.true;
});
+
+ itSub('Acala can send only up to its balance', async ({helper}) => {
+ // set Acala's sovereign account's balance
+ const acalaBalance = 10000n * (10n ** UNQ_DECIMALS);
+ const acalaSovereignAccount = helper.address.paraSiblingSovereignAccount(ACALA_CHAIN);
+ await helper.getSudo().balance.setBalanceSubstrate(alice, acalaSovereignAccount, acalaBalance);
+
+ const moreThanAcalaHas = acalaBalance * 2n;
+
+ let targetAccountBalance = 0n;
+ const [targetAccount] = await helper.arrange.createAccounts([targetAccountBalance], alice);
+
+ const uniqueMultilocation = {
+ V1: {
+ parents: 1,
+ interior: {
+ X1: {Parachain: UNIQUE_CHAIN},
+ },
+ },
+ };
+
+ const maliciousXcmProgram = helper.arrange.makeXcmProgramWithdrawDeposit(
+ targetAccount.addressRaw,
+ {
+ Concrete: {
+ parents: 0,
+ interior: 'Here',
+ },
+ },
+ moreThanAcalaHas,
+ );
+
+ // Try to trick Unique
+ await usingAcalaPlaygrounds(acalaUrl, async (helper) => {
+ await helper.getSudo().xcm.send(alice, uniqueMultilocation, maliciousXcmProgram);
+ });
+
+ const maxWaitBlocks = 3;
+
+ const xcmpQueueFailEvent = await helper.wait.eventOutcome<XcmV2TraitsError>(
+ maxWaitBlocks,
+ 'xcmpQueue',
+ 'Fail',
+ );
+
+ expect(
+ xcmpQueueFailEvent != null,
+ '\'xcmpQueue.FailEvent\' event is expected',
+ ).to.be.true;
+
+ expect(
+ xcmpQueueFailEvent!.isFailedToTransactAsset,
+ 'The XCM error should be \'FailedToTransactAsset\'',
+ ).to.be.true;
+
+ targetAccountBalance = await helper.balance.getSubstrate(targetAccount.address);
+ expect(targetAccountBalance).to.be.equal(0n);
+
+ // But Acala still can send the correct amount
+ const validTransferAmount = acalaBalance / 2n;
+ const validXcmProgram = helper.arrange.makeXcmProgramWithdrawDeposit(
+ targetAccount.addressRaw,
+ {
+ Concrete: {
+ parents: 0,
+ interior: 'Here',
+ },
+ },
+ validTransferAmount,
+ );
+
+ await usingAcalaPlaygrounds(acalaUrl, async (helper) => {
+ await helper.getSudo().xcm.send(alice, uniqueMultilocation, validXcmProgram);
+ });
+
+ await helper.wait.newBlocks(maxWaitBlocks);
+
+ targetAccountBalance = await helper.balance.getSubstrate(targetAccount.address);
+ expect(targetAccountBalance).to.be.equal(validTransferAmount);
+ });
+
+ itSub('Should not accept reserve transfer of UNQ from Acala', async ({helper}) => {
+ const testAmount = 10_000n * (10n ** UNQ_DECIMALS);
+ const [targetAccount] = await helper.arrange.createAccounts([0n], alice);
+
+ const uniqueMultilocation = {
+ V1: {
+ parents: 1,
+ interior: {
+ X1: {
+ Parachain: UNIQUE_CHAIN,
+ },
+ },
+ },
+ };
+
+ const maliciousXcmProgram = helper.arrange.makeXcmProgramReserveAssetDeposited(
+ targetAccount.addressRaw,
+ {
+ Concrete: {
+ parents: 1,
+ interior: {
+ X1: {
+ Parachain: UNIQUE_CHAIN,
+ },
+ },
+ },
+ },
+ testAmount,
+ );
+
+ await usingAcalaPlaygrounds(acalaUrl, async (helper) => {
+ await helper.getSudo().xcm.send(alice, uniqueMultilocation, maliciousXcmProgram);
+ });
+
+ const maxWaitBlocks = 3;
+
+ const xcmpQueueFailEvent = await helper.wait.eventOutcome<XcmV2TraitsError>(
+ maxWaitBlocks,
+ 'xcmpQueue',
+ 'Fail',
+ );
+
+ expect(
+ xcmpQueueFailEvent != null,
+ '\'xcmpQueue.FailEvent\' event is expected',
+ ).to.be.true;
+
+ expect(
+ xcmpQueueFailEvent!.isUntrustedReserveLocation,
+ 'The XCM error should be \'isUntrustedReserveLocation\'',
+ ).to.be.true;
+
+ const accountBalance = await helper.balance.getSubstrate(targetAccount.address);
+ expect(accountBalance).to.be.equal(0n);
+ });
});
-// These tests are relevant only when the foreign asset pallet is disabled
+// These tests are relevant only when
+// the the corresponding foreign assets are not registered
describeXCM('[XCM] Integration test: Unique rejects non-native tokens', () => {
let alice: IKeyringPair;
+ let alith: IKeyringPair;
+
+ const testAmount = 100_000_000_000n;
+ let uniqueParachainJunction;
+ let uniqueAccountJunction;
+
+ let uniqueParachainMultilocation: any;
+ let uniqueAccountMultilocation: any;
+ let uniqueCombinedMultilocation: any;
before(async () => {
await usingPlaygrounds(async (helper, privateKey) => {
alice = await privateKey('//Alice');
- // Set the default version to wrap the first message to other chains.
- await helper.getSudo().xcm.setSafeXcmVersion(alice, SAFE_XCM_VERSION);
- });
- });
+ uniqueParachainJunction = {Parachain: UNIQUE_CHAIN};
+ uniqueAccountJunction = {
+ AccountId32: {
+ network: 'Any',
+ id: alice.addressRaw,
+ },
+ };
- itSub('Unique rejects ACA tokens from Acala', async ({helper}) => {
- await usingAcalaPlaygrounds(acalaUrl, async (helper) => {
- const destination = {
+ uniqueParachainMultilocation = {
V1: {
parents: 1,
interior: {
- X2: [
- {Parachain: UNIQUE_CHAIN},
- {
- AccountId32: {
- network: 'Any',
- id: alice.addressRaw,
- },
- },
- ],
+ X1: uniqueParachainJunction,
+ },
+ },
+ };
+
+ uniqueAccountMultilocation = {
+ V1: {
+ parents: 0,
+ interior: {
+ X1: uniqueAccountJunction,
},
},
};
- const id = {
- Token: 'ACA',
+ uniqueCombinedMultilocation = {
+ V1: {
+ parents: 1,
+ interior: {
+ X2: [uniqueParachainJunction, uniqueAccountJunction],
+ },
+ },
};
- await helper.xTokens.transfer(alice, id, 100_000_000_000n, destination, 'Unlimited');
+ // Set the default version to wrap the first message to other chains.
+ await helper.getSudo().xcm.setSafeXcmVersion(alice, SAFE_XCM_VERSION);
});
+ // eslint-disable-next-line require-await
+ await usingMoonbeamPlaygrounds(moonbeamUrl, async (helper) => {
+ alith = helper.account.alithAccount();
+ });
+ });
+
+ const expectFailedToTransact = async (network: string, helper: DevUniqueHelper) => {
const maxWaitBlocks = 3;
- const xcmpQueueFailEvent = await helper.wait.event(maxWaitBlocks, 'xcmpQueue', 'Fail');
+ const xcmpQueueFailEvent = await helper.wait.eventOutcome<XcmV2TraitsError>(
+ maxWaitBlocks,
+ 'xcmpQueue',
+ 'Fail',
+ );
expect(
xcmpQueueFailEvent != null,
- '[Acala] xcmpQueue.FailEvent event is expected',
+ `[reject ${network} tokens] 'xcmpQueue.FailEvent' event is expected`,
).to.be.true;
-
- const event = xcmpQueueFailEvent!.event;
- const outcome = event.data[1] as XcmV2TraitsError;
expect(
- outcome.isFailedToTransactAsset,
- '[Acala] The XCM error should be `FailedToTransactAsset`',
+ xcmpQueueFailEvent!.isFailedToTransactAsset,
+ `[reject ${network} tokens] The XCM error should be 'FailedToTransactAsset'`,
).to.be.true;
+ };
+
+ itSub('Unique rejects ACA tokens from Acala', async ({helper}) => {
+ await usingAcalaPlaygrounds(acalaUrl, async (helper) => {
+ const id = {
+ Token: 'ACA',
+ };
+ const destination = uniqueCombinedMultilocation;
+ await helper.xTokens.transfer(alice, id, testAmount, destination, 'Unlimited');
+ });
+
+ await expectFailedToTransact('ACA', helper);
+ });
+
+ itSub('Unique rejects GLMR tokens from Moonbeam', async ({helper}) => {
+ await usingMoonbeamPlaygrounds(moonbeamUrl, async (helper) => {
+ const id = 'SelfReserve';
+ const destination = uniqueCombinedMultilocation;
+ await helper.xTokens.transfer(alith, id, testAmount, destination, 'Unlimited');
+ });
+
+ await expectFailedToTransact('GLMR', helper);
+ });
+
+ itSub('Unique rejects ASTR tokens from Astar', async ({helper}) => {
+ await usingAstarPlaygrounds(astarUrl, async (helper) => {
+ const destinationParachain = uniqueParachainMultilocation;
+ const beneficiary = uniqueAccountMultilocation;
+ const assets = {
+ V1: [{
+ id: {
+ Concrete: {
+ parents: 0,
+ interior: 'Here',
+ },
+ },
+ fun: {
+ Fungible: testAmount,
+ },
+ }],
+ };
+ const feeAssetItem = 0;
+
+ await helper.executeExtrinsic(alice, 'api.tx.polkadotXcm.reserveWithdrawAssets', [
+ destinationParachain,
+ beneficiary,
+ assets,
+ feeAssetItem,
+ ]);
+ });
+
+ await expectFailedToTransact('ASTR', helper);
});
});
@@ -984,6 +1199,16 @@
console.log('[Moonbeam -> Unique] transaction fees on Unique: %s UNQ', helper.util.bigIntToDecimals(unqFees));
expect(unqFees == 0n).to.be.true;
});
+
+ // eslint-disable-next-line require-await
+ itSub.skip('Moonbeam can send only up to its balance', async ({helper}) => {
+ throw Error('Not yet implemented');
+ });
+
+ // eslint-disable-next-line require-await
+ itSub.skip('Should not accept reserve transfer of UNQ from Moonbeam', async ({helper}) => {
+ throw Error('Not yet implemented');
+ });
});
describeXCM('[XCM] Integration test: Exchanging tokens with Astar', () => {
@@ -1196,59 +1421,140 @@
expect(balanceUNQ).to.eq(balanceAfterUniqueToAstarXCM + unqFromAstarTransfered);
});
- itSub.skip('Should not accept limitedReserveTransfer of UNQ from ASTAR', async ({helper}) => {
+ itSub('Astar can send only up to its balance', async ({helper}) => {
+ // set Astar's sovereign account's balance
+ const astarBalance = 10000n * (10n ** UNQ_DECIMALS);
+ const astarSovereignAccount = helper.address.paraSiblingSovereignAccount(ASTAR_CHAIN);
+ await helper.getSudo().balance.setBalanceSubstrate(alice, astarSovereignAccount, astarBalance);
+
+ const moreThanShidenHas = astarBalance * 2n;
+
+ let targetAccountBalance = 0n;
+ const [targetAccount] = await helper.arrange.createAccounts([targetAccountBalance], alice);
+
+ const uniqueMultilocation = {
+ V1: {
+ parents: 1,
+ interior: {
+ X1: {Parachain: UNIQUE_CHAIN},
+ },
+ },
+ };
+
+ const maliciousXcmProgram = helper.arrange.makeXcmProgramWithdrawDeposit(
+ targetAccount.addressRaw,
+ {
+ Concrete: {
+ parents: 0,
+ interior: 'Here',
+ },
+ },
+ moreThanShidenHas,
+ );
+
+ // Try to trick Unique
+ await usingAstarPlaygrounds(astarUrl, async (helper) => {
+ await helper.getSudo().xcm.send(alice, uniqueMultilocation, maliciousXcmProgram);
+ });
+
+ const maxWaitBlocks = 3;
+
+ const xcmpQueueFailEvent = await helper.wait.eventOutcome<XcmV2TraitsError>(
+ maxWaitBlocks,
+ 'xcmpQueue',
+ 'Fail',
+ );
+
+ expect(
+ xcmpQueueFailEvent != null,
+ '\'xcmpQueue.FailEvent\' event is expected',
+ ).to.be.true;
+
+ expect(
+ xcmpQueueFailEvent!.isFailedToTransactAsset,
+ 'The XCM error should be \'FailedToTransactAsset\'',
+ ).to.be.true;
+
+ targetAccountBalance = await helper.balance.getSubstrate(targetAccount.address);
+ expect(targetAccountBalance).to.be.equal(0n);
+
+ // But Astar still can send the correct amount
+ const validTransferAmount = astarBalance / 2n;
+ const validXcmProgram = helper.arrange.makeXcmProgramWithdrawDeposit(
+ targetAccount.addressRaw,
+ {
+ Concrete: {
+ parents: 0,
+ interior: 'Here',
+ },
+ },
+ validTransferAmount,
+ );
+
await usingAstarPlaygrounds(astarUrl, async (helper) => {
- const destination = {
- V1: {
- parents: 1,
- interior: {
- X1: {
- Parachain: UNIQUE_CHAIN,
- },
+ await helper.getSudo().xcm.send(alice, uniqueMultilocation, validXcmProgram);
+ });
+
+ await helper.wait.newBlocks(maxWaitBlocks);
+
+ targetAccountBalance = await helper.balance.getSubstrate(targetAccount.address);
+ expect(targetAccountBalance).to.be.equal(validTransferAmount);
+ });
+
+ itSub('Should not accept reserve transfer of UNQ from Astar', async ({helper}) => {
+ const testAmount = 10_000n * (10n ** UNQ_DECIMALS);
+ const [targetAccount] = await helper.arrange.createAccounts([0n], alice);
+
+ const uniqueMultilocation = {
+ V1: {
+ parents: 1,
+ interior: {
+ X1: {
+ Parachain: UNIQUE_CHAIN,
},
},
- };
+ },
+ };
- const beneficiary = {
- V1: {
- parents: 0,
+ const maliciousXcmProgram = helper.arrange.makeXcmProgramReserveAssetDeposited(
+ targetAccount.addressRaw,
+ {
+ Concrete: {
+ parents: 1,
interior: {
X1: {
- AccountId32: {
- network: 'Any',
- id: randomAccount.addressRaw,
- },
+ Parachain: UNIQUE_CHAIN,
},
},
},
- };
+ },
+ testAmount,
+ );
- const assets = {
- V1: [
- {
- id: {
- Concrete: {
- parents: 1,
- interior: {
- X1: {
- Parachain: UNIQUE_CHAIN,
- },
- },
- },
- },
- fun: {
- Fungible: unqFromAstarTransfered,
- },
- },
- ],
- };
+ await usingAstarPlaygrounds(astarUrl, async (helper) => {
+ await helper.getSudo().xcm.send(alice, uniqueMultilocation, maliciousXcmProgram);
+ });
+
+ const maxWaitBlocks = 3;
- // Initial balance is 1 ASTAR
- expect(await helper.balance.getSubstrate(randomAccount.address)).to.eq(astarInitialBalance);
+ const xcmpQueueFailEvent = await helper.wait.eventOutcome<XcmV2TraitsError>(
+ maxWaitBlocks,
+ 'xcmpQueue',
+ 'Fail',
+ );
- const feeAssetItem = 0;
- // TODO: expect rejected:
- await helper.xcm.limitedReserveTransferAssets(randomAccount, destination, beneficiary, assets, feeAssetItem, 'Unlimited');
- });
+ expect(
+ xcmpQueueFailEvent != null,
+ '\'xcmpQueue.FailEvent\' event is expected',
+ ).to.be.true;
+
+ expect(
+ xcmpQueueFailEvent!.isUntrustedReserveLocation,
+ 'The XCM error should be \'isUntrustedReserveLocation\'',
+ ).to.be.true;
+
+ const accountBalance = await helper.balance.getSubstrate(targetAccount.address);
+ expect(accountBalance).to.be.equal(0n);
});
+
});