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} from '../deprecated-helpers/helpers';25import waitNewBlocks from '../substrate/wait-new-blocks';26import getBalance from '../substrate/get-balance';2728chai.use(chaiAsPromised);29const expect = chai.expect;3031const UNIQUE_CHAIN = 1000;32const KARURA_CHAIN = 2000;33const KARURA_PORT = '9946';34const TRANSFER_AMOUNT = 2000000000000000000000000n;353637describe.skip('Integration test: Exchanging QTZ with Karura', () => {38 let alice: IKeyringPair;3940 before(async () => {41 await usingApi(async (api, privateKeyWrapper) => {42 alice = privateKeyWrapper('//Alice');43 });4445 const karuraApiOptions: ApiOptions = {46 provider: new WsProvider('ws://127.0.0.1:' + KARURA_PORT),47 };4849 await usingApi(async (api) => {50 const destination = {51 V0: {52 X2: [53 'Parent',54 {55 Parachain: UNIQUE_CHAIN,56 },57 ],58 },59 };6061 const metadata =62 {63 name: 'QTZ',64 symbol: 'QTZ',65 decimals: 18,66 minimalBalance: 1,67 };6869 const tx = api.tx.assetRegistry.registerForeignAsset(destination, metadata);70 const sudoTx = api.tx.sudo.sudo(tx as any);71 const events = await submitTransactionAsync(alice, sudoTx);72 const result = getGenericResult(events);73 expect(result.success).to.be.true;74 }, karuraApiOptions);75 });7677 it('Should connect and send QTZ to Karura', async () => {78 let balanceOnKaruraBefore: bigint;7980 await usingApi(async (api) => {81 const {free} = (await api.query.tokens.accounts(alice.addressRaw, {ForeignAssetId: 0})).toJSON() as any;82 balanceOnKaruraBefore = free;83 }, {provider: new WsProvider('ws://127.0.0.1:' + KARURA_PORT)});8485 await usingApi(async (api) => {86 const destination = {87 V0: {88 X2: [89 'Parent',90 {91 Parachain: KARURA_CHAIN,92 },93 ],94 },95 };9697 const beneficiary = {98 V0: {99 X1: {100 AccountId32: {101 network: 'Any',102 id: alice.addressRaw,103 },104 },105 },106 };107108 const assets = {109 V1: [110 {111 id: {112 Concrete: {113 parents: 0,114 interior: 'Here',115 },116 },117 fun: {118 Fungible: TRANSFER_AMOUNT,119 },120 },121 ],122 };123124 const feeAssetItem = 0;125126 const weightLimit = {127 Limited: 5000000000,128 };129130 const tx = api.tx.polkadotXcm.limitedReserveTransferAssets(destination, beneficiary, assets, feeAssetItem, weightLimit);131 const events = await submitTransactionAsync(alice, tx);132 const result = getGenericResult(events);133 expect(result.success).to.be.true;134 });135136 await usingApi(async (api) => {137 138 await waitNewBlocks(api, 3);139 const {free} = (await api.query.tokens.accounts(alice.addressRaw, {ForeignAssetId: 0})).toJSON() as any;140 expect(free > balanceOnKaruraBefore).to.be.true;141 }, {provider: new WsProvider('ws://127.0.0.1:' + KARURA_PORT)});142 });143144 it('Should connect to Karura and send QTZ back', async () => {145 let balanceBefore: bigint;146147 await usingApi(async (api) => {148 [balanceBefore] = await getBalance(api, [alice.address]);149 });150151 await usingApi(async (api) => {152 const destination = {153 V1: {154 parents: 1,155 interior: {156 X2: [157 {Parachain: UNIQUE_CHAIN},158 {AccountId32: {159 network: 'Any',160 id: alice.addressRaw,161 }},162 ],163 },164 },165 };166167 const id = {168 ForeignAssetId: 0,169 };170171 const amount = TRANSFER_AMOUNT;172 const destWeight = 50000000;173174 const tx = api.tx.xTokens.transfer(id, amount, destination, destWeight);175 const events = await submitTransactionAsync(alice, tx);176 const result = getGenericResult(events);177 expect(result.success).to.be.true;178 }, {provider: new WsProvider('ws://127.0.0.1:' + KARURA_PORT)});179180 await usingApi(async (api) => {181 182 await waitNewBlocks(api, 3);183 const [balanceAfter] = await getBalance(api, [alice.address]);184 expect(balanceAfter > balanceBefore).to.be.true;185 });186 });187});