git.delta.rocks / unique-network / refs/commits / 1dbc03134213

difftreelog

Fix tests

Max Andreev2022-09-08parent: #048106f.patch.diff
in: master
Add waiter function
Remove global before script
Remove useless logs
Fix refungible tests

3 files changed

modifiedtests/src/app-promotion.test.tsdiffbeforeafterboth
28import {encodeAddress} from '@polkadot/util-crypto';28import {encodeAddress} from '@polkadot/util-crypto';
29import {stringToU8a} from '@polkadot/util';29import {stringToU8a} from '@polkadot/util';
30import {SponsoringMode, contractHelpers, createEthAccountWithBalance, deployFlipper, itWeb3, transferBalanceToEth} from './eth/util/helpers';30import {SponsoringMode, contractHelpers, createEthAccountWithBalance, deployFlipper, itWeb3, transferBalanceToEth} from './eth/util/helpers';
31import {DevUniqueHelper} from './util/playgrounds/unique.dev';
31chai.use(chaiAsPromised);32chai.use(chaiAsPromised);
32const expect = chai.expect;33const expect = chai.expect;
3334
40const UNLOCKING_PERIOD = 10n; // 20 blocks of parachain41const UNLOCKING_PERIOD = 10n; // 20 blocks of parachain
41const rewardAvailableInBlock = (stakedInBlock: bigint) => (stakedInBlock - stakedInBlock % LOCKING_PERIOD) + (LOCKING_PERIOD * 2n);42const rewardAvailableInBlock = (stakedInBlock: bigint) => (stakedInBlock - stakedInBlock % LOCKING_PERIOD) + (LOCKING_PERIOD * 2n);
4243
43before(async function () {44const beforeEach = async (context: Mocha.Context) => {
44 await usingPlaygrounds(async (helper, privateKey) => {45 await usingPlaygrounds(async (helper, privateKey) => {
45 if (!getModuleNames(helper.api!).includes(Pallets.AppPromotion)) this.skip();46 if (!getModuleNames(helper.api!).includes(Pallets.AppPromotion)) context.skip();
46 alice = privateKey('//Alice');47 alice = privateKey('//Alice');
47 palletAdmin = privateKey('//Charlie'); // TODO use custom address48 palletAdmin = privateKey('//Charlie'); // TODO use custom address
48 await helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress({Substrate: palletAdmin.address})));49 await helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress({Substrate: palletAdmin.address})));
51 await helper.balance.transferToSubstrate(alice, palletAddress, 1000n * nominal);52 await helper.balance.transferToSubstrate(alice, palletAddress, 1000n * nominal);
52 accounts = await helper.arrange.createCrowd(100, 1000n, alice); // create accounts-pool to speed up tests53 accounts = await helper.arrange.createCrowd(100, 1000n, alice); // create accounts-pool to speed up tests
53 });54 });
54});55};
5556
56describe('app-promotions.stake extrinsic', () => {57describe('app-promotions.stake extrinsic', () => {
58 before(async function () {
59 await beforeEach(this);
60 });
61
57 it('should "lock" staking balance, add it to "staked" map, and increase "totalStaked" amount', async () => {62 it('should "lock" staking balance, add it to "staked" map, and increase "totalStaked" amount', async () => {
58 await usingPlaygrounds(async (helper) => {63 await usingPlaygrounds(async (helper) => {
128});133});
129134
130describe('unstake balance extrinsic', () => { 135describe('unstake balance extrinsic', () => {
136 before(async function () {
137 await beforeEach(this);
138 });
139
131 it('should change balance state from "frozen" to "reserved", add it to "pendingUnstake" map, and subtract it from totalStaked', async () => {140 it('should change balance state from "frozen" to "reserved", add it to "pendingUnstake" map, and subtract it from totalStaked', async () => {
132 await usingPlaygrounds(async helper => {141 await usingPlaygrounds(async helper => {
247});256});
248257
249describe('Admin adress', () => {258describe('Admin adress', () => {
259 before(async function () {
260 await beforeEach(this);
261 });
262
250 it('can be set by sudo only', async () => {263 it('can be set by sudo only', async () => {
251 await usingPlaygrounds(async (helper) => {264 await usingPlaygrounds(async (helper) => {
291304
292describe('App-promotion collection sponsoring', () => {305describe('App-promotion collection sponsoring', () => {
293 before(async function () {306 before(async function () {
307 await beforeEach(this);
294 await usingPlaygrounds(async (helper) => {308 await usingPlaygrounds(async (helper) => {
295 const tx = helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress({Substrate: palletAdmin.address}));309 const tx = helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress({Substrate: palletAdmin.address}));
296 await helper.signTransaction(alice, tx);310 await helper.signTransaction(alice, tx);
394});408});
395409
396describe('app-promotion stopSponsoringCollection', () => {410describe('app-promotion stopSponsoringCollection', () => {
411 before(async function () {
412 await beforeEach(this);
413 });
414
397 it('can not be called by non-admin', async () => { 415 it('can not be called by non-admin', async () => {
398 await usingPlaygrounds(async (helper) => {416 await usingPlaygrounds(async (helper) => {
450});468});
451469
452describe('app-promotion contract sponsoring', () => {470describe('app-promotion contract sponsoring', () => {
471 before(async function () {
472 await beforeEach(this);
473 });
474
453 itWeb3('should set palletes address as a sponsor', async ({api, web3, privateKeyWrapper}) => {475 itWeb3('should set palletes address as a sponsor', async ({api, web3, privateKeyWrapper}) => {
454 await usingPlaygrounds(async (helper) => {476 await usingPlaygrounds(async (helper) => {
570});592});
571593
572describe('app-promotion stopSponsoringContract', () => { 594describe('app-promotion stopSponsoringContract', () => {
595 before(async function () {
596 await beforeEach(this);
597 });
598
573 itWeb3('should remove pallet address from contract sponsors', async ({api, web3, privateKeyWrapper}) => {599 itWeb3('should remove pallet address from contract sponsors', async ({api, web3, privateKeyWrapper}) => {
574 await usingPlaygrounds(async (helper) => { 600 await usingPlaygrounds(async (helper) => {
624});650});
625651
626describe('app-promotion rewards', () => {652describe('app-promotion rewards', () => {
653 before(async function () {
654 await beforeEach(this);
655 });
656
627 it('can not be called by non admin', async () => {657 it('can not be called by non admin', async () => {
628 await usingPlaygrounds(async (helper) => {658 await usingPlaygrounds(async (helper) => {
635 await usingPlaygrounds(async helper => {665 await usingPlaygrounds(async helper => {
636 const staker = accounts.pop()!;666 const staker = accounts.pop()!;
637 667
668 await waitPromotionPeriodDoesntEnd(helper);
669
638 await helper.staking.stake(staker, 100n * nominal);670 await helper.staking.stake(staker, 100n * nominal);
639 await helper.staking.stake(staker, 200n * nominal);671 await helper.staking.stake(staker, 200n * nominal);
640672
641 // wair rewards are available:673 // wait rewards are available:
642 const stakedInBlock = (await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}))[1][0];674 const stakedInBlock = (await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}))[1][0];
643 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stakedInBlock));675 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stakedInBlock));
644676
688 });720 });
689 721
690 it('should bring compound interest', async () => {722 it('should bring compound interest', async () => {
691 // TODO flaky test
692 await usingPlaygrounds(async helper => {723 await usingPlaygrounds(async helper => {
693 const staker = accounts.pop()!;724 const staker = accounts.pop()!;
694 725
757 } else return income;788 } else return income;
758}789}
790
791// Wait while promotion period less than specified block, to avoid boundary cases
792// 0 if this should be the beginning of the period.
793async function waitPromotionPeriodDoesntEnd(helper: DevUniqueHelper, waitBlockLessThan = LOCKING_PERIOD / 3n) {
794 const relayBlockNumber = (await helper.api!.query.parachainSystem.validationData()).value.relayParentNumber.toNumber(); // await helper.chain.getLatestBlockNumber();
795 const currentPeriodBlock = BigInt(relayBlockNumber) % LOCKING_PERIOD;
796
797 if (currentPeriodBlock > waitBlockLessThan) {
798 await helper.wait.forRelayBlockNumber(BigInt(relayBlockNumber) + LOCKING_PERIOD - currentPeriodBlock);
799 }
800}
759801
modifiedtests/src/refungible.test.tsdiffbeforeafterboth
245 section: 'common',245 section: 'common',
246 index: '0x4202',246 index: '0x4202',
247 data: [ 247 data: [
248 collection.collectionId.toString(), 248 helper.api!.createType('u32', collection.collectionId).toHuman(),
249 token.tokenId.toString(), 249 helper.api!.createType('u32', token.tokenId).toHuman(),
250 {Substrate: alice.address}, 250 {Substrate: alice.address},
251 '100',251 '100',
252 ],252 ],
265 section: 'common',265 section: 'common',
266 index: '0x4203',266 index: '0x4203',
267 data: [ 267 data: [
268 collection.collectionId.toString(), 268 helper.api!.createType('u32', collection.collectionId).toHuman(),
269 token.tokenId.toString(), 269 helper.api!.createType('u32', token.tokenId).toHuman(),
270 {Substrate: alice.address}, 270 {Substrate: alice.address},
271 '50',271 '50',
272 ],272 ],
modifiedtests/src/util/playgrounds/unique.dev.tsdiffbeforeafterboth
35 for (const arg of args) {35 for (const arg of args) {
36 if (typeof arg !== 'string')36 if (typeof arg !== 'string')
37 continue;37 continue;
38 if (arg.includes('1000:: Normal connection closure') || arg.includes('Not decorating unknown runtime apis: UniqueApi/2, RmrkApi/1') || arg.includes('RPC methods not decorated:') || arg === 'Normal connection closure')38 if (arg.includes('1000:: Normal connection closure') || arg.includes('Not decorating unknown runtime apis:') || arg.includes('RPC methods not decorated:') || arg === 'Normal connection closure')
39 return;39 return;
40 }40 }
41 printer(...args);41 printer(...args);