difftreelog
Tests: add balance state asserts
in: master
3 files changed
tests/src/app-promotion.test.tsdiffbeforeafterboth63});63});646465describe('app-promotions.stake extrinsic', () => {65describe('app-promotions.stake extrinsic', () => {66 it('should change balance state to "locked", add it to "staked" map, and increase "totalStaked" amount', async () => {66 it('should "lock" some balance in system.account, add it to "staked" map, and increase "totalStaked" amount', async () => {67 await usingPlaygrounds(async (helper) => {67 await usingPlaygrounds(async (helper) => {68 const totalStakedBefore = await helper.staking.getTotalStaked();68 const totalStakedBefore = await helper.staking.getTotalStaked();69 const [staker] = await helper.arrange.createAccounts([400n], alice);69 const [staker, recepient] = await helper.arrange.createAccounts([400n, 0n], alice);70 70 71 // Minimum stake amount is 100:71 // Minimum stake amount is 100:72 await expect(helper.staking.stake(staker, 100n * nominal - 1n)).to.be.eventually.rejected;72 await expect(helper.staking.stake(staker, 100n * nominal - 1n)).to.be.eventually.rejected;73 await helper.staking.stake(staker, 100n * nominal);73 await helper.staking.stake(staker, 100n * nominal);7475 // Staker balance is: miscFrozen: 100, feeFrozen: 100, reserved: 0n, free less than 300...76 // ...so he can not transfer 30077 expect (await helper.balance.getSubstrateFull(staker.address)).to.contain({miscFrozen: 100n * nominal, feeFrozen: 100n * nominal, reserved: 0n});78 await expect(helper.balance.transferToSubstrate(staker, recepient.address, 300n * nominal)).to.be.rejected;79 74 expect(await helper.staking.getTotalStakingLocked({Substrate: staker.address})).to.be.equal(100n * nominal);80 expect(await helper.staking.getTotalStakingLocked({Substrate: staker.address})).to.be.equal(100n * nominal);75121 it('should change balance state to "reserved", add it to "pendingUnstake" map, and subtract it from totalStaked', async () => {126 it('should change balance state to "reserved", add it to "pendingUnstake" map, and subtract it from totalStaked', async () => {122 await usingPlaygrounds(async helper => {127 await usingPlaygrounds(async helper => {123 const totalStakedBefore = await helper.staking.getTotalStaked();128 const totalStakedBefore = await helper.staking.getTotalStaked();124 const [staker] = await helper.arrange.createAccounts([1000n], alice);129 const [staker, recepient] = await helper.arrange.createAccounts([600n, 0n], alice);125 await helper.staking.stake(staker, 500n * nominal);130 await helper.staking.stake(staker, 500n * nominal);126 await helper.staking.unstake(staker);131 await helper.staking.unstake(staker);132133 // Stakers balance now: {free: <100n, reserved: 500n, miscFrozen: 0, feeFrozen: 0};134 // Staker can not transfer 135 // TODO expect(await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 500n * nominal, miscFrozen: 0n, feeFrozen: 0n});136 await expect(helper.balance.transferToSubstrate(staker, recepient.address, 100n * nominal)).to.be.rejected;127137128 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(500n * nominal);138 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(500n * nominal);129 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(0n);139 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(0n);130 expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedBefore);140 expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedBefore);141142 // Wait for unstaking period. Balance now free ~600, and reserved, frozen, miscFrozeb 0n143 await waitForRelayBlock(helper.api!, 20);144 expect(await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n * nominal, miscFrozen: 0n, feeFrozen: 0n});145 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(599n);131 });146 });132 });147 });133148134 it('should remove multiple stakes', async () => {149 it('should successfully unstake multiple stakes', async () => {135 await usingPlaygrounds(async helper => {150 await usingPlaygrounds(async helper => {136 const [staker] = await helper.arrange.createAccounts([1000n], alice);151 const [staker] = await helper.arrange.createAccounts([1000n], alice);137 await helper.staking.stake(staker, 100n * nominal);152 await helper.staking.stake(staker, 100n * nominal);155 expect(stakedPerBlock).to.be.deep.equal([]);170 expect(stakedPerBlock).to.be.deep.equal([]);156 expect(unstakedPerBlock).to.be.deep.equal([600n * nominal]);171 expect(unstakedPerBlock).to.be.deep.equal([600n * nominal]);172173 expect (await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n, feeFrozen: 600n * nominal, miscFrozen: 600n * nominal});174 await waitForRelayBlock(helper.api!, 20);175 expect (await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n, feeFrozen: 0n, miscFrozen: 0n});176 expect (await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n);157 });177 });158 });178 });159179200 const [staker] = await helper.arrange.createAccounts([1000n], alice);220 const [staker] = await helper.arrange.createAccounts([1000n], alice);201 await helper.staking.stake(staker, 100n * nominal);221 await helper.staking.stake(staker, 100n * nominal);202 await helper.staking.unstake(staker);222 await helper.staking.unstake(staker);223 await waitForRelayBlock(helper.api!, 20);224 // expect balance unlocked203 expect.fail('Not implemented');225 expect.fail('Not implemented');204 });226 });205 });227 });tests/src/util/playgrounds/types.tsdiffbeforeafterboth--- a/tests/src/util/playgrounds/types.ts
+++ b/tests/src/util/playgrounds/types.ts
@@ -123,8 +123,15 @@
tokenSymbol: string[]
}
+export interface ISubstrateBalance {
+ free: bigint,
+ reserved: bigint,
+ miscFrozen: bigint,
+ feeFrozen: bigint
+}
+
export type TSubstrateAccount = string;
export type TEthereumAccount = string;
export type TApiAllowedListeners = 'connected' | 'disconnected' | 'error' | 'ready' | 'decorated';
export type TUniqueNetworks = 'opal' | 'quartz' | 'unique';
-export type TSigner = IKeyringPair; // | 'string'
\ No newline at end of file
+export type TSigner = IKeyringPair; // | 'string'
tests/src/util/playgrounds/unique.tsdiffbeforeafterboth--- a/tests/src/util/playgrounds/unique.ts
+++ b/tests/src/util/playgrounds/unique.ts
@@ -9,7 +9,7 @@
import {ApiInterfaceEvents} from '@polkadot/api/types';
import {encodeAddress, decodeAddress, keccakAsHex, evmToAddress, addressToEvm} from '@polkadot/util-crypto';
import {IKeyringPair} from '@polkadot/types/types';
-import {IApiListeners, IChainEvent, IChainProperties, ICollectionCreationOptions, ICollectionLimits, ICollectionPermissions, ICrossAccountId, ICrossAccountIdLower, ILogger, INestingPermissions, IProperty, IToken, ITokenPropertyPermission, ITransactionResult, IUniqueHelperLog, TApiAllowedListeners, TEthereumAccount, TSigner, TSubstrateAccount, TUniqueNetworks} from './types';
+import {IApiListeners, IChainEvent, IChainProperties, ICollectionCreationOptions, ICollectionLimits, ICollectionPermissions, ICrossAccountId, ICrossAccountIdLower, ILogger, INestingPermissions, IProperty, ISubstrateBalance, IToken, ITokenPropertyPermission, ITransactionResult, IUniqueHelperLog, TApiAllowedListeners, TEthereumAccount, TSigner, TSubstrateAccount, TUniqueNetworks} from './types';
const crossAccountIdFromLower = (lowerAddress: ICrossAccountIdLower): ICrossAccountId => {
const address = {} as ICrossAccountId;
@@ -1894,6 +1894,16 @@
}
/**
+ * Get full substrate balance including free, miscFrozen, feeFrozen, and reserved
+ * @param address substrate address
+ * @returns
+ */
+ async getSubstrateFull(address: TSubstrateAccount): Promise<ISubstrateBalance> {
+ const accountInfo = (await this.helper.callRpc('api.query.system.account', [address])).data;
+ return {free: accountInfo.free.toBigInt(), miscFrozen: accountInfo.miscFrozen.toBigInt(), feeFrozen: accountInfo.feeFrozen.toBigInt(), reserved: accountInfo.reserved.toBigInt()};
+ }
+
+ /**
* Get ethereum address balance
* @param address ethereum address
* @example getEthereum("0x9F0583DbB855d...")