git.delta.rocks / unique-network / refs/commits / d2b5cc3bc7de

difftreelog

source

tests/src/xcm/xcmTransferAcala.test.ts8.4 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;3031let UNIQUE_CHAIN = 0;32let ACALA_CHAIN = 0;3334// parse parachain id numbers35process.argv.forEach((val) => {3637  const ai = val.indexOf('acalaId=');38  const ui = val.indexOf('uniqueId=');39  if (ai != -1)40  {41    ACALA_CHAIN = Number(val.substring('acalaId='.length));42  }43  if (ui != -1)44  {45    UNIQUE_CHAIN = Number(val.substring('uniqueId='.length));46  }47});4849const ACALA_PORT = '9946';50const TRANSFER_AMOUNT = 2000000000000000000000000n;5152describe('Integration test: Exchanging UNQ with Acala', () => {53  let alice: IKeyringPair;54  let randomAccount: IKeyringPair;5556  let balanceUniqueTokenBefore: bigint;57  let balanceUniqueTokenAfter: bigint;58  let balanceUniqueTokenFinal: bigint;59  let balanceAcalaTokenBefore: bigint;60  let balanceAcalaTokenAfter: bigint;61  let balanceAcalaTokenFinal: bigint;62  let balanceUniqueForeignTokenAfter: bigint;63  let balanceUniqueForeignTokenBefore: bigint;64  let balanceUniqueForeignTokenFinal: bigint;6566  before(async () => {67    await usingApi(async (api, privateKeyWrapper) => {68      alice = privateKeyWrapper('//Alice');69      randomAccount = generateKeyringPair();70    });7172    const acalaApiOptions: ApiOptions = {73      provider: new WsProvider('ws://127.0.0.1:' + ACALA_PORT),74    };7576    await usingApi(77      async (api) => {78        const destination = {79          V0: {80            X2: [81              'Parent',82              {83                Parachain: UNIQUE_CHAIN,84              },85            ],86          },87        };8889        const metadata = {90          name: 'UNQ',91          symbol: 'UNQ',92          decimals: 18,93          minimalBalance: 1,94        };9596        const tx = api.tx.assetRegistry.registerForeignAsset(destination, metadata);97        const sudoTx = api.tx.sudo.sudo(tx as any);98        const events = await submitTransactionAsync(alice, sudoTx);99        const result = getGenericResult(events);100        expect(result.success).to.be.true;101102        const tx1 = api.tx.balances.transfer(randomAccount.address, 10000000000000n);103        const events1 = await submitTransactionAsync(alice, tx1);104        const result1 = getGenericResult(events1);105        expect(result1.success).to.be.true;106107        [balanceAcalaTokenBefore] = await getBalance(api, [randomAccount.address]);108        {109          const {free} = (await api.query.tokens.accounts(alice.addressRaw, {ForeignAsset: 0})).toJSON() as any;110          balanceUniqueForeignTokenBefore = BigInt(free);111        }112      },113      acalaApiOptions,114    );115116    await usingApi(async (api) => {117      const tx0 = api.tx.balances.transfer(randomAccount.address, 10n * TRANSFER_AMOUNT);118      const events0 = await submitTransactionAsync(alice, tx0);119      const result0 = getGenericResult(events0);120      expect(result0.success).to.be.true;121122      [balanceUniqueTokenBefore] = await getBalance(api, [randomAccount.address]);123    });124  });125126  it('Should connect and send UNQ to Acala', async () => {127128    await usingApi(async (api) => {129130      const destination = {131        V0: {132          X2: [133            'Parent',134            {135              Parachain: ACALA_CHAIN,136            },137          ],138        },139      };140141      const beneficiary = {142        V0: {143          X1: {144            AccountId32: {145              network: 'Any',146              id: randomAccount.addressRaw,147            },148          },149        },150      };151152      const assets = {153        V1: [154          {155            id: {156              Concrete: {157                parents: 0,158                interior: 'Here',159              },160            },161            fun: {162              Fungible: TRANSFER_AMOUNT,163            },164          },165        ],166      };167168      const feeAssetItem = 0;169170      const weightLimit = {171        Limited: 5000000000,172      };173174      const tx = api.tx.polkadotXcm.limitedReserveTransferAssets(destination, beneficiary, assets, feeAssetItem, weightLimit);175      const events = await submitTransactionAsync(randomAccount, tx);176      const result = getGenericResult(events);177      expect(result.success).to.be.true;178179      [balanceUniqueTokenAfter] = await getBalance(api, [randomAccount.address]);180181      expect((balanceUniqueTokenBefore - balanceUniqueTokenAfter) > 0n).to.be.true;182    });183184    await usingApi(185      async (api) => {186        // todo do something about instant sealing, where there might not be any new blocks187        await waitNewBlocks(api, 3);188        const {free} = (await api.query.tokens.accounts(randomAccount.addressRaw, {ForeignAsset: 0})).toJSON() as any;189        balanceUniqueForeignTokenAfter = BigInt(free);190191        [balanceAcalaTokenAfter] = await getBalance(api, [randomAccount.address]);192        const acaFees = balanceAcalaTokenBefore - balanceAcalaTokenAfter;193        const unqFees = balanceUniqueForeignTokenBefore - balanceUniqueForeignTokenAfter;194        console.log('Unique to Acala transaction fees on Acala: %s ACA', acaFees);195        console.log('Unique to Acala transaction fees on Acala: %s UNQ', unqFees);196        expect(acaFees == 0n).to.be.true;197        expect(unqFees == 0n).to.be.true;198      },199      {provider: new WsProvider('ws://127.0.0.1:' + ACALA_PORT)},200    );201  });202203  it('Should connect to Acala and send UNQ back', async () => {204205    await usingApi(206      async (api) => {207        const destination = {208          V1: {209            parents: 1,210            interior: {211              X2: [212                {Parachain: UNIQUE_CHAIN},213                {214                  AccountId32: {215                    network: 'Any',216                    id: randomAccount.addressRaw,217                  },218                },219              ],220            },221          },222        };223224        const id = {225          ForeignAsset: 0,226        };227228        const amount = TRANSFER_AMOUNT;229        const destWeight = 50000000;230231        const tx = api.tx.xTokens.transfer(id, amount, destination, destWeight);232        const events = await submitTransactionAsync(randomAccount, tx);233        const result = getGenericResult(events);234        expect(result.success).to.be.true;235236        [balanceAcalaTokenFinal] = await getBalance(api, [randomAccount.address]);237        {238          const {free} = (await api.query.tokens.accounts(randomAccount.addressRaw, {ForeignAsset: 0})).toJSON() as any;239          balanceUniqueForeignTokenFinal = BigInt(free);240        }241242        const acaFees = balanceAcalaTokenFinal - balanceAcalaTokenAfter;243        const unqFees = balanceUniqueForeignTokenFinal - balanceUniqueForeignTokenAfter;244        console.log('Acala to Unique transaction fees on Acala: %s ACA', acaFees);245        console.log('Acala to Unique transaction fees on Acala: %s UNQ', unqFees);246        expect(acaFees > 0).to.be.true;247        expect(unqFees == 0n).to.be.true;248      },249      {provider: new WsProvider('ws://127.0.0.1:' + ACALA_PORT)},250    );251252    await usingApi(async (api) => {253      // todo do something about instant sealing, where there might not be any new blocks254      await waitNewBlocks(api, 3);255256      [balanceUniqueTokenFinal] = await getBalance(api, [randomAccount.address]);257      const actuallyDelivered = balanceUniqueTokenFinal - balanceUniqueTokenAfter;258      expect(actuallyDelivered > 0).to.be.true;259260      const unqFees = TRANSFER_AMOUNT - actuallyDelivered;261      console.log('Acala to Unique transaction fees on Unique: %s UNQ', unqFees);262      expect(unqFees > 0).to.be.true;263    });264  });265});