git.delta.rocks / unique-network / refs/commits / d035b5d10ee1

difftreelog

Fixes after review

Max Andreev2023-03-29parent: #1550f8c.patch.diff
in: master

6 files changed

modified.docker/xcm-config/launch-config-xcm-quartz.j2diffbeforeafterboth
169 {169 {
170 "bin": "/astar/target/release/astar",170 "bin": "/astar/target/release/astar",
171 "id": "2007",171 "id": "2007",
172 "chain": "astar-dev",172 "chain": "shiden-dev",
173 "balance": "1000000000000000000000000",173 "balance": "1000000000000000000000000",
174 "nodes": [174 "nodes": [
175 {175 {
224 },224 },
225 {225 {
226 "sender": 2007,226 "sender": 2007,
227 "recipient": 1000,227 "recipient": 2095,
228 "maxCapacity": 8,228 "maxCapacity": 8,
229 "maxMessageSize": 512229 "maxMessageSize": 512
230 },230 },
231 {231 {
232 "sender": 1000,232 "sender": 2095,
233 "recipient": 2007,233 "recipient": 2007,
234 "maxCapacity": 8,234 "maxCapacity": 8,
235 "maxMessageSize": 512235 "maxMessageSize": 512
modified.docker/xcm-config/launch-config-xcm-unique.j2diffbeforeafterboth
--- a/.docker/xcm-config/launch-config-xcm-unique.j2
+++ b/.docker/xcm-config/launch-config-xcm-unique.j2
@@ -224,12 +224,12 @@
         },
         {
             "sender": 2006,
-            "recipient": 1000,
+            "recipient": 2037,
             "maxCapacity": 8,
             "maxMessageSize": 512
         },
         {
-            "sender": 1000,
+            "sender": 2037,
             "recipient": 2006,
             "maxCapacity": 8,
             "maxMessageSize": 512
modifiedtests/src/util/index.tsdiffbeforeafterboth
--- a/tests/src/util/index.ts
+++ b/tests/src/util/index.ts
@@ -11,7 +11,7 @@
 import config from '../config';
 import {ChainHelperBase} from './playgrounds/unique';
 import {ILogger} from './playgrounds/types';
-import {DevUniqueHelper, SilentLogger, SilentConsole, DevMoonbeamHelper, DevMoonriverHelper, DevAcalaHelper, DevKaruraHelper, DevRelayHelper, DevWestmintHelper, DevStatemineHelper, DevStatemintHelper, DevAstarHelper} from './playgrounds/unique.dev';
+import {DevUniqueHelper, SilentLogger, SilentConsole, DevMoonbeamHelper, DevMoonriverHelper, DevAcalaHelper, DevKaruraHelper, DevRelayHelper, DevWestmintHelper, DevStatemineHelper, DevStatemintHelper, DevAstarHelper, DevShidenHelper} from './playgrounds/unique.dev';
 import {dirname} from 'path';
 import {fileURLToPath} from 'url';
 
@@ -105,6 +105,9 @@
   return usingPlaygroundsGeneral<DevAstarHelper>(DevAstarHelper, url, code);
 };
 
+export const usingShidenPlaygrounds = (url: string, code: (helper: DevShidenHelper, privateKey: (seed: string) => Promise<IKeyringPair>) => Promise<void>) => {
+  return usingPlaygroundsGeneral<DevShidenHelper>(DevShidenHelper, url, code);
+};
 
 export const MINIMUM_DONOR_FUND = 100_000n;
 export const DONOR_FUNDING = 2_000_000n;
modifiedtests/src/util/playgrounds/unique.dev.tsdiffbeforeafterboth
--- a/tests/src/util/playgrounds/unique.dev.ts
+++ b/tests/src/util/playgrounds/unique.dev.ts
@@ -184,6 +184,17 @@
   }
 }
 
+export class DevShidenHelper extends AstarHelper {
+  wait: WaitGroup;
+
+  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {
+    options.helperBase = options.helperBase ?? DevShidenHelper;
+
+    super(logger, options);
+    this.wait = new WaitGroup(this);
+  }
+}
+
 export class DevAcalaHelper extends AcalaHelper {
   wait: WaitGroup;
 
modifiedtests/src/xcm/xcmQuartz.test.tsdiffbeforeafterboth
--- a/tests/src/xcm/xcmQuartz.test.ts
+++ b/tests/src/xcm/xcmQuartz.test.ts
@@ -18,7 +18,7 @@
 import {blake2AsHex} from '@polkadot/util-crypto';
 import config from '../config';
 import {XcmV2TraitsError} from '../interfaces';
-import {itSub, expect, describeXCM, usingPlaygrounds, usingKaruraPlaygrounds, usingRelayPlaygrounds, usingMoonriverPlaygrounds, usingStateminePlaygrounds, usingAstarPlaygrounds} from '../util';
+import {itSub, expect, describeXCM, usingPlaygrounds, usingKaruraPlaygrounds, usingRelayPlaygrounds, usingMoonriverPlaygrounds, usingStateminePlaygrounds, usingShidenPlaygrounds} from '../util';
 
 const QUARTZ_CHAIN = 2095;
 const STATEMINE_CHAIN = 1000;
@@ -983,19 +983,28 @@
 
 describeXCM('[XCM] Integration test: Exchanging tokens with Shiden', () => {
   let alice: IKeyringPair;
-  let randomAccount: IKeyringPair;
+  let sender: IKeyringPair;
+
+  // Quartz -> Shiden
+  const shidenInitialBalance = 1n * (10n ** 18n); // 1 SHD, existencial deposit required in order to perform XCM call
+  const unitsPerSecond = 228_000_000_000n; // This is Phala's value. What will be ours?
+  const qtzToShidenTransferred = 10n * (10n ** 18n); // 10 QTZ
+  const qtzToShidenArrived = 9_999_999_999_088_000_000n; // 9.999 ... QTZ, Shiden takes a commision in foreign tokens
+  const senderIinitialBalanceQTZ = 100n * (10n ** 18n); // How many QTZ sender has initially
+  const senderBalanceAfterXCM = 89_941967662676666465n; // 89.94... QTZ after XCM call
 
-  const shidenInitialBalance = 1n * (10n ** 18n);
-  const qtzToShidenAmount = 10n * (10n ** 18n);
+  // Shiden -> Quartz
+  const qtzFromShidenTransfered = 5n * (10n ** 18n); // 5 QTZ
+  const qtzOnShidenLeft = qtzToShidenArrived - qtzFromShidenTransfered; // 4.999_999_999_088_000_000n QTZ
 
   before(async () => {
     await usingPlaygrounds(async (helper, privateKey) => {
       alice = await privateKey('//Alice');
-      [randomAccount] = await helper.arrange.createAccounts([100n], alice);
-      console.log('randomAccount', randomAccount.address);
+      [sender] = await helper.arrange.createAccounts([100n], alice);
+      console.log('sender', sender.address);
     });
 
-    await usingAstarPlaygrounds(shidenUrl, async (helper) => {
+    await usingShidenPlaygrounds(shidenUrl, async (helper) => {
       console.log('1. Create foreign asset and metadata');
       await helper.assets.create(
         alice,
@@ -1027,12 +1036,10 @@
       await helper.getSudo().executeExtrinsic(alice, 'api.tx.xcAssetConfig.registerAssetLocation', [assetLocation, 1]);
 
       console.log('3. Set payment for computation');
-      // TODO this is Phala's price, what price will be for Unique?
-      const unitsPerSecond = 228_000_000_000n;
       await helper.getSudo().executeExtrinsic(alice, 'api.tx.xcAssetConfig.setAssetUnitsPerSecond', [assetLocation, unitsPerSecond]);
 
       console.log('4. Transfer 1 SDN to recepient');
-      await helper.balance.transferToSubstrate(alice, randomAccount.address, shidenInitialBalance);
+      await helper.balance.transferToSubstrate(alice, sender.address, shidenInitialBalance);
     });
   });
 
@@ -1055,7 +1062,7 @@
           X1: {
             AccountId32: {
               network: 'Any',
-              id: randomAccount.addressRaw,
+              id: sender.addressRaw,
             },
           },
         },
@@ -1072,34 +1079,42 @@
             },
           },
           fun: {
-            Fungible: qtzToShidenAmount,
+            Fungible: qtzToShidenTransferred,
           },
         },
       ],
     };
 
-    // Initial balance is 100 UNQ
-    expect(await helper.balance.getSubstrate(randomAccount.address)).to.eq(100n * (10n ** 18n));
+    // Initial balance is 100 QTZ
+    const balanceBefore = await helper.balance.getSubstrate(sender.address);
+    console.log(`Initial balance is: ${balanceBefore}`);
+    expect(balanceBefore).to.eq(senderIinitialBalanceQTZ);
 
     const feeAssetItem = 0;
-    await helper.xcm.limitedReserveTransferAssets(randomAccount, destination, beneficiary, assets, feeAssetItem, 'Unlimited');
+    await helper.xcm.limitedReserveTransferAssets(sender, destination, beneficiary, assets, feeAssetItem, 'Unlimited');
 
     // Balance after reserve transfer is less than 90
-    expect(await helper.balance.getSubstrate(randomAccount.address)).to.eq(89_941967662676666465n);
+    const balanceAfter = await helper.balance.getSubstrate(sender.address);
+    console.log(`QTZ Balance on Quartz after XCM is: ${balanceAfter}`);
+    console.log(`Quartz's QTZ commission is: ${balanceBefore - balanceAfter}`);
+    expect(balanceAfter).to.eq(senderBalanceAfterXCM);
 
-    await usingAstarPlaygrounds(shidenUrl, async (helper) => {
+    await usingShidenPlaygrounds(shidenUrl, async (helper) => {
       await helper.wait.newBlocks(3);
-      const xcUNQbalance = await helper.assets.account(1, randomAccount.address);
-      const astarBalance = await helper.balance.getSubstrate(randomAccount.address);
+      const xcQTZbalance = await helper.assets.account(1, sender.address);
+      const shidenBalance = await helper.balance.getSubstrate(sender.address);
 
-      expect(xcUNQbalance).to.eq(9_999_999_999_088_000_000n);
-      // Astar balance does not changed
-      expect(astarBalance).to.eq(1_000_000_000_000_000_000n);
+      console.log(`xcQTZ balance on Shiden after XCM is: ${xcQTZbalance}`);
+      console.log(`Shiden's QTZ commission is: ${qtzToShidenTransferred - xcQTZbalance!}`);
+
+      expect(xcQTZbalance).to.eq(qtzToShidenArrived);
+      // SHD balance does not changed:
+      expect(shidenBalance).to.eq(shidenInitialBalance);
     });
   });
 
   itSub.only('Should connect to Shiden and send QTZ back', async ({helper}) => {
-    await usingAstarPlaygrounds(shidenUrl, async (helper) => {
+    await usingShidenPlaygrounds(shidenUrl, async (helper) => {
       const destination = {
         V1: {
           parents: 1,
@@ -1118,7 +1133,7 @@
             X1: {
               AccountId32: {
                 network: 'Any',
-                id: randomAccount.addressRaw,
+                id: sender.addressRaw,
               },
             },
           },
@@ -1139,30 +1154,35 @@
               },
             },
             fun: {
-              Fungible: 5_000_000_000_000_000_000n,
+              Fungible: qtzFromShidenTransfered,
             },
           },
         ],
       };
 
       // Initial balance is 1 SDN
-      expect(await helper.balance.getSubstrate(randomAccount.address)).to.eq(1_000_000_000_000_000_000n);
+      const balanceSDNbefore = await helper.balance.getSubstrate(sender.address);
+      console.log(`SDN balance is: ${balanceSDNbefore}, it does not changed`);
+      expect(balanceSDNbefore).to.eq(shidenInitialBalance);
 
       const feeAssetItem = 0;
-      await helper.executeExtrinsic(randomAccount, 'api.tx.polkadotXcm.reserveWithdrawAssets', [destination, beneficiary, assets, feeAssetItem]);
+      // this is non-standard polkadotXcm extension for Astar only. It calls InitiateReserveWithdraw
+      await helper.executeExtrinsic(sender, 'api.tx.polkadotXcm.reserveWithdrawAssets', [destination, beneficiary, assets, feeAssetItem]);
 
       // Balance after reserve transfer is less than 1 SDN
-      const xcUNQbalance = await helper.assets.account(1, randomAccount.address);
-      const balanceSDN = await helper.balance.getSubstrate(randomAccount.address);
+      const xcQTZbalance = await helper.assets.account(1, sender.address);
+      const balanceSDN = await helper.balance.getSubstrate(sender.address);
+      console.log(`xcQTZ balance on Shiden after XCM is: ${xcQTZbalance}`);
 
-      // Assert: xcQTZ balance decreased
-      expect(xcUNQbalance).to.eq(4_999_999_999_088_000_000n);
+      // Assert: xcQTZ balance correctly decreased
+      expect(xcQTZbalance).to.eq(qtzOnShidenLeft);
       // Assert: SDN balance is 0.996...
       expect(balanceSDN / (10n ** 15n)).to.eq(996n);
     });
 
     await helper.wait.newBlocks(3);
-    const balanceUNQ = await helper.balance.getSubstrate(randomAccount.address);
-    expect(balanceUNQ).to.eq(89_941967662676666465n + 5_000_000_000_000_000_000n);
+    const balanceQTZ = await helper.balance.getSubstrate(sender.address);
+    console.log(`QTZ Balance on Quartz after XCM is: ${balanceQTZ}`);
+    expect(balanceQTZ).to.eq(senderBalanceAfterXCM + qtzFromShidenTransfered);
   });
 });
modifiedtests/src/xcm/xcmUnique.test.tsdiffbeforeafterboth
--- a/tests/src/xcm/xcmUnique.test.ts
+++ b/tests/src/xcm/xcmUnique.test.ts
@@ -987,8 +987,17 @@
   let alice: IKeyringPair;
   let randomAccount: IKeyringPair;
 
-  const astarInitialBalance = 1n * (10n ** 18n);
-  const unqToAstarAmount = 10n * (10n ** 18n);
+  // Unique -> Astar
+  const astarInitialBalance = 1n * (10n ** 18n); // 1 ASTR. Existencial deposit required in order to perform XCM call
+  const unitsPerSecond = 228_000_000_000n; // This is Phala's value. What will be ours?
+  const unqToAstarTransferred = 10n * (10n ** 18n); // 10 UNQ
+  const unqToAstarArrived = 9_999_999_999_088_000_000n; // 9.999 ... UNQ, Shiden takes a commision in foreign tokens
+  const senderIinitialBalanceUNQ = 100n * (10n ** 18n); // How many UNQ sender has initially
+  const senderBalanceAfterXCM = 89_941967662676666465n; // 89.94... UNQ after XCM call
+
+  // Astar -> Unique
+  const unqFromAstarTransfered = 5n * (10n ** 18n); // 5 UNQ
+  const unqOnAstarLeft = unqToAstarArrived - unqFromAstarTransfered; // 4.999_999_999_088_000_000n UNQ
 
   before(async () => {
     await usingPlaygrounds(async (helper, privateKey) => {
@@ -1029,8 +1038,6 @@
       await helper.getSudo().executeExtrinsic(alice, 'api.tx.xcAssetConfig.registerAssetLocation', [assetLocation, 1]);
 
       console.log('3. Set payment for computation');
-      // TODO this is Phala's price, what price will be for Unique?
-      const unitsPerSecond = 228_000_000_000n;
       await helper.getSudo().executeExtrinsic(alice, 'api.tx.xcAssetConfig.setAssetUnitsPerSecond', [assetLocation, unitsPerSecond]);
 
       console.log('4. Transfer 1 ASTR to recepient');
@@ -1074,29 +1081,37 @@
             },
           },
           fun: {
-            Fungible: unqToAstarAmount,
+            Fungible: unqToAstarTransferred,
           },
         },
       ],
     };
 
     // Initial balance is 100 UNQ
-    expect(await helper.balance.getSubstrate(randomAccount.address)).to.eq(100n * (10n ** 18n));
+    const balanceBefore = await helper.balance.getSubstrate(randomAccount.address);
+    console.log(`Initial balance is: ${balanceBefore}`);
+    expect(balanceBefore).to.eq(senderIinitialBalanceUNQ);
 
     const feeAssetItem = 0;
     await helper.xcm.limitedReserveTransferAssets(randomAccount, destination, beneficiary, assets, feeAssetItem, 'Unlimited');
 
     // Balance after reserve transfer is less than 90
-    expect(await helper.balance.getSubstrate(randomAccount.address)).to.eq(89_941967662676666465n);
+    const balanceAfter = await helper.balance.getSubstrate(randomAccount.address);
+    console.log(`UNQ Balance on Unique after XCM is: ${balanceAfter}`);
+    console.log(`Unique's UNQ commission is: ${balanceBefore - balanceAfter}`);
+    expect(balanceAfter).to.eq(senderBalanceAfterXCM);
 
     await usingAstarPlaygrounds(astarUrl, async (helper) => {
       await helper.wait.newBlocks(3);
       const xcUNQbalance = await helper.assets.account(1, randomAccount.address);
       const astarBalance = await helper.balance.getSubstrate(randomAccount.address);
 
-      expect(xcUNQbalance).to.eq(9_999_999_999_088_000_000n);
+      console.log(`xcUNQ balance on Astar after XCM is: ${xcUNQbalance}`);
+      console.log(`Astar's UNQ commission is: ${unqToAstarTransferred - xcUNQbalance!}`);
+
+      expect(xcUNQbalance).to.eq(unqToAstarArrived);
       // Astar balance does not changed
-      expect(astarBalance).to.eq(1_000_000_000_000_000_000n);
+      expect(astarBalance).to.eq(astarInitialBalance);
     });
   });
 
@@ -1141,30 +1156,35 @@
               },
             },
             fun: {
-              Fungible: 5_000_000_000_000_000_000n,
+              Fungible: unqFromAstarTransfered,
             },
           },
         ],
       };
 
       // Initial balance is 1 ASTR
-      expect(await helper.balance.getSubstrate(randomAccount.address)).to.eq(1_000_000_000_000_000_000n);
+      const balanceASTRbefore = await helper.balance.getSubstrate(randomAccount.address);
+      console.log(`ASTR balance is: ${balanceASTRbefore}, it does not changed`);
+      expect(balanceASTRbefore).to.eq(astarInitialBalance);
 
       const feeAssetItem = 0;
+      // this is non-standard polkadotXcm extension for Astar only. It calls InitiateReserveWithdraw
       await helper.executeExtrinsic(randomAccount, 'api.tx.polkadotXcm.reserveWithdrawAssets', [destination, beneficiary, assets, feeAssetItem]);
 
       const xcUNQbalance = await helper.assets.account(1, randomAccount.address);
       const balanceAstar = await helper.balance.getSubstrate(randomAccount.address);
+      console.log(`xcUNQ balance on Astar after XCM is: ${xcUNQbalance}`);
 
-      // Assert: xcUNQ balance decreased
-      expect(xcUNQbalance).to.eq(4_999_999_999_088_000_000n);
+      // Assert: xcUNQ balance correctly decreased
+      expect(xcUNQbalance).to.eq(unqOnAstarLeft);
       // Assert: ASTR balance is 0.996...
       expect(balanceAstar / (10n ** 15n)).to.eq(996n);
     });
 
     await helper.wait.newBlocks(3);
     const balanceUNQ = await helper.balance.getSubstrate(randomAccount.address);
-    expect(balanceUNQ).to.eq(89_941967662676666465n + 5_000_000_000_000_000_000n);
+    console.log(`UNQ Balance on Unique after XCM is: ${balanceUNQ}`);
+    expect(balanceUNQ).to.eq(senderBalanceAfterXCM + unqFromAstarTransfered);
   });
 
   itSub.skip('Should not accept limitedReserveTransfer of UNQ from ASTAR', async ({helper}) => {
@@ -1208,30 +1228,18 @@
               },
             },
             fun: {
-              Fungible: 5_000_000_000_000_000_000n, // TODO set another value
+              Fungible: unqFromAstarTransfered,
             },
           },
         ],
       };
 
       // Initial balance is 1 ASTAR
-      expect(await helper.balance.getSubstrate(randomAccount.address)).to.eq(1_000_000_000_000_000_000n);
+      expect(await helper.balance.getSubstrate(randomAccount.address)).to.eq(astarInitialBalance);
 
       const feeAssetItem = 0;
+      // TODO: expect rejected:
       await helper.xcm.limitedReserveTransferAssets(randomAccount, destination, beneficiary, assets, feeAssetItem, 'Unlimited');
-
-      // Balance after reserve transfer is less than 1 ASTAR
-      const xcUNQbalance = await helper.assets.account(1, randomAccount.address);
-      const balanceAstar = await helper.balance.getSubstrate(randomAccount.address);
-
-      // xcUNQ balance decreased
-      expect(xcUNQbalance).to.eq(4_999_999_999_088_000_000n);
-      // Astar balance is 0.997...
-      expect(balanceAstar / (10n ** 15n)).to.eq(997n);
     });
-
-    await helper.wait.newBlocks(3);
-    const balanceUNQ = await helper.balance.getSubstrate(randomAccount.address);
-    expect(balanceUNQ).to.eq(89_941967662676666465n + 5_000_000_000_000_000_000n);
   });
 });