difftreelog
feat(test xcm) added generic `low level` xcm tests
in: master
4 files changed
tests/package.jsondiffbeforeafterboth--- a/tests/package.json
+++ b/tests/package.json
@@ -112,6 +112,7 @@
"testCollators": "RUN_COLLATOR_TESTS=1 yarn _test ./**/collator-selection/**.*test.ts --timeout 49999999",
"testCollatorSelection": "RUN_COLLATOR_TESTS=1 yarn _test ./**/collatorSelection.*test.ts --timeout 49999999",
"testIdentity": "RUN_COLLATOR_TESTS=1 yarn _test ./**/identity.*test.ts --timeout 49999999",
+ "testLowLevelXcmUnique": "RUN_XCM_TESTS=1 yarn _test ./**/xcm/lowLevelXcmUnique.test.ts",
"testXcmUnique": "RUN_XCM_TESTS=1 yarn _test ./**/xcm/xcmUnique.test.ts",
"testXcmQuartz": "RUN_XCM_TESTS=1 yarn _test ./**/xcm/xcmQuartz.test.ts",
"testXcmOpal": "RUN_XCM_TESTS=1 yarn _test ./**/xcm/xcmOpal.test.ts",
@@ -154,4 +155,4 @@
},
"type": "module",
"packageManager": "yarn@3.6.1"
-}
+}
\ No newline at end of file
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 {blake2AsHex, encodeAddress, mnemonicGenerate} from '@polkadot/util-crypto';6import {UniqueHelper, ChainHelperBase, ChainHelperBaseConstructor, HelperGroup, UniqueHelperConstructor} 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, ILogger, IPovInfo, ISchedulerOptions, ITransactionResult, TSigner} from './types';12import {FrameSystemEventRecord, XcmV2TraitsError} from '@polkadot/types/lookup';13import {SignerOptions, VoidFn} from '@polkadot/api/types';14import {Pallets} from '..';15import {spawnSync} from 'child_process';16import {AcalaHelper, AstarHelper, MoonbeamHelper, PolkadexHelper, RelayHelper, WestmintHelper, ForeignAssetsGroup, XcmGroup, XTokensGroup, TokensGroup} from './unique.xcm';17import {CollectiveGroup, CollectiveMembershipGroup, DemocracyGroup, ICollectiveGroup, IFellowshipGroup, RankedCollectiveGroup, ReferendaGroup} from './unique.governance';1819export class SilentLogger {20 log(_msg: any, _level: any): void { }21 level = {22 ERROR: 'ERROR' as const,23 WARNING: 'WARNING' as const,24 INFO: 'INFO' as const,25 };26}2728export class SilentConsole {29 // TODO: Remove, this is temporary: Filter unneeded API output30 // (Jaco promised it will be removed in the next version)31 consoleErr: any;32 consoleLog: any;33 consoleWarn: any;3435 constructor() {36 this.consoleErr = console.error;37 this.consoleLog = console.log;38 this.consoleWarn = console.warn;39 }4041 enable() {42 const outFn = (printer: any) => (...args: any[]) => {43 for(const arg of args) {44 if(typeof arg !== 'string')45 continue;46 const skippedWarnings = ['1000:: Normal connection closure', 'Not decorating unknown runtime apis:', 'RPC methods not decorated:', 'Not decorating runtime apis', 'Bad input data provided to validate_transaction', 'account balance too low', '1006:: Abnormal Closure'];47 const needToSkip = skippedWarnings.reduce((a, b) => a || arg.includes(b), false);48 if(needToSkip || arg === 'Normal connection closure')49 return;50 }51 printer(...args);52 };5354 console.error = outFn(this.consoleErr.bind(console));55 console.log = outFn(this.consoleLog.bind(console));56 console.warn = outFn(this.consoleWarn.bind(console));57 }5859 disable() {60 console.error = this.consoleErr;61 console.log = this.consoleLog;62 console.warn = this.consoleWarn;63 }64}6566export interface IEventHelper {67 section(): string;6869 method(): string;7071 wrapEvent(data: any[]): any;72}7374// eslint-disable-next-line @typescript-eslint/naming-convention75function EventHelper(section: string, method: string, wrapEvent: (data: any[]) => any) {76 const helperClass = class implements IEventHelper {77 wrapEvent: (data: any[]) => any;78 _section: string;79 _method: string;8081 constructor() {82 this.wrapEvent = wrapEvent;83 this._section = section;84 this._method = method;85 }8687 section(): string {88 return this._section;89 }9091 method(): string {92 return this._method;93 }9495 filter(txres: ITransactionResult) {96 return txres.result.events.filter(e => e.event.section === section && e.event.method === method)97 .map(e => this.wrapEvent(e.event.data));98 }99100 find(txres: ITransactionResult) {101 const e = txres.result.events.find(e => e.event.section === section && e.event.method === method);102 return e ? this.wrapEvent(e.event.data) : null;103 }104105 expect(txres: ITransactionResult) {106 const e = this.find(txres);107 if(e) {108 return e;109 } else {110 throw Error(`Expected event ${section}.${method}`);111 }112 }113 };114115 return helperClass;116}117118function eventJsonData<T = any>(data: any[], index: number) {119 return data[index].toJSON() as T;120}121122function eventHumanData(data: any[], index: number) {123 return data[index].toHuman();124}125126function eventData<T = any>(data: any[], index: number) {127 return data[index] as T;128}129130// eslint-disable-next-line @typescript-eslint/naming-convention131function EventSection(section: string) {132 return class Section {133 static section = section;134135 static Method(name: string, wrapEvent: (data: any[]) => any = () => {}) {136 const helperClass = EventHelper(Section.section, name, wrapEvent);137 return new helperClass();138 }139 };140}141142function schedulerSection(schedulerInstance: string) {143 return class extends EventSection(schedulerInstance) {144 static Dispatched = this.Method('Dispatched', data => ({145 task: eventJsonData(data, 0),146 id: eventHumanData(data, 1),147 result: data[2],148 }));149150 static PriorityChanged = this.Method('PriorityChanged', data => ({151 task: eventJsonData(data, 0),152 priority: eventJsonData(data, 1),153 }));154 };155}156157export class Event {158 static Democracy = class extends EventSection('democracy') {159 static Proposed = this.Method('Proposed', data => ({160 proposalIndex: eventJsonData<number>(data, 0),161 }));162163 static ExternalTabled = this.Method('ExternalTabled');164165 static Started = this.Method('Started', data => ({166 referendumIndex: eventJsonData<number>(data, 0),167 threshold: eventHumanData(data, 1),168 }));169170 static Voted = this.Method('Voted', data => ({171 voter: eventJsonData(data, 0),172 referendumIndex: eventJsonData<number>(data, 1),173 vote: eventJsonData(data, 2),174 }));175176 static Passed = this.Method('Passed', data => ({177 referendumIndex: eventJsonData<number>(data, 0),178 }));179180 static ProposalCanceled = this.Method('ProposalCanceled', data => ({181 propIndex: eventJsonData<number>(data, 0),182 }));183184 static Cancelled = this.Method('Cancelled', data => ({185 propIndex: eventJsonData<number>(data, 0),186 }));187188 static Vetoed = this.Method('Vetoed', data => ({189 who: eventHumanData(data, 0),190 proposalHash: eventHumanData(data, 1),191 until: eventJsonData<number>(data, 1),192 }));193 };194195 static Council = class extends EventSection('council') {196 static Proposed = this.Method('Proposed', data => ({197 account: eventHumanData(data, 0),198 proposalIndex: eventJsonData<number>(data, 1),199 proposalHash: eventHumanData(data, 2),200 threshold: eventJsonData<number>(data, 3),201 }));202 static Closed = this.Method('Closed', data => ({203 proposalHash: eventHumanData(data, 0),204 yes: eventJsonData<number>(data, 1),205 no: eventJsonData<number>(data, 2),206 }));207 static Executed = this.Method('Executed', data => ({208 proposalHash: eventHumanData(data, 0),209 }));210 };211212 static TechnicalCommittee = class extends EventSection('technicalCommittee') {213 static Proposed = this.Method('Proposed', data => ({214 account: eventHumanData(data, 0),215 proposalIndex: eventJsonData<number>(data, 1),216 proposalHash: eventHumanData(data, 2),217 threshold: eventJsonData<number>(data, 3),218 }));219 static Closed = this.Method('Closed', data => ({220 proposalHash: eventHumanData(data, 0),221 yes: eventJsonData<number>(data, 1),222 no: eventJsonData<number>(data, 2),223 }));224 static Approved = this.Method('Approved', data => ({225 proposalHash: eventHumanData(data, 0),226 }));227 static Executed = this.Method('Executed', data => ({228 proposalHash: eventHumanData(data, 0),229 result: eventHumanData(data, 1),230 }));231 };232233 static FellowshipReferenda = class extends EventSection('fellowshipReferenda') {234 static Submitted = this.Method('Submitted', data => ({235 referendumIndex: eventJsonData<number>(data, 0),236 trackId: eventJsonData<number>(data, 1),237 proposal: eventJsonData(data, 2),238 }));239240 static Cancelled = this.Method('Cancelled', data => ({241 index: eventJsonData<number>(data, 0),242 tally: eventJsonData(data, 1),243 }));244 };245246 static UniqueScheduler = schedulerSection('uniqueScheduler');247 static Scheduler = schedulerSection('scheduler');248249 static XcmpQueue = class extends EventSection('xcmpQueue') {250 static XcmpMessageSent = this.Method('XcmpMessageSent', data => ({251 messageHash: eventJsonData(data, 0),252 }));253254 static Success = this.Method('Success', data => ({255 messageHash: eventJsonData(data, 0),256 }));257258 static Fail = this.Method('Fail', data => ({259 messageHash: eventJsonData(data, 0),260 outcome: eventData<XcmV2TraitsError>(data, 1),261 }));262 };263}264265// eslint-disable-next-line @typescript-eslint/naming-convention266export function SudoHelper<T extends ChainHelperBaseConstructor>(Base: T) {267 return class extends Base {268 constructor(...args: any[]) {269 super(...args);270 }271272 async executeExtrinsic(273 sender: IKeyringPair,274 extrinsic: string,275 params: any[],276 expectSuccess?: boolean,277 options: Partial<SignerOptions> | null = null,278 ): Promise<ITransactionResult> {279 const call = this.constructApiCall(extrinsic, params);280 const result = await super.executeExtrinsic(281 sender,282 'api.tx.sudo.sudo',283 [call],284 expectSuccess,285 options,286 );287288 if(result.status === 'Fail') return result;289290 const data = (result.result.events.find(x => x.event.section == 'sudo' && x.event.method == 'Sudid')?.event.data as any).sudoResult;291 if(data.isErr) {292 if(data.asErr.isModule) {293 const error = (result.result.events[1].event.data as any).sudoResult.asErr.asModule;294 const metaError = super.getApi()?.registry.findMetaError(error);295 throw new Error(`${metaError.section}.${metaError.name}`);296 } else if(data.asErr.isToken) {297 throw new Error(`Token: ${data.asErr.asToken}`);298 }299 // May be [object Object] in case of unhandled non-unit enum300 throw new Error(`Misc: ${data.asErr.toHuman()}`);301 }302 return result;303 }304 async executeExtrinsicUncheckedWeight(305 sender: IKeyringPair,306 extrinsic: string,307 params: any[],308 expectSuccess?: boolean,309 options: Partial<SignerOptions> | null = null,310 ): Promise<ITransactionResult> {311 const call = this.constructApiCall(extrinsic, params);312 const result = await super.executeExtrinsic(313 sender,314 'api.tx.sudo.sudoUncheckedWeight',315 [call, {refTime: 0, proofSize: 0}],316 expectSuccess,317 options,318 );319320 if(result.status === 'Fail') return result;321322 const data = (result.result.events.find(x => x.event.section == 'sudo' && x.event.method == 'Sudid')?.event.data as any).sudoResult;323 if(data.isErr) {324 if(data.asErr.isModule) {325 const error = (result.result.events[1].event.data as any).sudoResult.asErr.asModule;326 const metaError = super.getApi()?.registry.findMetaError(error);327 throw new Error(`${metaError.section}.${metaError.name}`);328 } else if(data.asErr.isToken) {329 throw new Error(`Token: ${data.asErr.asToken}`);330 }331 // May be [object Object] in case of unhandled non-unit enum332 throw new Error(`Misc: ${data.asErr.toHuman()}`);333 }334 return result;335 }336 };337}338339class SchedulerGroup extends HelperGroup<UniqueHelper> {340 constructor(helper: UniqueHelper) {341 super(helper);342 }343344 cancelScheduled(signer: TSigner, scheduledId: string) {345 return this.helper.executeExtrinsic(346 signer,347 'api.tx.scheduler.cancelNamed',348 [scheduledId],349 true,350 );351 }352353 changePriority(signer: TSigner, scheduledId: string, priority: number) {354 return this.helper.executeExtrinsic(355 signer,356 'api.tx.scheduler.changeNamedPriority',357 [scheduledId, priority],358 true,359 );360 }361362 scheduleAt<T extends DevUniqueHelper>(363 executionBlockNumber: number,364 options: ISchedulerOptions = {},365 ) {366 return this.schedule<T>('schedule', executionBlockNumber, options);367 }368369 scheduleAfter<T extends DevUniqueHelper>(370 blocksBeforeExecution: number,371 options: ISchedulerOptions = {},372 ) {373 return this.schedule<T>('scheduleAfter', blocksBeforeExecution, options);374 }375376 schedule<T extends UniqueHelper>(377 scheduleFn: 'schedule' | 'scheduleAfter',378 blocksNum: number,379 options: ISchedulerOptions = {},380 ) {381 // eslint-disable-next-line @typescript-eslint/naming-convention382 const ScheduledHelperType = ScheduledUniqueHelper(this.helper.helperBase);383 return this.helper.clone(ScheduledHelperType, {384 scheduleFn,385 blocksNum,386 options,387 }) as T;388 }389}390391class CollatorSelectionGroup extends HelperGroup<UniqueHelper> {392 //todo:collator documentation393 addInvulnerable(signer: TSigner, address: string) {394 return this.helper.executeExtrinsic(signer, 'api.tx.collatorSelection.addInvulnerable', [address]);395 }396397 removeInvulnerable(signer: TSigner, address: string) {398 return this.helper.executeExtrinsic(signer, 'api.tx.collatorSelection.removeInvulnerable', [address]);399 }400401 async getInvulnerables(): Promise<string[]> {402 return (await this.helper.callRpc('api.query.collatorSelection.invulnerables')).map((x: any) => x.toHuman());403 }404405 /** and also total max invulnerables */406 maxCollators(): number {407 return (this.helper.getApi().consts.configuration.defaultCollatorSelectionMaxCollators.toJSON() as number);408 }409410 async getDesiredCollators(): Promise<number> {411 return (await this.helper.callRpc('api.query.configuration.collatorSelectionDesiredCollatorsOverride')).toNumber();412 }413414 setLicenseBond(signer: TSigner, amount: bigint) {415 return this.helper.executeExtrinsic(signer, 'api.tx.configuration.setCollatorSelectionLicenseBond', [amount]);416 }417418 async getLicenseBond(): Promise<bigint> {419 return (await this.helper.callRpc('api.query.configuration.collatorSelectionLicenseBondOverride')).toBigInt();420 }421422 obtainLicense(signer: TSigner) {423 return this.helper.executeExtrinsic(signer, 'api.tx.collatorSelection.getLicense', []);424 }425426 releaseLicense(signer: TSigner) {427 return this.helper.executeExtrinsic(signer, 'api.tx.collatorSelection.releaseLicense', []);428 }429430 forceReleaseLicense(signer: TSigner, released: string) {431 return this.helper.executeExtrinsic(signer, 'api.tx.collatorSelection.forceReleaseLicense', [released]);432 }433434 async hasLicense(address: string): Promise<bigint> {435 return (await this.helper.callRpc('api.query.collatorSelection.licenseDepositOf', [address])).toBigInt();436 }437438 onboard(signer: TSigner) {439 return this.helper.executeExtrinsic(signer, 'api.tx.collatorSelection.onboard', []);440 }441442 offboard(signer: TSigner) {443 return this.helper.executeExtrinsic(signer, 'api.tx.collatorSelection.offboard', []);444 }445446 async getCandidates(): Promise<string[]> {447 return (await this.helper.callRpc('api.query.collatorSelection.candidates')).map((x: any) => x.toHuman());448 }449}450451export class DevUniqueHelper extends UniqueHelper {452 /**453 * Arrange methods for tests454 */455 arrange: ArrangeGroup;456 wait: WaitGroup;457 admin: AdminGroup;458 session: SessionGroup;459 testUtils: TestUtilGroup;460 foreignAssets: ForeignAssetsGroup;461 xcm: XcmGroup<DevUniqueHelper>;462 xTokens: XTokensGroup<DevUniqueHelper>;463 tokens: TokensGroup<DevUniqueHelper>;464 scheduler: SchedulerGroup;465 collatorSelection: CollatorSelectionGroup;466 council: ICollectiveGroup;467 technicalCommittee: ICollectiveGroup;468 fellowship: IFellowshipGroup;469 democracy: DemocracyGroup;470471 constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {472 options.helperBase = options.helperBase ?? DevUniqueHelper;473474 super(logger, options);475 this.arrange = new ArrangeGroup(this);476 this.wait = new WaitGroup(this);477 this.admin = new AdminGroup(this);478 this.testUtils = new TestUtilGroup(this);479 this.session = new SessionGroup(this);480 this.foreignAssets = new ForeignAssetsGroup(this);481 this.xcm = new XcmGroup(this, 'polkadotXcm');482 this.xTokens = new XTokensGroup(this);483 this.tokens = new TokensGroup(this);484 this.scheduler = new SchedulerGroup(this);485 this.collatorSelection = new CollatorSelectionGroup(this);486 this.council = {487 collective: new CollectiveGroup(this, 'council'),488 membership: new CollectiveMembershipGroup(this, 'councilMembership'),489 };490 this.technicalCommittee = {491 collective: new CollectiveGroup(this, 'technicalCommittee'),492 membership: new CollectiveMembershipGroup(this, 'technicalCommitteeMembership'),493 };494 this.fellowship = {495 collective: new RankedCollectiveGroup(this, 'fellowshipCollective'),496 referenda: new ReferendaGroup(this, 'fellowshipReferenda'),497 };498 this.democracy = new DemocracyGroup(this);499 }500501 async connect(wsEndpoint: string, _listeners?: any): Promise<void> {502 if(!wsEndpoint) throw new Error('wsEndpoint was not set');503 const wsProvider = new WsProvider(wsEndpoint);504 this.api = new ApiPromise({505 provider: wsProvider,506 signedExtensions: {507 ContractHelpers: {508 extrinsic: {},509 payload: {},510 },511 CheckMaintenance: {512 extrinsic: {},513 payload: {},514 },515 DisableIdentityCalls: {516 extrinsic: {},517 payload: {},518 },519 FakeTransactionFinalizer: {520 extrinsic: {},521 payload: {},522 },523 },524 rpc: {525 unique: defs.unique.rpc,526 appPromotion: defs.appPromotion.rpc,527 povinfo: defs.povinfo.rpc,528 eth: {529 feeHistory: {530 description: 'Dummy',531 params: [],532 type: 'u8',533 },534 maxPriorityFeePerGas: {535 description: 'Dummy',536 params: [],537 type: 'u8',538 },539 },540 },541 });542 await this.api.isReadyOrError;543 this.network = await UniqueHelper.detectNetwork(this.api);544 this.wsEndpoint = wsEndpoint;545 }546 getSudo<T extends DevUniqueHelper>() {547 // eslint-disable-next-line @typescript-eslint/naming-convention548 const SudoHelperType = SudoHelper(this.helperBase);549 return this.clone(SudoHelperType) as T;550 }551}552553export class DevRelayHelper extends RelayHelper {554 wait: WaitGroup;555556 constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {557 options.helperBase = options.helperBase ?? DevRelayHelper;558559 super(logger, options);560 this.wait = new WaitGroup(this);561 }562}563564export class DevWestmintHelper extends WestmintHelper {565 wait: WaitGroup;566567 constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {568 options.helperBase = options.helperBase ?? DevWestmintHelper;569570 super(logger, options);571 this.wait = new WaitGroup(this);572 }573}574575export class DevStatemineHelper extends DevWestmintHelper {}576577export class DevStatemintHelper extends DevWestmintHelper {}578579export class DevMoonbeamHelper extends MoonbeamHelper {580 account: MoonbeamAccountGroup;581 wait: WaitGroup;582 fastDemocracy: MoonbeamFastDemocracyGroup;583584 constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {585 options.helperBase = options.helperBase ?? DevMoonbeamHelper;586 options.notePreimagePallet = options.notePreimagePallet ?? 'preimage';587588 super(logger, options);589 this.account = new MoonbeamAccountGroup(this);590 this.wait = new WaitGroup(this);591 this.fastDemocracy = new MoonbeamFastDemocracyGroup(this);592 }593}594595export class DevMoonriverHelper extends DevMoonbeamHelper {596 constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {597 options.notePreimagePallet = options.notePreimagePallet ?? 'preimage';598 super(logger, options);599 }600}601602export class DevAstarHelper extends AstarHelper {603 wait: WaitGroup;604605 constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {606 options.helperBase = options.helperBase ?? DevAstarHelper;607608 super(logger, options);609 this.wait = new WaitGroup(this);610 }611612 getSudo<T extends AstarHelper>() {613 // eslint-disable-next-line @typescript-eslint/naming-convention614 const SudoHelperType = SudoHelper(this.helperBase);615 return this.clone(SudoHelperType) as T;616 }617}618619export class DevShidenHelper extends DevAstarHelper { }620621export class DevAcalaHelper extends AcalaHelper {622 wait: WaitGroup;623624 constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {625 options.helperBase = options.helperBase ?? DevAcalaHelper;626627 super(logger, options);628 this.wait = new WaitGroup(this);629 }630 getSudo() {631 // eslint-disable-next-line @typescript-eslint/naming-convention632 const SudoHelperType = SudoHelper(this.helperBase);633 return this.clone(SudoHelperType) as DevAcalaHelper;634 }635}636637export class DevPolkadexHelper extends PolkadexHelper {638 wait: WaitGroup;639 constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {640 options.helperBase = options.helperBase ?? PolkadexHelper;641642 super(logger, options);643 this.wait = new WaitGroup(this);644 }645646 getSudo() {647 // eslint-disable-next-line @typescript-eslint/naming-convention648 const SudoHelperType = SudoHelper(this.helperBase);649 return this.clone(SudoHelperType) as DevPolkadexHelper;650 }651}652653export class DevKaruraHelper extends DevAcalaHelper {}654655export class ArrangeGroup {656 helper: DevUniqueHelper;657658 scheduledIdSlider = 0;659660 constructor(helper: DevUniqueHelper) {661 this.helper = helper;662 }663664 /**665 * Generates accounts with the specified UNQ token balance666 * @param balances balances for generated accounts. Each balance will be multiplied by the token nominal.667 * @param donor donor account for balances668 * @returns array of newly created accounts669 * @example const [acc1, acc2, acc3] = await createAccounts([0n, 10n, 20n], donor);670 */671 createAccounts = async (balances: bigint[], donor: IKeyringPair): Promise<IKeyringPair[]> => {672 let nonce = await this.helper.chain.getNonce(donor.address);673 const wait = new WaitGroup(this.helper);674 const ss58Format = this.helper.chain.getChainProperties().ss58Format;675 const tokenNominal = this.helper.balance.getOneTokenNominal();676 const transactions = [];677 const accounts: IKeyringPair[] = [];678 for(const balance of balances) {679 const recipient = this.helper.util.fromSeed(mnemonicGenerate(), ss58Format);680 accounts.push(recipient);681 if(balance !== 0n) {682 const tx = this.helper.constructApiCall('api.tx.balances.transfer', [{Id: recipient.address}, balance * tokenNominal]);683 transactions.push(this.helper.signTransaction(donor, tx, {nonce, era: 0}, 'account generation'));684 nonce++;685 }686 }687688 await Promise.all(transactions).catch(_e => {});689690 //#region TODO remove this region, when nonce problem will be solved691 const checkBalances = async () => {692 let isSuccess = true;693 for(let i = 0; i < balances.length; i++) {694 const balance = await this.helper.balance.getSubstrate(accounts[i].address);695 if(balance !== balances[i] * tokenNominal) {696 isSuccess = false;697 break;698 }699 }700 return isSuccess;701 };702703 let accountsCreated = false;704 const maxBlocksChecked = await this.helper.arrange.isDevNode() ? 50 : 5;705 // checkBalances retry up to 5-50 blocks706 for(let index = 0; index < maxBlocksChecked; index++) {707 accountsCreated = await checkBalances();708 if(accountsCreated) break;709 await wait.newBlocks(1);710 }711712 if(!accountsCreated) throw Error('Accounts generation failed');713 //#endregion714715 return accounts;716 };717718 // TODO combine this method and createAccounts into one719 createCrowd = async (accountsToCreate: number, withBalance: bigint, donor: IKeyringPair): Promise<IKeyringPair[]> => {720 const createAsManyAsCan = async () => {721 let transactions: any = [];722 const accounts: IKeyringPair[] = [];723 let nonce = await this.helper.chain.getNonce(donor.address);724 const tokenNominal = this.helper.balance.getOneTokenNominal();725 const ss58Format = this.helper.chain.getChainProperties().ss58Format;726 for(let i = 0; i < accountsToCreate; i++) {727 if(i === 500) { // if there are too many accounts to create728 await Promise.allSettled(transactions); // wait while first 500 (should be 100 for devnode) tx will be settled729 transactions = []; //730 nonce = await this.helper.chain.getNonce(donor.address); // update nonce731 }732 const recipient = this.helper.util.fromSeed(mnemonicGenerate(), ss58Format);733 accounts.push(recipient);734 if(withBalance !== 0n) {735 const tx = this.helper.constructApiCall('api.tx.balances.transfer', [{Id: recipient.address}, withBalance * tokenNominal]);736 transactions.push(this.helper.signTransaction(donor, tx, {nonce}, 'account generation'));737 nonce++;738 }739 }740741 const fullfilledAccounts = [];742 await Promise.allSettled(transactions);743 for(const account of accounts) {744 const accountBalance = await this.helper.balance.getSubstrate(account.address);745 if(accountBalance === withBalance * tokenNominal) {746 fullfilledAccounts.push(account);747 }748 }749 return fullfilledAccounts;750 };751752753 const crowd: IKeyringPair[] = [];754 // do up to 5 retries755 for(let index = 0; index < 5 && accountsToCreate !== 0; index++) {756 const asManyAsCan = await createAsManyAsCan();757 crowd.push(...asManyAsCan);758 accountsToCreate -= asManyAsCan.length;759 }760761 if(accountsToCreate !== 0) throw Error(`Crowd generation failed: ${accountsToCreate} accounts left`);762763 return crowd;764 };765766 /**767 * Generates one account with zero balance768 * @returns the newly generated account769 * @example const account = await helper.arrange.createEmptyAccount();770 */771 createEmptyAccount = (): IKeyringPair => {772 const ss58Format = this.helper.chain.getChainProperties().ss58Format;773 return this.helper.util.fromSeed(mnemonicGenerate(), ss58Format);774 };775776 isDevNode = async () => {777 let blockNumber = (await this.helper.callRpc('api.query.system.number')).toJSON();778 if(blockNumber == 0) {779 await this.helper.wait.newBlocks(1);780 blockNumber = (await this.helper.callRpc('api.query.system.number')).toJSON();781 }782 const block2 = await this.helper.callRpc('api.rpc.chain.getBlock', [await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber])]);783 const block1 = await this.helper.callRpc('api.rpc.chain.getBlock', [await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber - 1])]);784 const findCreationDate = (block: any) => {785 const humanBlock = block.toHuman();786 let date;787 humanBlock.block.extrinsics.forEach((ext: any) => {788 if(ext.method.section === 'timestamp') {789 date = Number(ext.method.args.now.replaceAll(',', ''));790 }791 });792 return date;793 };794 const block1date = await findCreationDate(block1);795 const block2date = await findCreationDate(block2);796 if(block2date! - block1date! < 9000) return true;797 };798799 async calculcateFee(payer: ICrossAccountId, promise: () => Promise<any>): Promise<bigint> {800 const address = 'Substrate' in payer ? payer.Substrate : this.helper.address.ethToSubstrate(payer.Ethereum);801 let balance = await this.helper.balance.getSubstrate(address);802803 await promise();804805 balance -= await this.helper.balance.getSubstrate(address);806807 return balance;808 }809810 async calculatePoVInfo(txs: any[]): Promise<IPovInfo> {811 const rawPovInfo = await this.helper.callRpc('api.rpc.povinfo.estimateExtrinsicPoV', [txs]);812813 const kvJson: {[key: string]: string} = {};814815 for(const kv of rawPovInfo.keyValues) {816 kvJson[kv.key.toHex()] = kv.value.toHex();817 }818819 const kvStr = JSON.stringify(kvJson);820821 const chainql = spawnSync(822 'chainql',823 [824 `--tla-code=data=${kvStr}`,825 '-e', `function(data) cql.dump(cql.chain("${this.helper.getEndpoint()}").latest._meta, data, {omit_empty:true})`,826 ],827 );828829 if(!chainql.stdout) {830 throw Error('unable to get an output from the `chainql`');831 }832833 return {834 proofSize: rawPovInfo.proofSize.toNumber(),835 compactProofSize: rawPovInfo.compactProofSize.toNumber(),836 compressedProofSize: rawPovInfo.compressedProofSize.toNumber(),837 results: rawPovInfo.results,838 kv: JSON.parse(chainql.stdout.toString()),839 };840 }841842 calculatePalletAddress(palletId: any) {843 const address = stringToU8a(('modl' + palletId).padEnd(32, '\0'));844 return encodeAddress(address, this.helper.chain.getChainProperties().ss58Format);845 }846847 makeScheduledIds(num: number): string[] {848 function makeId(slider: number) {849 const scheduledIdSize = 64;850 const hexId = slider.toString(16);851 const prefixSize = scheduledIdSize - hexId.length;852853 const scheduledId = '0x' + '0'.repeat(prefixSize) + hexId;854855 return scheduledId;856 }857858 const ids = [];859 for(let i = 0; i < num; i++) {860 ids.push(makeId(this.scheduledIdSlider));861 this.scheduledIdSlider += 1;862 }863864 return ids;865 }866867 makeScheduledId(): string {868 return (this.makeScheduledIds(1))[0];869 }870871 async captureEvents(eventSection: string, eventMethod: string): Promise<EventCapture> {872 const capture = new EventCapture(this.helper, eventSection, eventMethod);873 await capture.startCapture();874875 return capture;876 }877878 makeXcmProgramWithdrawDeposit(beneficiary: Uint8Array, id: any, amount: bigint) {879 return {880 V2: [881 {882 WithdrawAsset: [883 {884 id,885 fun: {886 Fungible: amount,887 },888 },889 ],890 },891 {892 BuyExecution: {893 fees: {894 id,895 fun: {896 Fungible: amount,897 },898 },899 weightLimit: 'Unlimited',900 },901 },902 {903 DepositAsset: {904 assets: {905 Wild: 'All',906 },907 maxAssets: 1,908 beneficiary: {909 parents: 0,910 interior: {911 X1: {912 AccountId32: {913 network: 'Any',914 id: beneficiary,915 },916 },917 },918 },919 },920 },921 ],922 };923 }924925 makeXcmProgramReserveAssetDeposited(beneficiary: Uint8Array, id: any, amount: bigint) {926 return {927 V2: [928 {929 ReserveAssetDeposited: [930 {931 id,932 fun: {933 Fungible: amount,934 },935 },936 ],937 },938 {939 BuyExecution: {940 fees: {941 id,942 fun: {943 Fungible: amount,944 },945 },946 weightLimit: 'Unlimited',947 },948 },949 {950 DepositAsset: {951 assets: {952 Wild: 'All',953 },954 maxAssets: 1,955 beneficiary: {956 parents: 0,957 interior: {958 X1: {959 AccountId32: {960 network: 'Any',961 id: beneficiary,962 },963 },964 },965 },966 },967 },968 ],969 };970 }971}972973class MoonbeamAccountGroup {974 helper: MoonbeamHelper;975976 keyring: Keyring;977 _alithAccount: IKeyringPair;978 _baltatharAccount: IKeyringPair;979 _dorothyAccount: IKeyringPair;980981 constructor(helper: MoonbeamHelper) {982 this.helper = helper;983984 this.keyring = new Keyring({type: 'ethereum'});985 const alithPrivateKey = '0x5fb92d6e98884f76de468fa3f6278f8807c48bebc13595d45af5bdc4da702133';986 const baltatharPrivateKey = '0x8075991ce870b93a8870eca0c0f91913d12f47948ca0fd25b49c6fa7cdbeee8b';987 const dorothyPrivateKey = '0x39539ab1876910bbf3a223d84a29e28f1cb4e2e456503e7e91ed39b2e7223d68';988989 this._alithAccount = this.keyring.addFromUri(alithPrivateKey, undefined, 'ethereum');990 this._baltatharAccount = this.keyring.addFromUri(baltatharPrivateKey, undefined, 'ethereum');991 this._dorothyAccount = this.keyring.addFromUri(dorothyPrivateKey, undefined, 'ethereum');992 }993994 alithAccount() {995 return this._alithAccount;996 }997998 baltatharAccount() {999 return this._baltatharAccount;1000 }10011002 dorothyAccount() {1003 return this._dorothyAccount;1004 }10051006 create() {1007 return this.keyring.addFromUri(mnemonicGenerate());1008 }1009}10101011class MoonbeamFastDemocracyGroup {1012 helper: DevMoonbeamHelper;10131014 constructor(helper: DevMoonbeamHelper) {1015 this.helper = helper;1016 }10171018 async executeProposal(proposalDesciption: string, encodedProposal: string) {1019 const proposalHash = blake2AsHex(encodedProposal);10201021 const alithAccount = this.helper.account.alithAccount();1022 const baltatharAccount = this.helper.account.baltatharAccount();1023 const dorothyAccount = this.helper.account.dorothyAccount();10241025 const councilVotingThreshold = 2;1026 const technicalCommitteeThreshold = 2;1027 const fastTrackVotingPeriod = 3;1028 const fastTrackDelayPeriod = 0;10291030 console.log(`[democracy] executing '${proposalDesciption}' proposal`);10311032 // >>> Propose external motion through council >>>1033 console.log('\t* Propose external motion through council.......');1034 const externalMotion = this.helper.democracy.externalProposeMajority({Inline: encodedProposal});1035 const encodedMotion = externalMotion?.method.toHex() || '';1036 const motionHash = blake2AsHex(encodedMotion);1037 console.log('\t* Motion hash is %s', motionHash);10381039 await this.helper.collective.council.propose(1040 baltatharAccount,1041 councilVotingThreshold,1042 externalMotion,1043 externalMotion.encodedLength,1044 );10451046 const councilProposalIdx = await this.helper.collective.council.proposalCount() - 1;1047 await this.helper.collective.council.vote(dorothyAccount, motionHash, councilProposalIdx, true);1048 await this.helper.collective.council.vote(baltatharAccount, motionHash, councilProposalIdx, true);10491050 await this.helper.collective.council.close(1051 dorothyAccount,1052 motionHash,1053 councilProposalIdx,1054 {1055 refTime: 1_000_000_000,1056 proofSize: 1_000_000,1057 },1058 externalMotion.encodedLength,1059 );1060 console.log('\t* Propose external motion through council.......DONE');1061 // <<< Propose external motion through council <<<10621063 // >>> Fast track proposal through technical committee >>>1064 console.log('\t* Fast track proposal through technical committee.......');1065 const fastTrack = this.helper.democracy.fastTrack(proposalHash, fastTrackVotingPeriod, fastTrackDelayPeriod);1066 const encodedFastTrack = fastTrack?.method.toHex() || '';1067 const fastTrackHash = blake2AsHex(encodedFastTrack);1068 console.log('\t* FastTrack hash is %s', fastTrackHash);10691070 await this.helper.collective.techCommittee.propose(alithAccount, technicalCommitteeThreshold, fastTrack, fastTrack.encodedLength);10711072 const techProposalIdx = await this.helper.collective.techCommittee.proposalCount() - 1;1073 await this.helper.collective.techCommittee.vote(baltatharAccount, fastTrackHash, techProposalIdx, true);1074 await this.helper.collective.techCommittee.vote(alithAccount, fastTrackHash, techProposalIdx, true);10751076 await this.helper.collective.techCommittee.close(1077 baltatharAccount,1078 fastTrackHash,1079 techProposalIdx,1080 {1081 refTime: 1_000_000_000,1082 proofSize: 1_000_000,1083 },1084 fastTrack.encodedLength,1085 );1086 console.log('\t* Fast track proposal through technical committee.......DONE');1087 // <<< Fast track proposal through technical committee <<<10881089 const democracyStarted = await this.helper.wait.expectEvent(3, Event.Democracy.Started);1090 const referendumIndex = democracyStarted.referendumIndex;10911092 // >>> Referendum voting >>>1093 console.log(`\t* Referendum #${referendumIndex} voting.......`);1094 await this.helper.democracy.referendumVote(dorothyAccount, referendumIndex, {1095 balance: 10_000_000_000_000_000_000n,1096 vote: {aye: true, conviction: 1},1097 });1098 console.log(`\t* Referendum #${referendumIndex} voting.......DONE`);1099 // <<< Referendum voting <<<11001101 // Wait the proposal to pass1102 await this.helper.wait.expectEvent(3, Event.Democracy.Passed, event => event.referendumIndex == referendumIndex);11031104 await this.helper.wait.newBlocks(1);11051106 console.log(`[democracy] executing '${proposalDesciption}' proposal.......DONE`);1107 }1108}11091110class WaitGroup {1111 helper: ChainHelperBase;11121113 constructor(helper: ChainHelperBase) {1114 this.helper = helper;1115 }11161117 sleep(milliseconds: number) {1118 return new Promise((resolve) => setTimeout(resolve, milliseconds));1119 }11201121 private async waitWithTimeout(promise: Promise<any>, timeout: number) {1122 let isBlock = false;1123 promise.then(() => isBlock = true).catch(() => isBlock = true);1124 let totalTime = 0;1125 const step = 100;1126 while(!isBlock) {1127 await this.sleep(step);1128 totalTime += step;1129 if(totalTime >= timeout) throw Error('Blocks production failed');1130 }1131 return promise;1132 }11331134 /**1135 * Launch some async operation, or throw an error after some time. Note that it will still continue executing after the timeout.1136 * @param promise async operation to race against the timeout1137 * @param timeoutMS time after which to time out1138 * @param timeoutError error message to throw1139 * @returns promise of the same type the operation had1140 */1141 withTimeout<T>(1142 promise: Promise<T>,1143 timeoutMS = 30000,1144 timeoutError = 'The operation has timed out!',1145 ): Promise<T> {1146 const timeout = new Promise<never>((_, reject) => {1147 setTimeout(() => {1148 reject(new Error(timeoutError));1149 }, timeoutMS);1150 });11511152 return Promise.race<T>([promise, timeout]).catch(e => {throw new Error(e);});1153 }11541155 /**1156 * Wait for specified number of blocks1157 * @param blocksCount number of blocks to wait1158 * @returns1159 */1160 async newBlocks(blocksCount = 1, timeout?: number): Promise<void> {1161 timeout = timeout ?? blocksCount * 60_000;1162 // eslint-disable-next-line no-async-promise-executor1163 const promise = new Promise<void>(async (resolve) => {1164 const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(() => {1165 if(blocksCount > 0) {1166 blocksCount--;1167 } else {1168 unsubscribe();1169 resolve();1170 }1171 });1172 });1173 await this.waitWithTimeout(promise, timeout);1174 return promise;1175 }11761177 /**1178 * Wait for the specified number of sessions to pass.1179 * Only applicable if the Session pallet is turned on.1180 * @param sessionCount number of sessions to wait1181 * @param blockTimeout time in ms until panicking that the chain has stopped producing blocks1182 * @returns1183 */1184 async newSessions(sessionCount = 1, blockTimeout = 60000): Promise<void> {1185 console.log(`Waiting for ${sessionCount} new session${sessionCount>1's'''}.`1186 + ' This might take a while -- check SessionPeriod in pallet_session::Config for session time.');11871188 const expectedSessionIndex = await (this.helper as DevUniqueHelper).session.getIndex() + sessionCount;1189 let currentSessionIndex = -1;11901191 while(currentSessionIndex < expectedSessionIndex) {1192 // eslint-disable-next-line no-async-promise-executor1193 currentSessionIndex = await this.withTimeout(new Promise(async (resolve) => {1194 await this.newBlocks(1);1195 const res = await (this.helper as DevUniqueHelper).session.getIndex();1196 resolve(res);1197 }), blockTimeout, 'The chain has stopped producing blocks!');1198 }1199 }12001201 async forParachainBlockNumber(blockNumber: bigint | number, timeout?: number) {1202 timeout = timeout ?? 30 * 60 * 1000;1203 // eslint-disable-next-line no-async-promise-executor1204 const promise = new Promise<void>(async (resolve) => {1205 const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads((data: any) => {1206 if(data.number.toNumber() >= blockNumber) {1207 unsubscribe();1208 resolve();1209 }1210 });1211 });1212 await this.waitWithTimeout(promise, timeout);1213 return promise;1214 }12151216 async forRelayBlockNumber(blockNumber: bigint | number, timeout?: number) {1217 timeout = timeout ?? 30 * 60 * 1000;1218 // eslint-disable-next-line no-async-promise-executor1219 const promise = new Promise<void>(async (resolve) => {1220 const unsubscribe = await this.helper.getApi().query.parachainSystem.validationData((data: any) => {1221 if(data.value.relayParentNumber.toNumber() >= blockNumber) {1222 // @ts-ignore1223 unsubscribe();1224 resolve();1225 }1226 });1227 });1228 await this.waitWithTimeout(promise, timeout);1229 return promise;1230 }12311232 noScheduledTasks() {1233 const api = this.helper.getApi();12341235 // eslint-disable-next-line no-async-promise-executor1236 const promise = new Promise<void>(async resolve => {1237 const unsubscribe = await api.rpc.chain.subscribeNewHeads(async () => {1238 const areThereScheduledTasks = await api.query.scheduler.lookup.entries();12391240 if(areThereScheduledTasks.length == 0) {1241 unsubscribe();1242 resolve();1243 }1244 });1245 });12461247 return promise;1248 }12491250 parachainBlockMultiplesOf(val: bigint) {1251 // eslint-disable-next-line no-async-promise-executor1252 const promise = new Promise<void>(async resolve => {1253 const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads((data: any) => {1254 if(data.number.toBigInt() % val == 0n) {1255 console.log(`from waiter: ${data.number.toBigInt()}`);1256 unsubscribe();1257 resolve();1258 }1259 });1260 });1261 return promise;1262 }12631264 event<T extends IEventHelper>(1265 maxBlocksToWait: number,1266 eventHelper: T,1267 filter: (_: any) => boolean = () => true,1268 ): any {1269 // eslint-disable-next-line no-async-promise-executor1270 const promise = new Promise<T | null>(async (resolve) => {1271 const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(async header => {1272 const blockNumber = header.number.toHuman();1273 const blockHash = header.hash;1274 const eventIdStr = `${eventHelper.section()}.${eventHelper.method()}`;1275 const waitLimitStr = `wait blocks remaining: ${maxBlocksToWait}`;12761277 this.helper.logger.log(`[Block #${blockNumber}] Waiting for event \`${eventIdStr}\` (${waitLimitStr})`);12781279 const apiAt = await this.helper.getApi().at(blockHash);1280 const eventRecords = (await apiAt.query.system.events()) as any;12811282 const neededEvent = eventRecords.toArray()1283 .filter((r: FrameSystemEventRecord) => r.event.section == eventHelper.section() && r.event.method == eventHelper.method())1284 .map((r: FrameSystemEventRecord) => eventHelper.wrapEvent(r.event.data))1285 .find(filter);12861287 if(neededEvent) {1288 unsubscribe();1289 resolve(neededEvent);1290 } else if(maxBlocksToWait > 0) {1291 maxBlocksToWait--;1292 } else {1293 this.helper.logger.log(`Eligible event \`${eventIdStr}\` is NOT found.1294 The wait lasted until block ${blockNumber} inclusive`);1295 unsubscribe();1296 resolve(null);1297 }1298 });1299 });1300 return promise;1301 }13021303 async expectEvent<T extends IEventHelper>(1304 maxBlocksToWait: number,1305 eventHelper: T,1306 filter: (e: any) => boolean = () => true,1307 ) {1308 const e = await this.event(maxBlocksToWait, eventHelper, filter);1309 if(e == null) {1310 throw Error(`The event '${eventHelper.section()}.${eventHelper.method()}' is expected`);1311 } else {1312 return e;1313 }1314 }1315}13161317class SessionGroup {1318 helper: ChainHelperBase;13191320 constructor(helper: ChainHelperBase) {1321 this.helper = helper;1322 }13231324 //todo:collator documentation1325 async getIndex(): Promise<number> {1326 return (await this.helper.callRpc('api.query.session.currentIndex', [])).toNumber();1327 }13281329 newSessions(sessionCount = 1, blockTimeout = 24000): Promise<void> {1330 return (this.helper as DevUniqueHelper).wait.newSessions(sessionCount, blockTimeout);1331 }13321333 setOwnKeys(signer: TSigner, key: string) {1334 return this.helper.executeExtrinsic(1335 signer,1336 'api.tx.session.setKeys',1337 [key, '0x0'],1338 true,1339 );1340 }13411342 setOwnKeysFromAddress(signer: TSigner) {1343 return this.setOwnKeys(signer, '0x' + Buffer.from(signer.addressRaw).toString('hex'));1344 }1345}13461347class TestUtilGroup {1348 helper: DevUniqueHelper;13491350 constructor(helper: DevUniqueHelper) {1351 this.helper = helper;1352 }13531354 async enable() {1355 if(this.helper.fetchMissingPalletNames([Pallets.TestUtils]).length != 0) {1356 return;1357 }13581359 const signer = this.helper.util.fromSeed('//Alice');1360 await this.helper.getSudo<DevUniqueHelper>().executeExtrinsic(signer, 'api.tx.testUtils.enable', [], true);1361 }13621363 async setTestValue(signer: TSigner, testVal: number) {1364 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.setTestValue', [testVal], true);1365 }13661367 async incTestValue(signer: TSigner) {1368 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.incTestValue', [], true);1369 }13701371 async setTestValueAndRollback(signer: TSigner, testVal: number) {1372 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.setTestValueAndRollback', [testVal], true);1373 }13741375 async testValue(blockIdx?: number) {1376 const api = blockIdx1377 ? await this.helper.getApi().at(await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockIdx]))1378 : this.helper.getApi();13791380 return (await api.query.testUtils.testValue()).toJSON();1381 }13821383 async justTakeFee(signer: TSigner) {1384 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.justTakeFee', [], true);1385 }13861387 async selfCancelingInc(signer: TSigner, scheduledId: string, maxTestVal: number) {1388 await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.selfCancelingInc', [scheduledId, maxTestVal], true);1389 }1390}13911392class EventCapture {1393 helper: DevUniqueHelper;1394 eventSection: string;1395 eventMethod: string;1396 events: EventRecord[] = [];1397 unsubscribe: VoidFn | null = null;13981399 constructor(1400 helper: DevUniqueHelper,1401 eventSection: string,1402 eventMethod: string,1403 ) {1404 this.helper = helper;1405 this.eventSection = eventSection;1406 this.eventMethod = eventMethod;1407 }14081409 async startCapture() {1410 this.stopCapture();1411 this.unsubscribe = (await this.helper.getApi().query.system.events((eventRecords: FrameSystemEventRecord[]) => {1412 const newEvents = eventRecords.filter(r => r.event.section == this.eventSection && r.event.method == this.eventMethod);14131414 this.events.push(...newEvents);1415 })) as any;1416 }14171418 stopCapture() {1419 if(this.unsubscribe !== null) {1420 this.unsubscribe();1421 }1422 }14231424 extractCapturedEvents() {1425 return this.events;1426 }1427}14281429class AdminGroup {1430 helper: UniqueHelper;14311432 constructor(helper: UniqueHelper) {1433 this.helper = helper;1434 }14351436 async payoutStakers(signer: IKeyringPair, stakersToPayout: number): Promise<{staker: string, stake: bigint, payout: bigint}[]> {1437 const payoutResult = await this.helper.executeExtrinsic(signer, 'api.tx.appPromotion.payoutStakers', [stakersToPayout], true);1438 return payoutResult.result.events.filter(e => e.event.method === 'StakingRecalculation').map(e => ({1439 staker: e.event.data[0].toString(),1440 stake: e.event.data[1].toBigInt(),1441 payout: e.event.data[2].toBigInt(),1442 }));1443 }1444}14451446// eslint-disable-next-line @typescript-eslint/naming-convention1447function ScheduledUniqueHelper<T extends UniqueHelperConstructor>(Base: T) {1448 return class extends Base {1449 scheduleFn: 'schedule' | 'scheduleAfter';1450 blocksNum: number;1451 options: ISchedulerOptions;14521453 constructor(...args: any[]) {1454 const logger = args[0] as ILogger;1455 const options = args[1] as {1456 scheduleFn: 'schedule' | 'scheduleAfter',1457 blocksNum: number,1458 options: ISchedulerOptions1459 };14601461 super(logger);14621463 this.scheduleFn = options.scheduleFn;1464 this.blocksNum = options.blocksNum;1465 this.options = options.options;1466 }14671468 executeExtrinsic(sender: IKeyringPair, scheduledExtrinsic: string, scheduledParams: any[], expectSuccess?: boolean): Promise<ITransactionResult> {1469 const scheduledTx = this.constructApiCall(scheduledExtrinsic, scheduledParams);14701471 const mandatorySchedArgs = [1472 this.blocksNum,1473 this.options.periodic ? [this.options.periodic.period, this.options.periodic.repetitions] : null,1474 this.options.priority ?? null,1475 scheduledTx,1476 ];14771478 let schedArgs;1479 let scheduleFn;14801481 if(this.options.scheduledId) {1482 schedArgs = [this.options.scheduledId!, ...mandatorySchedArgs];14831484 if(this.scheduleFn == 'schedule') {1485 scheduleFn = 'scheduleNamed';1486 } else if(this.scheduleFn == 'scheduleAfter') {1487 scheduleFn = 'scheduleNamedAfter';1488 }1489 } else {1490 schedArgs = mandatorySchedArgs;1491 scheduleFn = this.scheduleFn;1492 }14931494 const extrinsic = 'api.tx.scheduler.' + scheduleFn;14951496 return super.executeExtrinsic(1497 sender,1498 extrinsic as any,1499 schedArgs,1500 expectSuccess,1501 );1502 }1503 };1504}tests/src/xcm/lowLevelXcmUnique.test.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/xcm/lowLevelXcmUnique.test.ts
@@ -0,0 +1,775 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Unique Network is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+import {IKeyringPair} from '@polkadot/types/types';
+import config from '../config';
+import {itSub, expect, describeXCM, usingPlaygrounds, usingAcalaPlaygrounds, usingMoonbeamPlaygrounds, usingAstarPlaygrounds, usingPolkadexPlaygrounds} from '../util';
+import {DevUniqueHelper, Event} from '../util/playgrounds/unique.dev';
+import {nToBigInt} from '@polkadot/util';
+import {hexToString} from '@polkadot/util';
+
+const UNIQUE_CHAIN = +(process.env.RELAY_UNIQUE_ID || 2037);
+const ACALA_CHAIN = +(process.env.RELAY_ACALA_ID || 2000);
+const MOONBEAM_CHAIN = +(process.env.RELAY_MOONBEAM_ID || 2004);
+const ASTAR_CHAIN = +(process.env.RELAY_ASTAR_ID || 2006);
+const POLKADEX_CHAIN = +(process.env.RELAY_POLKADEX_ID || 2040);
+
+
+
+const acalaUrl = config.acalaUrl;
+const moonbeamUrl = config.moonbeamUrl;
+const astarUrl = config.astarUrl;
+const polkadexUrl = config.polkadexUrl;
+
+const ASTAR_DECIMALS = 18n;
+const UNQ_DECIMALS = 18n;
+
+const TRANSFER_AMOUNT = 2000000_000_000_000_000_000_000n;
+const SENDER_BUDGET = 2n * TRANSFER_AMOUNT;
+const SENDBACK_AMOUNT = TRANSFER_AMOUNT / 2n;
+const STAYED_ON_TARGET_CHAIN = TRANSFER_AMOUNT - SENDBACK_AMOUNT;
+const TARGET_CHAIN_TOKEN_TRANSFER_AMOUNT = 100_000_000_000n;
+
+const SAFE_XCM_VERSION = 2;
+const maxWaitBlocks = 6;
+
+const uniqueMultilocation = {
+ V2: {
+ parents: 1,
+ interior: {
+ X1: {
+ Parachain: UNIQUE_CHAIN,
+ },
+ },
+ },
+};
+
+let balanceUniqueTokenInit: bigint;
+let balanceUniqueTokenMiddle: bigint;
+let balanceUniqueTokenFinal: bigint;
+let unqFees: bigint;
+
+const expectFailedToTransact = async (helper: DevUniqueHelper, messageSent: any) => {
+ await helper.wait.expectEvent(maxWaitBlocks, Event.XcmpQueue.Fail, event => event.messageHash == messageSent.messageHash
+ && event.outcome.isFailedToTransactAsset);
+};
+const expectUntrustedReserveLocationFail = async (helper: DevUniqueHelper, messageSent: any) => {
+ await helper.wait.expectEvent(maxWaitBlocks, Event.XcmpQueue.Fail, event => event.messageHash == messageSent.messageHash
+ && event.outcome.isUntrustedReserveLocation);
+};
+
+const NETWORKS = {
+ acala: usingAcalaPlaygrounds,
+ astar: usingAstarPlaygrounds,
+ polkadex: usingPolkadexPlaygrounds,
+ moonbeam: usingMoonbeamPlaygrounds,
+} as const;
+
+function mapToChainId(networkName: keyof typeof NETWORKS) {
+ switch (networkName) {
+ case 'acala':
+ return ACALA_CHAIN;
+ case 'astar':
+ return ASTAR_CHAIN;
+ case 'moonbeam':
+ return MOONBEAM_CHAIN;
+ case 'polkadex':
+ return POLKADEX_CHAIN;
+ }
+}
+
+function mapToChainUrl(networkName: keyof typeof NETWORKS): string {
+ switch (networkName) {
+ case 'acala':
+ return acalaUrl;
+ case 'astar':
+ return astarUrl;
+ case 'moonbeam':
+ return moonbeamUrl;
+ case 'polkadex':
+ return polkadexUrl;
+ }
+}
+
+function getDevPlayground<T extends keyof typeof NETWORKS>(name: T) {
+ return NETWORKS[name];
+}
+
+
+async function genericSendUnqTo(
+ networkName: keyof typeof NETWORKS,
+ randomAccount: IKeyringPair,
+ randomAccountOnTargetChain = randomAccount,
+) {
+ const networkUrl = mapToChainUrl(networkName);
+ const targetPlayground = getDevPlayground(networkName);
+ await usingPlaygrounds(async (helper) => {
+ balanceUniqueTokenInit = await helper.balance.getSubstrate(randomAccount.address);
+ const destination = {
+ V2: {
+ parents: 1,
+ interior: {
+ X1: {
+ Parachain: mapToChainId(networkName),
+ },
+ },
+ },
+ };
+
+ const beneficiary = {
+ V2: {
+ parents: 0,
+ interior: {
+ X1: (
+ networkName == 'moonbeam' ?
+ {
+ AccountKey20: {
+ network: 'Any',
+ key: randomAccountOnTargetChain.address,
+ },
+ }
+ :
+ {
+ AccountId32: {
+ network: 'Any',
+ id: randomAccountOnTargetChain.addressRaw,
+ },
+ }
+ ),
+ },
+ },
+ };
+
+ const assets = {
+ V2: [
+ {
+ id: {
+ Concrete: {
+ parents: 0,
+ interior: 'Here',
+ },
+ },
+ fun: {
+ Fungible: TRANSFER_AMOUNT,
+ },
+ },
+ ],
+ };
+ const feeAssetItem = 0;
+
+ await helper.xcm.limitedReserveTransferAssets(randomAccount, destination, beneficiary, assets, feeAssetItem, 'Unlimited');
+ const messageSent = await helper.wait.expectEvent(maxWaitBlocks, Event.XcmpQueue.XcmpMessageSent);
+ balanceUniqueTokenMiddle = await helper.balance.getSubstrate(randomAccount.address);
+
+ unqFees = balanceUniqueTokenInit - balanceUniqueTokenMiddle - TRANSFER_AMOUNT;
+ console.log('[Unique -> %s] transaction fees on Unique: %s UNQ', networkName, helper.util.bigIntToDecimals(unqFees));
+ expect(unqFees > 0n, 'Negative fees UNQ, looks like nothing was transferred').to.be.true;
+
+ await targetPlayground(networkUrl, async (helper) => {
+ /*
+ Since only the parachain part of the Polkadex
+ infrastructure is launched (without their
+ solochain validators), processing incoming
+ assets will lead to an error.
+ This error indicates that the Polkadex chain
+ received a message from the Unique network,
+ since the hash is being checked to ensure
+ it matches what was sent.
+ */
+ if(networkName == 'polkadex') {
+ await helper.wait.expectEvent(maxWaitBlocks, Event.XcmpQueue.Fail, event => event.messageHash == messageSent.messageHash);
+ } else {
+ await helper.wait.expectEvent(maxWaitBlocks, Event.XcmpQueue.Success, event => event.messageHash == messageSent.messageHash);
+ }
+ });
+
+ });
+}
+
+async function genericSendUnqBack(
+ networkName: keyof typeof NETWORKS,
+ sudoer: IKeyringPair,
+ randomAccountOnUnq: IKeyringPair,
+) {
+ const networkUrl = mapToChainUrl(networkName);
+
+ const targetPlayground = getDevPlayground(networkName);
+ await usingPlaygrounds(async (helper) => {
+
+ const xcmProgram = helper.arrange.makeXcmProgramWithdrawDeposit(
+ randomAccountOnUnq.addressRaw,
+ {
+ Concrete: {
+ parents: 1,
+ interior: {
+ X1: {Parachain: UNIQUE_CHAIN},
+ },
+ },
+ },
+ SENDBACK_AMOUNT,
+ );
+
+ let xcmProgramSent: any;
+
+
+ await targetPlayground(networkUrl, async (helper) => {
+ if('getSudo' in helper) {
+ await helper.getSudo().xcm.send(sudoer, uniqueMultilocation, xcmProgram);
+ xcmProgramSent = await helper.wait.expectEvent(maxWaitBlocks, Event.XcmpQueue.XcmpMessageSent);
+ } else if('fastDemocracy' in helper) {
+ const xcmSend = helper.constructApiCall('api.tx.polkadotXcm.send', [uniqueMultilocation, xcmProgram]);
+ // Needed to bypass the call filter.
+ const batchCall = helper.encodeApiCall('api.tx.utility.batch', [[xcmSend]]);
+ await helper.fastDemocracy.executeProposal('sending MoonBeam -> Unique via XCM program', batchCall);
+ xcmProgramSent = await helper.wait.expectEvent(maxWaitBlocks, Event.XcmpQueue.XcmpMessageSent);
+ }
+ });
+
+ await helper.wait.expectEvent(maxWaitBlocks, Event.XcmpQueue.Success, event => event.messageHash == xcmProgramSent.messageHash);
+
+ balanceUniqueTokenFinal = await helper.balance.getSubstrate(randomAccountOnUnq.address);
+
+ expect(balanceUniqueTokenFinal).to.be.equal(balanceUniqueTokenInit - unqFees - STAYED_ON_TARGET_CHAIN);
+
+ });
+}
+
+async function genericSendOnlyOwnedBalance(
+ networkName: keyof typeof NETWORKS,
+ sudoer: IKeyringPair,
+) {
+ const networkUrl = mapToChainUrl(networkName);
+ const targetPlayground = getDevPlayground(networkName);
+
+ const targetChainBalance = 10000n * (10n ** UNQ_DECIMALS);
+
+ await usingPlaygrounds(async (helper) => {
+ const targetChainSovereignAccount = helper.address.paraSiblingSovereignAccount(mapToChainId(networkName));
+ await helper.getSudo().balance.setBalanceSubstrate(sudoer, targetChainSovereignAccount, targetChainBalance);
+ const moreThanTargetChainHas = 2n * targetChainBalance;
+
+ const targetAccount = helper.arrange.createEmptyAccount();
+
+ const maliciousXcmProgram = helper.arrange.makeXcmProgramWithdrawDeposit(
+ targetAccount.addressRaw,
+ {
+ Concrete: {
+ parents: 0,
+ interior: 'Here',
+ },
+ },
+ moreThanTargetChainHas,
+ );
+
+ let maliciousXcmProgramSent: any;
+
+
+ await targetPlayground(networkUrl, async (helper) => {
+ if('getSudo' in helper) {
+ await helper.getSudo().xcm.send(sudoer, uniqueMultilocation, maliciousXcmProgram);
+ maliciousXcmProgramSent = await helper.wait.expectEvent(maxWaitBlocks, Event.XcmpQueue.XcmpMessageSent);
+ } else if('fastDemocracy' in helper) {
+ const xcmSend = helper.constructApiCall('api.tx.polkadotXcm.send', [uniqueMultilocation, maliciousXcmProgram]);
+ // Needed to bypass the call filter.
+ const batchCall = helper.encodeApiCall('api.tx.utility.batch', [[xcmSend]]);
+ await helper.fastDemocracy.executeProposal('sending MoonBeam -> Unique via XCM program', batchCall);
+ maliciousXcmProgramSent = await helper.wait.expectEvent(maxWaitBlocks, Event.XcmpQueue.XcmpMessageSent);
+ }
+ });
+
+ await expectFailedToTransact(helper, maliciousXcmProgramSent);
+
+ const targetAccountBalance = await helper.balance.getSubstrate(targetAccount.address);
+ expect(targetAccountBalance).to.be.equal(0n);
+ });
+}
+
+async function genericReserveTransferUNQfrom(netwokrName: keyof typeof NETWORKS, sudoer: IKeyringPair) {
+ const networkUrl = mapToChainUrl(netwokrName);
+ const targetPlayground = getDevPlayground(netwokrName);
+
+ await usingPlaygrounds(async (helper) => {
+ const testAmount = 10_000n * (10n ** UNQ_DECIMALS);
+ const targetAccount = helper.arrange.createEmptyAccount();
+
+ const maliciousXcmProgramFullId = helper.arrange.makeXcmProgramReserveAssetDeposited(
+ targetAccount.addressRaw,
+ {
+ Concrete: {
+ parents: 1,
+ interior: {
+ X1: {
+ Parachain: UNIQUE_CHAIN,
+ },
+ },
+ },
+ },
+ testAmount,
+ );
+
+ const maliciousXcmProgramHereId = helper.arrange.makeXcmProgramReserveAssetDeposited(
+ targetAccount.addressRaw,
+ {
+ Concrete: {
+ parents: 0,
+ interior: 'Here',
+ },
+ },
+ testAmount,
+ );
+
+ let maliciousXcmProgramFullIdSent: any;
+ let maliciousXcmProgramHereIdSent: any;
+ const maxWaitBlocks = 3;
+
+ // Try to trick Unique using full UNQ identification
+ await targetPlayground(networkUrl, async (helper) => {
+ if('getSudo' in helper) {
+ await helper.getSudo().xcm.send(sudoer, uniqueMultilocation, maliciousXcmProgramFullId);
+ maliciousXcmProgramFullIdSent = await helper.wait.expectEvent(maxWaitBlocks, Event.XcmpQueue.XcmpMessageSent);
+ }
+ // Moonbeam case
+ else if('fastDemocracy' in helper) {
+ const xcmSend = helper.constructApiCall('api.tx.polkadotXcm.send', [uniqueMultilocation, maliciousXcmProgramFullId]);
+
+ // Needed to bypass the call filter.
+ const batchCall = helper.encodeApiCall('api.tx.utility.batch', [[xcmSend]]);
+ await helper.fastDemocracy.executeProposal('try to act like a reserve location for UNQ using path asset identification', batchCall);
+
+ maliciousXcmProgramFullIdSent = await helper.wait.expectEvent(maxWaitBlocks, Event.XcmpQueue.XcmpMessageSent);
+ }
+ });
+
+
+ await expectUntrustedReserveLocationFail(helper, maliciousXcmProgramFullIdSent);
+
+ let accountBalance = await helper.balance.getSubstrate(targetAccount.address);
+ expect(accountBalance).to.be.equal(0n);
+
+ // Try to trick Unique using shortened UNQ identification
+ await targetPlayground(networkUrl, async (helper) => {
+ if('getSudo' in helper) {
+ await helper.getSudo().xcm.send(sudoer, uniqueMultilocation, maliciousXcmProgramHereId);
+ maliciousXcmProgramHereIdSent = await helper.wait.expectEvent(maxWaitBlocks, Event.XcmpQueue.XcmpMessageSent);
+ }
+ else if('fastDemocracy' in helper) {
+ const xcmSend = helper.constructApiCall('api.tx.polkadotXcm.send', [uniqueMultilocation, maliciousXcmProgramHereId]);
+
+ // Needed to bypass the call filter.
+ const batchCall = helper.encodeApiCall('api.tx.utility.batch', [[xcmSend]]);
+ await helper.fastDemocracy.executeProposal('try to act like a reserve location for UNQ using "here" asset identification', batchCall);
+
+ maliciousXcmProgramHereIdSent = await helper.wait.expectEvent(maxWaitBlocks, Event.XcmpQueue.XcmpMessageSent);
+ }
+ });
+
+ await expectUntrustedReserveLocationFail(helper, maliciousXcmProgramHereIdSent);
+
+ accountBalance = await helper.balance.getSubstrate(targetAccount.address);
+ expect(accountBalance).to.be.equal(0n);
+ });
+}
+
+async function genericRejectNativeToknsFrom(netwokrName: keyof typeof NETWORKS, sudoerOnTargetChain: IKeyringPair) {
+ const networkUrl = mapToChainUrl(netwokrName);
+ const targetPlayground = getDevPlayground(netwokrName);
+ let messageSent: any;
+
+ await usingPlaygrounds(async (helper) => {
+ const maliciousXcmProgramFullId = helper.arrange.makeXcmProgramReserveAssetDeposited(
+ helper.arrange.createEmptyAccount().addressRaw,
+ {
+ Concrete: {
+ parents: 1,
+ interior: {
+ X1: {
+ Parachain: mapToChainId(netwokrName),
+ },
+ },
+ },
+ },
+ TARGET_CHAIN_TOKEN_TRANSFER_AMOUNT,
+ );
+ await targetPlayground(networkUrl, async (helper) => {
+ if('getSudo' in helper) {
+ await helper.getSudo().xcm.send(sudoerOnTargetChain, uniqueMultilocation, maliciousXcmProgramFullId);
+ messageSent = await helper.wait.expectEvent(maxWaitBlocks, Event.XcmpQueue.XcmpMessageSent);
+ } else if('fastDemocracy' in helper) {
+ const xcmSend = helper.constructApiCall('api.tx.polkadotXcm.send', [uniqueMultilocation, maliciousXcmProgramFullId]);
+
+ // Needed to bypass the call filter.
+ const batchCall = helper.encodeApiCall('api.tx.utility.batch', [[xcmSend]]);
+ await helper.fastDemocracy.executeProposal('sending native tokens to the Unique via fast democracy', batchCall);
+
+ messageSent = await helper.wait.expectEvent(maxWaitBlocks, Event.XcmpQueue.XcmpMessageSent);
+ }
+ });
+
+ await expectFailedToTransact(helper, messageSent);
+ });
+}
+
+
+describeXCM('[XCMLL] Integration test: Exchanging tokens with Acala', () => {
+ let alice: IKeyringPair;
+ let randomAccount: IKeyringPair;
+
+ before(async () => {
+ await usingPlaygrounds(async (helper, privateKey) => {
+ alice = await privateKey('//Alice');
+ console.log(config.acalaUrl);
+ randomAccount = helper.arrange.createEmptyAccount();
+
+ // Set the default version to wrap the first message to other chains.
+ await helper.getSudo().xcm.setSafeXcmVersion(alice, SAFE_XCM_VERSION);
+ });
+
+ await usingAcalaPlaygrounds(acalaUrl, async (helper) => {
+ const destination = {
+ V2: {
+ parents: 1,
+ interior: {
+ X1: {
+ Parachain: UNIQUE_CHAIN,
+ },
+ },
+ },
+ };
+
+ const metadata = {
+ name: 'Unique Network',
+ symbol: 'UNQ',
+ decimals: 18,
+ minimalBalance: 1250_000_000_000_000_000n,
+ };
+ const assets = (await (helper.callRpc('api.query.assetRegistry.assetMetadatas.entries'))).map(([_k, v] : [any, any]) =>
+ hexToString(v.toJSON()['symbol'])) as string[];
+
+ if(!assets.includes('UNQ')) {
+ await helper.getSudo().assetRegistry.registerForeignAsset(alice, destination, metadata);
+ } else {
+ console.log('UNQ token already registered on Acala assetRegistry pallet');
+ }
+ await helper.balance.transferToSubstrate(alice, randomAccount.address, 10000000000000n);
+ });
+
+ await usingPlaygrounds(async (helper) => {
+ await helper.balance.transferToSubstrate(alice, randomAccount.address, SENDER_BUDGET);
+ balanceUniqueTokenInit = await helper.balance.getSubstrate(randomAccount.address);
+ });
+ });
+
+ itSub('Should connect and send UNQ to Acala', async () => {
+ await genericSendUnqTo('acala', randomAccount);
+ });
+
+ itSub('Should connect to Acala and send UNQ back', async () => {
+ await genericSendUnqBack('acala', alice, randomAccount);
+ });
+
+ itSub('Acala can send only up to its balance', async () => {
+ await genericSendOnlyOwnedBalance('acala', alice);
+ });
+
+ itSub('Should not accept reserve transfer of UNQ from Acala', async () => {
+ await genericReserveTransferUNQfrom('acala', alice);
+ });
+});
+
+describeXCM('[XCMLL] Integration test: Exchanging tokens with Polkadex', () => {
+ let alice: IKeyringPair;
+ let randomAccount: IKeyringPair;
+
+ const uniqueAssetId = {
+ Concrete: {
+ parents: 1,
+ interior: {
+ X1: {
+ Parachain: UNIQUE_CHAIN,
+ },
+ },
+ },
+ };
+
+ before(async () => {
+ await usingPlaygrounds(async (helper, privateKey) => {
+ alice = await privateKey('//Alice');
+ randomAccount = helper.arrange.createEmptyAccount();
+
+ // Set the default version to wrap the first message to other chains.
+ await helper.getSudo().xcm.setSafeXcmVersion(alice, SAFE_XCM_VERSION);
+ });
+
+ await usingPolkadexPlaygrounds(polkadexUrl, async (helper) => {
+ const isWhitelisted = ((await helper.callRpc('api.query.xcmHelper.whitelistedTokens', []))
+ .toJSON() as [])
+ .map(nToBigInt).length != 0;
+ /*
+ Check whether the Unique token has been added
+ to the whitelist, since an error will occur
+ if it is added again. Needed for debugging
+ when this test is run multiple times.
+ */
+ if(!isWhitelisted) {
+ await helper.getSudo().xcmHelper.whitelistToken(alice, uniqueAssetId);
+ }
+
+ await helper.balance.transferToSubstrate(alice, randomAccount.address, 10000000000000n);
+ });
+
+ await usingPlaygrounds(async (helper) => {
+ await helper.balance.transferToSubstrate(alice, randomAccount.address, SENDER_BUDGET);
+ balanceUniqueTokenInit = await helper.balance.getSubstrate(randomAccount.address);
+ });
+ });
+
+ itSub('Should connect and send UNQ to Polkadex', async () => {
+ await genericSendUnqTo('polkadex', randomAccount);
+ });
+
+
+ itSub('Should connect to Polkadex and send UNQ back', async () => {
+ await genericSendUnqBack('polkadex', alice, randomAccount);
+ });
+
+ itSub('Polkadex can send only up to its balance', async () => {
+ await genericSendOnlyOwnedBalance('polkadex', alice);
+ });
+
+ itSub('Should not accept reserve transfer of UNQ from Polkadex', async () => {
+ await genericReserveTransferUNQfrom('polkadex', alice);
+ });
+});
+
+// These tests are relevant only when
+// the the corresponding foreign assets are not registered
+describeXCM('[XCMLL] Integration test: Unique rejects non-native tokens', () => {
+ let alice: IKeyringPair;
+
+ 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);
+ });
+ });
+
+ itSub('Unique rejects ACA tokens from Acala', async () => {
+ await genericRejectNativeToknsFrom('acala', alice);
+ });
+
+ itSub('Unique rejects GLMR tokens from Moonbeam', async () => {
+ await genericRejectNativeToknsFrom('moonbeam', alice);
+ });
+
+ itSub('Unique rejects ASTR tokens from Astar', async () => {
+ await genericRejectNativeToknsFrom('astar', alice);
+ });
+
+ itSub('Unique rejects PDX tokens from Polkadex', async () => {
+ await genericRejectNativeToknsFrom('polkadex', alice);
+ });
+});
+
+describeXCM('[XCMLL] Integration test: Exchanging UNQ with Moonbeam', () => {
+ // Unique constants
+ let alice: IKeyringPair;
+ let uniqueAssetLocation;
+
+ let randomAccountUnique: IKeyringPair;
+ let randomAccountMoonbeam: IKeyringPair;
+
+ // Moonbeam constants
+ let assetId: string;
+
+ const uniqueAssetMetadata = {
+ name: 'xcUnique',
+ symbol: 'xcUNQ',
+ decimals: 18,
+ isFrozen: false,
+ minimalBalance: 1n,
+ };
+
+
+ before(async () => {
+ await usingPlaygrounds(async (helper, privateKey) => {
+ alice = await privateKey('//Alice');
+ [randomAccountUnique] = await helper.arrange.createAccounts([0n], alice);
+
+
+ // Set the default version to wrap the first message to other chains.
+ await helper.getSudo().xcm.setSafeXcmVersion(alice, SAFE_XCM_VERSION);
+ });
+
+ await usingMoonbeamPlaygrounds(moonbeamUrl, async (helper) => {
+ const alithAccount = helper.account.alithAccount();
+ const baltatharAccount = helper.account.baltatharAccount();
+ const dorothyAccount = helper.account.dorothyAccount();
+
+ randomAccountMoonbeam = helper.account.create();
+
+ // >>> Sponsoring Dorothy >>>
+ console.log('Sponsoring Dorothy.......');
+ await helper.balance.transferToEthereum(alithAccount, dorothyAccount.address, 11_000_000_000_000_000_000n);
+ console.log('Sponsoring Dorothy.......DONE');
+ // <<< Sponsoring Dorothy <<<
+ uniqueAssetLocation = {
+ XCM: {
+ parents: 1,
+ interior: {X1: {Parachain: UNIQUE_CHAIN}},
+ },
+ };
+ const existentialDeposit = 1n;
+ const isSufficient = true;
+ const unitsPerSecond = 1n;
+ const numAssetsWeightHint = 0;
+
+ if((await helper.assetManager.assetTypeId(uniqueAssetLocation)).toJSON()) {
+ console.log('Unique asset already registered');
+ } else {
+ const encodedProposal = helper.assetManager.makeRegisterForeignAssetProposal({
+ location: uniqueAssetLocation,
+ metadata: uniqueAssetMetadata,
+ existentialDeposit,
+ isSufficient,
+ unitsPerSecond,
+ numAssetsWeightHint,
+ });
+
+ console.log('Encoded proposal for registerForeignAsset & setAssetUnitsPerSecond is %s', encodedProposal);
+
+ await helper.fastDemocracy.executeProposal('register UNQ foreign asset', encodedProposal);
+
+ // >>> Acquire Unique AssetId Info on Moonbeam >>>
+ console.log('Acquire Unique AssetId Info on Moonbeam.......');
+
+ assetId = (await helper.assetManager.assetTypeId(uniqueAssetLocation)).toString();
+
+ console.log('UNQ asset ID is %s', assetId);
+ console.log('Acquire Unique AssetId Info on Moonbeam.......DONE');
+ }
+ // >>> Acquire Unique AssetId Info on Moonbeam >>>
+
+ // >>> Sponsoring random Account >>>
+ console.log('Sponsoring random Account.......');
+ await helper.balance.transferToEthereum(baltatharAccount, randomAccountMoonbeam.address, 11_000_000_000_000_000_000n);
+ console.log('Sponsoring random Account.......DONE');
+ // <<< Sponsoring random Account <<<
+ });
+
+ await usingPlaygrounds(async (helper) => {
+ await helper.balance.transferToSubstrate(alice, randomAccountUnique.address, SENDER_BUDGET);
+ balanceUniqueTokenInit = await helper.balance.getSubstrate(randomAccountUnique.address);
+ });
+ });
+
+ itSub('Should connect and send UNQ to Moonbeam', async () => {
+ await genericSendUnqTo('moonbeam', randomAccountUnique, randomAccountMoonbeam);
+ });
+
+ itSub('Should connect to Moonbeam and send UNQ back', async () => {
+ await genericSendUnqBack('moonbeam', alice, randomAccountUnique);
+ });
+
+ itSub('Moonbeam can send only up to its balance', async () => {
+ await genericSendOnlyOwnedBalance('moonbeam', alice);
+ });
+
+ itSub('Should not accept reserve transfer of UNQ from Moonbeam', async () => {
+ await genericReserveTransferUNQfrom('moonbeam', alice);
+ });
+});
+
+describeXCM('[XCMLL] Integration test: Exchanging tokens with Astar', () => {
+ let alice: IKeyringPair;
+ let randomAccount: IKeyringPair;
+
+ const UNQ_ASSET_ID_ON_ASTAR = 1;
+ const UNQ_MINIMAL_BALANCE_ON_ASTAR = 1n;
+
+ // Unique -> Astar
+ const astarInitialBalance = 1n * (10n ** ASTAR_DECIMALS); // 1 ASTR, existential deposit required to actually create the account on Astar.
+ const unitsPerSecond = 228_000_000_000n; // This is Phala's value. What will be ours?
+
+ before(async () => {
+ await usingPlaygrounds(async (helper, privateKey) => {
+ alice = await privateKey('//Alice');
+ randomAccount = helper.arrange.createEmptyAccount();
+ await helper.balance.transferToSubstrate(alice, randomAccount.address, SENDER_BUDGET);
+ console.log('randomAccount', randomAccount.address);
+
+ // Set the default version to wrap the first message to other chains.
+ await helper.getSudo().xcm.setSafeXcmVersion(alice, SAFE_XCM_VERSION);
+ });
+
+ await usingAstarPlaygrounds(astarUrl, async (helper) => {
+ if(!(await helper.callRpc('api.query.assets.asset', [UNQ_ASSET_ID_ON_ASTAR])).toJSON()) {
+ console.log('1. Create foreign asset and metadata');
+ // TODO update metadata with values from production
+ await helper.assets.create(
+ alice,
+ UNQ_ASSET_ID_ON_ASTAR,
+ alice.address,
+ UNQ_MINIMAL_BALANCE_ON_ASTAR,
+ );
+
+ await helper.assets.setMetadata(
+ alice,
+ UNQ_ASSET_ID_ON_ASTAR,
+ 'Cross chain UNQ',
+ 'xcUNQ',
+ Number(UNQ_DECIMALS),
+ );
+
+ console.log('2. Register asset location on Astar');
+ const assetLocation = {
+ V2: {
+ parents: 1,
+ interior: {
+ X1: {
+ Parachain: UNIQUE_CHAIN,
+ },
+ },
+ },
+ };
+
+ await helper.getSudo().executeExtrinsic(alice, 'api.tx.xcAssetConfig.registerAssetLocation', [assetLocation, UNQ_ASSET_ID_ON_ASTAR]);
+
+ console.log('3. Set UNQ payment for XCM execution on Astar');
+ await helper.getSudo().executeExtrinsic(alice, 'api.tx.xcAssetConfig.setAssetUnitsPerSecond', [assetLocation, unitsPerSecond]);
+ }
+ console.log('4. Transfer 1 ASTR to recipient to create the account (needed due to existential balance)');
+ await helper.balance.transferToSubstrate(alice, randomAccount.address, astarInitialBalance);
+ });
+ });
+
+ itSub('Should connect and send UNQ to Astar', async () => {
+ await genericSendUnqTo('astar', randomAccount);
+ });
+
+ itSub('Should connect to Astar and send UNQ back', async () => {
+ await genericSendUnqBack('astar', alice, randomAccount);
+ });
+
+ itSub('Astar can send only up to its balance', async () => {
+ await genericSendOnlyOwnedBalance('astar', alice);
+ });
+
+ itSub('Should not accept reserve transfer of UNQ from Astar', async () => {
+ await genericReserveTransferUNQfrom('astar', alice);
+ });
+});
tests/src/xcm/xcmUnique.test.tsdiffbeforeafterboth--- a/tests/src/xcm/xcmUnique.test.ts
+++ b/tests/src/xcm/xcmUnique.test.ts
@@ -18,7 +18,7 @@
import config from '../config';
import {itSub, expect, describeXCM, usingPlaygrounds, usingAcalaPlaygrounds, usingRelayPlaygrounds, usingMoonbeamPlaygrounds, usingStatemintPlaygrounds, usingAstarPlaygrounds, usingPolkadexPlaygrounds} from '../util';
import {DevUniqueHelper, Event} from '../util/playgrounds/unique.dev';
-import {nToBigInt} from '@polkadot/util';
+import {hexToString, nToBigInt} from '@polkadot/util';
const UNIQUE_CHAIN = +(process.env.RELAY_UNIQUE_ID || 2037);
const STATEMINT_CHAIN = +(process.env.RELAY_STATEMINT_ID || 1000);
@@ -57,6 +57,18 @@
const SAFE_XCM_VERSION = 2;
const maxWaitBlocks = 6;
+
+const uniqueMultilocation = {
+ V2: {
+ parents: 1,
+ interior: {
+ X1: {
+ Parachain: UNIQUE_CHAIN,
+ },
+ },
+ },
+};
+
const expectFailedToTransact = async (helper: DevUniqueHelper, messageSent: any) => {
await helper.wait.expectEvent(maxWaitBlocks, Event.XcmpQueue.Fail, event => event.messageHash == messageSent.messageHash
&& event.outcome.isFailedToTransactAsset);
@@ -509,11 +521,15 @@
decimals: 18,
minimalBalance: 1250000000000000000n,
};
+ const assets = (await (helper.callRpc('api.query.assetRegistry.assetMetadatas.entries'))).map(([_k, v] : [any, any]) =>
+ hexToString(v.toJSON()['symbol'])) as string[];
- await helper.getSudo().assetRegistry.registerForeignAsset(alice, destination, metadata);
+ if(!assets.includes('UNQ')) {
+ await helper.getSudo().assetRegistry.registerForeignAsset(alice, destination, metadata);
+ } else {
+ console.log('UNQ token already registered on Acala assetRegistry pallet');
+ }
await helper.balance.transferToSubstrate(alice, randomAccount.address, 10000000000000n);
- balanceAcalaTokenInit = await helper.balance.getSubstrate(randomAccount.address);
- balanceUniqueForeignTokenInit = await helper.tokens.accounts(randomAccount.address, {ForeignAsset: 0});
});
await usingPlaygrounds(async (helper) => {
@@ -669,15 +685,6 @@
let targetAccountBalance = 0n;
const [targetAccount] = await helper.arrange.createAccounts([targetAccountBalance], alice);
-
- const uniqueMultilocation = {
- V2: {
- parents: 1,
- interior: {
- X1: {Parachain: UNIQUE_CHAIN},
- },
- },
- };
const maliciousXcmProgram = helper.arrange.makeXcmProgramWithdrawDeposit(
targetAccount.addressRaw,
@@ -925,15 +932,6 @@
itSub('Should connect to Polkadex and send UNQ back', async ({helper}) => {
- const uniqueMultilocation = {
- V2: {
- parents: 1,
- interior: {
- X1: {Parachain: UNIQUE_CHAIN},
- },
- },
- };
-
const xcmProgram = helper.arrange.makeXcmProgramWithdrawDeposit(
randomAccount.addressRaw,
{
@@ -971,15 +969,6 @@
const targetAccount = helper.arrange.createEmptyAccount();
- const uniqueMultilocation = {
- V2: {
- parents: 1,
- interior: {
- X1: {Parachain: UNIQUE_CHAIN},
- },
- },
- };
-
const maliciousXcmProgram = helper.arrange.makeXcmProgramWithdrawDeposit(
targetAccount.addressRaw,
{
@@ -1304,26 +1293,30 @@
const unitsPerSecond = 1n;
const numAssetsWeightHint = 0;
- const encodedProposal = helper.assetManager.makeRegisterForeignAssetProposal({
- location: uniqueAssetLocation,
- metadata: uniqueAssetMetadata,
- existentialDeposit,
- isSufficient,
- unitsPerSecond,
- numAssetsWeightHint,
- });
+ if((await helper.assetManager.assetTypeId(uniqueAssetLocation)).toJSON) {
+ console.log('Unique asset is already registered on MoonBeam');
+ } else {
+ const encodedProposal = helper.assetManager.makeRegisterForeignAssetProposal({
+ location: uniqueAssetLocation,
+ metadata: uniqueAssetMetadata,
+ existentialDeposit,
+ isSufficient,
+ unitsPerSecond,
+ numAssetsWeightHint,
+ });
- console.log('Encoded proposal for registerForeignAsset & setAssetUnitsPerSecond is %s', encodedProposal);
+ console.log('Encoded proposal for registerForeignAsset & setAssetUnitsPerSecond is %s', encodedProposal);
- await helper.fastDemocracy.executeProposal('register UNQ foreign asset', encodedProposal);
+ await helper.fastDemocracy.executeProposal('register UNQ foreign asset', encodedProposal);
- // >>> Acquire Unique AssetId Info on Moonbeam >>>
- console.log('Acquire Unique AssetId Info on Moonbeam.......');
+ // >>> Acquire Unique AssetId Info on Moonbeam >>>
+ console.log('Acquire Unique AssetId Info on Moonbeam.......');
- assetId = (await helper.assetManager.assetTypeId(uniqueAssetLocation)).toString();
+ assetId = (await helper.assetManager.assetTypeId(uniqueAssetLocation)).toString();
- console.log('UNQ asset ID is %s', assetId);
- console.log('Acquire Unique AssetId Info on Moonbeam.......DONE');
+ console.log('UNQ asset ID is %s', assetId);
+ console.log('Acquire Unique AssetId Info on Moonbeam.......DONE');
+ }
// >>> Acquire Unique AssetId Info on Moonbeam >>>
// >>> Sponsoring random Account >>>
@@ -1455,15 +1448,6 @@
let targetAccountBalance = 0n;
const [targetAccount] = await helper.arrange.createAccounts([targetAccountBalance], alice);
-
- const uniqueMultilocation = {
- V2: {
- parents: 1,
- interior: {
- X1: {Parachain: UNIQUE_CHAIN},
- },
- },
- };
const maliciousXcmProgram = helper.arrange.makeXcmProgramWithdrawDeposit(
targetAccount.addressRaw,
@@ -1527,17 +1511,6 @@
const testAmount = 10_000n * (10n ** UNQ_DECIMALS);
const [targetAccount] = await helper.arrange.createAccounts([0n], alice);
- const uniqueMultilocation = {
- V2: {
- parents: 1,
- interior: {
- X1: {
- Parachain: UNIQUE_CHAIN,
- },
- },
- },
- };
-
const maliciousXcmProgramFullId = helper.arrange.makeXcmProgramReserveAssetDeposited(
targetAccount.addressRaw,
{
@@ -1634,40 +1607,41 @@
});
await usingAstarPlaygrounds(astarUrl, async (helper) => {
- console.log('1. Create foreign asset and metadata');
- // TODO update metadata with values from production
- await helper.assets.create(
- alice,
- UNQ_ASSET_ID_ON_ASTAR,
- alice.address,
- UNQ_MINIMAL_BALANCE_ON_ASTAR,
- );
+ if(!(await helper.callRpc('api.query.assets.asset', [UNQ_ASSET_ID_ON_ASTAR])).toJSON()) {
+ console.log('1. Create foreign asset and metadata');
+ // TODO update metadata with values from production
+ await helper.assets.create(
+ alice,
+ UNQ_ASSET_ID_ON_ASTAR,
+ alice.address,
+ UNQ_MINIMAL_BALANCE_ON_ASTAR,
+ );
- await helper.assets.setMetadata(
- alice,
- UNQ_ASSET_ID_ON_ASTAR,
- 'Cross chain UNQ',
- 'xcUNQ',
- Number(UNQ_DECIMALS),
- );
+ await helper.assets.setMetadata(
+ alice,
+ UNQ_ASSET_ID_ON_ASTAR,
+ 'Cross chain UNQ',
+ 'xcUNQ',
+ Number(UNQ_DECIMALS),
+ );
- console.log('2. Register asset location on Astar');
- const assetLocation = {
- V2: {
- parents: 1,
- interior: {
- X1: {
- Parachain: UNIQUE_CHAIN,
+ console.log('2. Register asset location on Astar');
+ const assetLocation = {
+ V2: {
+ parents: 1,
+ interior: {
+ X1: {
+ Parachain: UNIQUE_CHAIN,
+ },
},
},
- },
- };
-
- await helper.getSudo().executeExtrinsic(alice, 'api.tx.xcAssetConfig.registerAssetLocation', [assetLocation, UNQ_ASSET_ID_ON_ASTAR]);
+ };
- console.log('3. Set UNQ payment for XCM execution on Astar');
- await helper.getSudo().executeExtrinsic(alice, 'api.tx.xcAssetConfig.setAssetUnitsPerSecond', [assetLocation, unitsPerSecond]);
+ await helper.getSudo().executeExtrinsic(alice, 'api.tx.xcAssetConfig.registerAssetLocation', [assetLocation, UNQ_ASSET_ID_ON_ASTAR]);
+ console.log('3. Set UNQ payment for XCM execution on Astar');
+ await helper.getSudo().executeExtrinsic(alice, 'api.tx.xcAssetConfig.setAssetUnitsPerSecond', [assetLocation, unitsPerSecond]);
+ }
console.log('4. Transfer 1 ASTR to recipient to create the account (needed due to existential balance)');
await helper.balance.transferToSubstrate(alice, randomAccount.address, astarInitialBalance);
});
@@ -1824,15 +1798,6 @@
let targetAccountBalance = 0n;
const [targetAccount] = await helper.arrange.createAccounts([targetAccountBalance], alice);
-
- const uniqueMultilocation = {
- V2: {
- parents: 1,
- interior: {
- X1: {Parachain: UNIQUE_CHAIN},
- },
- },
- };
const maliciousXcmProgram = helper.arrange.makeXcmProgramWithdrawDeposit(
targetAccount.addressRaw,
@@ -1887,17 +1852,6 @@
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 = {
- V2: {
- parents: 1,
- interior: {
- X1: {
- Parachain: UNIQUE_CHAIN,
- },
- },
- },
- };
const maliciousXcmProgramFullId = helper.arrange.makeXcmProgramReserveAssetDeposited(
targetAccount.addressRaw,