--- a/tests/src/app-promotion.test.ts +++ b/tests/src/app-promotion.test.ts @@ -33,17 +33,16 @@ const expect = chai.expect; let alice: IKeyringPair; -let bob: IKeyringPair; let palletAdmin: IKeyringPair; let nominal: bigint; let promotionStartBlock: number | null = null; const palletAddress = calculatePalleteAddress('appstake'); +let accounts: IKeyringPair[] = []; before(async function () { await usingPlaygrounds(async (helper, privateKeyWrapper) => { if (!getModuleNames(helper.api!).includes(Pallets.AppPromotion)) this.skip(); alice = privateKeyWrapper('//Alice'); - bob = privateKeyWrapper('//Bob'); palletAdmin = privateKeyWrapper('//Charlie'); // TODO use custom address await helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.setAdminAddress({Substrate: palletAdmin.address}))); nominal = helper.balance.getOneTokenNominal(); @@ -53,6 +52,7 @@ promotionStartBlock = (await helper.api!.query.parachainSystem.lastRelayChainBlockNumber()).toNumber(); } await helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.startAppPromotion(promotionStartBlock!))); + accounts = await helper.arrange.createCrowd(100, 1000n, alice); // create accounts-pool to speed up tests }); }); @@ -65,51 +65,62 @@ describe('app-promotions.stake extrinsic', () => { it('should "lock" staking balance, add it to "staked" map, and increase "totalStaked" amount', async () => { await usingPlaygrounds(async (helper) => { + const [staker, recepient] = [accounts.pop()!, accounts.pop()!]; const totalStakedBefore = await helper.staking.getTotalStaked(); - const [staker, recepient] = await helper.arrange.createAccounts([400n, 0n], alice); // Minimum stake amount is 100: await expect(helper.staking.stake(staker, 100n * nominal - 1n)).to.be.eventually.rejected; await helper.staking.stake(staker, 100n * nominal); // Staker balance is: miscFrozen: 100, feeFrozen: 100, reserved: 0n... - // ...so he can not transfer 300 + // ...so he can not transfer 900 expect (await helper.balance.getSubstrateFull(staker.address)).to.contain({miscFrozen: 100n * nominal, feeFrozen: 100n * nominal, reserved: 0n}); - await expect(helper.balance.transferToSubstrate(staker, recepient.address, 300n * nominal)).to.be.rejected; + await expect(helper.balance.transferToSubstrate(staker, recepient.address, 900n * nominal)).to.be.rejected; - // 100 -> 24 locked 100; staked 0; 24 => locked 100 staked 100 expect(await helper.staking.getTotalStakingLocked({Substrate: staker.address})).to.be.equal(100n * nominal); - // TODO should be 0 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(100n * nominal); - expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(399n); + expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n); // it is potentially flaky test. Promotion can credited some tokens. Maybe we need to use closeTo? expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedBefore + 100n * nominal); // total tokens amount staked in app-promotion increased + await helper.staking.stake(staker, 200n * nominal); - expect(await helper.staking.getTotalStakingLocked({Substrate: staker.address})).to.be.equal(300n * nominal); expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(300n * nominal); expect((await helper.staking.getTotalStakedPerBlock({Substrate: staker.address})).map((x) => x[1])).to.be.deep.equal([100n * nominal, 200n * nominal]); }); }); + + it('should allow to stake with nonce', async () => { + await usingPlaygrounds(async (helper) => { + const staker = accounts.pop()!; + const transactions = []; + for (let nonce = 0; nonce < 9; nonce++) { + transactions.push(helper.signTransaction(staker, helper.api?.tx.promotion.stake(100n * nominal), 'Staker stakes with nonce', {nonce})); + } + await Promise.allSettled(transactions); + + expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(900n * nominal); + }); + }); it('should reject transaction if stake amount is more than total free balance minus frozen', async () => { await usingPlaygrounds(async helper => { - const [staker] = await helper.arrange.createAccounts([300n], alice); + const staker = accounts.pop()!; // Can't stake full balance because Alice needs to pay some fee - await expect(helper.staking.stake(staker, 300n * nominal)).to.be.eventually.rejected; - await helper.staking.stake(staker, 150n * nominal); + await expect(helper.staking.stake(staker, 1000n * nominal)).to.be.eventually.rejected; + await helper.staking.stake(staker, 500n * nominal); - // Can't stake 150 tkn because Alice has Less than 150 free - frozen tokens; - await expect(helper.staking.stake(staker, 150n * nominal)).to.be.eventually.rejected; - expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(150n * nominal); + // Can't stake 500 tkn because Alice has Less than 500 transferable; + await expect(helper.staking.stake(staker, 500n * nominal)).to.be.eventually.rejected; + expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(500n * nominal); }); }); it('for different accounts in one block is possible', async () => { await usingPlaygrounds(async helper => { - const crowd = await helper.arrange.createAccounts([1000n, 1000n, 1000n, 1000n], alice); + const crowd = [accounts.pop()!, accounts.pop()!, accounts.pop()!, accounts.pop()!]; const crowdStartsToStake = crowd.map(user => helper.staking.stake(user, 100n * nominal)); await expect(Promise.all(crowdStartsToStake)).to.be.eventually.fulfilled; @@ -118,24 +129,25 @@ expect(crowdStakes).to.deep.equal([100n * nominal, 100n * nominal, 100n * nominal, 100n * nominal]); }); }); + + it('should allow to create maximum 10 stakes for account', async () => { + + }); }); describe('unstake balance extrinsic', () => { - it('should change balance state to "reserved", add it to "pendingUnstake" map, and subtract it from totalStaked', async () => { + it('should change balance state from "frozen" to "reserved", add it to "pendingUnstake" map, and subtract it from totalStaked', async () => { await usingPlaygrounds(async helper => { + const [staker, recepient] = [accounts.pop()!, accounts.pop()!]; const totalStakedBefore = await helper.staking.getTotalStaked(); - const [staker, recepient] = await helper.arrange.createAccounts([600n, 0n], alice); - await helper.staking.stake(staker, 500n * nominal); + await helper.staking.stake(staker, 900n * nominal); await helper.staking.unstake(staker); - // balance state still - - // Stakers balance now: {free: <100n, reserved: 500n, miscFrozen: 0, feeFrozen: 0}; + // Right after unstake balance is reserved // Staker can not transfer - // TODO expect(await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 500n * nominal, miscFrozen: 0n, feeFrozen: 0n}); + expect(await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 900n * nominal, miscFrozen: 0n, feeFrozen: 0n}); await expect(helper.balance.transferToSubstrate(staker, recepient.address, 100n * nominal)).to.be.rejected; - - expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(500n * nominal); + expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(900n * nominal); expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(0n); expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedBefore); }); @@ -143,21 +155,24 @@ it('should unlock balance after unlocking period ends and subtract it from "pendingUnstake"', async () => { await usingPlaygrounds(async (helper) => { - const [staker] = await helper.arrange.createAccounts([1000n], alice); + const staker = accounts.pop()!; await helper.staking.stake(staker, 100n * nominal); await helper.staking.unstake(staker); - await waitForRelayBlock(helper.api!, 20); // Wait for unstaking period. Balance now free ~1000; reserved, frozen, miscFrozeb: 0n await waitForRelayBlock(helper.api!, 20); expect(await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n * nominal, miscFrozen: 0n, feeFrozen: 0n}); expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n); + + // staker can transfer: + await helper.balance.transferToSubstrate(staker, alice.address, 998n * nominal); + expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(1n); }); }); it('should successfully unstake multiple stakes', async () => { await usingPlaygrounds(async helper => { - const [staker] = await helper.arrange.createAccounts([1000n], alice); + const staker = accounts.pop()!; await helper.staking.stake(staker, 100n * nominal); await helper.staking.stake(staker, 200n * nominal); await helper.staking.stake(staker, 300n * nominal); @@ -188,7 +203,7 @@ it('should not have any effects if no active stakes', async () => { await usingPlaygrounds(async (helper) => { - const [staker] = await helper.arrange.createAccounts([1000n], alice); + const staker = accounts.pop()!; // unstake has no effect if no stakes at all await helper.staking.unstake(staker); @@ -211,7 +226,7 @@ it('should keep different unlocking block for each unlocking stake', async () => { await usingPlaygrounds(async (helper) => { - const [staker] = await helper.arrange.createAccounts([1000n], alice); + const staker = accounts.pop()!; await helper.staking.stake(staker, 100n * nominal); await helper.staking.unstake(staker); await helper.staking.stake(staker, 120n * nominal); @@ -226,7 +241,7 @@ it('should be possible for different accounts in one block', async () => { await usingPlaygrounds(async (helper) => { - const stakers = await helper.arrange.createAccounts([200n, 200n, 200n, 200n, 200n], alice); + const stakers = [accounts.pop()!, accounts.pop()!, accounts.pop()!, accounts.pop()!, accounts.pop()!]; await Promise.all(stakers.map(staker => helper.staking.stake(staker, 100n * nominal))); await Promise.all(stakers.map(staker => helper.staking.unstake(staker))); @@ -244,9 +259,10 @@ describe('Admin adress', () => { it('can be set by sudo only', async () => { await usingPlaygrounds(async (helper) => { - // Bob can not set admin not from himself nor as a sudo - await expect(helper.signTransaction(bob, helper.api!.tx.promotion.setAdminAddress({Substrate: bob.address}))).to.be.eventually.rejected; - await expect(helper.signTransaction(bob, helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.setAdminAddress({Substrate: bob.address})))).to.be.eventually.rejected; + const nonAdmin = accounts.pop()!; + // nonAdmin can not set admin not from himself nor as a sudo + await expect(helper.signTransaction(nonAdmin, helper.api!.tx.promotion.setAdminAddress({Substrate: nonAdmin.address}))).to.be.eventually.rejected; + await expect(helper.signTransaction(nonAdmin, helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.setAdminAddress({Substrate: nonAdmin.address})))).to.be.eventually.rejected; // Alice can await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.setAdminAddress({Substrate: palletAdmin.address})))).to.be.eventually.fulfilled; @@ -257,21 +273,21 @@ // We are not going to set an eth address as a sponsor, // but we do want to check, it doesn't break anything; await usingPlaygrounds(async (helper) => { - const [charlie] = await helper.arrange.createAccounts([10n], alice); - const ethCharlie = helper.address.substrateToEth(charlie.address); + const account = accounts.pop()!; + const ethAccount = helper.address.substrateToEth(account.address); // Alice sets Ethereum address as a sudo. Then Substrate address back... - await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.setAdminAddress({Ethereum: ethCharlie})))).to.be.eventually.fulfilled; + await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.setAdminAddress({Ethereum: ethAccount})))).to.be.eventually.fulfilled; await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.setAdminAddress({Substrate: palletAdmin.address})))).to.be.eventually.fulfilled; // ...It doesn't break anything; - const collection = await helper.nft.mintCollection(charlie, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'}); - await expect(helper.signTransaction(charlie, helper.api!.tx.promotion.sponsorCollection(collection.collectionId))).to.be.eventually.rejected; + const collection = await helper.nft.mintCollection(account, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'}); + await expect(helper.signTransaction(account, helper.api!.tx.promotion.sponsorCollection(collection.collectionId))).to.be.eventually.rejected; }); }); it('can be reassigned', async () => { await usingPlaygrounds(async (helper) => { - const [oldAdmin, newAdmin, collectionOwner] = await helper.arrange.createAccounts([10n, 10n, 10n], alice); + const [oldAdmin, newAdmin, collectionOwner] = [accounts.pop()!, accounts.pop()!, accounts.pop()!]; const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'}); await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.setAdminAddress(normalizeAccountId(oldAdmin))))).to.be.eventually.fulfilled; @@ -293,7 +309,7 @@ it('should actually sponsor transactions', async () => { await usingPlaygrounds(async (helper) => { - const [collectionOwner, tokenSender, receiver] = await helper.arrange.createAccounts([10n, 10n, 0n], alice); + const [collectionOwner, tokenSender, receiver] = [accounts.pop()!, accounts.pop()!, accounts.pop()!]; const collection = await helper.nft.mintCollection(collectionOwner, {name: 'Name', description: 'Description', tokenPrefix: 'Prefix', limits: {sponsorTransferTimeout: 0}}); const token = await collection.mintToken(collectionOwner, {Substrate: tokenSender.address}); await helper.signTransaction(palletAdmin, helper.api!.tx.promotion.sponsorCollection(collection.collectionId)); @@ -303,15 +319,15 @@ expect (await token.getOwner()).to.be.deep.equal({Substrate: receiver.address}); const palletBalanceAfter = await helper.balance.getSubstrate(palletAddress); - // senders balance the same - expect (await helper.balance.getSubstrate(tokenSender.address)).to.be.equal(10n * nominal); + // senders balance the same, transaction has sponsored + expect (await helper.balance.getSubstrate(tokenSender.address)).to.be.equal(1000n * nominal); expect (palletBalanceBefore > palletBalanceAfter).to.be.true; }); }); it('can not be set by non admin', async () => { await usingPlaygrounds(async (helper) => { - const [collectionOwner, nonAdmin] = await helper.arrange.createAccounts([10n, 10n], alice); + const [collectionOwner, nonAdmin] = [accounts.pop()!, accounts.pop()!]; const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'}); @@ -322,7 +338,7 @@ it('should set pallet address as confirmed admin', async () => { await usingPlaygrounds(async (helper) => { - const [collectionOwner, oldSponsor] = await helper.arrange.createAccounts([20n, 20n], alice); + const [collectionOwner, oldSponsor] = [accounts.pop()!, accounts.pop()!]; // Can set sponsoring for collection without sponsor const collectionWithoutSponsor = await helper.nft.mintCollection(collectionOwner, {name: 'No-sponsor', description: 'New Collection', tokenPrefix: 'Promotion'}); @@ -345,7 +361,7 @@ it('can be overwritten by collection owner', async () => { await usingPlaygrounds(async (helper) => { - const [collectionOwner, newSponsor] = await helper.arrange.createAccounts([20n, 0n], alice); + const [collectionOwner, newSponsor] = [accounts.pop()!, accounts.pop()!]; const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'}); const collectionId = collection.collectionId; @@ -374,7 +390,7 @@ it('should reject transaction if collection doesn\'t exist', async () => { await usingPlaygrounds(async (helper) => { - const [collectionOwner] = await helper.arrange.createAccounts([10n], alice); + const collectionOwner = accounts.pop()!; // collection has never existed await expect(helper.signTransaction(palletAdmin, helper.api!.tx.promotion.sponsorCollection(999999999))).to.be.eventually.rejected; @@ -390,7 +406,7 @@ describe('app-promotion stopSponsoringCollection', () => { it('can not be called by non-admin', async () => { await usingPlaygrounds(async (helper) => { - const [collectionOwner, nonAdmin] = await helper.arrange.createAccounts([10n, 10n], alice); + const [collectionOwner, nonAdmin] = [accounts.pop()!, accounts.pop()!]; const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'}); await expect(helper.signTransaction(palletAdmin, helper.api!.tx.promotion.sponsorCollection(collection.collectionId))).to.be.eventually.fulfilled; @@ -402,7 +418,7 @@ it('should set sponsoring as disabled', async () => { await usingPlaygrounds(async (helper) => { - const [collectionOwner, recepient] = await helper.arrange.createAccounts([10n, 0n], alice); + const [collectionOwner, recepient] = [accounts.pop()!, accounts.pop()!]; const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion', limits: {sponsorTransferTimeout: 0}}); const token = await collection.mintToken(collectionOwner, {Substrate: collectionOwner.address}); @@ -421,7 +437,7 @@ it('should not affect collection which is not sponsored by pallete', async () => { await usingPlaygrounds(async (helper) => { - const [collectionOwner] = await helper.arrange.createAccounts([10n, 10n], alice); + const collectionOwner = accounts.pop()!; const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion', pendingSponsor: collectionOwner.address}); await collection.confirmSponsorship(collectionOwner); @@ -433,7 +449,7 @@ it('should reject transaction if collection does not exist', async () => { await usingPlaygrounds(async (helper) => { - const [collectionOwner] = await helper.arrange.createAccounts([10n], alice); + const collectionOwner = accounts.pop()!; const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'}); await collection.burn(collectionOwner); @@ -515,7 +531,7 @@ itWeb3('can not be set by non admin', async ({api, web3, privateKeyWrapper}) => { await usingPlaygrounds(async (helper) => { - const [nonAdmin] = await helper.arrange.createAccounts([50n], alice); + const nonAdmin = accounts.pop()!; const contractOwner = (await createEthAccountWithBalance(api, web3, privateKeyWrapper)).toLowerCase(); const flipper = await deployFlipper(web3, contractOwner); const contractMethods = contractHelpers(web3, contractOwner); @@ -595,7 +611,7 @@ itWeb3('can not be called by non-admin', async ({api, web3, privateKeyWrapper}) => { await usingPlaygrounds(async (helper) => { - const [nonAdmin] = await helper.arrange.createAccounts([10n], alice); + const nonAdmin = accounts.pop()!; const contractOwner = (await createEthAccountWithBalance(api, web3, privateKeyWrapper)).toLowerCase(); const flipper = await deployFlipper(web3, contractOwner); @@ -606,7 +622,7 @@ itWeb3('should not affect a contract which is not sponsored by pallete', async ({api, web3, privateKeyWrapper}) => { await usingPlaygrounds(async (helper) => { - const [nonAdmin] = await helper.arrange.createAccounts([10n], alice); + const nonAdmin = accounts.pop()!; const contractOwner = (await createEthAccountWithBalance(api, web3, privateKeyWrapper)).toLowerCase(); const flipper = await deployFlipper(web3, contractOwner); const contractHelper = contractHelpers(web3, contractOwner); @@ -620,7 +636,7 @@ describe('app-promotion rewards', () => { it('can not be called by non admin', async () => { await usingPlaygrounds(async (helper) => { - const [nonAdmin] = await helper.arrange.createAccounts([10n], alice); + const nonAdmin = accounts.pop()!; await expect(helper.signTransaction(nonAdmin, helper.api!.tx.promotion.payoutStakers(50))).to.be.rejected; }); }); @@ -628,7 +644,7 @@ it('should credit 0.05% for staking period', async () => { // TODO flaky test await usingPlaygrounds(async helper => { - const [staker] = await helper.arrange.createAccounts([5000n], alice); + const staker = accounts.pop()!; await helper.staking.stake(staker, 100n * nominal); await helper.staking.stake(staker, 200n * nominal); @@ -642,7 +658,7 @@ it('shoud be paid for more than one period if payments was missed', async () => { await usingPlaygrounds(async (helper) => { - const [staker] = await helper.arrange.createAccounts([400n], alice); + const staker = accounts.pop()!; await helper.staking.stake(staker, 100n * nominal); await helper.staking.stake(staker, 200n * nominal); @@ -663,14 +679,14 @@ it('should not be credited for unstaked (reserved) balance', async () => { expect.fail('Test not implemented'); await usingPlaygrounds(async helper => { - const staker = await helper.arrange.createAccounts([1000n], alice); + const staker = accounts.pop()!; }); }); it('should bring compound interest', async () => { // TODO flaky test await usingPlaygrounds(async helper => { - const [staker] = await helper.arrange.createAccounts([800n], alice); + const staker = accounts.pop()!; await helper.staking.stake(staker, 100n * nominal); await helper.staking.stake(staker, 200n * nominal);