git.delta.rocks / unique-network / refs/commits / 7a09794fa753

difftreelog

Tests: add balance state asserts

Maksandre2022-09-01parent: #0889383.patch.diff
in: master

3 files changed

modifiedtests/src/app-promotion.test.tsdiffbeforeafterboth
63});63});
6464
65describe('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);
74
75 // Staker balance is: miscFrozen: 100, feeFrozen: 100, reserved: 0n, free less than 300...
76 // ...so he can not transfer 300
77 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);
75
121 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);
132
133 // 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;
127137
128 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);
141
142 // Wait for unstaking period. Balance now free ~600, and reserved, frozen, miscFrozeb 0n
143 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 });
133148
134 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]);
172
173 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 });
159179
200 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 unlocked
203 expect.fail('Not implemented');225 expect.fail('Not implemented');
204 });226 });
205 });227 });
modifiedtests/src/util/playgrounds/types.tsdiffbeforeafterboth
123 tokenSymbol: string[]123 tokenSymbol: string[]
124}124}
125
126export interface ISubstrateBalance {
127 free: bigint,
128 reserved: bigint,
129 miscFrozen: bigint,
130 feeFrozen: bigint
131}
125132
126export type TSubstrateAccount = string;133export type TSubstrateAccount = string;
127export type TEthereumAccount = string;134export type TEthereumAccount = string;
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, 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';
1313
14const 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 }
1895
1896 /**
1897 * Get full substrate balance including free, miscFrozen, feeFrozen, and reserved
1898 * @param address substrate address
1899 * @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 }
18951905
1896 /**1906 /**
1897 * Get ethereum address balance1907 * Get ethereum address balance