1234567891011121314151617import 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, paraSiblingSovereignAccount} from './../util/helpers';25import waitNewBlocks from './../substrate/wait-new-blocks';26import {normalizeAccountId} from './../util/helpers';27import getBalance from './../substrate/get-balance';282930chai.use(chaiAsPromised);31const expect = chai.expect;3233const STATEMINE_CHAIN = 1000;34const UNIQUE_CHAIN = 2095;3536const RELAY_PORT = '9844';37const UNIQUE_PORT = '9944';38const STATEMINE_PORT = '9946';39const STATEMINE_PALLET_INSTANCE = 50;40const ASSET_ID = 100;41const ASSET_METADATA_DECIMALS = 18;42const ASSET_METADATA_NAME = 'USDT';43const ASSET_METADATA_DESCRIPTION = 'USDT';44const ASSET_METADATA_MINIMAL_BALANCE = 1;4546const TRANSFER_AMOUNT = 1_000_000_000_000_000_000n;474849const ASSET_AMOUNT = 1_000_000_000_000_000_000_000n; 5051describe('Integration test: Exchanging USDT with Westmint', () => {52 let alice: IKeyringPair;53 let bob: IKeyringPair;54 55 let balanceStmnBefore: bigint;56 let balanceStmnAfter: bigint;5758 let balanceOpalBefore: bigint;59 let balanceOpalAfter: bigint;60 let balanceOpalFinal: bigint;6162 let balanceBobBefore: bigint;63 let balanceBobAfter: bigint;64 let balanceBobFinal: bigint;6566 let balanceBobRelayTokenBefore: bigint;67 let balanceBobRelayTokenAfter: bigint;686970 before(async () => {71 await usingApi(async (api, privateKeyWrapper) => {72 alice = privateKeyWrapper('//Alice');73 bob = privateKeyWrapper('//Bob'); 74 });7576 const statemineApiOptions: ApiOptions = {77 provider: new WsProvider('ws://127.0.0.1:' + STATEMINE_PORT),78 };7980 const uniqueApiOptions: ApiOptions = {81 provider: new WsProvider('ws://127.0.0.1:' + UNIQUE_PORT),82 };8384 const relayApiOptions: ApiOptions = {85 provider: new WsProvider('ws://127.0.0.1:' + RELAY_PORT),86 };8788 await usingApi(async (api) => {8990 91 const fundingAmount = 3_500_000_000_000; 9293 const tx = api.tx.assets.create(ASSET_ID, alice.addressRaw, ASSET_METADATA_MINIMAL_BALANCE);94 const events = await submitTransactionAsync(alice, tx);95 const result = getGenericResult(events);96 expect(result.success).to.be.true;9798 99 const tx2 = api.tx.assets.setMetadata(ASSET_ID, ASSET_METADATA_NAME, ASSET_METADATA_DESCRIPTION, ASSET_METADATA_DECIMALS);100 const events2 = await submitTransactionAsync(alice, tx2);101 const result2 = getGenericResult(events2);102 expect(result2.success).to.be.true;103104 105 const tx3 = api.tx.assets.mint(ASSET_ID, alice.addressRaw, ASSET_AMOUNT);106 const events3 = await submitTransactionAsync(alice, tx3);107 const result3 = getGenericResult(events3);108 expect(result3.success).to.be.true;109110 111 const parachainSovereingAccount = await paraSiblingSovereignAccount(UNIQUE_CHAIN);112 const tx4 = api.tx.balances.transfer(parachainSovereingAccount, fundingAmount);113 const events4 = await submitTransactionAsync(bob, tx4);114 const result4 = getGenericResult(events4);115 expect(result4.success).to.be.true;116117 }, statemineApiOptions);118119120 await usingApi(async (api) => {121122 const location = {123 V1: {124 parents: 1,125 interior: {X3: [126 {127 Parachain: STATEMINE_CHAIN,128 },129 {130 PalletInstance: STATEMINE_PALLET_INSTANCE,131 },132 {133 GeneralIndex: ASSET_ID,134 },135 ]},136 },137 };138139 const metadata =140 {141 name: ASSET_ID,142 symbol: ASSET_METADATA_NAME,143 decimals: ASSET_METADATA_DECIMALS,144 minimalBalance: ASSET_METADATA_MINIMAL_BALANCE,145 };146 147 const tx = api.tx.foreingAssets.registerForeignAsset(alice.addressRaw, location, metadata);148 const sudoTx = api.tx.sudo.sudo(tx as any);149 const events = await submitTransactionAsync(alice, sudoTx);150 const result = getGenericResult(events);151 expect(result.success).to.be.true;152153 [balanceOpalBefore] = await getBalance(api, [alice.address]);154155 }, uniqueApiOptions);156157158 159 await usingApi(async (api) => {160 const destination = {161 V1: {162 parents: 0,163 interior: {X1: {164 Parachain: UNIQUE_CHAIN,165 },166 },167 }};168169 const beneficiary = {170 V1: {171 parents: 0,172 interior: {X1: {173 AccountId32: {174 network: 'Any',175 id: alice.addressRaw,176 },177 }},178 },179 };180181 const assets = {182 V1: [183 {184 id: {185 Concrete: {186 parents: 0,187 interior: 'Here',188 },189 },190 fun: {191 Fungible: 50_000_000_000_000_000n,192 },193 },194 ],195 };196197 const feeAssetItem = 0;198199 const weightLimit = {200 Limited: 5_000_000_000,201 };202203 const tx = api.tx.xcmPallet.limitedReserveTransferAssets(destination, beneficiary, assets, feeAssetItem, weightLimit);204 const events = await submitTransactionAsync(alice, tx);205 const result = getGenericResult(events);206 expect(result.success).to.be.true;207 }, relayApiOptions);208 209 });210211 it('Should connect and send USDT from Westmint to Opal', async () => {212 213 const statemineApiOptions: ApiOptions = {214 provider: new WsProvider('ws://127.0.0.1:' + STATEMINE_PORT),215 };216217 const uniqueApiOptions: ApiOptions = {218 provider: new WsProvider('ws://127.0.0.1:' + UNIQUE_PORT),219 };220221 await usingApi(async (api) => {222223 const dest = {224 V1: {225 parents: 1,226 interior: {X1: {227 Parachain: UNIQUE_CHAIN,228 },229 },230 }};231232 const beneficiary = {233 V1: {234 parents: 0,235 interior: {X1: {236 AccountId32: {237 network: 'Any',238 id: alice.addressRaw,239 },240 }},241 },242 };243244 const assets = {245 V1: [246 {247 id: {248 Concrete: {249 parents: 0,250 interior: {251 X2: [252 {253 PalletInstance: STATEMINE_PALLET_INSTANCE,254 },255 {256 GeneralIndex: ASSET_ID,257 }, 258 ]},259 },260 },261 fun: {262 Fungible: TRANSFER_AMOUNT,263 },264 },265 ],266 };267268 const feeAssetItem = 0;269270 const weightLimit = {271 Limited: 5000000000,272 };273274 [balanceStmnBefore] = await getBalance(api, [alice.address]);275276 const tx = api.tx.polkadotXcm.limitedReserveTransferAssets(dest, beneficiary, assets, feeAssetItem, weightLimit);277 const events = await submitTransactionAsync(alice, tx);278 const result = getGenericResult(events);279 expect(result.success).to.be.true;280281 [balanceStmnAfter] = await getBalance(api, [alice.address]);282283 284 console.log('Opal to Westmint transaction fees on Westmint: %s WND', balanceStmnBefore - balanceStmnAfter);285 expect(balanceStmnBefore > balanceStmnAfter).to.be.true;286287 }, statemineApiOptions);288289290 291 await usingApi(async (api) => {292 await waitNewBlocks(api, 3);293 294 const free = (await api.query.fungible.balance(1, normalizeAccountId(alice.address))).toBigInt();295296 [balanceOpalAfter] = await getBalance(api, [alice.address]);297298 299 expect(free == TRANSFER_AMOUNT).to.be.true;300 console.log('Opal to Westmint transaction fees on Opal: %s USDT', TRANSFER_AMOUNT - free);301 302 expect(balanceOpalAfter == balanceOpalBefore).to.be.true;303 console.log('Opal to Westmint transaction fees on Opal: %s WND', balanceOpalAfter - balanceOpalBefore);304305 }, uniqueApiOptions);306 307 });308309 it('Should connect and send USDT from Unique to Statemine back', async () => {310311 const uniqueApiOptions: ApiOptions = {312 provider: new WsProvider('ws://127.0.0.1:' + UNIQUE_PORT),313 };314315 const statemineApiOptions: ApiOptions = {316 provider: new WsProvider('ws://127.0.0.1:' + STATEMINE_PORT),317 };318319 await usingApi(async (api) => {320 const destination = {321 V1: {322 parents: 1,323 interior: {X2: [324 {325 Parachain: STATEMINE_CHAIN,326 },327 {328 AccountId32: {329 network: 'Any',330 id: alice.addressRaw,331 },332 },333 ]},334 },335 };336337 const currencies = [[338 {339 ForeignAssetId: 0,340 },341 342 TRANSFER_AMOUNT,343 ], 344 [345 {346 NativeAssetId: 'Parent',347 },348 400_000_000_000_000n,349 ]];350351 const feeItem = 1;352 const destWeight = 500000000000;353354 const tx = api.tx.xTokens.transferMulticurrencies(currencies, feeItem, destination, destWeight);355 const events = await submitTransactionAsync(alice, tx);356 const result = getGenericResult(events);357 expect(result.success).to.be.true;358 359 360 [balanceOpalFinal] = await getBalance(api, [alice.address]);361 expect(balanceOpalAfter > balanceOpalFinal).to.be.true;362 }, uniqueApiOptions);363364 await usingApi(async (api) => {365 await waitNewBlocks(api, 3);366 367 368 369 const free = ((await api.query.assets.account(100, alice.address)).toHuman()) as any;370 expect(BigInt(free.balance.replace(/,/g, '')) == ASSET_AMOUNT).to.be.true;371 }, statemineApiOptions);372 });373374 it('Should connect and send Relay token to Unique', async () => {375376 const uniqueApiOptions: ApiOptions = {377 provider: new WsProvider('ws://127.0.0.1:' + UNIQUE_PORT),378 };379380 const uniqueApiOptions2: ApiOptions = {381 provider: new WsProvider('ws://127.0.0.1:' + UNIQUE_PORT),382 };383384 const relayApiOptions: ApiOptions = {385 provider: new WsProvider('ws://127.0.0.1:' + RELAY_PORT),386 };387388 const TRANSFER_AMOUNT_RELAY = 50_000_000_000_000_000n;389390 await usingApi(async (api) => {391 [balanceBobBefore] = await getBalance(api, [bob.address]);392 balanceBobRelayTokenBefore = BigInt(((await api.query.tokens.accounts(bob.addressRaw, {NativeAssetId: 'Parent'})).toJSON() as any).free);393394 }, uniqueApiOptions);395396 397 await usingApi(async (api) => {398 const destination = {399 V1: {400 parents: 0,401 interior: {X1: {402 Parachain: UNIQUE_CHAIN,403 },404 },405 }};406407 const beneficiary = {408 V1: {409 parents: 0,410 interior: {X1: {411 AccountId32: {412 network: 'Any',413 id: bob.addressRaw,414 },415 }},416 },417 };418419 const assets = {420 V1: [421 {422 id: {423 Concrete: {424 parents: 0,425 interior: 'Here',426 },427 },428 fun: {429 Fungible: TRANSFER_AMOUNT_RELAY,430 },431 },432 ],433 };434435 const feeAssetItem = 0;436437 const weightLimit = {438 Limited: 5_000_000_000,439 };440441 const tx = api.tx.xcmPallet.limitedReserveTransferAssets(destination, beneficiary, assets, feeAssetItem, weightLimit);442 const events = await submitTransactionAsync(bob, tx);443 const result = getGenericResult(events);444 expect(result.success).to.be.true;445 }, relayApiOptions);446 447448 await usingApi(async (api) => {449 await waitNewBlocks(api, 3);450451 [balanceBobAfter] = await getBalance(api, [bob.address]);452 balanceBobRelayTokenAfter = BigInt(((await api.query.tokens.accounts(bob.addressRaw, {NativeAssetId: 'Parent'})).toJSON() as any).free);453 const wndFee = balanceBobRelayTokenAfter - TRANSFER_AMOUNT_RELAY - balanceBobRelayTokenBefore; 454 console.log('Relay (Westend) to Opal transaction fees: %s OPL', balanceBobAfter - balanceBobBefore);455 console.log('Relay (Westend) to Opal transaction fees: %s WND', wndFee);456 expect(balanceBobBefore == balanceBobAfter).to.be.true;457 expect(balanceBobRelayTokenBefore < balanceBobRelayTokenAfter).to.be.true;458 }, uniqueApiOptions2);459460 });461462 it('Should connect and send Relay token back', async () => {463 const uniqueApiOptions: ApiOptions = {464 provider: new WsProvider('ws://127.0.0.1:' + UNIQUE_PORT),465 };466467 await usingApi(async (api) => {468 const destination = {469 V1: {470 parents: 1,471 interior: {X2: [472 {473 Parachain: STATEMINE_CHAIN,474 },475 {476 AccountId32: {477 network: 'Any',478 id: bob.addressRaw,479 },480 },481 ]},482 },483 };484485 const currencies = [486 [487 {488 NativeAssetId: 'Parent',489 },490 50_000_000_000_000_000n,491 ]];492493 const feeItem = 0;494 const destWeight = 500000000000;495496 const tx = api.tx.xTokens.transferMulticurrencies(currencies, feeItem, destination, destWeight);497 const events = await submitTransactionAsync(bob, tx);498 const result = getGenericResult(events);499 expect(result.success).to.be.true;500501 [balanceBobFinal] = await getBalance(api, [bob.address]);502 console.log('Relay (Westend) to Opal transaction fees: %s OPL', balanceBobAfter - balanceBobFinal);503504 }, uniqueApiOptions);505 });506507});