1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {executeTransaction} from './../substrate/substrate-api';19import {bigIntToDecimals, getGenericResult, paraSiblingSovereignAccount} from './../deprecated-helpers/helpers';20import getBalance from './../substrate/get-balance';21import {itSub, expect, describeXcm, usingPlaygrounds} from '../util/playgrounds';2223const STATEMINE_CHAIN = 1000;24const UNIQUE_CHAIN = 2095;2526const RELAY_PORT = '9844';27const STATEMINE_PORT = '9948';2829const relayUrl = 'ws://127.0.0.1:' + RELAY_PORT;30const statemineUrl = 'ws://127.0.0.1:' + STATEMINE_PORT;3132const STATEMINE_PALLET_INSTANCE = 50;33const ASSET_ID = 100;34const ASSET_METADATA_DECIMALS = 18;35const ASSET_METADATA_NAME = 'USDT';36const ASSET_METADATA_DESCRIPTION = 'USDT';37const ASSET_METADATA_MINIMAL_BALANCE = 1n;3839const WESTMINT_DECIMALS = 12;4041const TRANSFER_AMOUNT = 1_000_000_000_000_000_000n;424344const ASSET_AMOUNT = 1_000_000_000_000_000_000_000n; 4546describeXcm('[XCM] Integration test: Exchanging USDT with Westmint', () => {47 let alice: IKeyringPair;48 let bob: IKeyringPair;49 50 let balanceStmnBefore: bigint;51 let balanceStmnAfter: bigint;5253 let balanceOpalBefore: bigint;54 let balanceOpalAfter: bigint;55 let balanceOpalFinal: bigint;5657 let balanceBobBefore: bigint;58 let balanceBobAfter: bigint;59 let balanceBobFinal: bigint;6061 let balanceBobRelayTokenBefore: bigint;62 let balanceBobRelayTokenAfter: bigint;636465 before(async () => {66 await usingPlaygrounds(async (_helper, privateKey) => {67 alice = privateKey('//Alice');68 bob = privateKey('//Bob'); 69 });7071 await usingPlaygrounds.atUrl(statemineUrl, async (helper) => {72 const api = helper.getApi();7374 75 const fundingAmount = 3_500_000_000_000; 7677 const tx = api.tx.assets.create(ASSET_ID, alice.addressRaw, ASSET_METADATA_MINIMAL_BALANCE);78 const events = await executeTransaction(api, alice, tx);79 const result = getGenericResult(events);80 expect(result.success).to.be.true;8182 83 const tx2 = api.tx.assets.setMetadata(ASSET_ID, ASSET_METADATA_NAME, ASSET_METADATA_DESCRIPTION, ASSET_METADATA_DECIMALS);84 const events2 = await executeTransaction(api, alice, tx2);85 const result2 = getGenericResult(events2);86 expect(result2.success).to.be.true;8788 89 const tx3 = api.tx.assets.mint(ASSET_ID, alice.addressRaw, ASSET_AMOUNT);90 const events3 = await executeTransaction(api, alice, tx3);91 const result3 = getGenericResult(events3);92 expect(result3.success).to.be.true;9394 95 const parachainSovereingAccount = await paraSiblingSovereignAccount(UNIQUE_CHAIN);96 const tx4 = api.tx.balances.transfer(parachainSovereingAccount, fundingAmount);97 const events4 = await executeTransaction(api, bob, tx4);98 const result4 = getGenericResult(events4);99 expect(result4.success).to.be.true;100101 });102103104 await usingPlaygrounds(async (helper) => {105 const location = {106 V1: {107 parents: 1,108 interior: {X3: [109 {110 Parachain: STATEMINE_CHAIN,111 },112 {113 PalletInstance: STATEMINE_PALLET_INSTANCE,114 },115 {116 GeneralIndex: ASSET_ID,117 },118 ]},119 },120 };121122 const metadata =123 {124 name: ASSET_ID,125 symbol: ASSET_METADATA_NAME,126 decimals: ASSET_METADATA_DECIMALS,127 minimalBalance: ASSET_METADATA_MINIMAL_BALANCE,128 };129 await helper.getSudo().foreignAssets.register(alice, alice.address, location, metadata);130 131 132 133 134 135136 137 balanceOpalBefore = await helper.balance.getSubstrate(alice.address);138139 });140141142 143 await usingPlaygrounds.atUrl(relayUrl, async (helper) => {144 const api = helper.getApi();145146 const destination = {147 V1: {148 parents: 0,149 interior: {X1: {150 Parachain: UNIQUE_CHAIN,151 },152 },153 }};154155 const beneficiary = {156 V1: {157 parents: 0,158 interior: {X1: {159 AccountId32: {160 network: 'Any',161 id: alice.addressRaw,162 },163 }},164 },165 };166167 const assets = {168 V1: [169 {170 id: {171 Concrete: {172 parents: 0,173 interior: 'Here',174 },175 },176 fun: {177 Fungible: 50_000_000_000_000_000n,178 },179 },180 ],181 };182183 const feeAssetItem = 0;184185 const weightLimit = {186 Limited: 5_000_000_000,187 };188189 const tx = api.tx.xcmPallet.limitedReserveTransferAssets(destination, beneficiary, assets, feeAssetItem, weightLimit);190 const events = await executeTransaction(api, alice, tx);191 const result = getGenericResult(events);192 expect(result.success).to.be.true;193 });194 195 });196197 itSub('Should connect and send USDT from Westmint to Opal', async ({helper}) => {198 await usingPlaygrounds.atUrl(statemineUrl, async (helper) => {199 const api = helper.getApi();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: TRANSFER_AMOUNT,241 },242 },243 ],244 };245246 const feeAssetItem = 0;247248 const weightLimit = {249 Limited: 5000000000,250 };251252 [balanceStmnBefore] = await getBalance(api, [alice.address]);253254 const tx = api.tx.polkadotXcm.limitedReserveTransferAssets(dest, beneficiary, assets, feeAssetItem, weightLimit);255 const events = await executeTransaction(api, alice, tx);256 const result = getGenericResult(events);257 expect(result.success).to.be.true;258259 [balanceStmnAfter] = await getBalance(api, [alice.address]);260261 262 console.log(263 'Opal to Westmint transaction fees on Westmint: %s WND',264 bigIntToDecimals(balanceStmnBefore - balanceStmnAfter, WESTMINT_DECIMALS),265 );266 expect(balanceStmnBefore > balanceStmnAfter).to.be.true;267268 });269270271 272 273 await helper.wait.newBlocks(3);274275 276 277 const free = await helper.ft.getBalance(1, {Substrate: alice.address});278279 280 balanceOpalAfter = await helper.balance.getSubstrate(alice.address);281282 283 expect(free == TRANSFER_AMOUNT).to.be.true;284 console.log(285 'Opal to Westmint transaction fees on Opal: %s USDT',286 bigIntToDecimals(TRANSFER_AMOUNT - free),287 );288 289 expect(balanceOpalAfter == balanceOpalBefore).to.be.true;290 console.log(291 'Opal to Westmint transaction fees on Opal: %s WND',292 bigIntToDecimals(balanceOpalAfter - balanceOpalBefore, WESTMINT_DECIMALS),293 ); 294 });295296 itSub('Should connect and send USDT from Unique to Statemine back', async ({helper}) => {297 const api = helper.getApi();298299 const destination = {300 V1: {301 parents: 1,302 interior: {X2: [303 {304 Parachain: STATEMINE_CHAIN,305 },306 {307 AccountId32: {308 network: 'Any',309 id: alice.addressRaw,310 },311 },312 ]},313 },314 };315316 const currencies: [any, bigint][] = [317 [318 {319 ForeignAssetId: 0,320 },321 322 TRANSFER_AMOUNT,323 ], 324 [325 {326 NativeAssetId: 'Parent',327 },328 400_000_000_000_000n,329 ],330 ];331332 const feeItem = 1;333 const destWeight = 500000000000;334335 const tx = api.tx.xTokens.transferMulticurrencies(currencies, feeItem, destination, destWeight);336 const events = await executeTransaction(api, alice, tx);337 const result = getGenericResult(events);338 expect(result.success).to.be.true;339 340 341 [balanceOpalFinal] = await getBalance(api, [alice.address]);342 expect(balanceOpalAfter > balanceOpalFinal).to.be.true;343344 await usingPlaygrounds.atUrl(statemineUrl, async (helper) => {345 346 await helper.wait.newBlocks(3);347348 const api = helper.getApi();349 350 351 352 const free = ((await api.query.assets.account(100, alice.address)).toHuman()) as any;353 expect(BigInt(free.balance.replace(/,/g, '')) == ASSET_AMOUNT).to.be.true;354 });355 });356357 itSub('Should connect and send Relay token to Unique', async ({helper}) => {358 const TRANSFER_AMOUNT_RELAY = 50_000_000_000_000_000n;359360 361 362 balanceBobBefore = await helper.balance.getSubstrate(bob.address);363364 365 balanceBobRelayTokenBefore = BigInt(((await helper.getApi().query.tokens.accounts(bob.addressRaw, {NativeAssetId: 'Parent'})).toJSON() as any).free);366367 368 await usingPlaygrounds.atUrl(relayUrl, async (helper) => {369 const api = helper.getApi();370371 const destination = {372 V1: {373 parents: 0,374 interior: {X1: {375 Parachain: UNIQUE_CHAIN,376 },377 },378 }};379380 const beneficiary = {381 V1: {382 parents: 0,383 interior: {X1: {384 AccountId32: {385 network: 'Any',386 id: bob.addressRaw,387 },388 }},389 },390 };391392 const assets = {393 V1: [394 {395 id: {396 Concrete: {397 parents: 0,398 interior: 'Here',399 },400 },401 fun: {402 Fungible: TRANSFER_AMOUNT_RELAY,403 },404 },405 ],406 };407408 const feeAssetItem = 0;409410 const weightLimit = {411 Limited: 5_000_000_000,412 };413414 const tx = api.tx.xcmPallet.limitedReserveTransferAssets(destination, beneficiary, assets, feeAssetItem, weightLimit);415 const events = await executeTransaction(api, bob, tx);416 const result = getGenericResult(events);417 expect(result.success).to.be.true;418 });419 420421 422 await helper.wait.newBlocks(3);423424 425 426 balanceBobAfter = await helper.balance.getSubstrate(bob.address);427 428 429 balanceBobRelayTokenAfter = BigInt(((await helper.getApi().query.tokens.accounts(bob.addressRaw, {NativeAssetId: 'Parent'})).toJSON() as any).free);430431 const wndFee = balanceBobRelayTokenAfter - TRANSFER_AMOUNT_RELAY - balanceBobRelayTokenBefore; 432 console.log(433 'Relay (Westend) to Opal transaction fees: %s OPL',434 bigIntToDecimals(balanceBobAfter - balanceBobBefore),435 );436 console.log(437 'Relay (Westend) to Opal transaction fees: %s WND',438 bigIntToDecimals(wndFee, WESTMINT_DECIMALS),439 );440 expect(balanceBobBefore == balanceBobAfter).to.be.true;441 expect(balanceBobRelayTokenBefore < balanceBobRelayTokenAfter).to.be.true;442 });443444 itSub('Should connect and send Relay token back', async ({helper}) => {445 const destination = {446 V1: {447 parents: 1,448 interior: {X2: [449 {450 Parachain: STATEMINE_CHAIN,451 },452 {453 AccountId32: {454 network: 'Any',455 id: bob.addressRaw,456 },457 },458 ]},459 },460 };461462 const currencies: any = [463 [464 {465 NativeAssetId: 'Parent',466 },467 50_000_000_000_000_000n,468 ],469 ];470471 const feeItem = 0;472 const destWeight = 500000000000;473474 475 const api = helper.getApi();476 const tx = api.tx.xTokens.transferMulticurrencies(currencies, feeItem, destination, destWeight);477 const events = await executeTransaction(api, bob, tx);478 const result = getGenericResult(events);479 expect(result.success).to.be.true;480481 482 balanceBobFinal = await helper.balance.getSubstrate(bob.address);483 console.log('Relay (Westend) to Opal transaction fees: %s OPL', balanceBobAfter - balanceBobFinal);484 });485486});