git.delta.rocks / unique-network / refs/commits / 5ae285ceffc0

difftreelog

source

tests/src/xcmTransferAcala.test.ts8.3 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import 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 ACALA_CHAIN = 2000;33const ACALA_PORT = '9946';34const TRANSFER_AMOUNT = 2000000000000000000000000n;3536describe('Integration test: Exchanging UNQ with Acala', () => {37  let alice: IKeyringPair;38  let randomAccount: IKeyringPair;3940  let actuallySent1: bigint;41  let actuallySent2: bigint;4243  let balanceUnique1: bigint;44  let balanceUnique2: bigint;45  let balanceUnique3: bigint;46  47  let balanceAcalaUnq1: bigint;48  let balanceAcalaUnq2: bigint;49  let balanceAcalaUnq3: bigint;5051  let balanceAcalaAca1: bigint;52  let balanceAcalaAca2: bigint;53  let balanceAcalaAca3: bigint;5455  before(async () => {56    await usingApi(async (api, privateKeyWrapper) => {57      alice = privateKeyWrapper('//Alice');58      randomAccount = generateKeyringPair();59    });6061    const acalaApiOptions: ApiOptions = {62      provider: new WsProvider('ws://127.0.0.1:' + ACALA_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: 'UNQ',80          symbol: 'UNQ',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        [balanceAcalaAca1] = await getBalance(api, [randomAccount.address]);97        {98          const {free} = (await api.query.tokens.accounts(alice.addressRaw, {ForeignAsset: 0})).toJSON() as any;99          balanceAcalaUnq1 = BigInt(free);100        }101      },102      acalaApiOptions,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      [balanceUnique1] = await getBalance(api, [randomAccount.address]);112    });113  });114115  it('Should connect and send UNQ to Acala', async () => {116117    await usingApi(async (api) => {118119      const destination = {120        V0: {121          X2: [122            'Parent',123            {124              Parachain: ACALA_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      [balanceUnique2] = await getBalance(api, [randomAccount.address]);169170      const transactionFees = balanceUnique1 - balanceUnique2 - TRANSFER_AMOUNT;171      actuallySent1 = TRANSFER_AMOUNT;  // Why not TRANSFER_AMOUNT - transactionFees ???172      console.log('Unique to Acala transaction fees on Unique: %s UNQ', transactionFees);173      expect(transactionFees > 0).to.be.true;174    });175176    await usingApi(177      async (api) => {178        // todo do something about instant sealing, where there might not be any new blocks179        await waitNewBlocks(api, 3);180        const {free} = (await api.query.tokens.accounts(randomAccount.addressRaw, {ForeignAsset: 0})).toJSON() as any;181        balanceAcalaUnq2 = BigInt(free);182        expect(balanceAcalaUnq2 > balanceAcalaUnq1).to.be.true;183184        [balanceAcalaAca2] = await getBalance(api, [randomAccount.address]);185186        const acaFees = balanceAcalaAca1 - balanceAcalaAca2;187        const unqFees = actuallySent1 - balanceAcalaUnq2 + balanceAcalaUnq1;188        console.log('Unique to Acala transaction fees on Acala: %s ACA', acaFees);189        console.log('Unique to Acala transaction fees on Acala: %s UNQ', unqFees);190        expect(acaFees == 0n).to.be.true;191        expect(unqFees == 0n).to.be.true;192      },193      {provider: new WsProvider('ws://127.0.0.1:' + ACALA_PORT)},194    );195  });196197  it('Should connect to Acala and send UNQ 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        [balanceAcalaAca3] = await getBalance(api, [randomAccount.address]);231        {232          const {free} = (await api.query.tokens.accounts(randomAccount.addressRaw, {ForeignAsset: 0})).toJSON() as any;233          balanceAcalaUnq3 = BigInt(free);234        }235236        const acaFees = balanceAcalaAca2 - balanceAcalaAca3;237        const unqFees = balanceAcalaUnq2 - balanceAcalaUnq3 - amount;238        actuallySent2 = amount;  // Why not amount - UNQFees ???239        console.log('Acala to Unique transaction fees on Acala: %s ACA', acaFees);240        console.log('Acala to Unique transaction fees on Acala: %s UNQ', unqFees);241        expect(acaFees > 0).to.be.true;242        expect(unqFees == 0n).to.be.true;243      },244      {provider: new WsProvider('ws://127.0.0.1:' + ACALA_PORT)},245    );246247    await usingApi(async (api) => {248      // todo do something about instant sealing, where there might not be any new blocks249      await waitNewBlocks(api, 3);250251      [balanceUnique3] = await getBalance(api, [randomAccount.address]);252      const actuallyDelivered = balanceUnique3 - balanceUnique2;253      expect(actuallyDelivered > 0).to.be.true;254255      const unqFees = actuallySent2 - actuallyDelivered;256      console.log('Acala to Unique transaction fees on Unique: %s UNQ', unqFees);257      expect(unqFees > 0).to.be.true;258    });259  });260});