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

difftreelog

fix parse bitint to decimals

Daniel Shiposha2022-09-06parent: #1a6fe8b.patch.diff
in: master

4 files changed

modifiedtests/src/util/helpers.tsdiffbeforeafterboth
--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -103,6 +103,24 @@
   return api.registry.createType('AccountId', '0x' + number.toString(16).padStart(64, '0')).toJSON();
 }
 
+export function bigIntToDecimals(number: bigint, decimals = 18): string {
+  let numberStr = number.toString();
+  console.log('[0] str = ', numberStr);
+
+  // Get rid of `n` at the end
+  numberStr = numberStr.substring(0, numberStr.length - 1);
+  console.log('[1] str = ', numberStr);
+
+  const dotPos = numberStr.length - decimals;
+  if (dotPos <= 0) {
+    return '0.' + numberStr;
+  } else {
+    const intPart = numberStr.substring(0, dotPos);
+    const fractPart = numberStr.substring(dotPos);
+    return intPart + '.' + fractPart;
+  }
+}
+
 export function normalizeAccountId(input: string | AccountId | CrossAccountId | IKeyringPair): CrossAccountId {
   if (typeof input === 'string') {
     if (input.length >= 47) {
modifiedtests/src/xcm/xcmOpal.test.tsdiffbeforeafterboth
--- a/tests/src/xcm/xcmOpal.test.ts
+++ b/tests/src/xcm/xcmOpal.test.ts
@@ -21,7 +21,7 @@
 import {ApiOptions} from '@polkadot/api/types';
 import {IKeyringPair} from '@polkadot/types/types';
 import usingApi, {submitTransactionAsync} from './../substrate/substrate-api';
-import {getGenericResult, paraSiblingSovereignAccount} from './../util/helpers';
+import {bigIntToDecimals, getGenericResult, paraSiblingSovereignAccount} from './../util/helpers';
 import waitNewBlocks from './../substrate/wait-new-blocks';
 import {normalizeAccountId} from './../util/helpers';
 import getBalance from './../substrate/get-balance';
@@ -43,6 +43,8 @@
 const ASSET_METADATA_DESCRIPTION = 'USDT';
 const ASSET_METADATA_MINIMAL_BALANCE = 1;
 
+const WESTMINT_DECIMALS = 12;
+
 const TRANSFER_AMOUNT = 1_000_000_000_000_000_000n;
 
 // 10,000.00 (ten thousands) USDT
@@ -281,7 +283,10 @@
       [balanceStmnAfter] = await getBalance(api, [alice.address]);
 
       // common good parachain take commission in it native token
-      console.log('Opal to Westmint transaction fees on Westmint: %s WND', balanceStmnBefore - balanceStmnAfter);
+      console.log(
+        'Opal to Westmint transaction fees on Westmint: %s WND',
+        bigIntToDecimals(balanceStmnBefore - balanceStmnAfter, WESTMINT_DECIMALS),
+      );
       expect(balanceStmnBefore > balanceStmnAfter).to.be.true;
 
     }, statemineApiOptions);
@@ -297,10 +302,16 @@
 
       // commission has not paid in USDT token
       expect(free == TRANSFER_AMOUNT).to.be.true;
-      console.log('Opal to Westmint transaction fees on Opal: %s USDT', TRANSFER_AMOUNT - free);
+      console.log(
+        'Opal to Westmint transaction fees on Opal: %s USDT',
+        bigIntToDecimals(TRANSFER_AMOUNT - free),
+      );
       // ... and parachain native token
       expect(balanceOpalAfter == balanceOpalBefore).to.be.true;
-      console.log('Opal to Westmint transaction fees on Opal: %s WND', balanceOpalAfter - balanceOpalBefore);
+      console.log(
+        'Opal to Westmint transaction fees on Opal: %s WND',
+        bigIntToDecimals(balanceOpalAfter - balanceOpalBefore, WESTMINT_DECIMALS),
+      );
 
     }, uniqueApiOptions);
     
@@ -451,8 +462,14 @@
       [balanceBobAfter] = await getBalance(api, [bob.address]);
       balanceBobRelayTokenAfter = BigInt(((await api.query.tokens.accounts(bob.addressRaw, {NativeAssetId: 'Parent'})).toJSON() as any).free);
       const wndFee = balanceBobRelayTokenAfter - TRANSFER_AMOUNT_RELAY - balanceBobRelayTokenBefore; 
-      console.log('Relay (Westend) to Opal transaction fees: %s OPL', balanceBobAfter - balanceBobBefore);
-      console.log('Relay (Westend) to Opal transaction fees: %s WND', wndFee);
+      console.log(
+        'Relay (Westend) to Opal transaction fees: %s OPL',
+        bigIntToDecimals(balanceBobAfter - balanceBobBefore),
+      );
+      console.log(
+        'Relay (Westend) to Opal transaction fees: %s WND',
+        bigIntToDecimals(wndFee, WESTMINT_DECIMALS),
+      );
       expect(balanceBobBefore == balanceBobAfter).to.be.true;
       expect(balanceBobRelayTokenBefore < balanceBobRelayTokenAfter).to.be.true;
     }, uniqueApiOptions2);
@@ -504,4 +521,4 @@
     }, uniqueApiOptions);
   });
 
-});
\ No newline at end of file
+});
modifiedtests/src/xcm/xcmQuartz.test.tsdiffbeforeafterboth
--- a/tests/src/xcm/xcmQuartz.test.ts
+++ b/tests/src/xcm/xcmQuartz.test.ts
@@ -21,7 +21,7 @@
 import {ApiOptions} from '@polkadot/api/types';
 import {IKeyringPair} from '@polkadot/types/types';
 import usingApi, {submitTransactionAsync} from '../substrate/substrate-api';
-import {getGenericResult, generateKeyringPair, waitEvent, describe_xcm} from '../util/helpers';
+import {getGenericResult, generateKeyringPair, waitEvent, describe_xcm, bigIntToDecimals} from '../util/helpers';
 import {MultiLocation} from '@polkadot/types/interfaces';
 import {blake2AsHex} from '@polkadot/util-crypto';
 import waitNewBlocks from '../substrate/wait-new-blocks';
@@ -37,6 +37,8 @@
 const KARURA_PORT = 9946;
 const MOONRIVER_PORT = 9947;
 
+const KARURA_DECIMALS = 12;
+
 const TRANSFER_AMOUNT = 2000000000000000000000000n;
 
 function parachainApiOptions(port: number): ApiOptions {
@@ -182,7 +184,7 @@
       [balanceQuartzTokenMiddle] = await getBalance(api, [randomAccount.address]);
   
       const qtzFees = balanceQuartzTokenInit - balanceQuartzTokenMiddle - TRANSFER_AMOUNT;
-      console.log('[Quartz -> Karura] transaction fees on Quartz: %s QTZ', qtzFees);
+      console.log('[Quartz -> Karura] transaction fees on Quartz: %s QTZ', bigIntToDecimals(qtzFees));
       expect(qtzFees > 0n).to.be.true;
     });
   
@@ -198,8 +200,11 @@
         const karFees = balanceKaruraTokenInit - balanceKaruraTokenMiddle;
         const qtzIncomeTransfer = balanceQuartzForeignTokenMiddle - balanceQuartzForeignTokenInit;
 
-        console.log('[Quartz -> Karura] transaction fees on Karura: %s KAR', karFees);
-        console.log('[Quartz -> Karura] income %s QTZ', qtzIncomeTransfer);
+        console.log('
+          [Quartz -> Karura] transaction fees on Karura: %s KAR',
+          bigIntToDecimals(karFees, KARURA_DECIMALS),
+        );
+        console.log('[Quartz -> Karura] income %s QTZ', bigIntToDecimals(qtzIncomeTransfer));
         expect(karFees == 0n).to.be.true;
         expect(qtzIncomeTransfer == TRANSFER_AMOUNT).to.be.true;
       },
@@ -249,8 +254,11 @@
         const karFees = balanceKaruraTokenMiddle - balanceKaruraTokenFinal;
         const qtzOutcomeTransfer = balanceQuartzForeignTokenMiddle - balanceQuartzForeignTokenFinal;
 
-        console.log('[Karura -> Quartz] transaction fees on Karura: %s KAR', karFees);
-        console.log('[Karura -> Quartz] outcome %s QTZ', qtzOutcomeTransfer);
+        console.log(
+          '[Karura -> Quartz] transaction fees on Karura: %s KAR',
+          bigIntToDecimals(karFees, KARURA_DECIMALS),
+        );
+        console.log('[Karura -> Quartz] outcome %s QTZ', bigIntToDecimals(qtzOutcomeTransfer));
 
         expect(karFees > 0).to.be.true;
         expect(qtzOutcomeTransfer == TRANSFER_AMOUNT).to.be.true;
@@ -266,10 +274,10 @@
       const actuallyDelivered = balanceQuartzTokenFinal - balanceQuartzTokenMiddle;
       expect(actuallyDelivered > 0).to.be.true;
 
-      console.log('[Karura -> Quartz] actually delivered %s QTZ', actuallyDelivered);
+      console.log('[Karura -> Quartz] actually delivered %s QTZ', bigIntToDecimals(actuallyDelivered));
   
       const qtzFees = TRANSFER_AMOUNT - actuallyDelivered;
-      console.log('[Karura -> Quartz] transaction fees on Quartz: %s QTZ', qtzFees);
+      console.log('[Karura -> Quartz] transaction fees on Quartz: %s QTZ', bigIntToDecimals(qtzFees));
       expect(qtzFees == 0n).to.be.true;
     });
   });
@@ -581,7 +589,7 @@
       expect(balanceQuartzTokenMiddle < balanceQuartzTokenInit).to.be.true;
 
       const transactionFees = balanceQuartzTokenInit - balanceQuartzTokenMiddle - TRANSFER_AMOUNT;
-      console.log('[Quartz -> Moonriver] transaction fees on Quartz: %s QTZ', transactionFees);
+      console.log('[Quartz -> Moonriver] transaction fees on Quartz: %s QTZ', bigIntToDecimals(transactionFees));
       expect(transactionFees > 0).to.be.true;
     });
 
@@ -592,7 +600,7 @@
         [balanceMovrTokenMiddle] = await getBalance(api, [randomAccountMoonriver.address]);
 
         const movrFees = balanceMovrTokenInit - balanceMovrTokenMiddle;
-        console.log('[Quartz -> Moonriver] transaction fees on Moonriver: %s MOVR', movrFees);
+        console.log('[Quartz -> Moonriver] transaction fees on Moonriver: %s MOVR',bigIntToDecimals(movrFees));
         expect(movrFees == 0n).to.be.true;
 
         const qtzRandomAccountAsset = (
@@ -601,7 +609,7 @@
 
         balanceForeignQtzTokenMiddle = BigInt(qtzRandomAccountAsset['balance']);
         const qtzIncomeTransfer = balanceForeignQtzTokenMiddle - balanceForeignQtzTokenInit;
-        console.log('[Quartz -> Moonriver] income %s QTZ', qtzIncomeTransfer);
+        console.log('[Quartz -> Moonriver] income %s QTZ', bigIntToDecimals(qtzIncomeTransfer));
         expect(qtzIncomeTransfer == TRANSFER_AMOUNT).to.be.true;
       },
       moonriverOptions(),
@@ -647,7 +655,7 @@
         [balanceMovrTokenFinal] = await getBalance(api, [randomAccountMoonriver.address]);
 
         const movrFees = balanceMovrTokenMiddle - balanceMovrTokenFinal;
-        console.log('[Moonriver -> Quartz] transaction fees on Moonriver: %s MOVR', movrFees);
+        console.log('[Moonriver -> Quartz] transaction fees on Moonriver: %s MOVR', bigIntToDecimals(movrFees));
         expect(movrFees > 0).to.be.true;
 
         const qtzRandomAccountAsset = (
@@ -659,7 +667,7 @@
         balanceForeignQtzTokenFinal = 0n;
 
         const qtzOutcomeTransfer = balanceForeignQtzTokenMiddle - balanceForeignQtzTokenFinal;
-        console.log('[Quartz -> Moonriver] outcome %s QTZ', qtzOutcomeTransfer);
+        console.log('[Quartz -> Moonriver] outcome %s QTZ', bigIntToDecimals(qtzOutcomeTransfer));
         expect(qtzOutcomeTransfer == TRANSFER_AMOUNT).to.be.true;
       },
       moonriverOptions(),
@@ -672,10 +680,10 @@
       const actuallyDelivered = balanceQuartzTokenFinal - balanceQuartzTokenMiddle;
       expect(actuallyDelivered > 0).to.be.true;
 
-      console.log('[Moonriver -> Quartz] actually delivered %s QTZ', actuallyDelivered);
+      console.log('[Moonriver -> Quartz] actually delivered %s QTZ', bigIntToDecimals(actuallyDelivered));
 
       const qtzFees = TRANSFER_AMOUNT - actuallyDelivered;
-      console.log('[Moonriver -> Quartz] transaction fees on Quartz: %s QTZ', qtzFees);
+      console.log('[Moonriver -> Quartz] transaction fees on Quartz: %s QTZ', bigIntToDecimals(qtzFees));
       expect(qtzFees == 0n).to.be.true;
     });
   });
modifiedtests/src/xcm/xcmUnique.test.tsdiffbeforeafterboth
21import {ApiOptions} from '@polkadot/api/types';21import {ApiOptions} from '@polkadot/api/types';
22import {IKeyringPair} from '@polkadot/types/types';22import {IKeyringPair} from '@polkadot/types/types';
23import usingApi, {submitTransactionAsync} from '../substrate/substrate-api';23import usingApi, {submitTransactionAsync} from '../substrate/substrate-api';
24import {getGenericResult, generateKeyringPair, waitEvent, describe_xcm} from '../util/helpers';24import {getGenericResult, generateKeyringPair, waitEvent, describe_xcm, bigIntToDecimals} from '../util/helpers';
25import {MultiLocation} from '@polkadot/types/interfaces';25import {MultiLocation} from '@polkadot/types/interfaces';
26import {blake2AsHex} from '@polkadot/util-crypto';26import {blake2AsHex} from '@polkadot/util-crypto';
27import waitNewBlocks from '../substrate/wait-new-blocks';27import waitNewBlocks from '../substrate/wait-new-blocks';
37const ACALA_PORT = 9946;37const ACALA_PORT = 9946;
38const MOONBEAM_PORT = 9947;38const MOONBEAM_PORT = 9947;
39
40const ACALA_DECIMALS = 12;
3941
40const TRANSFER_AMOUNT = 2000000000000000000000000n;42const TRANSFER_AMOUNT = 2000000000000000000000000n;
4143
182 [balanceUniqueTokenMiddle] = await getBalance(api, [randomAccount.address]);184 [balanceUniqueTokenMiddle] = await getBalance(api, [randomAccount.address]);
183 185
184 const unqFees = balanceUniqueTokenInit - balanceUniqueTokenMiddle - TRANSFER_AMOUNT;186 const unqFees = balanceUniqueTokenInit - balanceUniqueTokenMiddle - TRANSFER_AMOUNT;
185 console.log('[Unique -> Acala] transaction fees on Unique: %s UNQ', unqFees);187 console.log('[Unique -> Acala] transaction fees on Unique: %s UNQ', bigIntToDecimals(unqFees));
186 expect(unqFees > 0n).to.be.true;188 expect(unqFees > 0n).to.be.true;
187 });189 });
188 190
200202
201 console.log('[Unique -> Acala] transaction fees on Acala: %s ACA', acaFees);203 console.log(
204 '[Unique -> Acala] transaction fees on Acala: %s ACA',
205 bigIntToDecimals(acaFees, ACALA_DECIMALS),
206 );
202 console.log('[Unique -> Acala] income %s UNQ', unqIncomeTransfer);207 console.log('[Unique -> Acala] income %s UNQ', bigIntToDecimals(unqIncomeTransfer));
203 expect(acaFees == 0n).to.be.true;208 expect(acaFees == 0n).to.be.true;
204 expect(unqIncomeTransfer == TRANSFER_AMOUNT).to.be.true;209 expect(unqIncomeTransfer == TRANSFER_AMOUNT).to.be.true;
205 },210 },
251256
252 console.log('[Acala -> Unique] transaction fees on Acala: %s ACA', acaFees);257 console.log(
258 '[Acala -> Unique] transaction fees on Acala: %s ACA',
259 bigIntToDecimals(acaFees, ACALA_DECIMALS),
260 );
253 console.log('[Acala -> Unique] outcome %s UNQ', unqOutcomeTransfer);261 console.log('[Acala -> Unique] outcome %s UNQ', bigIntToDecimals(unqOutcomeTransfer));
254262
255 expect(acaFees > 0).to.be.true;263 expect(acaFees > 0).to.be.true;
256 expect(unqOutcomeTransfer == TRANSFER_AMOUNT).to.be.true;264 expect(unqOutcomeTransfer == TRANSFER_AMOUNT).to.be.true;
266 const actuallyDelivered = balanceUniqueTokenFinal - balanceUniqueTokenMiddle;274 const actuallyDelivered = balanceUniqueTokenFinal - balanceUniqueTokenMiddle;
267 expect(actuallyDelivered > 0).to.be.true;275 expect(actuallyDelivered > 0).to.be.true;
268276
269 console.log('[Acala -> Unique] actually delivered %s UNQ', actuallyDelivered);277 console.log('[Acala -> Unique] actually delivered %s UNQ', bigIntToDecimals(actuallyDelivered));
270 278
271 const unqFees = TRANSFER_AMOUNT - actuallyDelivered;279 const unqFees = TRANSFER_AMOUNT - actuallyDelivered;
272 console.log('[Acala -> Unique] transaction fees on Unique: %s UNQ', unqFees);280 console.log('[Acala -> Unique] transaction fees on Unique: %s UNQ', bigIntToDecimals(unqFees));
273 expect(unqFees == 0n).to.be.true;281 expect(unqFees == 0n).to.be.true;
274 });282 });
275 });283 });
581 expect(balanceUniqueTokenMiddle < balanceUniqueTokenInit).to.be.true;589 expect(balanceUniqueTokenMiddle < balanceUniqueTokenInit).to.be.true;
582590
583 const transactionFees = balanceUniqueTokenInit - balanceUniqueTokenMiddle - TRANSFER_AMOUNT;591 const transactionFees = balanceUniqueTokenInit - balanceUniqueTokenMiddle - TRANSFER_AMOUNT;
584 console.log('[Unique -> Moonbeam] transaction fees on Unique: %s UNQ', transactionFees);592 console.log('[Unique -> Moonbeam] transaction fees on Unique: %s UNQ', bigIntToDecimals(transactionFees));
585 expect(transactionFees > 0).to.be.true;593 expect(transactionFees > 0).to.be.true;
586 });594 });
587595
592 [balanceGlmrTokenMiddle] = await getBalance(api, [randomAccountMoonbeam.address]);600 [balanceGlmrTokenMiddle] = await getBalance(api, [randomAccountMoonbeam.address]);
593601
594 const glmrFees = balanceGlmrTokenInit - balanceGlmrTokenMiddle;602 const glmrFees = balanceGlmrTokenInit - balanceGlmrTokenMiddle;
595 console.log('[Unique -> Moonbeam] transaction fees on Moonbeam: %s GLMR', glmrFees);603 console.log('[Unique -> Moonbeam] transaction fees on Moonbeam: %s GLMR', bigIntToDecimals(glmrFees));
596 expect(glmrFees == 0n).to.be.true;604 expect(glmrFees == 0n).to.be.true;
597605
598 const unqRandomAccountAsset = (606 const unqRandomAccountAsset = (
601609
602 balanceForeignUnqTokenMiddle = BigInt(unqRandomAccountAsset['balance']);610 balanceForeignUnqTokenMiddle = BigInt(unqRandomAccountAsset['balance']);
603 const unqIncomeTransfer = balanceForeignUnqTokenMiddle - balanceForeignUnqTokenInit;611 const unqIncomeTransfer = balanceForeignUnqTokenMiddle - balanceForeignUnqTokenInit;
604 console.log('[Unique -> Moonbeam] income %s UNQ', unqIncomeTransfer);612 console.log('[Unique -> Moonbeam] income %s UNQ', bigIntToDecimals(unqIncomeTransfer));
605 expect(unqIncomeTransfer == TRANSFER_AMOUNT).to.be.true;613 expect(unqIncomeTransfer == TRANSFER_AMOUNT).to.be.true;
606 },614 },
607 moonbeamOptions(),615 moonbeamOptions(),
647 [balanceGlmrTokenFinal] = await getBalance(api, [randomAccountMoonbeam.address]);655 [balanceGlmrTokenFinal] = await getBalance(api, [randomAccountMoonbeam.address]);
648656
649 const glmrFees = balanceGlmrTokenMiddle - balanceGlmrTokenFinal;657 const glmrFees = balanceGlmrTokenMiddle - balanceGlmrTokenFinal;
650 console.log('[Moonbeam -> Unique] transaction fees on Moonbeam: %s GLMR', glmrFees);658 console.log('[Moonbeam -> Unique] transaction fees on Moonbeam: %s GLMR', bigIntToDecimals(glmrFees));
651 expect(glmrFees > 0).to.be.true;659 expect(glmrFees > 0).to.be.true;
652660
653 const unqRandomAccountAsset = (661 const unqRandomAccountAsset = (
659 balanceForeignUnqTokenFinal = 0n;667 balanceForeignUnqTokenFinal = 0n;
660668
661 const unqOutcomeTransfer = balanceForeignUnqTokenMiddle - balanceForeignUnqTokenFinal;669 const unqOutcomeTransfer = balanceForeignUnqTokenMiddle - balanceForeignUnqTokenFinal;
662 console.log('[Unique -> Moonbeam] outcome %s UNQ', unqOutcomeTransfer);670 console.log('[Unique -> Moonbeam] outcome %s UNQ', bigIntToDecimals(unqOutcomeTransfer));
663 expect(unqOutcomeTransfer == TRANSFER_AMOUNT).to.be.true;671 expect(unqOutcomeTransfer == TRANSFER_AMOUNT).to.be.true;
664 },672 },
665 moonbeamOptions(),673 moonbeamOptions(),
672 const actuallyDelivered = balanceUniqueTokenFinal - balanceUniqueTokenMiddle;680 const actuallyDelivered = balanceUniqueTokenFinal - balanceUniqueTokenMiddle;
673 expect(actuallyDelivered > 0).to.be.true;681 expect(actuallyDelivered > 0).to.be.true;
674682
675 console.log('[Moonbeam -> Unique] actually delivered %s UNQ', actuallyDelivered);683 console.log('[Moonbeam -> Unique] actually delivered %s UNQ', bigIntToDecimals(actuallyDelivered));
676684
677 const unqFees = TRANSFER_AMOUNT - actuallyDelivered;685 const unqFees = TRANSFER_AMOUNT - actuallyDelivered;
678 console.log('[Moonbeam -> Unique] transaction fees on Unique: %s UNQ', unqFees);686 console.log('[Moonbeam -> Unique] transaction fees on Unique: %s UNQ', bigIntToDecimals(unqFees));
679 expect(unqFees == 0n).to.be.true;687 expect(unqFees == 0n).to.be.true;
680 });688 });
681 });689 });