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
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, paraSiblingSovereignAccount} from './../util/helpers';24import {bigIntToDecimals, getGenericResult, paraSiblingSovereignAccount} from './../util/helpers';
25import waitNewBlocks from './../substrate/wait-new-blocks';25import waitNewBlocks from './../substrate/wait-new-blocks';
26import {normalizeAccountId} from './../util/helpers';26import {normalizeAccountId} from './../util/helpers';
27import getBalance from './../substrate/get-balance';27import getBalance from './../substrate/get-balance';
43const ASSET_METADATA_DESCRIPTION = 'USDT';43const ASSET_METADATA_DESCRIPTION = 'USDT';
44const ASSET_METADATA_MINIMAL_BALANCE = 1;44const ASSET_METADATA_MINIMAL_BALANCE = 1;
45
46const WESTMINT_DECIMALS = 12;
4547
46const TRANSFER_AMOUNT = 1_000_000_000_000_000_000n;48const TRANSFER_AMOUNT = 1_000_000_000_000_000_000n;
4749
283 // common good parachain take commission in it native token285 // common good parachain take commission in it native token
284 console.log('Opal to Westmint transaction fees on Westmint: %s WND', balanceStmnBefore - balanceStmnAfter);286 console.log(
287 'Opal to Westmint transaction fees on Westmint: %s WND',
288 bigIntToDecimals(balanceStmnBefore - balanceStmnAfter, WESTMINT_DECIMALS),
289 );
285 expect(balanceStmnBefore > balanceStmnAfter).to.be.true;290 expect(balanceStmnBefore > balanceStmnAfter).to.be.true;
286291
299 expect(free == TRANSFER_AMOUNT).to.be.true;304 expect(free == TRANSFER_AMOUNT).to.be.true;
300 console.log('Opal to Westmint transaction fees on Opal: %s USDT', TRANSFER_AMOUNT - free);305 console.log(
306 'Opal to Westmint transaction fees on Opal: %s USDT',
307 bigIntToDecimals(TRANSFER_AMOUNT - free),
308 );
301 // ... and parachain native token309 // ... and parachain native token
302 expect(balanceOpalAfter == balanceOpalBefore).to.be.true;310 expect(balanceOpalAfter == balanceOpalBefore).to.be.true;
303 console.log('Opal to Westmint transaction fees on Opal: %s WND', balanceOpalAfter - balanceOpalBefore);311 console.log(
312 'Opal to Westmint transaction fees on Opal: %s WND',
313 bigIntToDecimals(balanceOpalAfter - balanceOpalBefore, WESTMINT_DECIMALS),
314 );
304315
305 }, uniqueApiOptions);316 }, uniqueApiOptions);
453 const wndFee = balanceBobRelayTokenAfter - TRANSFER_AMOUNT_RELAY - balanceBobRelayTokenBefore; 464 const wndFee = balanceBobRelayTokenAfter - TRANSFER_AMOUNT_RELAY - balanceBobRelayTokenBefore;
454 console.log('Relay (Westend) to Opal transaction fees: %s OPL', balanceBobAfter - balanceBobBefore);465 console.log(
466 'Relay (Westend) to Opal transaction fees: %s OPL',
467 bigIntToDecimals(balanceBobAfter - balanceBobBefore),
468 );
455 console.log('Relay (Westend) to Opal transaction fees: %s WND', wndFee);469 console.log(
470 'Relay (Westend) to Opal transaction fees: %s WND',
471 bigIntToDecimals(wndFee, WESTMINT_DECIMALS),
472 );
456 expect(balanceBobBefore == balanceBobAfter).to.be.true;473 expect(balanceBobBefore == balanceBobAfter).to.be.true;
457 expect(balanceBobRelayTokenBefore < balanceBobRelayTokenAfter).to.be.true;474 expect(balanceBobRelayTokenBefore < balanceBobRelayTokenAfter).to.be.true;
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
--- a/tests/src/xcm/xcmUnique.test.ts
+++ b/tests/src/xcm/xcmUnique.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 ACALA_PORT = 9946;
 const MOONBEAM_PORT = 9947;
 
+const ACALA_DECIMALS = 12;
+
 const TRANSFER_AMOUNT = 2000000000000000000000000n;
 
 function parachainApiOptions(port: number): ApiOptions {
@@ -182,7 +184,7 @@
       [balanceUniqueTokenMiddle] = await getBalance(api, [randomAccount.address]);
   
       const unqFees = balanceUniqueTokenInit - balanceUniqueTokenMiddle - TRANSFER_AMOUNT;
-      console.log('[Unique -> Acala] transaction fees on Unique: %s UNQ', unqFees);
+      console.log('[Unique -> Acala] transaction fees on Unique: %s UNQ', bigIntToDecimals(unqFees));
       expect(unqFees > 0n).to.be.true;
     });
   
@@ -198,8 +200,11 @@
         const acaFees = balanceAcalaTokenInit - balanceAcalaTokenMiddle;
         const unqIncomeTransfer = balanceUniqueForeignTokenMiddle - balanceUniqueForeignTokenInit;
 
-        console.log('[Unique -> Acala] transaction fees on Acala: %s ACA', acaFees);
-        console.log('[Unique -> Acala] income %s UNQ', unqIncomeTransfer);
+        console.log(
+          '[Unique -> Acala] transaction fees on Acala: %s ACA',
+          bigIntToDecimals(acaFees, ACALA_DECIMALS),
+        );
+        console.log('[Unique -> Acala] income %s UNQ', bigIntToDecimals(unqIncomeTransfer));
         expect(acaFees == 0n).to.be.true;
         expect(unqIncomeTransfer == TRANSFER_AMOUNT).to.be.true;
       },
@@ -249,8 +254,11 @@
         const acaFees = balanceAcalaTokenMiddle - balanceAcalaTokenFinal;
         const unqOutcomeTransfer = balanceUniqueForeignTokenMiddle - balanceUniqueForeignTokenFinal;
 
-        console.log('[Acala -> Unique] transaction fees on Acala: %s ACA', acaFees);
-        console.log('[Acala -> Unique] outcome %s UNQ', unqOutcomeTransfer);
+        console.log(
+          '[Acala -> Unique] transaction fees on Acala: %s ACA',
+          bigIntToDecimals(acaFees, ACALA_DECIMALS),
+        );
+        console.log('[Acala -> Unique] outcome %s UNQ', bigIntToDecimals(unqOutcomeTransfer));
 
         expect(acaFees > 0).to.be.true;
         expect(unqOutcomeTransfer == TRANSFER_AMOUNT).to.be.true;
@@ -266,10 +274,10 @@
       const actuallyDelivered = balanceUniqueTokenFinal - balanceUniqueTokenMiddle;
       expect(actuallyDelivered > 0).to.be.true;
 
-      console.log('[Acala -> Unique] actually delivered %s UNQ', actuallyDelivered);
+      console.log('[Acala -> Unique] actually delivered %s UNQ', bigIntToDecimals(actuallyDelivered));
   
       const unqFees = TRANSFER_AMOUNT - actuallyDelivered;
-      console.log('[Acala -> Unique] transaction fees on Unique: %s UNQ', unqFees);
+      console.log('[Acala -> Unique] transaction fees on Unique: %s UNQ', bigIntToDecimals(unqFees));
       expect(unqFees == 0n).to.be.true;
     });
   });
@@ -581,7 +589,7 @@
       expect(balanceUniqueTokenMiddle < balanceUniqueTokenInit).to.be.true;
 
       const transactionFees = balanceUniqueTokenInit - balanceUniqueTokenMiddle - TRANSFER_AMOUNT;
-      console.log('[Unique -> Moonbeam] transaction fees on Unique: %s UNQ', transactionFees);
+      console.log('[Unique -> Moonbeam] transaction fees on Unique: %s UNQ', bigIntToDecimals(transactionFees));
       expect(transactionFees > 0).to.be.true;
     });
 
@@ -592,7 +600,7 @@
         [balanceGlmrTokenMiddle] = await getBalance(api, [randomAccountMoonbeam.address]);
 
         const glmrFees = balanceGlmrTokenInit - balanceGlmrTokenMiddle;
-        console.log('[Unique -> Moonbeam] transaction fees on Moonbeam: %s GLMR', glmrFees);
+        console.log('[Unique -> Moonbeam] transaction fees on Moonbeam: %s GLMR', bigIntToDecimals(glmrFees));
         expect(glmrFees == 0n).to.be.true;
 
         const unqRandomAccountAsset = (
@@ -601,7 +609,7 @@
 
         balanceForeignUnqTokenMiddle = BigInt(unqRandomAccountAsset['balance']);
         const unqIncomeTransfer = balanceForeignUnqTokenMiddle - balanceForeignUnqTokenInit;
-        console.log('[Unique -> Moonbeam] income %s UNQ', unqIncomeTransfer);
+        console.log('[Unique -> Moonbeam] income %s UNQ', bigIntToDecimals(unqIncomeTransfer));
         expect(unqIncomeTransfer == TRANSFER_AMOUNT).to.be.true;
       },
       moonbeamOptions(),
@@ -647,7 +655,7 @@
         [balanceGlmrTokenFinal] = await getBalance(api, [randomAccountMoonbeam.address]);
 
         const glmrFees = balanceGlmrTokenMiddle - balanceGlmrTokenFinal;
-        console.log('[Moonbeam -> Unique] transaction fees on Moonbeam: %s GLMR', glmrFees);
+        console.log('[Moonbeam -> Unique] transaction fees on Moonbeam: %s GLMR', bigIntToDecimals(glmrFees));
         expect(glmrFees > 0).to.be.true;
 
         const unqRandomAccountAsset = (
@@ -659,7 +667,7 @@
         balanceForeignUnqTokenFinal = 0n;
 
         const unqOutcomeTransfer = balanceForeignUnqTokenMiddle - balanceForeignUnqTokenFinal;
-        console.log('[Unique -> Moonbeam] outcome %s UNQ', unqOutcomeTransfer);
+        console.log('[Unique -> Moonbeam] outcome %s UNQ', bigIntToDecimals(unqOutcomeTransfer));
         expect(unqOutcomeTransfer == TRANSFER_AMOUNT).to.be.true;
       },
       moonbeamOptions(),
@@ -672,10 +680,10 @@
       const actuallyDelivered = balanceUniqueTokenFinal - balanceUniqueTokenMiddle;
       expect(actuallyDelivered > 0).to.be.true;
 
-      console.log('[Moonbeam -> Unique] actually delivered %s UNQ', actuallyDelivered);
+      console.log('[Moonbeam -> Unique] actually delivered %s UNQ', bigIntToDecimals(actuallyDelivered));
 
       const unqFees = TRANSFER_AMOUNT - actuallyDelivered;
-      console.log('[Moonbeam -> Unique] transaction fees on Unique: %s UNQ', unqFees);
+      console.log('[Moonbeam -> Unique] transaction fees on Unique: %s UNQ', bigIntToDecimals(unqFees));
       expect(unqFees == 0n).to.be.true;
     });
   });