1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {19 itSub, usingPlaygrounds, Pallets, requirePalletsOrSkip, LOCKING_PERIOD, UNLOCKING_PERIOD,20} from '../../util';21import {DevUniqueHelper} from '../../util/playgrounds/unique.dev';22import {itEth, expect, SponsoringMode} from '../../eth/util';2324let donor: IKeyringPair;25let palletAdmin: IKeyringPair;26let nominal: bigint;27let palletAddress: string;28let accounts: IKeyringPair[];29let usedAccounts: IKeyringPair[] = [];3031async function getAccounts(accountsNumber: number, balance?: bigint) {32 let accs: IKeyringPair[] = [];33 if (balance) {34 await usingPlaygrounds(async (helper) => {35 accs = await helper.arrange.createAccounts(new Array(accountsNumber).fill(balance), donor);36 });37 } else {38 accs = accounts.splice(0, accountsNumber);39 }40 usedAccounts.push(...accs);41 return accs;42}4344454647describe('App promotion', () => {48 before(async function () {49 await usingPlaygrounds(async (helper, privateKey) => {50 requirePalletsOrSkip(this, helper, [Pallets.AppPromotion]);51 donor = await privateKey({url: import.meta.url});52 palletAddress = helper.arrange.calculatePalletAddress('appstake');53 palletAdmin = await privateKey('//PromotionAdmin');54 nominal = helper.balance.getOneTokenNominal();5556 const accountBalances = new Array(200).fill(1000n);57 accounts = await helper.arrange.createAccounts(accountBalances, donor); 58 });59 });6061 afterEach(async () => {62 await usingPlaygrounds(async (helper) => {63 let unstakeTxs = [];64 for (const account of usedAccounts) {65 if (unstakeTxs.length === 3) {66 await Promise.all(unstakeTxs);67 unstakeTxs = [];68 }69 unstakeTxs.push(helper.staking.unstakeAll(account));70 }71 await Promise.all(unstakeTxs);72 usedAccounts = [];73 expect(await helper.staking.getTotalStaked()).to.eq(0n); 74 });75 });7677 describe('stake extrinsic', () => {78 itSub('should "lock" staking balance, add it to "staked" map, and increase "totalStaked" amount', async ({helper}) => {79 const [staker, recepient] = await getAccounts(2);80 const totalStakedBefore = await helper.staking.getTotalStaked();8182 83 await expect(helper.staking.stake(staker, 100n * nominal - 1n)).to.be.rejected;84 await helper.staking.stake(staker, 100n * nominal);8586 87 88 expect(await helper.balance.getSubstrateFull(staker.address)).to.contain({miscFrozen: 100n * nominal, feeFrozen: 100n * nominal, reserved: 0n});89 expect(await helper.balance.getLocked(staker.address)).to.deep.eq([{id: 'appstake', amount: 100n * nominal, reasons: 'All'}]);90 await expect(helper.balance.transferToSubstrate(staker, recepient.address, 900n * nominal)).to.be.rejectedWith('balances.LiquidityRestrictions');9192 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(100n * nominal);93 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n);94 95 expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedBefore + 100n * nominal); 969798 await helper.staking.stake(staker, 200n * nominal);99 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(300n * nominal);100 const totalStakedPerBlock = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});101 expect(totalStakedPerBlock[0].amount).to.equal(100n * nominal);102 expect(totalStakedPerBlock[1].amount).to.equal(200n * nominal);103 });104105 [106 {unstake: 'unstakeAll' as const},107 {unstake: 'unstakePartial' as const},108 ].map(testCase => {109 itSub(`[${testCase.unstake}] should allow to create maximum 10 stakes for account`, async ({helper}) => {110 const [staker] = await getAccounts(1, 2000n);111 const ONE_STAKE = 100n * nominal;112 for (let i = 0; i < 10; i++) {113 await helper.staking.stake(staker, ONE_STAKE);114 }115116 117 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(1000n * nominal);118 expect(await helper.staking.getTotalStakedPerBlock({Substrate: staker.address})).to.have.length(10);119120 await expect(helper.staking.stake(staker, ONE_STAKE)).to.be.rejectedWith('appPromotion.NoPermission');121122 123124 125 if (testCase.unstake === 'unstakeAll') {126 await helper.staking.unstakeAll(staker);127 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).to.eq(0);128 await helper.staking.stake(staker, 100n * nominal);129 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.equal(100n * nominal);130 }131 132 else {133 await helper.staking.unstakePartial(staker, ONE_STAKE);134 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).to.eq(9);135 await helper.staking.stake(staker, 100n * nominal);136 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).to.eq(10);137 await expect(helper.staking.stake(staker, 100n * nominal)).to.be.rejectedWith('appPromotion.NoPermission');138 await helper.staking.unstakePartial(staker, 150n * nominal);139 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).to.eq(9);140 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.equal(850n * nominal);141 }142 });143 });144145 itSub('should allow to stake() if balance is locked with different id', async ({helper}) => {146 const [staker] = await getAccounts(1);147148 149 await helper.balance.vestedTransfer(donor, staker.address, {start: 0n, period: 1n, periodCount: 1n, perPeriod: 200n * nominal});150 expect(await helper.balance.getSubstrateFull(staker.address))151 .to.deep.contain({free: 1200n * nominal, miscFrozen: 200n * nominal, feeFrozen: 200n * nominal, reserved: 0n});152153 154 await helper.staking.stake(staker, 1000n * nominal);155 await helper.staking.stake(staker, 199n * nominal);156 157 expect(await helper.balance.getLocked(staker.address)).to.deep.eq([{id: 'ormlvest', amount: 200n * nominal, reasons: 'All'}, {id: 'appstake', amount: 1199n * nominal, reasons: 'All'}]);158 expect(await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n, miscFrozen: 1199n * nominal, feeFrozen: 1199n * nominal});159 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.eq(1199n);160 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.eq(1199n * nominal);161162 163 await helper.staking.unstakeAll(staker);164 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.eq(1199n * nominal);165 const [pendingUnstake] = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});166 await helper.wait.forParachainBlockNumber(pendingUnstake.block);167168 169 expect(await helper.balance.getLocked(staker.address)).to.deep.eq([{id: 'ormlvest', amount: 200n * nominal, reasons: 'All'}]);170 expect(await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n, miscFrozen: 200n * nominal, feeFrozen: 200n * nominal});171 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.eq(1199n);172 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.eq(0n);173174 175 await helper.balance.transferToSubstrate(staker, donor.address, 900n * nominal);176 });177178 itSub('should not allow to stake(), if stake amount is more than total free balance minus locked by staking', async ({helper}) => {179 const [staker] = await getAccounts(1);180181 182 await expect(helper.staking.stake(staker, 1000n * nominal)).to.be.rejected; 183 await helper.staking.stake(staker, 500n * nominal);184185 186 await expect(helper.staking.stake(staker, 500n * nominal)).to.be.rejected; 187 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(500n * nominal);188 });189190 itSub('for different accounts in one block is possible', async ({helper}) => {191 const crowd = await getAccounts(4);192193 const crowdStartsToStake = crowd.map(user => helper.staking.stake(user, 100n * nominal));194 await expect(Promise.all(crowdStartsToStake)).to.be.fulfilled;195196 const crowdStakes = await Promise.all(crowd.map(address => helper.staking.getTotalStaked({Substrate: address.address})));197 expect(crowdStakes).to.deep.equal([100n * nominal, 100n * nominal, 100n * nominal, 100n * nominal]);198 });199 });200201 describe('Unstaking', () => {202 [203 {method: 'unstakeAll' as const},204 {method: 'unstakePartial' as const},205 ].map(testCase => {206 itSub(`[${testCase.method}] should move tokens to "pendingUnstake" and subtract it from totalStaked`, async ({helper}) => {207 const [staker, recepient] = await getAccounts(2);208 const totalStakedBefore = await helper.staking.getTotalStaked();209 const STAKE_AMOUNT = 900n * nominal;210211 await helper.staking.stake(staker, STAKE_AMOUNT);212 testCase.method === 'unstakeAll'213 ? await helper.staking.unstakeAll(staker)214 : await helper.staking.unstakePartial(staker, STAKE_AMOUNT);215216 217 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).to.eq(0);218 expect(await helper.balance.getLocked(staker.address)).to.deep.eq([{id: 'appstake', amount: STAKE_AMOUNT, reasons: 'All'}]);219 expect(await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n, miscFrozen: STAKE_AMOUNT, feeFrozen: STAKE_AMOUNT});220 221 await expect(helper.balance.transferToSubstrate(staker, recepient.address, 100n * nominal)).to.be.rejectedWith('balances.LiquidityRestrictions');222 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(STAKE_AMOUNT);223 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(0n);224 expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedBefore);225 });226 });227228 [229 {method: 'unstakeAll' as const},230 {method: 'unstakePartial' as const},231 ].map(testCase => {232 itSub(`[${testCase.method}] should unlock balance after unlocking period ends and remove it from "pendingUnstake"`, async ({helper}) => {233 const [staker] = await getAccounts(1);234 await helper.staking.stake(staker, 100n * nominal);235 testCase.method === 'unstakeAll'236 ? await helper.staking.unstakeAll(staker)237 : await helper.staking.unstakePartial(staker, 100n * nominal);238 const [pendingUnstake] = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});239240 241 await helper.wait.forParachainBlockNumber(pendingUnstake.block);242 expect(await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n, miscFrozen: 0n, feeFrozen: 0n});243 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n);244245 246 await helper.balance.transferToSubstrate(staker, donor.address, 998n * nominal);247 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(1n);248 });249 });250251 [252 {method: 'unstakeAll' as const},253 {method: 'unstakePartial' as const},254 ].map(testCase => {255 itSub(`[${testCase.method}] should successfully unstake multiple stakes`, async ({helper}) => {256 const [staker] = await getAccounts(1);257 await helper.staking.stake(staker, 100n * nominal);258 await helper.staking.stake(staker, 200n * nominal);259 await helper.staking.stake(staker, 300n * nominal);260261 262 let totalPendingUnstake = await helper.staking.getPendingUnstake({Substrate: staker.address});263 let pendingUnstake = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});264 let stakes = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});265 expect(totalPendingUnstake).to.be.deep.equal(0n);266 expect(pendingUnstake).to.be.deep.equal([]);267 expect(stakes[0].amount).to.equal(100n * nominal);268 expect(stakes[1].amount).to.equal(200n * nominal);269 expect(stakes[2].amount).to.equal(300n * nominal);270271 272 testCase.method === 'unstakeAll'273 ? await helper.staking.unstakeAll(staker)274 : await helper.staking.unstakePartial(staker, 600n * nominal);275276 pendingUnstake = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});277 totalPendingUnstake = await helper.staking.getPendingUnstake({Substrate: staker.address});278 stakes = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});279 expect(totalPendingUnstake).to.be.equal(600n * nominal);280 expect(stakes).to.be.deep.equal([]);281 expect(pendingUnstake[0].amount).to.equal(600n * nominal);282283 expect (await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n, feeFrozen: 600n * nominal, miscFrozen: 600n * nominal});284 expect (await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n);285 await helper.wait.forParachainBlockNumber(pendingUnstake[0].block);286 expect (await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n, feeFrozen: 0n, miscFrozen: 0n});287 expect (await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n);288 });289 });290291 [292 {method: 'unstakeAll' as const},293 {method: 'unstakePartial' as const},294 ].map(testCase => {295 itSub(`[${testCase.method}] should not have any effects if no active stakes`, async ({helper}) => {296 const [staker] = await getAccounts(1);297298 299 testCase.method === 'unstakeAll'300 ? await helper.staking.unstakeAll(staker)301 : await expect(helper.staking.unstakePartial(staker, 100n * nominal)).to.be.rejectedWith('appPromotion.InsufficientStakedBalance');302303 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(0n);304 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.be.equal(999n); 305306 307308 309 await helper.staking.stake(staker, 100n * nominal);310311 if (testCase.method === 'unstakeAll') {312 await helper.staking.unstakeAll(staker);313 await helper.staking.unstakeAll(staker);314 } else {315 await helper.staking.unstakePartial(staker, 100n * nominal);316 await expect(helper.staking.unstakePartial(staker, 100n * nominal)).to.be.rejectedWith('appPromotion.InsufficientStakedBalance');317 }318319 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).to.eq(0);320 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(100n * nominal);321 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(0n);322 });323 });324325 [326 {method: 'unstakeAll' as const},327 {method: 'unstakePartial' as const},328 ].map(testCase => {329 itSub(`[${testCase.method}] should create different pending-unlock for each unlocking stake`, async ({helper}) => {330 const [staker] = await getAccounts(1);331 await helper.staking.stake(staker, 100n * nominal);332 testCase.method === 'unstakeAll'333 ? await helper.staking.unstakeAll(staker)334 : await helper.staking.unstakePartial(staker, 100n * nominal);335 await helper.staking.stake(staker, 120n * nominal);336 testCase.method === 'unstakeAll'337 ? await helper.staking.unstakeAll(staker)338 : await helper.staking.unstakePartial(staker, 120n * nominal);339340 const unstakingPerBlock = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});341 expect(unstakingPerBlock).has.length(2);342 expect(unstakingPerBlock[0].amount).to.equal(100n * nominal);343 expect(unstakingPerBlock[1].amount).to.equal(120n * nominal);344 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).to.equal(0);345 });346 });347348 [349 {method: 'unstakeAll' as const},350 {method: 'unstakePartial' as const},351 ].map(testCase => {352 itSub(`[${testCase.method}] should be possible for 3 accounts in one block`, async ({helper}) => {353 const stakers = await getAccounts(3);354355 await Promise.all(stakers.map(staker => helper.staking.stake(staker, 100n * nominal)));356 await Promise.all(stakers.map(staker => {357 return testCase.method === 'unstakeAll'358 ? helper.staking.unstakeAll(staker)359 : helper.staking.unstakePartial(staker, 100n * nominal);360 }));361362 await Promise.all(stakers.map(async (staker) => {363 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.be.equal(100n * nominal);364 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(0n);365 }));366 });367 });368369 itSub('should not be possible for more than 3 accounts in one block', async ({helper}) => {370 if (!await helper.arrange.isDevNode()) {371 const stakers = await getAccounts(10);372373 await Promise.all(stakers.map(staker => helper.staking.stake(staker, 100n * nominal)));374 const unstakingResults = await Promise.allSettled(stakers.map((staker, i) => {375 return i % 2 === 0376 ? helper.staking.unstakeAll(staker)377 : helper.staking.unstakePartial(staker, 100n * nominal);378 }));379380 const successfulUnstakes = unstakingResults.filter(result => result.status === 'fulfilled');381 expect(successfulUnstakes).to.have.length(3);382 }383 });384385 itSub('Cannot partially unstake more than staked', async ({helper}) => {386 const [staker] = await getAccounts(1);387 388 await helper.staking.stake(staker, 100n * nominal);389 await helper.staking.stake(staker, 200n * nominal);390391 392 await expect(helper.staking.unstakePartial(staker, 300n * nominal + 1n)).to.be.rejectedWith('appPromotion.InsufficientStakedBalance');393 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).eq(2);394395 await helper.staking.unstakePartial(staker, 150n * nominal);396 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).eq(1);397 await expect(helper.staking.unstakePartial(staker, 150n * nominal + 1n)).to.be.rejectedWith('appPromotion.InsufficientStakedBalance');398 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).eq(1);399400 401 await helper.staking.unstakePartial(staker, 150n * nominal);402 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).eq(0);403 });404405 itSub('Can partially unstake arbitrary amount', async ({helper}) => {406 const [staker] = await getAccounts(1);407 await helper.staking.stake(staker, 100n * nominal);408 await helper.staking.stake(staker, 200n * nominal);409410 411 await expect(helper.staking.unstakePartial(staker, -1n)).to.be.rejected;412413 414 await helper.staking.unstakePartial(staker, 0n);415 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).to.eq(2);416 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.eq(300n * nominal);417 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.eq(0n);418419 420 await helper.staking.unstakePartial(staker, 1n);421 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).to.eq(2);422 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.eq(300n * nominal - 1n);423 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.eq(1n);424 425 let [stake1, stake2] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});426 expect(stake1.amount).to.eq(100n * nominal - 1n);427 expect(stake2.amount).to.eq(200n * nominal);428429 430 await helper.staking.unstakePartial(staker, 100n * nominal - 2n);431 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).to.eq(2);432 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.eq(200n * nominal + 1n);433 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.eq(100n * nominal - 1n);434 [stake1, stake2] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});435 expect(stake1.amount).to.eq(1n);436 expect(stake2.amount).to.eq(200n * nominal);437 });438439 itSub('can mix different type of unstakes', async ({helper}) => {440 const [staker] = await getAccounts(1);441 await helper.staking.stake(staker, 100n * nominal);442 await helper.staking.stake(staker, 200n * nominal);443444 await helper.staking.unstakePartial(staker, 50n * nominal);445 await helper.staking.unstakeAll(staker);446 expect(await helper.staking.getStakesNumber({Substrate: staker.address})).to.eq(0);447 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.eq(0n);448 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.eq(300n * nominal);449450 const [_unstake1, unstake2] = await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address});451 await helper.wait.forParachainBlockNumber(unstake2.block);452453 expect(await helper.balance.getLocked(staker.address)).to.deep.eq([]);454 expect(await helper.balance.getSubstrateFull(staker.address)).to.deep.contain({reserved: 0n, miscFrozen: 0n, feeFrozen: 0n});455 expect(await helper.balance.getSubstrate(staker.address) / nominal).to.eq(999n);456 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.eq(0n);457 expect(await helper.staking.getPendingUnstake({Substrate: staker.address})).to.eq(0n);458 expect(await helper.staking.getPendingUnstakePerBlock({Substrate: staker.address})).to.deep.eq([]);459 });460 });461462 describe('collection sponsoring', () => {463 itSub('should actually sponsor transactions', async ({helper}) => {464 const api = helper.getApi();465 const [collectionOwner, tokenSender, receiver] = await getAccounts(3);466 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'Name', description: 'Description', tokenPrefix: 'Prefix', limits: {sponsorTransferTimeout: 0}});467 const token = await collection.mintToken(collectionOwner, {Substrate: tokenSender.address});468 await helper.signTransaction(palletAdmin, api.tx.appPromotion.sponsorCollection(collection.collectionId));469 const palletBalanceBefore = await helper.balance.getSubstrate(palletAddress);470471 await token.transfer(tokenSender, {Substrate: receiver.address});472 expect (await token.getOwner()).to.be.deep.equal({Substrate: receiver.address});473 const palletBalanceAfter = await helper.balance.getSubstrate(palletAddress);474475 476 expect (await helper.balance.getSubstrate(tokenSender.address)).to.be.equal(1000n * nominal);477 expect (palletBalanceBefore > palletBalanceAfter).to.be.true;478 });479480 itSub('can not be set by non admin', async ({helper}) => {481 const api = helper.getApi();482 const [collectionOwner, nonAdmin] = await getAccounts(2);483484 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'});485486 await expect(helper.signTransaction(nonAdmin, api.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.rejected;487 expect((await collection.getData())?.raw.sponsorship).to.equal('Disabled');488 });489490 itSub('should set pallet address as confirmed admin', async ({helper}) => {491 const api = helper.getApi();492 const [collectionOwner, oldSponsor] = await getAccounts(2);493494 495 const collectionWithoutSponsor = await helper.nft.mintCollection(collectionOwner, {name: 'No-sponsor', description: 'New Collection', tokenPrefix: 'Promotion'});496 await expect(helper.signTransaction(palletAdmin, api.tx.appPromotion.sponsorCollection(collectionWithoutSponsor.collectionId))).to.be.fulfilled;497 expect((await collectionWithoutSponsor.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress});498499 500 const collectionWithUnconfirmedSponsor = await helper.nft.mintCollection(collectionOwner, {name: 'Unconfirmed', description: 'New Collection', tokenPrefix: 'Promotion', pendingSponsor: oldSponsor.address});501 expect((await collectionWithUnconfirmedSponsor.getData())?.raw.sponsorship).to.be.deep.equal({Unconfirmed: oldSponsor.address});502 await expect(helper.signTransaction(palletAdmin, api.tx.appPromotion.sponsorCollection(collectionWithUnconfirmedSponsor.collectionId))).to.be.fulfilled;503 expect((await collectionWithUnconfirmedSponsor.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress});504505 506 const collectionWithConfirmedSponsor = await helper.nft.mintCollection(collectionOwner, {name: 'Confirmed', description: 'New Collection', tokenPrefix: 'Promotion', pendingSponsor: oldSponsor.address});507 await collectionWithConfirmedSponsor.confirmSponsorship(oldSponsor);508 await expect(helper.signTransaction(palletAdmin, api.tx.appPromotion.sponsorCollection(collectionWithConfirmedSponsor.collectionId))).to.be.fulfilled;509 expect((await collectionWithConfirmedSponsor.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress});510 });511512 itSub('can be overwritten by collection owner', async ({helper}) => {513 const api = helper.getApi();514 const [collectionOwner, newSponsor] = await getAccounts(2);515 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'});516 const collectionId = collection.collectionId;517518 await expect(helper.signTransaction(palletAdmin, api.tx.appPromotion.sponsorCollection(collectionId))).to.be.fulfilled;519520 521 expect(await collection.setLimits(collectionOwner, {sponsorTransferTimeout: 0})).to.be.true;522 expect((await collection.getData())?.raw.limits.sponsorTransferTimeout).to.be.equal(0);523 expect((await collection.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress});524525 526 expect((await collection.setSponsor(collectionOwner, newSponsor.address))).to.be.true;527 expect((await collection.getData())?.raw.sponsorship).to.be.deep.equal({Unconfirmed: newSponsor.address});528 });529530 itSub('should not overwrite collection limits set by the owner earlier', async ({helper}) => {531 const [owner] = await getAccounts(1);532 const api = helper.getApi();533 const limits = {ownerCanDestroy: true, ownerCanTransfer: true, sponsorTransferTimeout: 0};534 const collectionWithLimits = await helper.nft.mintCollection(owner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion', limits});535536 await expect(helper.signTransaction(palletAdmin, api.tx.appPromotion.sponsorCollection(collectionWithLimits.collectionId))).to.be.fulfilled;537 expect((await collectionWithLimits.getData())?.raw.limits).to.be.deep.contain(limits);538 });539540 itSub('should reject transaction if collection doesn\'t exist', async ({helper}) => {541 const api = helper.getApi();542 const [collectionOwner] = await getAccounts(1);543544 545 await expect(helper.signTransaction(palletAdmin, api.tx.appPromotion.sponsorCollection(999999999))).to.be.rejected;546 547 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'});548 await collection.burn(collectionOwner);549550 await expect(helper.signTransaction(palletAdmin, api.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.rejected;551 });552 });553554 describe('stopSponsoringCollection', () => {555 itSub('can not be called by non-admin', async ({helper}) => {556 const api = helper.getApi();557 const [collectionOwner, nonAdmin] = await getAccounts(2);558 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'});559560 await expect(helper.signTransaction(palletAdmin, api.tx.appPromotion.sponsorCollection(collection.collectionId))).to.be.fulfilled;561562 await expect(helper.signTransaction(nonAdmin, api.tx.appPromotion.stopSponsoringCollection(collection.collectionId))).to.be.rejected;563 expect((await collection.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: palletAddress});564 });565566 itSub('should set sponsoring as disabled', async ({helper}) => {567 const api = helper.getApi();568 const [collectionOwner, recepient] = await getAccounts(2);569 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion', limits: {sponsorTransferTimeout: 0}});570 const token = await collection.mintToken(collectionOwner, {Substrate: collectionOwner.address});571572 await helper.signTransaction(palletAdmin, api.tx.appPromotion.sponsorCollection(collection.collectionId));573 await helper.signTransaction(palletAdmin, api.tx.appPromotion.stopSponsoringCollection(collection.collectionId));574575 expect((await collection.getData())?.raw.sponsorship).to.be.equal('Disabled');576577 578 const ownerBalanceBefore = await helper.balance.getSubstrate(collectionOwner.address);579 await token.transfer(collectionOwner, {Substrate: recepient.address});580 const ownerBalanceAfter = await helper.balance.getSubstrate(collectionOwner.address);581 expect(ownerBalanceAfter < ownerBalanceBefore).to.be.equal(true);582 });583584 itSub('should not affect collection which is not sponsored by pallete', async ({helper}) => {585 const api = helper.getApi();586 const [collectionOwner] = await getAccounts(1);587 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion', pendingSponsor: collectionOwner.address});588 await collection.confirmSponsorship(collectionOwner);589590 await expect(helper.signTransaction(palletAdmin, api.tx.appPromotion.stopSponsoringCollection(collection.collectionId))).to.be.rejected;591592 expect((await collection.getData())?.raw.sponsorship).to.be.deep.equal({Confirmed: collectionOwner.address});593 });594595 itSub('should reject transaction if collection does not exist', async ({helper}) => {596 const [collectionOwner] = await getAccounts(1);597 const collection = await helper.nft.mintCollection(collectionOwner, {name: 'New', description: 'New Collection', tokenPrefix: 'Promotion'});598599 await collection.burn(collectionOwner);600 await expect(helper.executeExtrinsic(palletAdmin, 'api.tx.appPromotion.stopSponsoringCollection', [collection.collectionId], true)).to.be.rejectedWith('common.CollectionNotFound');601 await expect(helper.executeExtrinsic(palletAdmin, 'api.tx.appPromotion.stopSponsoringCollection', [999_999_999], true)).to.be.rejectedWith('common.CollectionNotFound');602 });603 });604605 describe('contract sponsoring', () => {606 itEth('should set palletes address as a sponsor', async ({helper}) => {607 const contractOwner = (await helper.eth.createAccountWithBalance(donor, 1000n)).toLowerCase();608 const flipper = await helper.eth.deployFlipper(contractOwner); 609 const contractHelper = await helper.ethNativeContract.contractHelpers(contractOwner);610611 await helper.executeExtrinsic(palletAdmin, 'api.tx.appPromotion.sponsorContract', [flipper.options.address]);612613 expect(await contractHelper.methods.hasSponsor(flipper.options.address).call()).to.be.true;614 expect((await helper.callRpc('api.query.evmContractHelpers.owner', [flipper.options.address])).toJSON()).to.be.equal(contractOwner);615 expect((await helper.callRpc('api.query.evmContractHelpers.sponsoring', [flipper.options.address])).toJSON()).to.deep.equal({616 confirmed: {617 substrate: palletAddress,618 },619 });620 });621622 itEth('should overwrite sponsoring mode and existed sponsor', async ({helper}) => {623 const contractOwner = (await helper.eth.createAccountWithBalance(donor, 1000n)).toLowerCase();624 const flipper = await helper.eth.deployFlipper(contractOwner); 625 const contractHelper = await helper.ethNativeContract.contractHelpers(contractOwner);626627 await expect(contractHelper.methods.selfSponsoredEnable(flipper.options.address).send()).to.be.fulfilled;628629 630 expect((await helper.callRpc('api.query.evmContractHelpers.sponsoring', [flipper.options.address])).toJSON()).to.be.deep.equal({631 confirmed: {632 ethereum: flipper.options.address.toLowerCase(),633 },634 });635636 637 await helper.executeExtrinsic(palletAdmin, 'api.tx.appPromotion.sponsorContract', [flipper.options.address], true);638639 640 expect(await contractHelper.methods.hasSponsor(flipper.options.address).call()).to.be.true;641 expect((await helper.callRpc('api.query.evmContractHelpers.owner', [flipper.options.address])).toJSON()).to.be.equal(contractOwner);642 expect((await helper.callRpc('api.query.evmContractHelpers.sponsoring', [flipper.options.address])).toJSON()).to.deep.equal({643 confirmed: {644 substrate: palletAddress,645 },646 });647 });648649 itEth('can be overwritten by contract owner', async ({helper}) => {650 const contractOwner = (await helper.eth.createAccountWithBalance(donor, 1000n)).toLowerCase();651 const flipper = await helper.eth.deployFlipper(contractOwner); 652 const contractHelper = await helper.ethNativeContract.contractHelpers(contractOwner);653654 655 await helper.executeExtrinsic(palletAdmin, 'api.tx.appPromotion.sponsorContract', [flipper.options.address], true);656657 658 await expect(contractHelper.methods.selfSponsoredEnable(flipper.options.address).send()).to.be.not.rejected;659660 expect(await contractHelper.methods.hasSponsor(flipper.options.address).call()).to.be.true;661 expect((await helper.callRpc('api.query.evmContractHelpers.owner', [flipper.options.address])).toJSON()).to.be.equal(contractOwner);662 expect((await helper.callRpc('api.query.evmContractHelpers.sponsoring', [flipper.options.address])).toJSON()).to.deep.equal({663 confirmed: {664 ethereum: flipper.options.address.toLowerCase(),665 },666 });667 });668669 itEth('can not be set by non admin', async ({helper}) => {670 const [nonAdmin] = await getAccounts(1);671 const contractOwner = (await helper.eth.createAccountWithBalance(donor, 1000n)).toLowerCase();672 const flipper = await helper.eth.deployFlipper(contractOwner); 673 const contractHelper = await helper.ethNativeContract.contractHelpers(contractOwner);674675 await expect(contractHelper.methods.selfSponsoredEnable(flipper.options.address).send()).to.be.fulfilled;676677 678 await expect(helper.executeExtrinsic(nonAdmin, 'api.tx.appPromotion.sponsorContract', [flipper.options.address], true)).to.be.rejectedWith('appPromotion.NoPermission');679680 681 expect((await helper.callRpc('api.query.evmContractHelpers.sponsoring', [flipper.options.address])).toJSON()).to.deep.equal({682 confirmed: {683 ethereum: flipper.options.address.toLowerCase(),684 },685 });686 });687688 itEth('should actually sponsor transactions', async ({helper}) => {689 690 const caller = await helper.eth.createAccountWithBalance(donor, 1000n);691 const palletBalanceBefore = await helper.balance.getSubstrate(palletAddress);692693 694 const contractOwner = (await helper.eth.createAccountWithBalance(donor, 1000n)).toLowerCase();695 const flipper = await helper.eth.deployFlipper(contractOwner); 696 const contractHelper = await helper.ethNativeContract.contractHelpers(contractOwner);697698 699 await contractHelper.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: contractOwner});700 await contractHelper.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: contractOwner});701 await helper.eth.transferBalanceFromSubstrate(donor, flipper.options.address, 1000n); 702703 704 await helper.executeExtrinsic(palletAdmin, 'api.tx.appPromotion.sponsorContract', [flipper.options.address], true);705706 707 await flipper.methods.flip().send({from: caller});708 expect(await flipper.methods.getValue().call()).to.be.true;709710 711 const callerBalance = await helper.balance.getEthereum(caller);712 const contractBalanceAfter = await helper.balance.getEthereum(flipper.options.address);713 expect(callerBalance).to.be.equal(1000n * nominal);714 expect(1000n * nominal === contractBalanceAfter).to.be.true;715716 717 const palletBalanceAfter = await helper.balance.getSubstrate(palletAddress);718 expect(palletBalanceAfter < palletBalanceBefore).to.be.true;719 });720 });721722 describe('stopSponsoringContract', () => {723 itEth('should remove pallet address from contract sponsors', async ({helper}) => {724 const caller = await helper.eth.createAccountWithBalance(donor, 1000n);725 const contractOwner = (await helper.eth.createAccountWithBalance(donor, 1000n)).toLowerCase();726 const flipper = await helper.eth.deployFlipper(contractOwner);727 await helper.eth.transferBalanceFromSubstrate(donor, flipper.options.address);728 const contractHelper = await helper.ethNativeContract.contractHelpers(contractOwner);729730 await contractHelper.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: contractOwner});731 await helper.executeExtrinsic(palletAdmin, 'api.tx.appPromotion.sponsorContract', [flipper.options.address], true);732 await helper.executeExtrinsic(palletAdmin, 'api.tx.appPromotion.stopSponsoringContract', [flipper.options.address], true);733734 expect(await contractHelper.methods.hasSponsor(flipper.options.address).call()).to.be.false;735 expect((await helper.callRpc('api.query.evmContractHelpers.owner', [flipper.options.address])).toJSON()).to.be.equal(contractOwner);736 expect((await helper.callRpc('api.query.evmContractHelpers.sponsoring', [flipper.options.address])).toJSON()).to.deep.equal({737 disabled: null,738 });739740 await flipper.methods.flip().send({from: caller});741 expect(await flipper.methods.getValue().call()).to.be.true;742743 const callerBalance = await helper.balance.getEthereum(caller);744 const contractBalanceAfter = await helper.balance.getEthereum(flipper.options.address);745746 747 expect(1000n * nominal > callerBalance).to.be.true;748 expect(contractBalanceAfter).to.be.equal(100n * nominal);749 });750751 itEth('can not be called by non-admin', async ({helper}) => {752 const [nonAdmin] = await getAccounts(1);753 const contractOwner = (await helper.eth.createAccountWithBalance(donor, 1000n)).toLowerCase();754 const flipper = await helper.eth.deployFlipper(contractOwner);755756 await helper.executeExtrinsic(palletAdmin, 'api.tx.appPromotion.sponsorContract', [flipper.options.address]);757 await expect(helper.executeExtrinsic(nonAdmin, 'api.tx.appPromotion.stopSponsoringContract', [flipper.options.address]))758 .to.be.rejectedWith(/appPromotion\.NoPermission/);759 });760761 itEth('should not affect a contract which is not sponsored by pallete', async ({helper}) => {762 const [nonAdmin] = await getAccounts(1);763 const contractOwner = (await helper.eth.createAccountWithBalance(donor, 1000n)).toLowerCase();764 const flipper = await helper.eth.deployFlipper(contractOwner);765 const contractHelper = await helper.ethNativeContract.contractHelpers(contractOwner);766 await expect(contractHelper.methods.selfSponsoredEnable(flipper.options.address).send()).to.be.fulfilled;767768 await expect(helper.executeExtrinsic(nonAdmin, 'api.tx.appPromotion.stopSponsoringContract', [flipper.options.address], true)).to.be.rejectedWith('appPromotion.NoPermission');769 });770 });771772 describe('payoutStakers', () => {773 itSub('can not be called by non admin', async ({helper}) => {774 const [nonAdmin] = await getAccounts(1);775 await expect(helper.admin.payoutStakers(nonAdmin, 100)).to.be.rejectedWith('appPromotion.NoPermission');776 });777778 itSub('should increase total staked', async ({helper}) => {779 const [staker] = await getAccounts(1);780 const totalStakedBefore = await helper.staking.getTotalStaked();781 await helper.staking.stake(staker, 100n * nominal);782783 784 const [stakedInBlock] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});785 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stakedInBlock.block));786 const totalPayout = (await helper.admin.payoutStakers(palletAdmin, 100)).reduce((prev, payout) => prev + payout.payout, 0n);787788 const totalStakedAfter = await helper.staking.getTotalStaked();789 expect(totalStakedAfter).to.equal(totalStakedBefore + (100n * nominal) + totalPayout);790 791 await helper.staking.unstakeAll(staker);792 expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedAfter - calculateIncome(100n * nominal));793 });794795 itSub('should credit 0.05% for staking period', async ({helper}) => {796 const [staker] = await getAccounts(1);797798 await waitPromotionPeriodDoesntEnd(helper);799800 await helper.staking.stake(staker, 100n * nominal);801 await helper.staking.stake(staker, 200n * nominal);802803 804 const [_stake1, stake2] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});805 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake2.block));806807 const payoutToStaker = (await helper.admin.payoutStakers(palletAdmin, 100)).find((payout) => payout.staker === staker.address)!.payout;808 expect(payoutToStaker + 300n * nominal).to.equal(calculateIncome(300n * nominal));809810 const totalStakedPerBlock = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});811 const income1 = calculateIncome(100n * nominal);812 const income2 = calculateIncome(200n * nominal);813 expect(totalStakedPerBlock[0].amount).to.equal(income1);814 expect(totalStakedPerBlock[1].amount).to.equal(income2);815816 const stakerBalance = await helper.balance.getSubstrateFull(staker.address);817 expect(stakerBalance).to.contain({miscFrozen: income1 + income2, feeFrozen: income1 + income2, reserved: 0n});818 expect(stakerBalance.free / nominal).to.eq(999n);819 });820821 itSub('shoud be paid for more than one period if payments was missed', async ({helper}) => {822 const [staker] = await getAccounts(1);823824 await helper.staking.stake(staker, 100n * nominal);825 826 let [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});827 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block) + LOCKING_PERIOD);828829 await helper.admin.payoutStakers(palletAdmin, 100);830 [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});831 const frozenBalanceShouldBe = calculateIncome(100n * nominal, 2);832 expect(stake.amount).to.be.equal(frozenBalanceShouldBe);833834 const stakerFullBalance = await helper.balance.getSubstrateFull(staker.address);835836 expect(stakerFullBalance).to.contain({reserved: 0n, feeFrozen: frozenBalanceShouldBe, miscFrozen: frozenBalanceShouldBe});837 });838839 itSub('should not be credited for pending-unstaked tokens', async ({helper}) => {840 841 const [staker] = await getAccounts(1);842 await helper.staking.stake(staker, 100n * nominal);843 const [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});844 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block) + LOCKING_PERIOD);845 await helper.staking.unstakeAll(staker);846847 848 const totalBalanceBefore = await helper.balance.getSubstrate(staker.address);849 await helper.admin.payoutStakers(palletAdmin, 100);850 const totalBalanceAfter = await helper.balance.getSubstrate(staker.address);851852 expect(totalBalanceBefore).to.be.equal(totalBalanceAfter);853 });854855 itSub('should bring compound interest', async ({helper}) => {856 const [staker] = await getAccounts(1);857858 await helper.staking.stake(staker, 100n * nominal);859860 let [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});861 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block));862863 await helper.admin.payoutStakers(palletAdmin, 100);864 [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});865 expect(stake.amount).to.equal(calculateIncome(100n * nominal));866867 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block) + LOCKING_PERIOD);868 await helper.admin.payoutStakers(palletAdmin, 100);869 [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});870 expect(stake.amount).to.equal(calculateIncome(100n * nominal, 2));871 });872873 itSub('can calculate reward for tiny stake', async ({helper}) => {874 const [staker] = await getAccounts(1);875 await helper.staking.stake(staker, 100n * nominal);876 await helper.staking.stake(staker, 100n * nominal);877 await helper.staking.unstakePartial(staker, 100n * nominal - 1n);878879 const [_stake1, stake2] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});880 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake2.block));881882 const stakerPayout = await payUntilRewardFor(staker.address, helper);883 expect(stakerPayout.stake).to.eq(100n * nominal + 1n);884 });885886 itSub('can eventually pay all rewards', async ({helper}) => {887 const stakers = await getAccounts(30);888 889 await Promise.all(stakers.map(staker => helper.staking.stake(staker, 100n * nominal)));890891 let unstakingTxs = [];892 for (const staker of stakers) {893 if (unstakingTxs.length == 3) {894 await Promise.all(unstakingTxs);895 unstakingTxs = [];896 }897 unstakingTxs.push(helper.staking.unstakePartial(staker, 100n * nominal - 1n));898 }899900 const [staker] = await getAccounts(1);901 await helper.staking.stake(staker, 100n * nominal);902 const [stake] = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});903 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake.block));904905 let payouts;906 do {907 payouts = await helper.admin.payoutStakers(palletAdmin, 20);908 } while (payouts.length !== 0);909 });910 });911912 describe('events', () => {913 [914 {method: 'unstakePartial' as const},915 {method: 'unstakeAll' as const},916 ].map(testCase => {917 itSub(testCase.method, async ({helper}) => {918 const unstakeParams = testCase.method === 'unstakePartial'919 ? [100n * nominal - 1n]920 : [];921 const [staker] = await getAccounts(1);922 await helper.staking.stake(staker, 100n * nominal);923 await helper.staking.stake(staker, 200n * nominal);924 const {result} = await helper.executeExtrinsic(staker, `api.tx.appPromotion.${testCase.method}`, unstakeParams);925926 const event = result.events.find(e => e.event.section === 'appPromotion' && e.event.method === 'Unstake');927 const unstakerEvents = event?.event.data[0].toString();928 const unstakedEvents = BigInt(event?.event.data[1].toString());929 expect(unstakerEvents).to.eq(staker.address);930 expect(unstakedEvents).to.eq(testCase.method === 'unstakeAll' ? 300n * nominal : 100n * nominal - 1n);931 });932 });933934 itSub('stake', async ({helper}) => {935 const [staker] = await getAccounts(1);936 const {result} = await helper.executeExtrinsic(staker, 'api.tx.appPromotion.stake', [100n * nominal]);937938 const event = result.events.find(e => e.event.section === 'appPromotion' && e.event.method === 'Stake');939 const stakerEvents = event?.event.data[0].toString();940 const stakedEvents = BigInt(event?.event.data[1].toString());941 expect(stakerEvents).to.eq(staker.address);942 expect(stakedEvents).to.eq(100n * nominal);943 });944945 946 itSub.skip('payoutStakers', async ({helper}) => {947 const [staker1, staker2] = await getAccounts(2);948 const STAKE1 = 100n * nominal;949 const STAKE2 = 200n * nominal;950 await helper.staking.stake(staker1, STAKE1);951 await helper.staking.stake(staker2, STAKE2);952953 const [stake2] = await helper.staking.getTotalStakedPerBlock({Substrate: staker2.address});954 await helper.wait.forRelayBlockNumber(rewardAvailableInBlock(stake2.block));955956 const results = await helper.admin.payoutStakers(palletAdmin, 100);957 const stakersEvents = results.filter(ev => ev.staker === staker1.address || ev.staker === staker2.address);958 expect(stakersEvents).has.length(2);959 expect(stakersEvents).has.not.ordered.members([960 {staker: staker1.address, stake: STAKE1, payout: calculateIncome(STAKE1) - STAKE1},961 {staker: staker2.address, stake: STAKE2, payout: calculateIncome(STAKE2) - STAKE2},962 ]);963 });964 });965});966967968969async function payUntilRewardFor(account: string, helper: DevUniqueHelper) {970 for (let i = 0; i < 3; i++) {971 const payouts = await helper.admin.payoutStakers(palletAdmin, 100);972 const accountPayout = payouts.find(p => p.staker === account);973 if (accountPayout) return accountPayout;974 }975 throw Error(`Cannot find payout for ${account}`);976}977978function calculateIncome(base: bigint, iter = 0, calcPeriod: bigint = UNLOCKING_PERIOD): bigint {979 const DAY = 7200n;980 const ACCURACY = 1_000_000_000n;981 982 const income = base + base * (ACCURACY * (calcPeriod * 5n) / (10_000n * DAY)) / ACCURACY ;983984 if (iter > 1) {985 return calculateIncome(income, iter - 1, calcPeriod);986 } else return income;987}988989function rewardAvailableInBlock(stakedInBlock: bigint) {990 if (stakedInBlock % LOCKING_PERIOD === 0n) return stakedInBlock + LOCKING_PERIOD;991 return (stakedInBlock - stakedInBlock % LOCKING_PERIOD) + (LOCKING_PERIOD * 2n);992}993994995996async function waitPromotionPeriodDoesntEnd(helper: DevUniqueHelper, waitBlockLessThan = LOCKING_PERIOD / 3n) {997 const relayBlockNumber = (await helper.callRpc('api.query.parachainSystem.validationData', [])).value.relayParentNumber.toNumber(); 998 const currentPeriodBlock = BigInt(relayBlockNumber) % LOCKING_PERIOD;9991000 if (currentPeriodBlock > waitBlockLessThan) {1001 await helper.wait.forRelayBlockNumber(BigInt(relayBlockNumber) + LOCKING_PERIOD - currentPeriodBlock);1002 }1003}