1234567891011121314151617import chai from 'chai';18import chaiAsPromised from 'chai-as-promised';1920import {Keyring, WsProvider} from '@polkadot/api';21import {ApiOptions} from '@polkadot/api/types';22import {IKeyringPair} from '@polkadot/types/types';23import usingApi, {submitTransactionAsync} from './substrate/substrate-api';24import {getGenericResult, generateKeyringPair} from './util/helpers';25import {MultiLocation} from '@polkadot/types/interfaces';26import {blake2AsHex} from '@polkadot/util-crypto';27import getBalance from './substrate/get-balance';28import waitNewBlocks from './substrate/wait-new-blocks';2930chai.use(chaiAsPromised);31const expect = chai.expect;3233const QUARTZ_CHAIN = 5000;34const MOONRIVER_CHAIN = 1000;35const QUARTZ_PORT = '9944';36const MOONRIVER_PORT = '9946';37const TRANSFER_AMOUNT = 2000000000000000000000000n;3839describe('Integration test: Exchanging QTZ with Moonriver', () => {4041 42 let quartzAlice: IKeyringPair;43 let quartzAssetLocation;4445 let randomAccountQuartz: IKeyringPair;46 let randomAccountMoonriver: IKeyringPair;4748 49 let assetId: Uint8Array;5051 const moonriverKeyring = new Keyring({type: 'ethereum'});52 const alithPrivateKey = '0x5fb92d6e98884f76de468fa3f6278f8807c48bebc13595d45af5bdc4da702133';53 const baltatharPrivateKey = '0x8075991ce870b93a8870eca0c0f91913d12f47948ca0fd25b49c6fa7cdbeee8b';54 const dorothyPrivateKey = '0x39539ab1876910bbf3a223d84a29e28f1cb4e2e456503e7e91ed39b2e7223d68';5556 const alithAccount = moonriverKeyring.addFromUri(alithPrivateKey, undefined, 'ethereum');57 const baltatharAccount = moonriverKeyring.addFromUri(baltatharPrivateKey, undefined, 'ethereum');58 const dorothyAccount = moonriverKeyring.addFromUri(dorothyPrivateKey, undefined, 'ethereum');5960 const councilVotingThreshold = 2;61 const technicalCommitteeThreshold = 2;62 const votingPeriod = 3;63 const delayPeriod = 0;6465 const uniqueAssetMetadata = {66 name: 'xcQuartz',67 symbol: 'xcQTZ',68 decimals: 18,69 isFrozen: false,70 minimalBalance: 1,71 };7273 let actuallySent1: bigint;74 let actuallySent2: bigint;7576 let balanceQuartz1: bigint;77 let balanceQuartz2: bigint;78 let balanceQuartz3: bigint;79 80 let balanceMoonriverMovr1: bigint;81 let balanceMoonriverMovr2: bigint;82 let balanceMoonriverMovr3: bigint;8384 before(async () => {85 await usingApi(async (api, privateKeyWrapper) => {86 quartzAlice = privateKeyWrapper('//Alice');87 randomAccountQuartz = generateKeyringPair();88 randomAccountMoonriver = generateKeyringPair('ethereum');89 });9091 const moonriverApiOptions: ApiOptions = {92 provider: new WsProvider('ws://127.0.0.1:' + MOONRIVER_PORT),93 };9495 await usingApi(96 async (api) => {9798 99 const tx0 = api.tx.balances.transfer(dorothyAccount.address, 11_000_000_000_000_000_000n);100 const events0 = await submitTransactionAsync(alithAccount, tx0);101 const result0 = getGenericResult(events0);102 expect(result0.success).to.be.true;103 104105 const sourceLocation: MultiLocation = api.createType(106 'MultiLocation',107 {108 parents: 1,109 interior: {X1: {Parachain: QUARTZ_CHAIN}},110 },111 );112113 assetId = api.registry.hash(sourceLocation.toU8a()).slice(0, 16).reverse();114 console.log('Internal asset ID is %s', assetId);115 quartzAssetLocation = {XCM: sourceLocation};116 const existentialDeposit = 1;117 const isSufficient = true;118 const unitsPerSecond = '1';119 const numAssetsWeightHint = 0;120121 const registerTx = api.tx.assetManager.registerForeignAsset(122 quartzAssetLocation,123 uniqueAssetMetadata,124 existentialDeposit,125 isSufficient,126 );127 console.log('Encoded proposal for registerAsset is %s', registerTx.method.toHex() || '');128129 const setUnitsTx = api.tx.assetManager.setAssetUnitsPerSecond(130 quartzAssetLocation,131 unitsPerSecond,132 numAssetsWeightHint,133 );134 console.log('Encoded proposal for setAssetUnitsPerSecond is %s', setUnitsTx.method.toHex() || '');135136 const batchCall = api.tx.utility.batchAll([registerTx, setUnitsTx]);137 console.log('Encoded proposal for batchCall is %s', batchCall.method.toHex() || '');138139 140 const encodedProposal = batchCall?.method.toHex() || '';141 const proposalHash = blake2AsHex(encodedProposal);142 console.log('Encoded proposal for batch utility after schedule is %s', encodedProposal);143 console.log('Encoded proposal hash for batch utility after schedule is %s', proposalHash);144 console.log('Encoded length %d', encodedProposal.length);145146 const tx1 = api.tx.democracy.notePreimage(encodedProposal);147 const events1 = await submitTransactionAsync(baltatharAccount, tx1);148 const result1 = getGenericResult(events1);149 expect(result1.success).to.be.true;150 151152 153 const externalMotion = api.tx.democracy.externalProposeMajority(proposalHash);154 const tx2 = api.tx.councilCollective.propose(155 councilVotingThreshold,156 externalMotion,157 externalMotion.encodedLength,158 );159 const events2 = await submitTransactionAsync(baltatharAccount, tx2);160 const result2 = getGenericResult(events2);161 expect(result2.success).to.be.true;162163 const encodedMotion = externalMotion?.method.toHex() || '';164 const motionHash = blake2AsHex(encodedMotion);165 console.log('Motion hash is %s', motionHash);166167 const tx3 = api.tx.councilCollective.vote(motionHash, 0, true);168 {169 const events3 = await submitTransactionAsync(dorothyAccount, tx3);170 const result3 = getGenericResult(events3);171 expect(result3.success).to.be.true;172 }173 {174 const events3 = await submitTransactionAsync(baltatharAccount, tx3);175 const result3 = getGenericResult(events3);176 expect(result3.success).to.be.true;177 }178179 const tx4 = api.tx.councilCollective.close(motionHash, 0, 1_000_000_000, externalMotion.encodedLength);180 const events4 = await submitTransactionAsync(dorothyAccount, tx4);181 const result4 = getGenericResult(events4);182 expect(result4.success).to.be.true;183 184185 186 const fastTrack = api.tx.democracy.fastTrack(proposalHash, votingPeriod, delayPeriod);187 const tx5 = api.tx.techCommitteeCollective.propose(188 technicalCommitteeThreshold,189 fastTrack,190 fastTrack.encodedLength,191 );192 const events5 = await submitTransactionAsync(alithAccount, tx5);193 const result5 = getGenericResult(events5);194 expect(result5.success).to.be.true;195196 const encodedFastTrack = fastTrack?.method.toHex() || '';197 const fastTrackHash = blake2AsHex(encodedFastTrack);198 console.log('FastTrack hash is %s', fastTrackHash);199200 const proposalIdx = Number(await api.query.techCommitteeCollective.proposalCount()) - 1;201 const tx6 = api.tx.techCommitteeCollective.vote(fastTrackHash, proposalIdx, true);202 {203 const events6 = await submitTransactionAsync(baltatharAccount, tx6);204 const result6 = getGenericResult(events6);205 expect(result6.success).to.be.true;206 }207 {208 const events6 = await submitTransactionAsync(alithAccount, tx6);209 const result6 = getGenericResult(events6);210 expect(result6.success).to.be.true;211 }212213 const tx7 = api.tx.techCommitteeCollective214 .close(fastTrackHash, proposalIdx, 1_000_000_000, fastTrack.encodedLength);215 const events7 = await submitTransactionAsync(baltatharAccount, tx7);216 const result7 = getGenericResult(events7);217 expect(result7.success).to.be.true;218 219220 221 const tx8 = api.tx.democracy.vote(222 0,223 {Standard: {balance: 10_000_000_000_000_000_000n, vote: {aye: true, conviction: 1}}},224 );225 const events8 = await submitTransactionAsync(dorothyAccount, tx8);226 const result8 = getGenericResult(events8);227 expect(result8.success).to.be.true;228 229230 231 const tx9 = api.tx.balances.transfer(randomAccountMoonriver.address, 11_000_000_000_000_000_000n);232 const events9 = await submitTransactionAsync(baltatharAccount, tx9);233 const result9 = getGenericResult(events9);234 expect(result9.success).to.be.true;235 236237 [balanceMoonriverMovr1] = await getBalance(api, [randomAccountMoonriver.address]);238 },239 moonriverApiOptions,240 );241242 await usingApi(async (api) => {243 const tx0 = api.tx.balances.transfer(randomAccountQuartz.address, 10n * TRANSFER_AMOUNT);244 const events0 = await submitTransactionAsync(quartzAlice, tx0);245 const result0 = getGenericResult(events0);246 expect(result0.success).to.be.true;247248 [balanceQuartz1] = await getBalance(api, [randomAccountQuartz.address]);249 });250 });251252 it('Should connect and send QTZ to Moonriver', async () => {253 await usingApi(async (api) => {254 const currencyId = {255 NativeAssetId: 'Here',256 };257 const dest = {258 V1: {259 parents: 1,260 interior: {261 X2: [262 {Parachain: MOONRIVER_CHAIN},263 {AccountKey20: {network: 'Any', key: randomAccountMoonriver.address}},264 ],265 },266 },267 };268 const amount = TRANSFER_AMOUNT;269 const destWeight = 50000000;270271 const tx = api.tx.xTokens.transfer(currencyId, amount, dest, destWeight);272 const events = await submitTransactionAsync(quartzAlice, tx);273 const result = getGenericResult(events);274 expect(result.success).to.be.true;275276 [balanceQuartz2] = await getBalance(api, [randomAccountQuartz.address]);277 expect(balanceQuartz2 < balanceQuartz1).to.be.true;278279 const transactionFees = balanceQuartz1 - balanceQuartz2 - TRANSFER_AMOUNT;280 actuallySent1 = TRANSFER_AMOUNT; 281 console.log('Quartz to Moonriver transaction fees on Quartz: %s QTZ', transactionFees);282 expect(transactionFees > 0).to.be.true;283 });284285 await usingApi(286 async (api) => {287 288 await waitNewBlocks(api, 3);289290 [balanceMoonriverMovr2] = await getBalance(api, [randomAccountMoonriver.address]);291292 const movrFees = balanceMoonriverMovr1 - balanceMoonriverMovr2;293 console.log('Quartz to Moonriver transaction fees on Moonriver: %s MOVR', movrFees);294 expect(movrFees == 0n).to.be.true;295 },296 {provider: new WsProvider('ws://127.0.0.1:' + MOONRIVER_PORT)},297 );298 });299300 it('Should connect to Moonriver and send QTZ back', async () => {301 await usingApi(302 async (api) => {303 const amount = TRANSFER_AMOUNT / 2n;304 const asset = {305 V1: {306 id: {307 Concrete: {308 parents: 1,309 interior: {310 X1: {Parachain: QUARTZ_CHAIN},311 },312 },313 },314 fun: {315 Fungible: amount,316 },317 },318 };319 const destination = {320 V1: {321 parents: 1,322 interior: {323 X2: [324 {Parachain: QUARTZ_CHAIN},325 {AccountId32: {network: 'Any', id: randomAccountQuartz.addressRaw}},326 ],327 },328 },329 };330 const destWeight = 50000000;331332 const tx = api.tx.xTokens.transferMultiasset(asset, destination, destWeight);333 const events = await submitTransactionAsync(randomAccountMoonriver, tx);334 const result = getGenericResult(events);335 expect(result.success).to.be.true;336337 [balanceMoonriverMovr3] = await getBalance(api, [randomAccountMoonriver.address]);338339 const movrFees = balanceMoonriverMovr2 - balanceMoonriverMovr3;340 actuallySent2 = amount;341 console.log('Moonriver to Quartz transaction fees on Moonriver: %s MOVR', movrFees);342 expect(movrFees > 0).to.be.true;343 },344 {provider: new WsProvider('ws://127.0.0.1:' + MOONRIVER_PORT)},345 );346347 await usingApi(async (api) => {348 349 await waitNewBlocks(api, 3);350351 [balanceQuartz3] = await getBalance(api, [randomAccountQuartz.address]);352 const actuallyDelivered = balanceQuartz3 - balanceQuartz2;353 expect(actuallyDelivered > 0).to.be.true;354355 const qtzFees = actuallySent2 - actuallyDelivered;356 console.log('Moonriver to Quartz transaction fees on Quartz: %s QTZ', qtzFees);357 expect(qtzFees > 0).to.be.true;358 });359 });360});