1234567891011121314151617import chai from 'chai';18import chaiAsPromised from 'chai-as-promised';1920import {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 waitNewBlocks from './substrate/wait-new-blocks';26import getBalance from './substrate/get-balance';2728chai.use(chaiAsPromised);29const expect = chai.expect;3031const UNIQUE_CHAIN = 5000;32const KARURA_CHAIN = 2000;33const KARURA_PORT = '9946';34const TRANSFER_AMOUNT = 2000000000000000000000000n;3536describe('Integration test: Exchanging QTZ with Karura', () => {37 let alice: IKeyringPair;38 let randomAccount: IKeyringPair;3940 let actuallySent1: bigint;41 let actuallySent2: bigint;4243 let balanceQuartz1: bigint;44 let balanceQuartz2: bigint;45 let balanceQuartz3: bigint;4647 let balanceKaruraQtz1: bigint;48 let balanceKaruraQtz2: bigint;49 let balanceKaruraQtz3: bigint;5051 let balanceKaruraKar1: bigint;52 let balanceKaruraKar2: bigint;53 let balanceKaruraKar3: bigint;5455 before(async () => {56 await usingApi(async (api, privateKeyWrapper) => {57 alice = privateKeyWrapper('//Alice');58 randomAccount = generateKeyringPair();59 });6061 const karuraApiOptions: ApiOptions = {62 provider: new WsProvider('ws://127.0.0.1:' + KARURA_PORT),63 };6465 await usingApi(66 async (api) => {67 const destination = {68 V0: {69 X2: [70 'Parent',71 {72 Parachain: UNIQUE_CHAIN,73 },74 ],75 },76 };7778 const metadata = {79 name: 'QTZ',80 symbol: 'QTZ',81 decimals: 18,82 minimalBalance: 1,83 };8485 const tx = api.tx.assetRegistry.registerForeignAsset(destination, metadata);86 const sudoTx = api.tx.sudo.sudo(tx as any);87 const events = await submitTransactionAsync(alice, sudoTx);88 const result = getGenericResult(events);89 expect(result.success).to.be.true;9091 const tx1 = api.tx.balances.transfer(randomAccount.address, 10000000000000n);92 const events1 = await submitTransactionAsync(alice, tx1);93 const result1 = getGenericResult(events1);94 expect(result1.success).to.be.true;9596 [balanceKaruraKar1] = await getBalance(api, [randomAccount.address]);97 {98 const {free} = (await api.query.tokens.accounts(alice.addressRaw, {ForeignAsset: 0})).toJSON() as any;99 balanceKaruraQtz1 = BigInt(free);100 }101 },102 karuraApiOptions,103 );104105 await usingApi(async (api) => {106 const tx0 = api.tx.balances.transfer(randomAccount.address, 10n * TRANSFER_AMOUNT);107 const events0 = await submitTransactionAsync(alice, tx0);108 const result0 = getGenericResult(events0);109 expect(result0.success).to.be.true;110111 [balanceQuartz1] = await getBalance(api, [randomAccount.address]);112 });113 });114115 it('Should connect and send QTZ to Karura', async () => {116117 await usingApi(async (api) => {118119 const destination = {120 V0: {121 X2: [122 'Parent',123 {124 Parachain: KARURA_CHAIN,125 },126 ],127 },128 };129130 const beneficiary = {131 V0: {132 X1: {133 AccountId32: {134 network: 'Any',135 id: randomAccount.addressRaw,136 },137 },138 },139 };140141 const assets = {142 V1: [143 {144 id: {145 Concrete: {146 parents: 0,147 interior: 'Here',148 },149 },150 fun: {151 Fungible: TRANSFER_AMOUNT,152 },153 },154 ],155 };156157 const feeAssetItem = 0;158159 const weightLimit = {160 Limited: 5000000000,161 };162163 const tx = api.tx.polkadotXcm.limitedReserveTransferAssets(destination, beneficiary, assets, feeAssetItem, weightLimit);164 const events = await submitTransactionAsync(randomAccount, tx);165 const result = getGenericResult(events);166 expect(result.success).to.be.true;167168 [balanceQuartz2] = await getBalance(api, [randomAccount.address]);169170 const transactionFees = balanceQuartz1 - balanceQuartz2 - TRANSFER_AMOUNT;171 actuallySent1 = TRANSFER_AMOUNT; 172 console.log('Quartz to Karura transaction fees on Quartz: %s QTZ', transactionFees);173 expect(transactionFees > 0).to.be.true;174 });175176 await usingApi(177 async (api) => {178 179 await waitNewBlocks(api, 3);180 const {free} = (await api.query.tokens.accounts(randomAccount.addressRaw, {ForeignAsset: 0})).toJSON() as any;181 balanceKaruraQtz2 = BigInt(free);182 expect(balanceKaruraQtz2 > balanceKaruraQtz1).to.be.true;183184 [balanceKaruraKar2] = await getBalance(api, [randomAccount.address]);185186 const karFees = balanceKaruraKar1 - balanceKaruraKar2;187 const qtzFees = actuallySent1 - balanceKaruraQtz2 + balanceKaruraQtz1;188 console.log('Quartz to Karura transaction fees on Karura: %s KAR', karFees);189 console.log('Quartz to Karura transaction fees on Karura: %s QTZ', qtzFees);190 expect(karFees == 0n).to.be.true;191 expect(qtzFees == 0n).to.be.true;192 },193 {provider: new WsProvider('ws://127.0.0.1:' + KARURA_PORT)},194 );195 });196197 it('Should connect to Karura and send QTZ back', async () => {198199 await usingApi(200 async (api) => {201 const destination = {202 V1: {203 parents: 1,204 interior: {205 X2: [206 {Parachain: UNIQUE_CHAIN},207 {208 AccountId32: {209 network: 'Any',210 id: randomAccount.addressRaw,211 },212 },213 ],214 },215 },216 };217218 const id = {219 ForeignAsset: 0,220 };221222 const amount = TRANSFER_AMOUNT;223 const destWeight = 50000000;224225 const tx = api.tx.xTokens.transfer(id, amount, destination, destWeight);226 const events = await submitTransactionAsync(randomAccount, tx);227 const result = getGenericResult(events);228 expect(result.success).to.be.true;229230 [balanceKaruraKar3] = await getBalance(api, [randomAccount.address]);231 {232 const {free} = (await api.query.tokens.accounts(randomAccount.addressRaw, {ForeignAsset: 0})).toJSON() as any;233 balanceKaruraQtz3 = BigInt(free);234 }235236 const karFees = balanceKaruraKar2 - balanceKaruraKar3;237 const qtzFees = balanceKaruraQtz2 - balanceKaruraQtz3 - amount;238 actuallySent2 = amount; 239 console.log('Karura to Quartz transaction fees on Karura: %s KAR', karFees);240 console.log('Karura to Quartz transaction fees on Karura: %s QTZ', qtzFees);241 expect(karFees > 0).to.be.true;242 expect(qtzFees == 0n).to.be.true;243 },244 {provider: new WsProvider('ws://127.0.0.1:' + KARURA_PORT)},245 );246247 await usingApi(async (api) => {248 249 await waitNewBlocks(api, 3);250251 [balanceQuartz3] = await getBalance(api, [randomAccount.address]);252 const actuallyDelivered = balanceQuartz3 - balanceQuartz2;253 expect(actuallyDelivered > 0).to.be.true;254255 const qtzFees = actuallySent2 - actuallyDelivered;256 console.log('Karura to Quartz transaction fees on Quartz: %s QTZ', qtzFees);257 expect(qtzFees > 0).to.be.true;258 });259 });260});