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

difftreelog

Merge pull request #742 from UniqueNetwork/tests/vesting

ut-akuznetsov2022-12-21parents: #ccd7200 #eb0e651.patch.diff
in: master

3 files changed

modifiedtests/src/app-promotion.test.tsdiffbeforeafterboth
57 // Staker balance is: miscFrozen: 100, feeFrozen: 100, reserved: 0n...57 // Staker balance is: miscFrozen: 100, feeFrozen: 100, reserved: 0n...
58 // ...so he can not transfer 90058 // ...so he can not transfer 900
59 expect (await helper.balance.getSubstrateFull(staker.address)).to.contain({miscFrozen: 100n * nominal, feeFrozen: 100n * nominal, reserved: 0n});59 expect(await helper.balance.getSubstrateFull(staker.address)).to.contain({miscFrozen: 100n * nominal, feeFrozen: 100n * nominal, reserved: 0n});
60 expect(await helper.balance.getLocked(staker.address)).to.deep.eq([{id: 'appstake', amount: 100n * nominal, reasons: 'All'}]);
60 await expect(helper.balance.transferToSubstrate(staker, recepient.address, 900n * nominal)).to.be.rejectedWith('balances.LiquidityRestrictions');61 await expect(helper.balance.transferToSubstrate(staker, recepient.address, 900n * nominal)).to.be.rejectedWith('balances.LiquidityRestrictions');
61 62
62 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(100n * nominal);63 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(100n * nominal);
530 });531 });
531 });532 });
532 533
533 describe('rewards', () => {534 describe('payoutStakers', () => {
534 itSub('can not be called by non admin', async ({helper}) => {535 itSub('can not be called by non admin', async ({helper}) => {
535 const nonAdmin = accounts.pop()!;536 const nonAdmin = accounts.pop()!;
536 await expect(helper.admin.payoutStakers(nonAdmin, 100)).to.be.rejectedWith('appPromotion.NoPermission');537 await expect(helper.admin.payoutStakers(nonAdmin, 100)).to.be.rejectedWith('appPromotion.NoPermission');
569 expect(payoutToStaker + 300n * nominal).to.equal(calculateIncome(300n * nominal));570 expect(payoutToStaker + 300n * nominal).to.equal(calculateIncome(300n * nominal));
570 571
571 const totalStakedPerBlock = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});572 const totalStakedPerBlock = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
573 const income1 = calculateIncome(100n * nominal);
574 const income2 = calculateIncome(200n * nominal);
572 expect(totalStakedPerBlock[0].amount).to.equal(calculateIncome(100n * nominal));575 expect(totalStakedPerBlock[0].amount).to.equal(income1);
573 expect(totalStakedPerBlock[1].amount).to.equal(calculateIncome(200n * nominal));576 expect(totalStakedPerBlock[1].amount).to.equal(income2);
577
578 const stakerBalance = await helper.balance.getSubstrateFull(staker.address);
579 expect(stakerBalance).to.contain({miscFrozen: income1 + income2, feeFrozen: income1 + income2, reserved: 0n});
580 expect(stakerBalance.free / nominal).to.eq(999n);
574 });581 });
575 582
576 itSub('shoud be paid for more than one period if payments was missed', async ({helper}) => {583 itSub('shoud be paid for more than one period if payments was missed', async ({helper}) => {
modifiedtests/src/util/playgrounds/unique.tsdiffbeforeafterboth
2199 return (await this.helper.callRpc('api.rpc.chain.getBlock', [blockHash])).toHuman().block;2199 return (await this.helper.callRpc('api.rpc.chain.getBlock', [blockHash])).toHuman().block;
2200 }2200 }
2201
2202 /**
2203 * Get latest relay block
2204 * @returns {number} relay block
2205 */
2206 async getRelayBlockNumber(): Promise<bigint> {
2207 const blockNumber = (await this.helper.callRpc('api.query.parachainSystem.validationData')).toJSON().relayParentNumber;
2208 return BigInt(blockNumber);
2209 }
22012210
2202 /**2211 /**
2203 * Get account nonce2212 * Get account nonce
2258 return {free: accountInfo.free.toBigInt(), miscFrozen: accountInfo.miscFrozen.toBigInt(), feeFrozen: accountInfo.feeFrozen.toBigInt(), reserved: accountInfo.reserved.toBigInt()};2267 return {free: accountInfo.free.toBigInt(), miscFrozen: accountInfo.miscFrozen.toBigInt(), feeFrozen: accountInfo.feeFrozen.toBigInt(), reserved: accountInfo.reserved.toBigInt()};
2259 }2268 }
2269
2270 async getLocked(address: TSubstrateAccount): Promise<[{id: string, amount: bigint, reason: string}]> {
2271 const locks = (await this.helper.callRpc('api.query.balances.locks', [address])).toHuman();
2272 return locks.map((lock: any) => {return {id: lock.id, amount: BigInt(lock.amount.replace(/,/g, '')), reasons: lock.reasons};});
2273 }
2260}2274}
22612275
2262class EthereumBalanceGroup<T extends ChainHelperBase> extends HelperGroup<T> {2276class EthereumBalanceGroup<T extends ChainHelperBase> extends HelperGroup<T> {
2340 return this.subBalanceGroup.getSubstrateFull(address);2354 return this.subBalanceGroup.getSubstrateFull(address);
2341 }2355 }
2356
2357 /**
2358 * Get locked balances
2359 * @param address substrate address
2360 * @returns locked balances with reason via api.query.balances.locks
2361 */
2362 getLocked(address: TSubstrateAccount) {
2363 return this.subBalanceGroup.getLocked(address);
2364 }
23422365
2343 /**2366 /**
2344 * Get ethereum address balance2367 * Get ethereum address balance
2381 return isSuccess;2404 return isSuccess;
2382 }2405 }
2406
2407 /**
2408 * Transfer tokens with the unlock period
2409 * @param signer signers Keyring
2410 * @param address Substrate address of recipient
2411 * @param schedule Schedule params
2412 * @example vestedTransfer(signer, recepient.address, 20000, 100, 10, 50 * nominal); // total amount of vested tokens will be 100 * 50 = 5000
2413 */
2414 async vestedTransfer(signer: TSigner, address: TSubstrateAccount, schedule: {start: bigint, period: bigint, periodCount: bigint, perPeriod: bigint}): Promise<void> {
2415 const result = await this.helper.executeExtrinsic(signer, 'api.tx.vesting.vestedTransfer', [address, schedule]);
2416 const event = result.result.events
2417 .find(e => e.event.section === 'vesting' &&
2418 e.event.method === 'VestingScheduleAdded' &&
2419 e.event.data[0].toHuman() === signer.address);
2420 if (!event) throw Error('Cannot find transfer in events');
2421 }
2422
2423 /**
2424 * Get schedule for recepient of vested transfer
2425 * @param address Substrate address of recipient
2426 * @returns
2427 */
2428 async getVestingSchedules(address: TSubstrateAccount): Promise<{start: bigint, period: bigint, periodCount: bigint, perPeriod: bigint}[]> {
2429 const schedule = (await this.helper.callRpc('api.query.vesting.vestingSchedules', [address])).toJSON();
2430 return schedule.map((schedule: any) => {
2431 return {
2432 start: BigInt(schedule.start),
2433 period: BigInt(schedule.period),
2434 periodCount: BigInt(schedule.periodCount),
2435 perPeriod: BigInt(schedule.perPeriod),
2436 };
2437 });
2438 }
2439
2440 /**
2441 * Claim vested tokens
2442 * @param signer signers Keyring
2443 */
2444 async claim(signer: TSigner) {
2445 const result = await this.helper.executeExtrinsic(signer, 'api.tx.vesting.claim', []);
2446 const event = result.result.events
2447 .find(e => e.event.section === 'vesting' &&
2448 e.event.method === 'Claimed' &&
2449 e.event.data[0].toHuman() === signer.address);
2450 if (!event) throw Error('Cannot find claim in events');
2451 }
2383}2452}
23842453
2385class AddressGroup extends HelperGroup<ChainHelperBase> {2454class AddressGroup extends HelperGroup<ChainHelperBase> {
addedtests/src/vesting.test.tsdiffbeforeafterboth

no changes