git.delta.rocks / unique-network / refs/commits / 62fb30561fe6

difftreelog

source

tests/src/xcmTransferStatemine.test.ts9.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} from './util/helpers';25import waitNewBlocks from './substrate/wait-new-blocks';26import {normalizeAccountId} from './util/helpers';272829chai.use(chaiAsPromised);30const expect = chai.expect;3132const RELAY_PORT = '9844';33const UNIQUE_CHAIN = 5000;34const UNIQUE_PORT = '9944';35const STATEMINE_CHAIN = 1000;36const STATEMINE_PORT = '9946';37const STATEMINE_PALLET_INSTANCE = 50;38const ASSET_ID = 100;39const ASSET_METADATA_DECIMALS = 18;40const ASSET_METADATA_NAME = 'USDT';41const ASSET_METADATA_DESCRIPTION = 'USDT';42const ASSET_METADATA_MINIMAL_BALANCE = 1;4344describe('Integration test: Exchanging USDT with Statemine', () => {45  let alice: IKeyringPair;46  let bob: IKeyringPair;47  48  before(async () => {49    await usingApi(async (api, privateKeyWrapper) => {50      alice = privateKeyWrapper('//Alice');51      bob = privateKeyWrapper('//Bob'); // funds donor52    });5354    const statemineApiOptions: ApiOptions = {55      provider: new WsProvider('ws://127.0.0.1:' + STATEMINE_PORT),56    };5758    const uniqueApiOptions: ApiOptions = {59      provider: new WsProvider('ws://127.0.0.1:' + UNIQUE_PORT),60    };6162    const relayApiOptions: ApiOptions = {63      provider: new WsProvider('ws://127.0.0.1:' + RELAY_PORT),64    };6566    await usingApi(async (api) => {6768      // 10,000.00 (ten thousands) USDT69      const assetAmount = 1_000_000_000_000_000_000_000n; 70      // 350.00 (three hundred fifty) DOT71      const fundingAmount = 3_500_000_000_000; 7273      const tx = api.tx.assets.create(ASSET_ID, alice.addressRaw, ASSET_METADATA_MINIMAL_BALANCE);74      const events = await submitTransactionAsync(alice, tx);75      const result = getGenericResult(events);76      expect(result.success).to.be.true;7778      // set metadata79      const tx2 = api.tx.assets.setMetadata(ASSET_ID, ASSET_METADATA_NAME, ASSET_METADATA_DESCRIPTION, ASSET_METADATA_DECIMALS);80      const events2 = await submitTransactionAsync(alice, tx2);81      const result2 = getGenericResult(events2);82      expect(result2.success).to.be.true;8384      // mint some amount of asset85      const tx3 = api.tx.assets.mint(ASSET_ID, alice.addressRaw, assetAmount);86      const events3 = await submitTransactionAsync(alice, tx3);87      const result3 = getGenericResult(events3);88      expect(result3.success).to.be.true;8990      // funding parachain sovereing account (Parachain: 5000)91      const parachainSovereingAccount = '0x7369626c88130000000000000000000000000000000000000000000000000000';92      const tx4 = api.tx.balances.transfer(parachainSovereingAccount, fundingAmount);93      const events4 = await submitTransactionAsync(bob, tx4);94      const result4 = getGenericResult(events4);95      expect(result4.success).to.be.true;9697    }, statemineApiOptions);9899100    await usingApi(async (api) => {101102      const location = {103        V1: {104          parents: 1,105          interior: {X3: [106            {107              Parachain: STATEMINE_CHAIN,108            },109            {110              PalletInstance: STATEMINE_PALLET_INSTANCE,111            },112            {113              GeneralIndex: ASSET_ID,114            },115          ]},116        },117      };118119      const metadata =120      {121        name: ASSET_ID,122        symbol: ASSET_METADATA_NAME,123        decimals: ASSET_METADATA_DECIMALS,124        minimalBalance: ASSET_METADATA_MINIMAL_BALANCE,125      };126      //registerForeignAsset(owner, location, metadata)127      const tx = api.tx.foreingAssets.registerForeignAsset(alice.addressRaw, location, metadata);128      const sudoTx = api.tx.sudo.sudo(tx as any);129      const events = await submitTransactionAsync(alice, sudoTx);130      const result = getGenericResult(events);131      expect(result.success).to.be.true;132133    }, uniqueApiOptions);134135136    // Providing the relay currency to the unique sender account137    await usingApi(async (api) => {138      const destination = {139        V1: {140          parents: 0,141          interior: {X1: {142            Parachain: UNIQUE_CHAIN,143          },144          },145        }};146147      const beneficiary = {148        V1: {149          parents: 0,150          interior: {X1: {151            AccountId32: {152              network: 'Any',153              id: alice.addressRaw,154            },155          }},156        },157      };158159      const assets = {160        V1: [161          {162            id: {163              Concrete: {164                parents: 0,165                interior: 'Here',166              },167            },168            fun: {169              Fungible: 50_000_000_000_000_000n,170            },171          },172        ],173      };174175      const feeAssetItem = 0;176177      const weightLimit = {178        Limited: 5_000_000_000,179      };180181      const tx = api.tx.xcmPallet.limitedReserveTransferAssets(destination, beneficiary, assets, feeAssetItem, weightLimit);182      const events = await submitTransactionAsync(alice, tx);183      const result = getGenericResult(events);184      expect(result.success).to.be.true;185    }, relayApiOptions);186  187  });188189  it('Should connect and send USDT from Statemine to Unique', async () => {190    191    const statemineApiOptions: ApiOptions = {192      provider: new WsProvider('ws://127.0.0.1:' + STATEMINE_PORT),193    };194195    const uniqueApiOptions: ApiOptions = {196      provider: new WsProvider('ws://127.0.0.1:' + UNIQUE_PORT),197    };198199    await usingApi(async (api) => {200201      const dest = {202        V1: {203          parents: 1,204          interior: {X1: {205            Parachain: UNIQUE_CHAIN,206          },207          },208        }};209210      const beneficiary = {211        V1: {212          parents: 0,213          interior: {X1: {214            AccountId32: {215              network: 'Any',216              id: alice.addressRaw,217            },218          }},219        },220      };221222      const assets = {223        V1: [224          {225            id: {226              Concrete: {227                parents: 0,228                interior: {229                  X2: [230                    {231                      PalletInstance: STATEMINE_PALLET_INSTANCE,232                    },233                    {234                      GeneralIndex: ASSET_ID,235                    }, 236                  ]},237              },238            },239            fun: {240              Fungible: 1_000_000_000_000_000_000n,241            },242          },243        ],244      };245246      const feeAssetItem = 0;247248      const weightLimit = {249        Limited: 5000000000,250      };251252      const tx = api.tx.polkadotXcm.limitedReserveTransferAssets(dest, beneficiary, assets, feeAssetItem, weightLimit);253      const events = await submitTransactionAsync(alice, tx);254      const result = getGenericResult(events);255      expect(result.success).to.be.true;256    }, statemineApiOptions);257258259    // ensure that asset has been delivered260    await usingApi(async (api) => {261      await waitNewBlocks(api, 3);262      // expext collection id will be with id 1263      const free = (await api.query.fungible.balance(1, normalizeAccountId(alice.address))).toBigInt();264      expect(free > 0).to.be.true;265    }, uniqueApiOptions);266  });267268  it('Should connect and send USDT from Unique to Statemine back', async () => {269    let balanceBefore: bigint;270    const uniqueApiOptions: ApiOptions = {271      provider: new WsProvider('ws://127.0.0.1:' + UNIQUE_PORT),272    };273274    await usingApi(async (api) => {275      balanceBefore = (await api.query.fungible.balance(1, normalizeAccountId(alice.address))).toBigInt();276277      const destination = {278        V1: {279          parents: 1,280          interior: {X2: [281            {282              Parachain: STATEMINE_CHAIN,283            },284            {285              AccountId32: {286                network: 'Any',287                id: alice.addressRaw,288              },289            },290          ]},291        },292      };293294      const currencies = [[295        {296          ForeignAssetId: 0,297        },298        10_000_000_000_000_000n,299      ], 300      [301        {302          NativeAssetId: 'Parent',303        },304        400_000_000_000_000n,305      ]];306307      const feeItem = 1;308      const destWeight = 500000000000;309310      const tx = api.tx.xTokens.transferMulticurrencies(currencies, feeItem, destination, destWeight);311      const events = await submitTransactionAsync(alice, tx);312      const result = getGenericResult(events);313      expect(result.success).to.be.true;314315      // todo do something about instant sealing, where there might not be any new blocks316      await waitNewBlocks(api, 3);317      const balanceAfter = (await api.query.fungible.balance(1, normalizeAccountId(alice.address))).toBigInt();318      expect(balanceAfter < balanceBefore).to.be.true;319    }, uniqueApiOptions);320  });321});