git.delta.rocks / unique-network / refs/commits / 0c093d5c3f30

difftreelog

use itSub

Max Andreev2022-09-14parent: #bbf501e.patch.diff
in: master

1 file changed

modifiedtests/src/app-promotion.test.tsdiffbeforeafterboth
2323
24import chai from 'chai';24import chai from 'chai';
25import chaiAsPromised from 'chai-as-promised';25import chaiAsPromised from 'chai-as-promised';
26import {usingPlaygrounds} from './util/playgrounds';26import {itSub, usingPlaygrounds} from './util/playgrounds';
2727
28import {encodeAddress} from '@polkadot/util-crypto';28import {encodeAddress} from '@polkadot/util-crypto';
29import {stringToU8a} from '@polkadot/util';29import {stringToU8a} from '@polkadot/util';
59 await beforeEach(this);59 await beforeEach(this);
60 });60 });
6161
62 it('should "lock" staking balance, add it to "staked" map, and increase "totalStaked" amount', async () => {62 itSub('should "lock" staking balance, add it to "staked" map, and increase "totalStaked" amount', async ({helper}) => {
63 await usingPlaygrounds(async (helper) => {
64 const [staker, recepient] = [accounts.pop()!, accounts.pop()!];63 const [staker, recepient] = [accounts.pop()!, accounts.pop()!];
65 const totalStakedBefore = await helper.staking.getTotalStaked();64 const totalStakedBefore = await helper.staking.getTotalStaked();
6665
67 // Minimum stake amount is 100:66 // Minimum stake amount is 100:
68 await expect(helper.staking.stake(staker, 100n * nominal - 1n)).to.be.eventually.rejected;67 await expect(helper.staking.stake(staker, 100n * nominal - 1n)).to.be.eventually.rejected;
69 await helper.staking.stake(staker, 100n * nominal);68 await helper.staking.stake(staker, 100n * nominal);
7069
71 // Staker balance is: miscFrozen: 100, feeFrozen: 100, reserved: 0n...70 // Staker balance is: miscFrozen: 100, feeFrozen: 100, reserved: 0n...
72 // ...so he can not transfer 90071 // ...so he can not transfer 900
73 expect (await helper.balance.getSubstrateFull(staker.address)).to.contain({miscFrozen: 100n * nominal, feeFrozen: 100n * nominal, reserved: 0n});72 expect (await helper.balance.getSubstrateFull(staker.address)).to.contain({miscFrozen: 100n * nominal, feeFrozen: 100n * nominal, reserved: 0n});
74 await expect(helper.balance.transferToSubstrate(staker, recepient.address, 900n * nominal)).to.be.rejected;73 await expect(helper.balance.transferToSubstrate(staker, recepient.address, 900n * nominal)).to.be.rejected;
75 74
76 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(100n * nominal);75 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(100n * nominal);
77 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n);76 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n);
78 // it is potentially flaky test. Promotion can credited some tokens. Maybe we need to use closeTo? 77 // it is potentially flaky test. Promotion can credited some tokens. Maybe we need to use closeTo?
79 expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedBefore + 100n * nominal); // total tokens amount staked in app-promotion increased 78 expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedBefore + 100n * nominal); // total tokens amount staked in app-promotion increased
8079
81 80
82 await helper.staking.stake(staker, 200n * nominal);81 await helper.staking.stake(staker, 200n * nominal);
83 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(300n * nominal);82 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(300n * nominal);
84 const totalStakedPerBlock = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});83 const totalStakedPerBlock = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
85 expect(totalStakedPerBlock[0].amount).to.equal(100n * nominal);84 expect(totalStakedPerBlock[0].amount).to.equal(100n * nominal);
86 expect(totalStakedPerBlock[1].amount).to.equal(200n * nominal);85 expect(totalStakedPerBlock[1].amount).to.equal(200n * nominal);
87 });
88 });86 });
8987
90 it('should allow to create maximum 10 stakes for account', async () => {88 itSub('should allow to create maximum 10 stakes for account', async ({helper}) => {
91 await usingPlaygrounds(async (helper) => {
92 const [staker] = await helper.arrange.createAccounts([2000n], alice);89 const [staker] = await helper.arrange.createAccounts([2000n], alice);
93 for (let i = 0; i < 10; i++) {90 for (let i = 0; i < 10; i++) {
94 await helper.staking.stake(staker, 100n * nominal);91 await helper.staking.stake(staker, 100n * nominal);
95 }92 }
9693
97 // can have 10 stakes94 // can have 10 stakes
98 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(1000n * nominal);95 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(1000n * nominal);
99 expect(await helper.staking.getTotalStakedPerBlock({Substrate: staker.address})).to.have.length(10);96 expect(await helper.staking.getTotalStakedPerBlock({Substrate: staker.address})).to.have.length(10);
10097
101 await expect(helper.staking.stake(staker, 100n * nominal)).to.be.rejected;98 await expect(helper.staking.stake(staker, 100n * nominal)).to.be.rejected;
10299
103 // After unstake can stake again100 // After unstake can stake again
104 await helper.staking.unstake(staker);101 await helper.staking.unstake(staker);
105 await helper.staking.stake(staker, 100n * nominal);102 await helper.staking.stake(staker, 100n * nominal);
106 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.equal(100n * nominal);103 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.equal(100n * nominal);
107 });
108 });104 });
109 105
110 it('should reject transaction if stake amount is more than total free balance minus frozen', async () => {106 itSub('should reject transaction if stake amount is more than total free balance minus frozen', async ({helper}) => {
111 await usingPlaygrounds(async helper => {
112 const staker = accounts.pop()!;107 const staker = accounts.pop()!;
113108
114 // Can't stake full balance because Alice needs to pay some fee109 // Can't stake full balance because Alice needs to pay some fee
115 await expect(helper.staking.stake(staker, 1000n * nominal)).to.be.eventually.rejected;110 await expect(helper.staking.stake(staker, 1000n * nominal)).to.be.eventually.rejected;
116 await helper.staking.stake(staker, 500n * nominal);111 await helper.staking.stake(staker, 500n * nominal);
117112
118 // Can't stake 500 tkn because Alice has Less than 500 transferable;113 // Can't stake 500 tkn because Alice has Less than 500 transferable;
119 await expect(helper.staking.stake(staker, 500n * nominal)).to.be.eventually.rejected; 114 await expect(helper.staking.stake(staker, 500n * nominal)).to.be.eventually.rejected;
120 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(500n * nominal);115 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(500n * nominal);
121 });
122 });116 });
123 117
124 it('for different accounts in one block is possible', async () => {118 itSub('for different accounts in one block is possible', async ({helper}) => {
125 await usingPlaygrounds(async helper => {
126 const crowd = [accounts.pop()!, accounts.pop()!, accounts.pop()!, accounts.pop()!];119 const crowd = [accounts.pop()!, accounts.pop()!, accounts.pop()!, accounts.pop()!];
127 120
128 const crowdStartsToStake = crowd.map(user => helper.staking.stake(user, 100n * nominal));121 const crowdStartsToStake = crowd.map(user => helper.staking.stake(user, 100n * nominal));
129 await expect(Promise.all(crowdStartsToStake)).to.be.eventually.fulfilled;122 await expect(Promise.all(crowdStartsToStake)).to.be.eventually.fulfilled;
130123
131 const crowdStakes = await Promise.all(crowd.map(address => helper.staking.getTotalStaked({Substrate: address.address})));124 const crowdStakes = await Promise.all(crowd.map(address => helper.staking.getTotalStaked({Substrate: address.address})));
132 expect(crowdStakes).to.deep.equal([100n * nominal, 100n * nominal, 100n * nominal, 100n * nominal]);125 expect(crowdStakes).to.deep.equal([100n * nominal, 100n * nominal, 100n * nominal, 100n * nominal]);
133 });
134 });126 });
135});127});
136128
139 await beforeEach(this);131 await beforeEach(this);
140 });132 });
141133
142 it('should change balance state from "frozen" to "reserved", add it to "pendingUnstake" map, and subtract it from totalStaked', async () => {134 itSub('should change balance state from "frozen" to "reserved", add it to "pendingUnstake" map, and subtract it from totalStaked', async ({helper}) => {
143 await usingPlaygrounds(async helper => {
144 const [staker, recepient] = [accounts.pop()!, accounts.pop()!];135 const [staker, recepient] = [accounts.pop()!, accounts.pop()!];
145 const totalStakedBefore = await helper.staking.getTotalStaked();136 const totalStakedBefore = await helper.staking.getTotalStaked();
146 await helper.staking.stake(staker, 900n * nominal);137 await helper.staking.stake(staker, 900n * nominal);
147 await helper.staking.unstake(staker);138 await helper.staking.unstake(staker);
148139
149 // Right after unstake balance is reserved140 // Right after unstake balance is reserved
150 // Staker can not transfer 141 // Staker can not transfer
151 expect(await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 900n * nominal, miscFrozen: 0n, feeFrozen: 0n});142 expect(await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 900n * nominal, miscFrozen: 0n, feeFrozen: 0n});
152 await expect(helper.balance.transferToSubstrate(staker, recepient.address, 100n * nominal)).to.be.rejected;143 await expect(helper.balance.transferToSubstrate(staker, recepient.address, 100n * nominal)).to.be.rejected;
153 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(900n * nominal);144 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(900n * nominal);
154 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(0n);145 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(0n);
155 expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedBefore);146 expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedBefore);
156 });
157 });147 });
158148
159 it('should unlock balance after unlocking period ends and remove it from "pendingUnstake"', async () => {149 itSub('should unlock balance after unlocking period ends and remove it from "pendingUnstake"', async ({helper}) => {
160 await usingPlaygrounds(async (helper) => {
161 const staker = accounts.pop()!;150 const staker = accounts.pop()!;
162 await helper.staking.stake(staker, 100n * nominal);151 await helper.staking.stake(staker, 100n * nominal);
163 await helper.staking.unstake(staker);152 await helper.staking.unstake(staker);
164 const [pendingUnstake] = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});153 const [pendingUnstake] = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});
165154
166 // Wait for unstaking period. Balance now free ~1000; reserved, frozen, miscFrozeb: 0n155 // Wait for unstaking period. Balance now free ~1000; reserved, frozen, miscFrozeb: 0n
167 await helper.wait.forParachainBlockNumber(pendingUnstake.block);156 await helper.wait.forParachainBlockNumber(pendingUnstake.block);
168 expect(await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n, miscFrozen: 0n, feeFrozen: 0n});157 expect(await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n, miscFrozen: 0n, feeFrozen: 0n});
169 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n);158 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n);
170159
171 // staker can transfer:160 // staker can transfer:
172 await helper.balance.transferToSubstrate(staker, alice.address, 998n * nominal);161 await helper.balance.transferToSubstrate(staker, alice.address, 998n * nominal);
173 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(1n);162 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(1n);
174 });
175 });163 });
176164
177 it('should successfully unstake multiple stakes', async () => {165 itSub('should successfully unstake multiple stakes', async ({helper}) => {
178 await usingPlaygrounds(async helper => {
179 const staker = accounts.pop()!;166 const staker = accounts.pop()!;
180 await helper.staking.stake(staker, 100n * nominal);167 await helper.staking.stake(staker, 100n * nominal);
181 await helper.staking.stake(staker, 200n * nominal);168 await helper.staking.stake(staker, 200n * nominal);
182 await helper.staking.stake(staker, 300n * nominal);169 await helper.staking.stake(staker, 300n * nominal);
183170
184 // staked: [100, 200, 300]; unstaked: 0171 // staked: [100, 200, 300]; unstaked: 0
185 let totalPendingUnstake = await helper.staking.getPendingUnstake({Substrate: staker.address});172 let totalPendingUnstake = await helper.staking.getPendingUnstake({Substrate: staker.address});
186 let pendingUnstake = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});173 let pendingUnstake = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});
187 let stakes = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});174 let stakes = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
188 expect(totalPendingUnstake).to.be.deep.equal(0n);175 expect(totalPendingUnstake).to.be.deep.equal(0n);
189 expect(pendingUnstake).to.be.deep.equal([]);176 expect(pendingUnstake).to.be.deep.equal([]);
190 expect(stakes[0].amount).to.equal(100n * nominal);177 expect(stakes[0].amount).to.equal(100n * nominal);
191 expect(stakes[1].amount).to.equal(200n * nominal);178 expect(stakes[1].amount).to.equal(200n * nominal);
192 expect(stakes[2].amount).to.equal(300n * nominal);179 expect(stakes[2].amount).to.equal(300n * nominal);
193 180
194 // Can unstake multiple stakes181 // Can unstake multiple stakes
195 await helper.staking.unstake(staker);182 await helper.staking.unstake(staker);
196 pendingUnstake = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});183 pendingUnstake = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});
197 totalPendingUnstake = await helper.staking.getPendingUnstake({Substrate: staker.address});184 totalPendingUnstake = await helper.staking.getPendingUnstake({Substrate: staker.address});
198 stakes = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});185 stakes = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
199 expect(totalPendingUnstake).to.be.equal(600n * nominal);186 expect(totalPendingUnstake).to.be.equal(600n * nominal);
200 expect(stakes).to.be.deep.equal([]);187 expect(stakes).to.be.deep.equal([]);
201 expect(pendingUnstake[0].amount).to.equal(600n * nominal);188 expect(pendingUnstake[0].amount).to.equal(600n * nominal);
202189
203 expect (await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 600n * nominal, feeFrozen: 0n, miscFrozen: 0n});190 expect (await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 600n * nominal, feeFrozen: 0n, miscFrozen: 0n});
204 await helper.wait.forParachainBlockNumber(pendingUnstake[0].block);191 await helper.wait.forParachainBlockNumber(pendingUnstake[0].block);
205 expect (await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n, feeFrozen: 0n, miscFrozen: 0n});192 expect (await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n, feeFrozen: 0n, miscFrozen: 0n});
206 expect (await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n);193 expect (await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n);
207 });
208 });194 });
209195
210 it('should not have any effects if no active stakes', async () => {196 itSub('should not have any effects if no active stakes', async ({helper}) => {
211 await usingPlaygrounds(async (helper) => {
212 const staker = accounts.pop()!;197 const staker = accounts.pop()!;
213 198
214 // unstake has no effect if no stakes at all199 // unstake has no effect if no stakes at all
215 await helper.staking.unstake(staker);200 await helper.staking.unstake(staker);
216 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(0n);201 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(0n);
217 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n); // TODO bigint closeTo helper202 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n); // TODO bigint closeTo helper
218203
219 // TODO stake() unstake() waitUnstaked() unstake();204 // TODO stake() unstake() waitUnstaked() unstake();
220205
221 // can't unstake if there are only pendingUnstakes206 // can't unstake if there are only pendingUnstakes
222 await helper.staking.stake(staker, 100n * nominal);207 await helper.staking.stake(staker, 100n * nominal);
223 await helper.staking.unstake(staker);208 await helper.staking.unstake(staker);
224 await helper.staking.unstake(staker);209 await helper.staking.unstake(staker);
225210
226 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(100n * nominal);211 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(100n * nominal);
227 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(0n);212 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(0n);
228 });
229 });213 });
230214
231 it('should keep different unlocking block for each unlocking stake', async () => {215 itSub('should keep different unlocking block for each unlocking stake', async ({helper}) => {
232 await usingPlaygrounds(async (helper) => {
233 const staker = accounts.pop()!;216 const staker = accounts.pop()!;
234 await helper.staking.stake(staker, 100n * nominal);217 await helper.staking.stake(staker, 100n * nominal);
235 await helper.staking.unstake(staker);218 await helper.staking.unstake(staker);
236 await helper.staking.stake(staker, 120n * nominal);219 await helper.staking.stake(staker, 120n * nominal);
237 await helper.staking.unstake(staker);220 await helper.staking.unstake(staker);
238221
239 const unstakingPerBlock = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});222 const unstakingPerBlock = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});
240 expect(unstakingPerBlock).has.length(2);223 expect(unstakingPerBlock).has.length(2);
241 expect(unstakingPerBlock[0].amount).to.equal(100n * nominal);224 expect(unstakingPerBlock[0].amount).to.equal(100n * nominal);
242 expect(unstakingPerBlock[1].amount).to.equal(120n * nominal);225 expect(unstakingPerBlock[1].amount).to.equal(120n * nominal);
243 });
244 });226 });
245227
246 it('should be possible for different accounts in one block', async () => {228 itSub('should be possible for different accounts in one block', async ({helper}) => {
247 await usingPlaygrounds(async (helper) => {
248 const stakers = [accounts.pop()!, accounts.pop()!, accounts.pop()!];229 const stakers = [accounts.pop()!, accounts.pop()!, accounts.pop()!];
249230
250 await Promise.all(stakers.map(staker => helper.staking.stake(staker, 100n * nominal)));231 await Promise.all(stakers.map(staker => helper.staking.stake(staker, 100n * nominal)));
251 await Promise.all(stakers.map(staker => helper.staking.unstake(staker)));232 await Promise.all(stakers.map(staker => helper.staking.unstake(staker)));
252233
253 await Promise.all(stakers.map(async (staker) => {234 await Promise.all(stakers.map(async (staker) => {
254 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(100n * nominal);235 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(100n * nominal);
255 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(0n);236 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(0n);
256 }));237 }));
257 });
258 });238 });
259});239});
260240
263 await beforeEach(this);243 await beforeEach(this);
264 });244 });
265245
266 it('can be set by sudo only', async () => {246 itSub('can be set by sudo only', async ({helper}) => {
267 await usingPlaygrounds(async (helper) => {
268 const nonAdmin = accounts.pop()!;247 const nonAdmin = accounts.pop()!;
269 // nonAdmin can not set admin not from himself nor as a sudo248 // nonAdmin can not set admin not from himself nor as a sudo
270 await expect(helper.signTransaction(nonAdmin, helper.api!.tx.appPromotion.setAdminAddress({Substrate: nonAdmin.address}))).to.be.eventually.rejected;249 await expect(helper.signTransaction(nonAdmin, helper.api!.tx.appPromotion.setAdminAddress({Substrate: nonAdmin.address}))).to.be.eventually.rejected;
271 await expect(helper.signTransaction(nonAdmin, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress({Substrate: nonAdmin.address})))).to.be.eventually.rejected;250 await expect(helper.signTransaction(nonAdmin, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress({Substrate: nonAdmin.address})))).to.be.eventually.rejected;
272251
273 // Alice can252 // Alice can
274 await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress({Substrate: palletAdmin.address})))).to.be.eventually.fulfilled;253 await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress({Substrate: palletAdmin.address})))).to.be.eventually.fulfilled;
275 });
276 });254 });
277 255
278 it('can be any valid CrossAccountId', async () => {256 itSub('can be any valid CrossAccountId', async ({helper}) => {
279 // We are not going to set an eth address as a sponsor,257 // We are not going to set an eth address as a sponsor,
280 // but we do want to check, it doesn't break anything;258 // but we do want to check, it doesn't break anything;
281 await usingPlaygrounds(async (helper) => {259 const account = accounts.pop()!;
282 const account = accounts.pop()!;
283 const ethAccount = helper.address.substrateToEth(account.address); 260 const ethAccount = helper.address.substrateToEth(account.address);
284 // Alice sets Ethereum address as a sudo. Then Substrate address back...261 // Alice sets Ethereum address as a sudo. Then Substrate address back...
285 await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress({Ethereum: ethAccount})))).to.be.eventually.fulfilled;262 await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress({Ethereum: ethAccount})))).to.be.eventually.fulfilled;
286 await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress({Substrate: palletAdmin.address})))).to.be.eventually.fulfilled;263 await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress({Substrate: palletAdmin.address})))).to.be.eventually.fulfilled;
287 264
288 // ...It doesn't break anything;265 // ...It doesn't break anything;
289 const collection = await helper.nft.mintCollection(account, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'});266 const collection = await helper.nft.mintCollection(account, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'});
290 await expect(helper.signTransaction(account, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.eventually.rejected;267 await expect(helper.signTransaction(account, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.eventually.rejected;
291 });
292 });268 });
293269
294 it('can be reassigned', async () => {270 itSub('can be reassigned', async ({helper}) => {
295 await usingPlaygrounds(async (helper) => {
296 const [oldAdmin, newAdmin, collectionOwner] = [accounts.pop()!, accounts.pop()!, accounts.pop()!];271 const [oldAdmin, newAdmin, collectionOwner] = [accounts.pop()!, accounts.pop()!, accounts.pop()!];
297 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'});272 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'});
298 273
299 await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress(normalizeAccountId(oldAdmin))))).to.be.eventually.fulfilled;274 await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress(normalizeAccountId(oldAdmin))))).to.be.eventually.fulfilled;
300 await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress(normalizeAccountId(newAdmin))))).to.be.eventually.fulfilled;275 await expect(helper.signTransaction(alice, helper.api!.tx.sudo.sudo(helper.api!.tx.appPromotion.setAdminAddress(normalizeAccountId(newAdmin))))).to.be.eventually.fulfilled;
301 await expect(helper.signTransaction(oldAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.eventually.rejected;276 await expect(helper.signTransaction(oldAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.eventually.rejected;
302 277
303 await expect(helper.signTransaction(newAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.eventually.fulfilled;278 await expect(helper.signTransaction(newAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.eventually.fulfilled;
304 });
305 });279 });
306});280});
307281
314 });288 });
315 });289 });
316290
317 it('should actually sponsor transactions', async () => {291 itSub('should actually sponsor transactions', async ({helper}) => {
318 await usingPlaygrounds(async (helper) => {
319 const [collectionOwner, tokenSender, receiver] = [accounts.pop()!, accounts.pop()!, accounts.pop()!];292 const [collectionOwner, tokenSender, receiver] = [accounts.pop()!, accounts.pop()!, accounts.pop()!];
320 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'Name', description: 'Description', tokenPrefix: 'Prefix', limits: {sponsorTransferTimeout: 0}});293 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'Name', description: 'Description', tokenPrefix: 'Prefix', limits: {sponsorTransferTimeout: 0}});
321 const token = await collection.mintToken(collectionOwner, {Substrate: tokenSender.address});294 const token = await collection.mintToken(collectionOwner, {Substrate: tokenSender.address});
322 await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId));295 await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId));
323 const palletBalanceBefore = await helper.balance.getSubstrate(palletAddress);296 const palletBalanceBefore = await helper.balance.getSubstrate(palletAddress);
324297
325 await token.transfer(tokenSender, {Substrate: receiver.address});298 await token.transfer(tokenSender, {Substrate: receiver.address});
326 expect (await token.getOwner()).to.be.deep.equal({Substrate: receiver.address});299 expect (await token.getOwner()).to.be.deep.equal({Substrate: receiver.address});
327 const palletBalanceAfter = await helper.balance.getSubstrate(palletAddress);300 const palletBalanceAfter = await helper.balance.getSubstrate(palletAddress);
328301
329 // senders balance the same, transaction has sponsored302 // senders balance the same, transaction has sponsored
330 expect (await helper.balance.getSubstrate(tokenSender.address)).to.be.equal(1000n * nominal);303 expect (await helper.balance.getSubstrate(tokenSender.address)).to.be.equal(1000n * nominal);
331 expect (palletBalanceBefore > palletBalanceAfter).to.be.true;304 expect (palletBalanceBefore > palletBalanceAfter).to.be.true;
332 });
333 });305 });
334306
335 it('can not be set by non admin', async () => {307 itSub('can not be set by non admin', async ({helper}) => {
336 await usingPlaygrounds(async (helper) => {
337 const [collectionOwner, nonAdmin] = [accounts.pop()!, accounts.pop()!];308 const [collectionOwner, nonAdmin] = [accounts.pop()!, accounts.pop()!];
338309
339 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'});310 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'});
340 311
341 await expect(helper.signTransaction(nonAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.eventually.rejected;312 await expect(helper.signTransaction(nonAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.eventually.rejected;
342 expect((await collection.getData())?.raw.sponsorship).to.equal('Disabled');313 expect((await collection.getData())?.raw.sponsorship).to.equal('Disabled');
343 });
344 });314 });
345315
346 it('should set pallet address as confirmed admin', async () => {316 itSub('should set pallet address as confirmed admin', async ({helper}) => {
347 await usingPlaygrounds(async (helper) => {
348 const [collectionOwner, oldSponsor] = [accounts.pop()!, accounts.pop()!];317 const [collectionOwner, oldSponsor] = [accounts.pop()!, accounts.pop()!];
349 318
350 // Can set sponsoring for collection without sponsor319 // Can set sponsoring for collection without sponsor
351 const collectionWithoutSponsor = await helper.nft.mintCollection(collectionOwner, {name: 'No-sponsor', description: 'New Collection', tokenPrefix: 'Promotion'});320 const collectionWithoutSponsor = await helper.nft.mintCollection(collectionOwner, {name: 'No-sponsor', description: 'New Collection', tokenPrefix: 'Promotion'});
352 await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collectionWithoutSponsor.collectionId))).to.be.eventually.fulfilled;321 await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collectionWithoutSponsor.collectionId))).to.be.eventually.fulfilled;
353 expect((await collectionWithoutSponsor.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress});322 expect((await collectionWithoutSponsor.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress});
354323
355 // Can set sponsoring for collection with unconfirmed sponsor324 // Can set sponsoring for collection with unconfirmed sponsor
356 const collectionWithUnconfirmedSponsor = await helper.nft.mintCollection(collectionOwner, {name: 'Unconfirmed', description: 'New Collection', tokenPrefix: 'Promotion', pendingSponsor: oldSponsor.address});325 const collectionWithUnconfirmedSponsor = await helper.nft.mintCollection(collectionOwner, {name: 'Unconfirmed', description: 'New Collection', tokenPrefix: 'Promotion', pendingSponsor: oldSponsor.address});
357 expect((await collectionWithUnconfirmedSponsor.getData())?.raw.sponsorship).to.be.deep.equal({Unconfirmed: oldSponsor.address});326 expect((await collectionWithUnconfirmedSponsor.getData())?.raw.sponsorship).to.be.deep.equal({Unconfirmed: oldSponsor.address});
358 await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collectionWithUnconfirmedSponsor.collectionId))).to.be.eventually.fulfilled;327 await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collectionWithUnconfirmedSponsor.collectionId))).to.be.eventually.fulfilled;
359 expect((await collectionWithUnconfirmedSponsor.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress});328 expect((await collectionWithUnconfirmedSponsor.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress});
360329
361 // Can set sponsoring for collection with confirmed sponsor330 // Can set sponsoring for collection with confirmed sponsor
362 const collectionWithConfirmedSponsor = await helper.nft.mintCollection(collectionOwner, {name: 'Confirmed', description: 'New Collection', tokenPrefix: 'Promotion', pendingSponsor: oldSponsor.address});331 const collectionWithConfirmedSponsor = await helper.nft.mintCollection(collectionOwner, {name: 'Confirmed', description: 'New Collection', tokenPrefix: 'Promotion', pendingSponsor: oldSponsor.address});
363 await collectionWithConfirmedSponsor.confirmSponsorship(oldSponsor);332 await collectionWithConfirmedSponsor.confirmSponsorship(oldSponsor);
364 await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collectionWithConfirmedSponsor.collectionId))).to.be.eventually.fulfilled;333 await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collectionWithConfirmedSponsor.collectionId))).to.be.eventually.fulfilled;
365 expect((await collectionWithConfirmedSponsor.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress});334 expect((await collectionWithConfirmedSponsor.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress});
366 });
367 });335 });
368336
369 it('can be overwritten by collection owner', async () => { 337 itSub('can be overwritten by collection owner', async ({helper}) => {
370 await usingPlaygrounds(async (helper) => {338 const [collectionOwner, newSponsor] = [accounts.pop()!, accounts.pop()!];
371 const [collectionOwner, newSponsor] = [accounts.pop()!, accounts.pop()!];
372 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'});339 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'});
373 const collectionId = collection.collectionId;340 const collectionId = collection.collectionId;
374 341
375 await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collectionId))).to.be.eventually.fulfilled;342 await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collectionId))).to.be.eventually.fulfilled;
376 343
377 // Collection limits still can be changed by the owner344 // Collection limits still can be changed by the owner
378 expect(await collection.setLimits(collectionOwner, {sponsorTransferTimeout: 0})).to.be.true;345 expect(await collection.setLimits(collectionOwner, {sponsorTransferTimeout: 0})).to.be.true;
379 expect((await collection.getData())?.raw.limits.sponsorTransferTimeout).to.be.equal(0);346 expect((await collection.getData())?.raw.limits.sponsorTransferTimeout).to.be.equal(0);
380 expect((await collection.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress});347 expect((await collection.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress});
381348
382 // Collection sponsor can be changed too349 // Collection sponsor can be changed too
383 expect((await collection.setSponsor(collectionOwner, newSponsor.address))).to.be.true;350 expect((await collection.setSponsor(collectionOwner, newSponsor.address))).to.be.true;
384 expect((await collection.getData())?.raw.sponsorship).to.be.deep.equal({Unconfirmed: newSponsor.address});351 expect((await collection.getData())?.raw.sponsorship).to.be.deep.equal({Unconfirmed: newSponsor.address});
385 });
386 });352 });
387 353
388 it('should not overwrite collection limits set by the owner earlier', async () => {354 itSub('should not overwrite collection limits set by the owner earlier', async ({helper}) => {
389 await usingPlaygrounds(async (helper) => {
390 const limits = {ownerCanDestroy: true, ownerCanTransfer: true, sponsorTransferTimeout: 0};355 const limits = {ownerCanDestroy: true, ownerCanTransfer: true, sponsorTransferTimeout: 0};
391 const collectionWithLimits = await helper.nft.mintCollection(alice, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion', limits});356 const collectionWithLimits = await helper.nft.mintCollection(alice, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion', limits});
392357
393 await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collectionWithLimits.collectionId))).to.be.eventually.fulfilled;358 await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collectionWithLimits.collectionId))).to.be.eventually.fulfilled;
394 expect((await collectionWithLimits.getData())?.raw.limits).to.be.deep.contain(limits);359 expect((await collectionWithLimits.getData())?.raw.limits).to.be.deep.contain(limits);
395 });
396 });360 });
397 361
398 it('should reject transaction if collection doesn\'t exist', async () => {362 itSub('should reject transaction if collection doesn\'t exist', async ({helper}) => {
399 await usingPlaygrounds(async (helper) => {
400 const collectionOwner = accounts.pop()!;363 const collectionOwner = accounts.pop()!;
401 364
402 // collection has never existed365 // collection has never existed
403 await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(999999999))).to.be.eventually.rejected;366 await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(999999999))).to.be.eventually.rejected;
404 // collection has been burned367 // collection has been burned
405 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'});368 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'});
406 await collection.burn(collectionOwner);369 await collection.burn(collectionOwner);
407370
408 await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.eventually.rejected;371 await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.eventually.rejected;
409 });
410 });372 });
411});373});
412374
415 await beforeEach(this);377 await beforeEach(this);
416 });378 });
417379
418 it('can not be called by non-admin', async () => { 380 itSub('can not be called by non-admin', async ({helper}) => {
419 await usingPlaygrounds(async (helper) => {381 const [collectionOwner, nonAdmin] = [accounts.pop()!, accounts.pop()!];
420 const [collectionOwner, nonAdmin] = [accounts.pop()!, accounts.pop()!];
421 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'});382 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'});
422 383
423 await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.eventually.fulfilled;384 await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.eventually.fulfilled;
424 385
425 await expect(helper.signTransaction(nonAdmin, helper.api!.tx.appPromotion.stopSponsoringCollection(collection.collectionId))).to.be.eventually.rejected;386 await expect(helper.signTransaction(nonAdmin, helper.api!.tx.appPromotion.stopSponsoringCollection(collection.collectionId))).to.be.eventually.rejected;
426 expect((await collection.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress});387 expect((await collection.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress});
427 });
428 });388 });
429389
430 it('should set sponsoring as disabled', async () => {390 itSub('should set sponsoring as disabled', async ({helper}) => {
431 await usingPlaygrounds(async (helper) => {
432 const [collectionOwner, recepient] = [accounts.pop()!, accounts.pop()!];391 const [collectionOwner, recepient] = [accounts.pop()!, accounts.pop()!];
433 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion', limits: {sponsorTransferTimeout: 0}});392 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion', limits: {sponsorTransferTimeout: 0}});
434 const token = await collection.mintToken(collectionOwner, {Substrate: collectionOwner.address});393 const token = await collection.mintToken(collectionOwner, {Substrate: collectionOwner.address});
435 394
436 await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId));395 await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.sponsorCollection(collection.collectionId));
437 await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.stopSponsoringCollection(collection.collectionId));396 await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.stopSponsoringCollection(collection.collectionId));
438 397
439 expect((await collection.getData())?.raw.sponsorship).to.be.equal('Disabled');398 expect((await collection.getData())?.raw.sponsorship).to.be.equal('Disabled');
440399
441 // Transactions are not sponsored anymore:400 // Transactions are not sponsored anymore:
442 const ownerBalanceBefore = await helper.balance.getSubstrate(collectionOwner.address);401 const ownerBalanceBefore = await helper.balance.getSubstrate(collectionOwner.address);
443 await token.transfer(collectionOwner, {Substrate: recepient.address});402 await token.transfer(collectionOwner, {Substrate: recepient.address});
444 const ownerBalanceAfter = await helper.balance.getSubstrate(collectionOwner.address);403 const ownerBalanceAfter = await helper.balance.getSubstrate(collectionOwner.address);
445 expect(ownerBalanceAfter < ownerBalanceBefore).to.be.equal(true);404 expect(ownerBalanceAfter < ownerBalanceBefore).to.be.equal(true);
446 });
447 });405 });
448406
449 it('should not affect collection which is not sponsored by pallete', async () => {407 itSub('should not affect collection which is not sponsored by pallete', async ({helper}) => {
450 await usingPlaygrounds(async (helper) => {
451 const collectionOwner = accounts.pop()!;408 const collectionOwner = accounts.pop()!;
452 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion', pendingSponsor: collectionOwner.address});409 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion', pendingSponsor: collectionOwner.address});
453 await collection.confirmSponsorship(collectionOwner);410 await collection.confirmSponsorship(collectionOwner);
454 411
455 await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.stopSponsoringCollection(collection.collectionId))).to.be.eventually.rejected;412 await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.stopSponsoringCollection(collection.collectionId))).to.be.eventually.rejected;
456 413
457 expect((await collection.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: collectionOwner.address});414 expect((await collection.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: collectionOwner.address});
458 });
459 });415 });
460416
461 it('should reject transaction if collection does not exist', async () => { 417 itSub('should reject transaction if collection does not exist', async ({helper}) => {
462 await usingPlaygrounds(async (helper) => {418 const collectionOwner = accounts.pop()!;
463 const collectionOwner = accounts.pop()!;
464 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'});419 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'});
465 420
466 await collection.burn(collectionOwner);421 await collection.burn(collectionOwner);
467 await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.stopSponsoringCollection(collection.collectionId))).to.be.eventually.rejected;422 await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.stopSponsoringCollection(collection.collectionId))).to.be.eventually.rejected;
468 await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.stopSponsoringCollection(999999999))).to.be.eventually.rejected;423 await expect(helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.stopSponsoringCollection(999999999))).to.be.eventually.rejected;
469 });
470 });424 });
471});425});
472426
657 await beforeEach(this);611 await beforeEach(this);
658 });612 });
659613
660 it('can not be called by non admin', async () => {614 itSub('can not be called by non admin', async ({helper}) => {
661 await usingPlaygrounds(async (helper) => {
662 const nonAdmin = accounts.pop()!;615 const nonAdmin = accounts.pop()!;
663 await expect(helper.signTransaction(nonAdmin, helper.api!.tx.appPromotion.payoutStakers(100))).to.be.rejected;616 await expect(helper.signTransaction(nonAdmin, helper.api!.tx.appPromotion.payoutStakers(100))).to.be.rejected;
664 });
665 });617 });
666618
667 it('should increase total staked', async () => {619 itSub('should increase total staked', async ({helper}) => {
668 await usingPlaygrounds(async (helper) => {
669 const staker = accounts.pop()!;620 const staker = accounts.pop()!;
670 const totalStakedBefore = await helper.staking.getTotalStaked();621 const totalStakedBefore = await helper.staking.getTotalStaked();
671 await helper.staking.stake(staker, 100n * nominal);622 await helper.staking.stake(staker, 100n * nominal);
672623
673 // Wait for rewards and pay624 // Wait for rewards and pay
674 const [stakedInBlock] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});625 const [stakedInBlock] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
675 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stakedInBlock.block));626 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stakedInBlock.block));
676 await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100));627 await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100));
677628
678 const totalStakedAfter = await helper.staking.getTotalStaked();629 const totalStakedAfter = await helper.staking.getTotalStaked();
679 expect(totalStakedAfter >= totalStakedBefore + calculateIncome(100n * nominal, 10n)).to.be.true;630 expect(totalStakedAfter >= totalStakedBefore + calculateIncome(100n * nominal, 10n)).to.be.true;
680631
681 // staker can unstake632 // staker can unstake
682 await helper.staking.unstake(staker);633 await helper.staking.unstake(staker);
683 expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedAfter - calculateIncome(100n * nominal, 10n));634 expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedAfter - calculateIncome(100n * nominal, 10n));
684 });
685 });635 });
686636
687 it('should credit 0.05% for staking period', async () => { 637 itSub('should credit 0.05% for staking period', async ({helper}) => {
688 await usingPlaygrounds(async helper => {
689 const staker = accounts.pop()!;638 const staker = accounts.pop()!;
690639
691 await waitPromotionPeriodDoesntEnd(helper);640 await waitPromotionPeriodDoesntEnd(helper);
692 641
693 await helper.staking.stake(staker, 100n * nominal);642 await helper.staking.stake(staker, 100n * nominal);
694 await helper.staking.stake(staker, 200n * nominal);643 await helper.staking.stake(staker, 200n * nominal);
695644
696 // wait rewards are available:645 // wait rewards are available:
697 const [_, stake2] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});646 const [_, stake2] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
698 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake2.block));647 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake2.block));
699648
700 await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100));649 await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100));
701650
702 const totalStakedPerBlock = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});651 const totalStakedPerBlock = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
703 expect(totalStakedPerBlock[0].amount).to.equal(calculateIncome(100n * nominal, 10n));652 expect(totalStakedPerBlock[0].amount).to.equal(calculateIncome(100n * nominal, 10n));
704 expect(totalStakedPerBlock[1].amount).to.equal(calculateIncome(200n * nominal, 10n));653 expect(totalStakedPerBlock[1].amount).to.equal(calculateIncome(200n * nominal, 10n));
705 });
706 });654 });
707655
708 it('shoud be paid for more than one period if payments was missed', async () => {656 itSub('shoud be paid for more than one period if payments was missed', async ({helper}) => {
709 await usingPlaygrounds(async (helper) => {
710 const staker = accounts.pop()!;657 const staker = accounts.pop()!;
711658
712 await helper.staking.stake(staker, 100n * nominal);659 await helper.staking.stake(staker, 100n * nominal);
713 // wait for two rewards are available:660 // wait for two rewards are available:
714 let [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});661 let [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
715 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block) + LOCKING_PERIOD);662 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block) + LOCKING_PERIOD);
716663
717 await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100));664 await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100));
718 [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});665 [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
719 const frozenBalanceShouldBe = calculateIncome(100n * nominal, 10n, 2);666 const frozenBalanceShouldBe = calculateIncome(100n * nominal, 10n, 2);
720 expect(stake.amount).to.be.equal(frozenBalanceShouldBe);667 expect(stake.amount).to.be.equal(frozenBalanceShouldBe);
721668
722 const stakerFullBalance = await helper.balance.getSubstrateFull(staker.address);669 const stakerFullBalance = await helper.balance.getSubstrateFull(staker.address);
723670
724 expect(stakerFullBalance).to.contain({reserved: 0n, feeFrozen: frozenBalanceShouldBe, miscFrozen: frozenBalanceShouldBe});671 expect(stakerFullBalance).to.contain({reserved: 0n, feeFrozen: frozenBalanceShouldBe, miscFrozen: frozenBalanceShouldBe});
725 });
726 });672 });
727 673
728 it('should not be credited for unstaked (reserved) balance', async () => {674 itSub('should not be credited for unstaked (reserved) balance', async ({helper}) => {
729 await usingPlaygrounds(async helper => {
730 // staker unstakes before rewards has been payed675 // staker unstakes before rewards has been payed
731 const staker = accounts.pop()!;676 const staker = accounts.pop()!;
732 await helper.staking.stake(staker, 100n * nominal);677 await helper.staking.stake(staker, 100n * nominal);
733 const [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});678 const [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
734 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block) + LOCKING_PERIOD);679 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block) + LOCKING_PERIOD);
735 await helper.staking.unstake(staker);680 await helper.staking.unstake(staker);
736 681
737 // so he did not receive any rewards682 // so he did not receive any rewards
738 const totalBalanceBefore = await helper.balance.getSubstrate(staker.address);683 const totalBalanceBefore = await helper.balance.getSubstrate(staker.address);
739 await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100));684 await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100));
740 const totalBalanceAfter = await helper.balance.getSubstrate(staker.address);685 const totalBalanceAfter = await helper.balance.getSubstrate(staker.address);
741686
742 expect(totalBalanceBefore).to.be.equal(totalBalanceAfter);687 expect(totalBalanceBefore).to.be.equal(totalBalanceAfter);
743 });
744 });688 });
745 689
746 it('should bring compound interest', async () => {690 itSub('should bring compound interest', async ({helper}) => {
747 await usingPlaygrounds(async helper => {
748 const staker = accounts.pop()!;691 const staker = accounts.pop()!;
749 692
750 await helper.staking.stake(staker, 100n * nominal);693 await helper.staking.stake(staker, 100n * nominal);
751694
752 let [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});695 let [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
753 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block));696 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block));
754 697
755 await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100));698 await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100));
756 [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});699 [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
757 expect(stake.amount).to.equal(calculateIncome(100n * nominal, 10n));700 expect(stake.amount).to.equal(calculateIncome(100n * nominal, 10n));
758 701
759 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block) + LOCKING_PERIOD);702 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block) + LOCKING_PERIOD);
760 await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100));703 await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100));
761 [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});704 [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});
762 expect(stake.amount).to.equal(calculateIncome(100n * nominal, 10n, 2));705 expect(stake.amount).to.equal(calculateIncome(100n * nominal, 10n, 2));
763 });
764 });706 });
765707
766 it.skip('can be paid 1000 rewards in a time', async () => {708 itSub.skip('can be paid 1000 rewards in a time', async ({helper}) => {
767 // all other stakes should be unstaked709 // all other stakes should be unstaked
768 await usingPlaygrounds(async (helper) => {710 const oneHundredStakers = await helper.arrange.createCrowd(100, 1050n, alice);
769 const oneHundredStakers = await helper.arrange.createCrowd(100, 1050n, alice);
770711
771 // stakers stakes 10 times each712 // stakers stakes 10 times each
772 for (let i = 0; i < 10; i++) {713 for (let i = 0; i < 10; i++) {
773 await Promise.all(oneHundredStakers.map(staker => helper.staking.stake(staker, 100n * nominal)));714 await Promise.all(oneHundredStakers.map(staker => helper.staking.stake(staker, 100n * nominal)));
774 }715 }
775 await helper.wait.newBlocks(40);716 await helper.wait.newBlocks(40);
776 const result = await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100));717 const result = await helper.signTransaction(palletAdmin, helper.api!.tx.appPromotion.payoutStakers(100));
777 });
778 });718 });
779719
780 it.skip('can handle 40.000 rewards', async () => {720 itSub.skip('can handle 40.000 rewards', async ({helper}) => {
781 await usingPlaygrounds(async (helper) => {
782 const [donor] = await helper.arrange.createAccounts([7_000_000n], alice);721 const [donor] = await helper.arrange.createAccounts([7_000_000n], alice);
783 const crowdStakes = async () => {722 const crowdStakes = async () => {
784 // each account in the crowd stakes 2 times723 // each account in the crowd stakes 2 times
785 const crowd = await helper.arrange.createCrowd(500, 300n, donor);724 const crowd = await helper.arrange.createCrowd(500, 300n, donor);
786 await Promise.all(crowd.map(account => helper.staking.stake(account, 100n * nominal)));725 await Promise.all(crowd.map(account => helper.staking.stake(account, 100n * nominal)));
787 await Promise.all(crowd.map(account => helper.staking.stake(account, 100n * nominal)));726 await Promise.all(crowd.map(account => helper.staking.stake(account, 100n * nominal)));
788 // 727 //
789 };728 };
790729
791 for (let i = 0; i < 40; i++) {730 for (let i = 0; i < 40; i++) {
792 await crowdStakes();731 await crowdStakes();
793 }732 }
794733
795 // TODO pay rewards for some period734 // TODO pay rewards for some period
796 });
797 });735 });
798});736});
799737