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

difftreelog

Return (un)staking info in more convinient view

Max Andreev2022-09-14parent: #85b56f6.patch.diff
in: master
- Refactor getTotalStakedPerBlock getPendingUnstakePerBlock methods.
- Add docs

3 files changed

modifiedtests/src/app-promotion.test.tsdiffbeforeafterboth
--- a/tests/src/app-promotion.test.ts
+++ b/tests/src/app-promotion.test.ts
@@ -81,7 +81,9 @@
       
       await helper.staking.stake(staker, 200n * nominal);
       expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(300n * nominal);
-      expect((await helper.staking.getTotalStakedPerBlock({Substrate: staker.address})).map((x) => x[1])).to.be.deep.equal([100n * nominal, 200n * nominal]);
+      const totalStakedPerBlock = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
+      expect(totalStakedPerBlock[0].amount).to.equal(100n * nominal);
+      expect(totalStakedPerBlock[1].amount).to.equal(200n * nominal);
     });
   });
 
@@ -159,10 +161,10 @@
       const staker = accounts.pop()!;
       await helper.staking.stake(staker, 100n * nominal);
       await helper.staking.unstake(staker);
-      const unstakedInBlock = (await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address}))[0][0];
+      const [pendingUnstake] = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});
 
       // Wait for unstaking period. Balance now free ~1000; reserved, frozen, miscFrozeb: 0n
-      await helper.wait.forParachainBlockNumber(unstakedInBlock);
+      await helper.wait.forParachainBlockNumber(pendingUnstake.block);
       expect(await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n, miscFrozen: 0n, feeFrozen: 0n});
       expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n);
 
@@ -180,25 +182,26 @@
       await helper.staking.stake(staker, 300n * nominal);
 
       // staked: [100, 200, 300]; unstaked: 0
-      let pendingUnstake = await helper.staking.getPendingUnstake({Substrate: staker.address});
-      let unstakedPerBlock = (await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address})).map(stake => stake[1]);
-      let stakedPerBlock = (await helper.staking.getTotalStakedPerBlock({Substrate: staker.address})).map(stake => stake[1]);
-      expect(pendingUnstake).to.be.deep.equal(0n);
-      expect(unstakedPerBlock).to.be.deep.equal([]);
-      expect(stakedPerBlock).to.be.deep.equal([100n * nominal, 200n * nominal, 300n * nominal]);
+      let totalPendingUnstake = await helper.staking.getPendingUnstake({Substrate: staker.address});
+      let pendingUnstake = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});
+      let stakes = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
+      expect(totalPendingUnstake).to.be.deep.equal(0n);
+      expect(pendingUnstake).to.be.deep.equal([]);
+      expect(stakes[0].amount).to.equal(100n * nominal);
+      expect(stakes[1].amount).to.equal(200n * nominal);
+      expect(stakes[2].amount).to.equal(300n * nominal);
      
       // Can unstake multiple stakes
       await helper.staking.unstake(staker);
-      const unstakingBlock = (await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address}))[0][0];
-      pendingUnstake = await helper.staking.getPendingUnstake({Substrate: staker.address});
-      unstakedPerBlock = (await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address})).map(stake => stake[1]);
-      stakedPerBlock = (await helper.staking.getTotalStakedPerBlock({Substrate: staker.address})).map(stake => stake[1]);
-      expect(pendingUnstake).to.be.equal(600n * nominal);
-      expect(stakedPerBlock).to.be.deep.equal([]);
-      expect(unstakedPerBlock).to.be.deep.equal([600n * nominal]);
+      pendingUnstake = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});
+      totalPendingUnstake = await helper.staking.getPendingUnstake({Substrate: staker.address});
+      stakes = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
+      expect(totalPendingUnstake).to.be.equal(600n * nominal);
+      expect(stakes).to.be.deep.equal([]);
+      expect(pendingUnstake[0].amount).to.equal(600n * nominal);
 
       expect (await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 600n * nominal, feeFrozen: 0n, miscFrozen: 0n});
-      await helper.wait.forParachainBlockNumber(unstakingBlock);
+      await helper.wait.forParachainBlockNumber(pendingUnstake[0].block);
       expect (await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n, feeFrozen: 0n, miscFrozen: 0n});
       expect (await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n);
     });
@@ -235,8 +238,8 @@
 
       const unstakingPerBlock = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});
       expect(unstakingPerBlock).has.length(2);
-      expect(unstakingPerBlock[0][1]).to.equal(100n * nominal);
-      expect(unstakingPerBlock[1][1]).to.equal(120n * nominal);
+      expect(unstakingPerBlock[0].amount).to.equal(100n * nominal);
+      expect(unstakingPerBlock[1].amount).to.equal(120n * nominal);
     });
   });
 
@@ -661,15 +664,15 @@
     });
   });
 
-  it('should increase total staked', async() => {
+  it('should increase total staked', async () => {
     await usingPlaygrounds(async (helper) => {
       const staker = accounts.pop()!;
       const totalStakedBefore = await helper.staking.getTotalStaked();
       await helper.staking.stake(staker, 100n * nominal);
 
       // Wait for rewards and pay
-      const stakedInBlock = (await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}))[0][0];
-      await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stakedInBlock));
+      const [stakedInBlock] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
+      await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stakedInBlock.block));
       await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100));
 
       const totalStakedAfter = await helper.staking.getTotalStaked();
@@ -691,13 +694,14 @@
       await helper.staking.stake(staker, 200n * nominal);
 
       // wait rewards are available:
-      const stakedInBlock = (await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}))[1][0];
-      await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stakedInBlock));
+      const [_, stake2] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
+      await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake2.block));
 
       await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100));
 
-      const totalStakedPerBlock = (await helper.staking.getTotalStakedPerBlock({Substrate: staker.address})).map(s => s[1]);
-      expect(totalStakedPerBlock).to.be.deep.equal([calculateIncome(100n * nominal, 10n), calculateIncome(200n * nominal, 10n)]);
+      const totalStakedPerBlock = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
+      expect(totalStakedPerBlock[0].amount).to.equal(calculateIncome(100n * nominal, 10n));
+      expect(totalStakedPerBlock[1].amount).to.equal(calculateIncome(200n * nominal, 10n));
     });
   });
 
@@ -707,13 +711,13 @@
 
       await helper.staking.stake(staker, 100n * nominal);
       // wait for two rewards are available:
-      const stakedInBlock = (await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}))[0][0];
-      await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stakedInBlock) + LOCKING_PERIOD);
+      let [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
+      await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block) + LOCKING_PERIOD);
 
       await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100));
-      const stakedPerBlock = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
+      [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
       const frozenBalanceShouldBe = calculateIncome(100n * nominal, 10n, 2);
-      expect(stakedPerBlock[0][1]).to.be.equal(frozenBalanceShouldBe);
+      expect(stake.amount).to.be.equal(frozenBalanceShouldBe);
 
       const stakerFullBalance = await helper.balance.getSubstrateFull(staker.address);
 
@@ -726,8 +730,8 @@
       // staker unstakes before rewards has been payed
       const staker = accounts.pop()!;
       await helper.staking.stake(staker, 100n * nominal);
-      const stakedInBlock = (await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}))[0][0];
-      await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stakedInBlock) + LOCKING_PERIOD);
+      const [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
+      await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block) + LOCKING_PERIOD);
       await helper.staking.unstake(staker);
       
       // so he did not receive any rewards
@@ -745,17 +749,17 @@
             
       await helper.staking.stake(staker, 100n * nominal);
 
-      const stakedInBlock = (await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}))[0][0];
-      await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stakedInBlock));
+      let [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
+      await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block));
       
       await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100));
-      let totalStakedPerBlock = (await helper.staking.getTotalStakedPerBlock({Substrate: staker.address})).map(s => s[1]);
-      expect(totalStakedPerBlock).to.deep.equal([calculateIncome(100n * nominal, 10n)]);
+      [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
+      expect(stake.amount).to.equal(calculateIncome(100n * nominal, 10n));
       
-      await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stakedInBlock) + LOCKING_PERIOD);
+      await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block) + LOCKING_PERIOD);
       await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100));
-      totalStakedPerBlock = (await helper.staking.getTotalStakedPerBlock({Substrate: staker.address})).map(s => s[1]);
-      expect(totalStakedPerBlock).to.deep.equal([calculateIncome(100n * nominal, 10n, 2)]);      
+      [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
+      expect(stake.amount).to.equal(calculateIncome(100n * nominal, 10n, 2));
     });
   });
 
modifiedtests/src/util/playgrounds/types.tsdiffbeforeafterboth
--- a/tests/src/util/playgrounds/types.ts
+++ b/tests/src/util/playgrounds/types.ts
@@ -147,6 +147,11 @@
   feeFrozen: bigint
 }
 
+export interface IStakingInfo {
+  block: bigint,
+  amount: bigint,
+}
+
 export type TSubstrateAccount = string;
 export type TEthereumAccount = string;
 export type TApiAllowedListeners = 'connected' | 'disconnected' | 'error' | 'ready' | 'decorated';
modifiedtests/src/util/playgrounds/unique.tsdiffbeforeafterboth
9import {ApiInterfaceEvents} from '@polkadot/api/types';9import {ApiInterfaceEvents} from '@polkadot/api/types';
10import {encodeAddress, decodeAddress, keccakAsHex, evmToAddress, addressToEvm} from '@polkadot/util-crypto';10import {encodeAddress, decodeAddress, keccakAsHex, evmToAddress, addressToEvm} from '@polkadot/util-crypto';
11import {IKeyringPair} from '@polkadot/types/types';11import {IKeyringPair} from '@polkadot/types/types';
12import {IApiListeners, IBlock, IChainEvent, IChainProperties, ICollectionCreationOptions, ICollectionLimits, ICollectionPermissions, ICrossAccountId, ICrossAccountIdLower, ILogger, INestingPermissions, IProperty, ISubstrateBalance, IToken, ITokenPropertyPermission, ITransactionResult, IUniqueHelperLog, TApiAllowedListeners, TEthereumAccount, TSigner, TSubstrateAccount, TUniqueNetworks} from './types';12import {IApiListeners, IBlock, IChainEvent, IChainProperties, ICollectionCreationOptions, ICollectionLimits, ICollectionPermissions, ICrossAccountId, ICrossAccountIdLower, ILogger, INestingPermissions, IProperty, IStakingInfo, ISubstrateBalance, IToken, ITokenPropertyPermission, ITransactionResult, IUniqueHelperLog, TApiAllowedListeners, TEthereumAccount, TSigner, TSubstrateAccount, TUniqueNetworks} from './types';
1313
14export const crossAccountIdFromLower = (lowerAddress: ICrossAccountIdLower): ICrossAccountId => {14export const crossAccountIdFromLower = (lowerAddress: ICrossAccountIdLower): ICrossAccountId => {
15 const address = {} as ICrossAccountId;15 const address = {} as ICrossAccountId;
2029 return 1;2029 return 1;
2030 }2030 }
20312031
2032 /**
2033 * Get total staked amount for address
2034 * @param address substrate or ethereum address
2035 * @returns total staked amount
2036 */
2032 async getTotalStaked(address?: ICrossAccountId): Promise<bigint> {2037 async getTotalStaked(address?: ICrossAccountId): Promise<bigint> {
2033 if (address) return (await this.helper.callRpc('api.rpc.appPromotion.totalStaked', [address])).toBigInt();2038 if (address) return (await this.helper.callRpc('api.rpc.appPromotion.totalStaked', [address])).toBigInt();
2034 return (await this.helper.callRpc('api.rpc.appPromotion.totalStaked')).toBigInt();2039 return (await this.helper.callRpc('api.rpc.appPromotion.totalStaked')).toBigInt();
2035 }2040 }
20362041
2042 /**
2043 * Get total staked per block
2044 * @param address substrate or ethereum address
2045 * @returns array of stakes. `block` – the number of the block in which the stake was made. `amount` - the number of tokens staked in the block
2046 */
2037 async getTotalStakedPerBlock(address: ICrossAccountId): Promise<bigint[][]> {2047 async getTotalStakedPerBlock(address: ICrossAccountId): Promise<IStakingInfo[]> {
2048 const rawTotalStakerdPerBlock = await this.helper.callRpc('api.rpc.appPromotion.totalStakedPerBlock', [address]);
2038 return (await this.helper.callRpc('api.rpc.appPromotion.totalStakedPerBlock', [address])).map(([block, amount]: any[]) => [block.toBigInt(), amount.toBigInt()]);2049 return rawTotalStakerdPerBlock.map(([block, amount]: any[]) => {
2050 return {
2051 block: block.toBigInt(),
2052 amount: amount.toBigInt(),
2053 };
2054 });
2039 }2055 }
20402056
2057 /**
2058 * Get total pending unstake amount for address
2059 * @param address substrate or ethereum address
2060 * @returns total pending unstake amount
2061 */
2041 async getPendingUnstake(address: ICrossAccountId): Promise<bigint> {2062 async getPendingUnstake(address: ICrossAccountId): Promise<bigint> {
2042 return (await this.helper.callRpc('api.rpc.appPromotion.pendingUnstake', [address])).toBigInt();2063 return (await this.helper.callRpc('api.rpc.appPromotion.pendingUnstake', [address])).toBigInt();
2043 }2064 }
20442065
2066 /**
2067 * Get pending unstake amount per block for address
2068 * @param address substrate or ethereum address
2069 * @returns array of pending stakes. `block` – the number of the block in which the unstake was made. `amount` - the number of tokens unstaked in the block
2070 */
2045 async getPendingUnstakePerBlock(address: ICrossAccountId): Promise<bigint[][]> {2071 async getPendingUnstakePerBlock(address: ICrossAccountId): Promise<IStakingInfo[]> {
2072 const rawUnstakedPerBlock = await this.helper.callRpc('api.rpc.appPromotion.pendingUnstakePerBlock', [address]);
2046 return (await this.helper.callRpc('api.rpc.appPromotion.pendingUnstakePerBlock', [address])).map(([block, amount]: any[]) => [block.toBigInt(), amount.toBigInt()]);2073 const result = rawUnstakedPerBlock.map(([block, amount]: any[]) => {
2074 return {
2075 block: block.toBigInt(),
2076 amount: amount.toBigInt(),
2077 };
2078 });
2079 return result;
2047 }2080 }
2048}2081}
20492082