git.delta.rocks / unique-network / refs/commits / 641d3fa29e37

difftreelog

source

tests/src/xcm/xcmOpal.test.ts11.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 {IKeyringPair} from '@polkadot/types/types';18import {bigIntToDecimals, paraSiblingSovereignAccount} from './../deprecated-helpers/helpers';19import {itSub, expect, describeXcm, usingPlaygrounds, usingWestmintPlaygrounds, usingRelayPlaygrounds} from '../util/playgrounds';2021const STATEMINE_CHAIN = 1000;22const UNIQUE_CHAIN = 2095;2324const RELAY_PORT = '9844';25const STATEMINE_PORT = '9948';2627const relayUrl = 'ws://127.0.0.1:' + RELAY_PORT;28const statemineUrl = 'ws://127.0.0.1:' + STATEMINE_PORT;2930const STATEMINE_PALLET_INSTANCE = 50;31const ASSET_ID = 100;32const ASSET_METADATA_DECIMALS = 18;33const ASSET_METADATA_NAME = 'USDT';34const ASSET_METADATA_DESCRIPTION = 'USDT';35const ASSET_METADATA_MINIMAL_BALANCE = 1n;3637const WESTMINT_DECIMALS = 12;3839const TRANSFER_AMOUNT = 1_000_000_000_000_000_000n;4041// 10,000.00 (ten thousands) USDT42const ASSET_AMOUNT = 1_000_000_000_000_000_000_000n; 4344describeXcm('[XCM] Integration test: Exchanging USDT with Westmint', () => {45  let alice: IKeyringPair;46  let bob: IKeyringPair;47  48  let balanceStmnBefore: bigint;49  let balanceStmnAfter: bigint;5051  let balanceOpalBefore: bigint;52  let balanceOpalAfter: bigint;53  let balanceOpalFinal: bigint;5455  let balanceBobBefore: bigint;56  let balanceBobAfter: bigint;57  let balanceBobFinal: bigint;5859  let balanceBobRelayTokenBefore: bigint;60  let balanceBobRelayTokenAfter: bigint;616263  before(async () => {64    await usingPlaygrounds(async (_helper, privateKey) => {65      alice = privateKey('//Alice');66      bob = privateKey('//Bob'); // funds donor67    });6869    await usingWestmintPlaygrounds(statemineUrl, async (helper) => {70      // 350.00 (three hundred fifty) DOT71      const fundingAmount = 3_500_000_000_000n; 7273      await helper.assets.create(alice, ASSET_ID, alice.address, ASSET_METADATA_MINIMAL_BALANCE);74      await helper.assets.setMetadata(alice, ASSET_ID, ASSET_METADATA_NAME, ASSET_METADATA_DESCRIPTION, ASSET_METADATA_DECIMALS);75      await helper.assets.mint(alice, ASSET_ID, alice.address, ASSET_AMOUNT);7677      // funding parachain sovereing account (Parachain: 2095)78      const parachainSovereingAccount = await paraSiblingSovereignAccount(UNIQUE_CHAIN);79      await helper.balance.transferToSubstrate(bob, parachainSovereingAccount, fundingAmount);80    });818283    await usingPlaygrounds(async (helper) => {84      const location = {85        V1: {86          parents: 1,87          interior: {X3: [88            {89              Parachain: STATEMINE_CHAIN,90            },91            {92              PalletInstance: STATEMINE_PALLET_INSTANCE,93            },94            {95              GeneralIndex: ASSET_ID,96            },97          ]},98        },99      };100101      const metadata =102      {103        name: ASSET_ID,104        symbol: ASSET_METADATA_NAME,105        decimals: ASSET_METADATA_DECIMALS,106        minimalBalance: ASSET_METADATA_MINIMAL_BALANCE,107      };108      await helper.getSudo().foreignAssets.register(alice, alice.address, location, metadata);109      balanceOpalBefore = await helper.balance.getSubstrate(alice.address);110    });111112113    // Providing the relay currency to the unique sender account114    await usingRelayPlaygrounds(relayUrl, async (helper) => {115      const destination = {116        V1: {117          parents: 0,118          interior: {X1: {119            Parachain: UNIQUE_CHAIN,120          },121          },122        }};123124      const beneficiary = {125        V1: {126          parents: 0,127          interior: {X1: {128            AccountId32: {129              network: 'Any',130              id: alice.addressRaw,131            },132          }},133        },134      };135136      const assets = {137        V1: [138          {139            id: {140              Concrete: {141                parents: 0,142                interior: 'Here',143              },144            },145            fun: {146              Fungible: 50_000_000_000_000_000n,147            },148          },149        ],150      };151152      const feeAssetItem = 0;153      const weightLimit = 5_000_000_000;154155      await helper.xcm.limitedReserveTransferAssets(alice, destination, beneficiary, assets, feeAssetItem, weightLimit);156    });157  158  });159160  itSub('Should connect and send USDT from Westmint to Opal', async ({helper}) => {161    await usingWestmintPlaygrounds(statemineUrl, async (helper) => {162      const dest = {163        V1: {164          parents: 1,165          interior: {X1: {166            Parachain: UNIQUE_CHAIN,167          },168          },169        }};170171      const beneficiary = {172        V1: {173          parents: 0,174          interior: {X1: {175            AccountId32: {176              network: 'Any',177              id: alice.addressRaw,178            },179          }},180        },181      };182183      const assets = {184        V1: [185          {186            id: {187              Concrete: {188                parents: 0,189                interior: {190                  X2: [191                    {192                      PalletInstance: STATEMINE_PALLET_INSTANCE,193                    },194                    {195                      GeneralIndex: ASSET_ID,196                    }, 197                  ]},198              },199            },200            fun: {201              Fungible: TRANSFER_AMOUNT,202            },203          },204        ],205      };206207      const feeAssetItem = 0;208      const weightLimit = 5000000000;209210      balanceStmnBefore = await helper.balance.getSubstrate(alice.address);211      await helper.xcm.limitedReserveTransferAssets(alice, dest, beneficiary, assets, feeAssetItem, weightLimit);212213      balanceStmnAfter = await helper.balance.getSubstrate(alice.address);214215      // common good parachain take commission in it native token216      console.log(217        'Opal to Westmint transaction fees on Westmint: %s WND',218        bigIntToDecimals(balanceStmnBefore - balanceStmnAfter, WESTMINT_DECIMALS),219      );220      expect(balanceStmnBefore > balanceStmnAfter).to.be.true;221222    });223224225    // ensure that asset has been delivered226    await helper.wait.newBlocks(3);227228    // expext collection id will be with id 1229    const free = await helper.ft.getBalance(1, {Substrate: alice.address});230231    balanceOpalAfter = await helper.balance.getSubstrate(alice.address);232233    // commission has not paid in USDT token234    expect(free == TRANSFER_AMOUNT).to.be.true;235    console.log(236      'Opal to Westmint transaction fees on Opal: %s USDT',237      bigIntToDecimals(TRANSFER_AMOUNT - free),238    );239    // ... and parachain native token240    expect(balanceOpalAfter == balanceOpalBefore).to.be.true;241    console.log(242      'Opal to Westmint transaction fees on Opal: %s WND',243      bigIntToDecimals(balanceOpalAfter - balanceOpalBefore, WESTMINT_DECIMALS),244    );    245  });246247  itSub('Should connect and send USDT from Unique to Statemine back', async ({helper}) => {248    const destination = {249      V1: {250        parents: 1,251        interior: {X2: [252          {253            Parachain: STATEMINE_CHAIN,254          },255          {256            AccountId32: {257              network: 'Any',258              id: alice.addressRaw,259            },260          },261        ]},262      },263    };264265    const currencies: [any, bigint][] = [266      [267        {268          ForeignAssetId: 0,269        },270        //10_000_000_000_000_000n,271        TRANSFER_AMOUNT,272      ], 273      [274        {275          NativeAssetId: 'Parent',276        },277        400_000_000_000_000n,278      ],279    ];280281    const feeItem = 1;282    const destWeight = 500000000000;283284    await helper.xTokens.transferMulticurrencies(alice, currencies, feeItem, destination, destWeight);285    286    // the commission has been paid in parachain native token287    balanceOpalFinal = await helper.balance.getSubstrate(alice.address);288    expect(balanceOpalAfter > balanceOpalFinal).to.be.true;289290    await usingWestmintPlaygrounds(statemineUrl, async (helper) => {291      await helper.wait.newBlocks(3);292      293      // The USDT token never paid fees. Its amount not changed from begin value.294      // Also check that xcm transfer has been succeeded 295      expect((await helper.assets.account(ASSET_ID, alice.address))! == ASSET_AMOUNT).to.be.true;296    });297  });298299  itSub('Should connect and send Relay token to Unique', async ({helper}) => {300    const TRANSFER_AMOUNT_RELAY = 50_000_000_000_000_000n;301302    balanceBobBefore = await helper.balance.getSubstrate(bob.address);303    balanceBobRelayTokenBefore = await helper.tokens.accounts(bob.address, {NativeAssetId: 'Parent'});304305    // Providing the relay currency to the unique sender account306    await usingRelayPlaygrounds(relayUrl, async (helper) => {307      const destination = {308        V1: {309          parents: 0,310          interior: {X1: {311            Parachain: UNIQUE_CHAIN,312          },313          },314        }};315316      const beneficiary = {317        V1: {318          parents: 0,319          interior: {X1: {320            AccountId32: {321              network: 'Any',322              id: bob.addressRaw,323            },324          }},325        },326      };327328      const assets = {329        V1: [330          {331            id: {332              Concrete: {333                parents: 0,334                interior: 'Here',335              },336            },337            fun: {338              Fungible: TRANSFER_AMOUNT_RELAY,339            },340          },341        ],342      };343344      const feeAssetItem = 0;345      const weightLimit = 5_000_000_000;346347      await helper.xcm.limitedReserveTransferAssets(bob, destination, beneficiary, assets, feeAssetItem, weightLimit);348    });349  350    await helper.wait.newBlocks(3);351352    balanceBobAfter = await helper.balance.getSubstrate(bob.address);  353    balanceBobRelayTokenAfter = await helper.tokens.accounts(bob.address, {NativeAssetId: 'Parent'});354355    const wndFee = balanceBobRelayTokenAfter - TRANSFER_AMOUNT_RELAY - balanceBobRelayTokenBefore; 356    console.log(357      'Relay (Westend) to Opal transaction fees: %s OPL',358      bigIntToDecimals(balanceBobAfter - balanceBobBefore),359    );360    console.log(361      'Relay (Westend) to Opal transaction fees: %s WND',362      bigIntToDecimals(wndFee, WESTMINT_DECIMALS),363    );364    expect(balanceBobBefore == balanceBobAfter).to.be.true;365    expect(balanceBobRelayTokenBefore < balanceBobRelayTokenAfter).to.be.true;366  });367368  itSub('Should connect and send Relay token back', async ({helper}) => {369    const destination = {370      V1: {371        parents: 1,372        interior: {X2: [373          {374            Parachain: STATEMINE_CHAIN,375          },376          {377            AccountId32: {378              network: 'Any',379              id: bob.addressRaw,380            },381          },382        ]},383      },384    };385386    const currencies: any = [387      [388        {389          NativeAssetId: 'Parent',390        },391        50_000_000_000_000_000n,392      ],393    ];394395    const feeItem = 0;396    const destWeight = 500000000000;397398    await helper.xTokens.transferMulticurrencies(bob, currencies, feeItem, destination, destWeight);399400    balanceBobFinal = await helper.balance.getSubstrate(bob.address);401    console.log('Relay (Westend) to Opal transaction fees: %s OPL', balanceBobAfter - balanceBobFinal);402  });403});