git.delta.rocks / unique-network / refs/commits / 9fa2d18c4c7a

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 {itSub, expect, describeXcm, usingPlaygrounds, usingWestmintPlaygrounds, usingRelayPlaygrounds} from '../util/playgrounds';1920const STATEMINE_CHAIN = 1000;21const UNIQUE_CHAIN = 2095;2223const RELAY_PORT = '9844';24const STATEMINE_PORT = '9948';2526const relayUrl = 'ws://127.0.0.1:' + RELAY_PORT;27const statemineUrl = 'ws://127.0.0.1:' + STATEMINE_PORT;2829const STATEMINE_PALLET_INSTANCE = 50;30const ASSET_ID = 100;31const ASSET_METADATA_DECIMALS = 18;32const ASSET_METADATA_NAME = 'USDT';33const ASSET_METADATA_DESCRIPTION = 'USDT';34const ASSET_METADATA_MINIMAL_BALANCE = 1n;3536const WESTMINT_DECIMALS = 12;3738const TRANSFER_AMOUNT = 1_000_000_000_000_000_000n;3940// 10,000.00 (ten thousands) USDT41const ASSET_AMOUNT = 1_000_000_000_000_000_000_000n; 4243describeXcm('[XCM] Integration test: Exchanging USDT with Westmint', () => {44  let alice: IKeyringPair;45  let bob: IKeyringPair;46  47  let balanceStmnBefore: bigint;48  let balanceStmnAfter: bigint;4950  let balanceOpalBefore: bigint;51  let balanceOpalAfter: bigint;52  let balanceOpalFinal: bigint;5354  let balanceBobBefore: bigint;55  let balanceBobAfter: bigint;56  let balanceBobFinal: bigint;5758  let balanceBobRelayTokenBefore: bigint;59  let balanceBobRelayTokenAfter: bigint;606162  before(async () => {63    await usingPlaygrounds(async (_helper, privateKey) => {64      alice = privateKey('//Alice');65      bob = privateKey('//Bob'); // funds donor66    });6768    await usingWestmintPlaygrounds(statemineUrl, async (helper) => {69      // 350.00 (three hundred fifty) DOT70      const fundingAmount = 3_500_000_000_000n; 7172      await helper.assets.create(alice, ASSET_ID, alice.address, ASSET_METADATA_MINIMAL_BALANCE);73      await helper.assets.setMetadata(alice, ASSET_ID, ASSET_METADATA_NAME, ASSET_METADATA_DESCRIPTION, ASSET_METADATA_DECIMALS);74      await helper.assets.mint(alice, ASSET_ID, alice.address, ASSET_AMOUNT);7576      // funding parachain sovereing account (Parachain: 2095)77      const parachainSovereingAccount = helper.address.paraSiblingSovereignAccount(UNIQUE_CHAIN);78      await helper.balance.transferToSubstrate(bob, parachainSovereingAccount, fundingAmount);79    });808182    await usingPlaygrounds(async (helper) => {83      const location = {84        V1: {85          parents: 1,86          interior: {X3: [87            {88              Parachain: STATEMINE_CHAIN,89            },90            {91              PalletInstance: STATEMINE_PALLET_INSTANCE,92            },93            {94              GeneralIndex: ASSET_ID,95            },96          ]},97        },98      };99100      const metadata =101      {102        name: ASSET_ID,103        symbol: ASSET_METADATA_NAME,104        decimals: ASSET_METADATA_DECIMALS,105        minimalBalance: ASSET_METADATA_MINIMAL_BALANCE,106      };107      await helper.getSudo().foreignAssets.register(alice, alice.address, location, metadata);108      balanceOpalBefore = await helper.balance.getSubstrate(alice.address);109    });110111112    // Providing the relay currency to the unique sender account113    await usingRelayPlaygrounds(relayUrl, async (helper) => {114      const destination = {115        V1: {116          parents: 0,117          interior: {X1: {118            Parachain: UNIQUE_CHAIN,119          },120          },121        }};122123      const beneficiary = {124        V1: {125          parents: 0,126          interior: {X1: {127            AccountId32: {128              network: 'Any',129              id: alice.addressRaw,130            },131          }},132        },133      };134135      const assets = {136        V1: [137          {138            id: {139              Concrete: {140                parents: 0,141                interior: 'Here',142              },143            },144            fun: {145              Fungible: 50_000_000_000_000_000n,146            },147          },148        ],149      };150151      const feeAssetItem = 0;152      const weightLimit = 5_000_000_000;153154      await helper.xcm.limitedReserveTransferAssets(alice, destination, beneficiary, assets, feeAssetItem, weightLimit);155    });156  157  });158159  itSub('Should connect and send USDT from Westmint to Opal', async ({helper}) => {160    await usingWestmintPlaygrounds(statemineUrl, async (helper) => {161      const dest = {162        V1: {163          parents: 1,164          interior: {X1: {165            Parachain: UNIQUE_CHAIN,166          },167          },168        }};169170      const beneficiary = {171        V1: {172          parents: 0,173          interior: {X1: {174            AccountId32: {175              network: 'Any',176              id: alice.addressRaw,177            },178          }},179        },180      };181182      const assets = {183        V1: [184          {185            id: {186              Concrete: {187                parents: 0,188                interior: {189                  X2: [190                    {191                      PalletInstance: STATEMINE_PALLET_INSTANCE,192                    },193                    {194                      GeneralIndex: ASSET_ID,195                    }, 196                  ]},197              },198            },199            fun: {200              Fungible: TRANSFER_AMOUNT,201            },202          },203        ],204      };205206      const feeAssetItem = 0;207      const weightLimit = 5000000000;208209      balanceStmnBefore = await helper.balance.getSubstrate(alice.address);210      await helper.xcm.limitedReserveTransferAssets(alice, dest, beneficiary, assets, feeAssetItem, weightLimit);211212      balanceStmnAfter = await helper.balance.getSubstrate(alice.address);213214      // common good parachain take commission in it native token215      console.log(216        'Opal to Westmint transaction fees on Westmint: %s WND',217        helper.util.bigIntToDecimals(balanceStmnBefore - balanceStmnAfter, WESTMINT_DECIMALS),218      );219      expect(balanceStmnBefore > balanceStmnAfter).to.be.true;220221    });222223224    // ensure that asset has been delivered225    await helper.wait.newBlocks(3);226227    // expext collection id will be with id 1228    const free = await helper.ft.getBalance(1, {Substrate: alice.address});229230    balanceOpalAfter = await helper.balance.getSubstrate(alice.address);231232    // commission has not paid in USDT token233    expect(free == TRANSFER_AMOUNT).to.be.true;234    console.log(235      'Opal to Westmint transaction fees on Opal: %s USDT',236      helper.util.bigIntToDecimals(TRANSFER_AMOUNT - free),237    );238    // ... and parachain native token239    expect(balanceOpalAfter == balanceOpalBefore).to.be.true;240    console.log(241      'Opal to Westmint transaction fees on Opal: %s WND',242      helper.util.bigIntToDecimals(balanceOpalAfter - balanceOpalBefore, WESTMINT_DECIMALS),243    );    244  });245246  itSub('Should connect and send USDT from Unique to Statemine back', async ({helper}) => {247    const destination = {248      V1: {249        parents: 1,250        interior: {X2: [251          {252            Parachain: STATEMINE_CHAIN,253          },254          {255            AccountId32: {256              network: 'Any',257              id: alice.addressRaw,258            },259          },260        ]},261      },262    };263264    const currencies: [any, bigint][] = [265      [266        {267          ForeignAssetId: 0,268        },269        //10_000_000_000_000_000n,270        TRANSFER_AMOUNT,271      ], 272      [273        {274          NativeAssetId: 'Parent',275        },276        400_000_000_000_000n,277      ],278    ];279280    const feeItem = 1;281    const destWeight = 500000000000;282283    await helper.xTokens.transferMulticurrencies(alice, currencies, feeItem, destination, destWeight);284    285    // the commission has been paid in parachain native token286    balanceOpalFinal = await helper.balance.getSubstrate(alice.address);287    expect(balanceOpalAfter > balanceOpalFinal).to.be.true;288289    await usingWestmintPlaygrounds(statemineUrl, async (helper) => {290      await helper.wait.newBlocks(3);291      292      // The USDT token never paid fees. Its amount not changed from begin value.293      // Also check that xcm transfer has been succeeded 294      expect((await helper.assets.account(ASSET_ID, alice.address))! == ASSET_AMOUNT).to.be.true;295    });296  });297298  itSub('Should connect and send Relay token to Unique', async ({helper}) => {299    const TRANSFER_AMOUNT_RELAY = 50_000_000_000_000_000n;300301    balanceBobBefore = await helper.balance.getSubstrate(bob.address);302    balanceBobRelayTokenBefore = await helper.tokens.accounts(bob.address, {NativeAssetId: 'Parent'});303304    // Providing the relay currency to the unique sender account305    await usingRelayPlaygrounds(relayUrl, async (helper) => {306      const destination = {307        V1: {308          parents: 0,309          interior: {X1: {310            Parachain: UNIQUE_CHAIN,311          },312          },313        }};314315      const beneficiary = {316        V1: {317          parents: 0,318          interior: {X1: {319            AccountId32: {320              network: 'Any',321              id: bob.addressRaw,322            },323          }},324        },325      };326327      const assets = {328        V1: [329          {330            id: {331              Concrete: {332                parents: 0,333                interior: 'Here',334              },335            },336            fun: {337              Fungible: TRANSFER_AMOUNT_RELAY,338            },339          },340        ],341      };342343      const feeAssetItem = 0;344      const weightLimit = 5_000_000_000;345346      await helper.xcm.limitedReserveTransferAssets(bob, destination, beneficiary, assets, feeAssetItem, weightLimit);347    });348  349    await helper.wait.newBlocks(3);350351    balanceBobAfter = await helper.balance.getSubstrate(bob.address);  352    balanceBobRelayTokenAfter = await helper.tokens.accounts(bob.address, {NativeAssetId: 'Parent'});353354    const wndFee = balanceBobRelayTokenAfter - TRANSFER_AMOUNT_RELAY - balanceBobRelayTokenBefore; 355    console.log(356      'Relay (Westend) to Opal transaction fees: %s OPL',357      helper.util.bigIntToDecimals(balanceBobAfter - balanceBobBefore),358    );359    console.log(360      'Relay (Westend) to Opal transaction fees: %s WND',361      helper.util.bigIntToDecimals(wndFee, WESTMINT_DECIMALS),362    );363    expect(balanceBobBefore == balanceBobAfter).to.be.true;364    expect(balanceBobRelayTokenBefore < balanceBobRelayTokenAfter).to.be.true;365  });366367  itSub('Should connect and send Relay token back', async ({helper}) => {368    const destination = {369      V1: {370        parents: 1,371        interior: {X2: [372          {373            Parachain: STATEMINE_CHAIN,374          },375          {376            AccountId32: {377              network: 'Any',378              id: bob.addressRaw,379            },380          },381        ]},382      },383    };384385    const currencies: any = [386      [387        {388          NativeAssetId: 'Parent',389        },390        50_000_000_000_000_000n,391      ],392    ];393394    const feeItem = 0;395    const destWeight = 500000000000;396397    await helper.xTokens.transferMulticurrencies(bob, currencies, feeItem, destination, destWeight);398399    balanceBobFinal = await helper.balance.getSubstrate(bob.address);400    console.log('Relay (Westend) to Opal transaction fees: %s OPL', balanceBobAfter - balanceBobFinal);401  });402});