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.tsdiffbeforeafterboth123 tokenSymbol: string[]123 tokenSymbol: string[]124}124}125126export interface ISubstrateBalance {127 free: bigint,128 reserved: bigint,129 miscFrozen: bigint,130 feeFrozen: bigint131}125132126export type TSubstrateAccount = string;133export type TSubstrateAccount = string;127export type TEthereumAccount = string;134export type TEthereumAccount = string;tests/src/util/playgrounds/unique.tsdiffbeforeafterboth9import {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, IChainEvent, IChainProperties, ICollectionCreationOptions, ICollectionLimits, ICollectionPermissions, ICrossAccountId, ICrossAccountIdLower, ILogger, INestingPermissions, IProperty, IToken, ITokenPropertyPermission, ITransactionResult, IUniqueHelperLog, TApiAllowedListeners, TEthereumAccount, TSigner, TSubstrateAccount, TUniqueNetworks} from './types';12import {IApiListeners, IChainEvent, IChainProperties, ICollectionCreationOptions, ICollectionLimits, ICollectionPermissions, ICrossAccountId, ICrossAccountIdLower, ILogger, INestingPermissions, IProperty, ISubstrateBalance, IToken, ITokenPropertyPermission, ITransactionResult, IUniqueHelperLog, TApiAllowedListeners, TEthereumAccount, TSigner, TSubstrateAccount, TUniqueNetworks} from './types';131314const crossAccountIdFromLower = (lowerAddress: ICrossAccountIdLower): ICrossAccountId => {14const crossAccountIdFromLower = (lowerAddress: ICrossAccountIdLower): ICrossAccountId => {15 const address = {} as ICrossAccountId;15 const address = {} as ICrossAccountId;1893 return (await this.helper.callRpc('api.query.system.account', [address])).data.free.toBigInt();1893 return (await this.helper.callRpc('api.query.system.account', [address])).data.free.toBigInt();1894 }1894 }18951896 /**1897 * Get full substrate balance including free, miscFrozen, feeFrozen, and reserved1898 * @param address substrate address1899 * @returns 1900 */1901 async getSubstrateFull(address: TSubstrateAccount): Promise<ISubstrateBalance> {1902 const accountInfo = (await this.helper.callRpc('api.query.system.account', [address])).data;1903 return {free: accountInfo.free.toBigInt(), miscFrozen: accountInfo.miscFrozen.toBigInt(), feeFrozen: accountInfo.feeFrozen.toBigInt(), reserved: accountInfo.reserved.toBigInt()};1904 }189519051896 /**1906 /**1897 * Get ethereum address balance1907 * Get ethereum address balance