1234567891011121314151617import {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;394041const 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'); 66 });6768 await usingWestmintPlaygrounds(statemineUrl, async (helper) => {69 70 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 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 113 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 215 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 225 await helper.wait.newBlocks(3);226227 228 const free = await helper.ft.getBalance(1, {Substrate: alice.address});229230 balanceOpalAfter = await helper.balance.getSubstrate(alice.address);231232 233 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 239 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 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 286 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 293 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 305 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});