git.delta.rocks / unique-network / refs/commits / 8ee00409a037

difftreelog

Add missing tests

Max Andreev2022-08-28parent: #160555a.patch.diff
in: master

1 file changed

modifiedtests/src/app-promotion.test.tsdiffbeforeafterboth
43 alice = privateKeyWrapper('//Alice');43 alice = privateKeyWrapper('//Alice');
44 bob = privateKeyWrapper('//Bob');44 bob = privateKeyWrapper('//Bob');
45 palletAdmin = privateKeyWrapper('//palletAdmin');45 palletAdmin = privateKeyWrapper('//palletAdmin');
46 const tx = helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.setAdminAddress(normalizeAccountId(palletAdmin)));46 await helper.balance.transferToSubstrate(alice, palletAdmin.address, 10n * helper.balance.getOneTokenNominal());
47 await helper.signTransaction(alice, tx);
48 nominal = helper.balance.getOneTokenNominal();47 nominal = helper.balance.getOneTokenNominal();
49 });48 });
50});49});
161 expect(stakedPerBlock).to.deep.equal([]);160 expect(stakedPerBlock).to.deep.equal([]);
162 expect(unstakedPerBlock).to.deep.equal([5n * nominal, 10n * nominal, 45n * nominal]);161 expect(unstakedPerBlock).to.deep.equal([5n * nominal, 10n * nominal, 45n * nominal]);
163 });162 });
164
165 // TODO it('try to hack unstaking with nonce')
166 });163 });
167});
168164
169describe('Admin adress', () => {165 it('should reject transaction if unstake amount is greater than staked', async () => {
170 before(async function () {
171 await usingPlaygrounds(async (helper, privateKeyWrapper) => {
172 if (!getModuleNames(helper.api!).includes(Pallets.AppPromotion)) this.skip();166 await usingPlaygrounds(async (helper) => {
173 alice = privateKeyWrapper('//Alice');
174 bob = privateKeyWrapper('//Bob');167 const [staker] = await helper.arrange.creteAccounts([10n], alice);
175 palletAdmin = privateKeyWrapper('//palletAdmin');
176 await helper.balance.transferToSubstrate(alice, palletAdmin.address, 10n * helper.balance.getOneTokenNominal());
177 const tx = helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.setAdminAddress(normalizeAccountId(palletAdmin)));
178 168
179 await helper.signTransaction(alice, tx);169 // can't unstsake more than one stake amount
170 await helper.staking.stake(staker, 1n * nominal);
180 171 await expect(helper.staking.unstake(staker, 1n * nominal + 1n)).to.be.eventually.rejected;
181 nominal = helper.balance.getOneTokenNominal();172 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(0n);
173 expect(await helper.staking.getTotalStakingLocked({Substrate: staker.address})).to.be.equal(1n * nominal);
174
175 // can't unstsake more than two stakes amount
176 await helper.staking.stake(staker, 1n * nominal);
177 await expect(helper.staking.unstake(staker, 2n * nominal + 1n)).to.be.eventually.rejected;
178 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(0n);
179 expect(await helper.staking.getTotalStakingLocked({Substrate: staker.address})).to.be.equal(2n * nominal);
180
181 // can't unstake more than have with nonce // TODO not sure we need this assertion
182 const nonce1 = await helper.chain.getNonce(staker.address);
183 const nonce2 = nonce1 + 1;
184 const unstakeMoreThanHaveWithNonce = Promise.all([
185 helper.signTransaction(staker, helper.constructApiCall('api.tx.promotion.unstake', [1n * nominal]), 'unstaking 1', {nonce: nonce1}),
186 helper.signTransaction(staker, helper.constructApiCall('api.tx.promotion.unstake', [1n * nominal + 1n]), 'unstaking 1+', {nonce: nonce2}),
187 ]);
188 await expect(unstakeMoreThanHaveWithNonce).to.be.rejected;
189 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(1n * nominal);
182 });190 });
183 });191 });
184192
193 it('should allow to unstake even smallest unit', async () => {
194 await usingPlaygrounds(async (helper) => {
195 const [staker] = await helper.arrange.creteAccounts([10n], alice);
196 await helper.staking.stake(staker, nominal);
197 // unstake .000...001 is possible
198 await helper.staking.unstake(staker, 1n);
199 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(1n);
200 });
201 });
202
203 it('should work fine if stake amount is smallest unit', async () => {
204 await usingPlaygrounds(async (helper) => {
205 const [staker] = await helper.arrange.creteAccounts([10n], alice);
206 await helper.staking.stake(staker, nominal);
207 await helper.staking.unstake(staker, nominal - 1n);
208 await waitForRecalculationBlock(helper.api!);
209
210 // Everything fine, blockchain alive
211 await helper.nft.mintCollection(staker, {name: 'name', description: 'description', tokenPrefix: 'prefix'});
212 });
213 });
214
215 // TODO will return balance to "available" state after the period of unstaking is finished, and subtract it from "pendingUnstake
216 // TODO for different accounts in one block is possible
217});
218
219describe('Admin adress', () => {
185 it('can be set by sudo only', async () => {220 it('can be set by sudo only', async () => {
186 // assert: Sudo calls appPromotion.setAdminAddress(Alice) /// Sudo successfully sets Alice as admin
187 // assert: Bob calls appPromotion.setAdminAddress(Bob) throws /// Random account can not set admin
188 // assert: Alice calls appPromotion.setAdminAddress(Bob) throws /// Admin account can not set admin
189 await usingPlaygrounds(async (helper) => {221 await usingPlaygrounds(async (helper) => {
190 await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.setAdminAddress(normalizeAccountId(alice))))).to.be.eventually.fulfilled;222 // Bob can not set admin not from himself nor as a sudo
223 await expect(helper.signTransaction(bob, helper.api!.tx.promotion.setAdminAddress({Substrate: bob.address}))).to.be.eventually.rejected;
191 await expect(helper.signTransaction(bob, helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.setAdminAddress(normalizeAccountId(bob))))).to.be.eventually.rejected;224 await expect(helper.signTransaction(bob, helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.setAdminAddress({Substrate: bob.address})))).to.be.eventually.rejected;
225
192 await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.setAdminAddress(normalizeAccountId(bob))))).to.be.eventually.fulfilled;226 // Alice can
227 await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.setAdminAddress({Substrate: palletAdmin.address})))).to.be.eventually.fulfilled;
193 });228 });
194
195 });229 });
196 230
197 it('can be any valid CrossAccountId', async () => {231 it('can be any valid CrossAccountId', async () => {
198 /// We are not going to set an eth address as a sponsor,232 // We are not going to set an eth address as a sponsor,
199 /// but we do want to check, it doesn't break anything;233 // but we do want to check, it doesn't break anything;
200
201 // arrange: Charlie creates Punks
202 // arrange: Sudo calls appPromotion.setAdminAddress(0x0...) success
203 // arrange: Sudo calls appPromotion.setAdminAddress(Alice) success
204
205 // assert: Alice calls appPromotion.sponsorCollection(Punks.id) success
206
207 await usingPlaygrounds(async (helper) => {234 await usingPlaygrounds(async (helper) => {
235 const [charlie] = await helper.arrange.creteAccounts([10n], alice);
236 const ethCharlie = helper.address.substrateToEth(charlie.address);
237 // Alice sets Ethereum address as a sudo. Then Substrate address back...
238 await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.setAdminAddress({Ethereum: ethCharlie})))).to.be.eventually.fulfilled;
239 await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.setAdminAddress({Substrate: palletAdmin.address})))).to.be.eventually.fulfilled;
208 240
209 const ethAcc = {Ethereum: '0x67fb3503a61b284dc83fa96dceec4192db47dc7c'};241 // ...It doesn't break anything;
210 242 console.log(await helper.balance.getSubstrate(charlie.address));
211 await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.setAdminAddress(ethAcc)))).to.be.eventually.fulfilled;
212 await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.setAdminAddress(normalizeAccountId(palletAdmin))))).to.be.eventually.fulfilled;243 console.log(await helper.balance.getSubstrate(palletAdmin.address));
213 244 const collection = await helper.nft.mintCollection(charlie, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'});
214 const collection = await helper.nft.mintCollection(alice, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'});
215 245 await expect(helper.signTransaction(charlie, helper.api!.tx.promotion.sponsorCollection(collection.collectionId))).to.be.eventually.rejected;
216 await expect(helper.signTransaction(palletAdmin, helper.api!.tx.promotion.sponsorCollection(collection.collectionId))).to.be.eventually.fulfilled;
217 });246 });
218
219 });247 });
220248
221 it('can be reassigned', async () => {249 it('can be reassigned', async () => {
222 // arrange: Charlie creates Punks
223 // arrange: Sudo calls appPromotion.setAdminAddress(Alice)
224 // act: Sudo calls appPromotion.setAdminAddress(Bob)
225
226 // assert: Alice calls appPromotion.sponsorCollection(Punks.id) throws /// Alice can not set collection sponsor
227 // assert: Bob calls appPromotion.sponsorCollection(Punks.id) successful /// Bob can set collection sponsor
228
229 // act: Sudo calls appPromotion.setAdminAddress(null) successful /// Sudo can set null as a sponsor
230 // assert: Bob calls appPromotion.stopSponsoringCollection(Punks.id) throws /// Bob is no longer an admin
231
232 await usingPlaygrounds(async (helper) => {250 await usingPlaygrounds(async (helper) => {
233 const collection = await helper.nft.mintCollection(alice, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'});251 const [oldAdmin, newAdmin, collectionOwner] = await helper.arrange.creteAccounts([10n, 10n, 10n], alice);
252 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'});
234 253
235 await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.setAdminAddress(normalizeAccountId(alice))))).to.be.eventually.fulfilled;254 await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.setAdminAddress(normalizeAccountId(oldAdmin))))).to.be.eventually.fulfilled;
236 await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.setAdminAddress(normalizeAccountId(bob))))).to.be.eventually.fulfilled;255 await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.promotion.setAdminAddress(normalizeAccountId(newAdmin))))).to.be.eventually.fulfilled;
237 await expect(helper.signTransaction(alice, helper.api!.tx.promotion.sponsorCollection(collection.collectionId))).to.be.eventually.rejected;256 await expect(helper.signTransaction(oldAdmin, helper.api!.tx.promotion.sponsorCollection(collection.collectionId))).to.be.eventually.rejected;
238 257
239 await expect(helper.signTransaction(bob, helper.api!.tx.promotion.sponsorCollection(collection.collectionId))).to.be.eventually.fulfilled;258 await expect(helper.signTransaction(newAdmin, helper.api!.tx.promotion.sponsorCollection(collection.collectionId))).to.be.eventually.fulfilled;
240 });259 });
241
242 });260 });
243
244});261});
245262
246describe('App-promotion collection sponsoring', () => {263describe('App-promotion collection sponsoring', () => {
625642
626describe('app-promotion rewards', () => {643describe('app-promotion rewards', () => {
627 const DAY = 7200n;644 const DAY = 7200n;
645
646 // TODO (load test. Can pay reward for 10000 addresses)
628 647
629 648
630 before(async function () {649 before(async function () {
768 }787 }
769 });788 });
770 });789 });
771
772}790}
773791
774792