--- a/tests/src/app-promotion.test.ts +++ b/tests/src/app-promotion.test.ts @@ -14,36 +14,20 @@ // You should have received a copy of the GNU General Public License // along with Unique Network. If not, see . -import {default as usingApi, submitTransactionAsync} from './substrate/substrate-api'; -import {IKeyringPair, ITuple} from '@polkadot/types/types'; +import {IKeyringPair} from '@polkadot/types/types'; import { - createMultipleItemsExpectSuccess, - isTokenExists, - getLastTokenId, - getAllowance, - approve, - transferFrom, - createCollection, - transfer, - burnItem, normalizeAccountId, - CrossAccountId, - createFungibleItemExpectSuccess, - U128_MAX, - burnFromExpectSuccess, - UNIQUE, getModuleNames, Pallets, - getBlockNumber, } from './util/helpers'; -import chai, {use} from 'chai'; +import chai from 'chai'; import chaiAsPromised from 'chai-as-promised'; import {usingPlaygrounds} from './util/playgrounds'; import {default as waitNewBlocks} from './substrate/wait-new-blocks'; -import {encodeAddress, hdEthereum, mnemonicGenerate} from '@polkadot/util-crypto'; +import {encodeAddress, mnemonicGenerate} from '@polkadot/util-crypto'; import {stringToU8a} from '@polkadot/util'; import {UniqueHelper} from './util/playgrounds/unique'; import {ApiPromise} from '@polkadot/api'; @@ -81,26 +65,25 @@ // assert: query appPromotion.staked(Alice) equal [100, 200] // assert: query appPromotion.totalStaked() increased by 200 - await usingPlaygrounds(async (helper, privateKeyWrapper) => { - const totalStakedBefore = (await helper.api!.rpc.unique.totalStaked()).toBigInt(); + await usingPlaygrounds(async (helper) => { + const totalStakedBefore = await helper.staking.getTotalStaked(); const staker = await createUser(); - + await expect(helper.staking.stake(staker, nominal - 1n)).to.be.eventually.rejected; + await expect(helper.staking.stake(staker, nominal * 1n)).to.be.eventually.fulfilled; + expect(await helper.staking.getTotalStakingLocked({Substrate: staker.address})).to.be.equal(nominal); + + // TODO add helpers to assert bigints. Check balance close to 10 + expect(await helper.balance.getSubstrate(staker.address) - 9n * nominal >= (nominal / 2n)).to.be.true; + expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(nominal); + expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedBefore + nominal); + + await helper.arrange.waitNewBlocks(1); - await expect(helper.signTransaction(staker, helper.api!.tx.promotion.stake(nominal / 2n))).to.be.eventually.rejected; - await expect(helper.signTransaction(staker, helper.api!.tx.promotion.stake(1n * nominal))).to.be.eventually.fulfilled; - expect((await helper.api!.rpc.unique.totalStakingLocked(normalizeAccountId(staker))).toBigInt()).to.be.equal(nominal); - expect(9n * nominal - await helper.balance.getSubstrate(staker.address) <= nominal / 2n).to.be.true; - expect((await helper.api!.rpc.unique.totalStaked(normalizeAccountId(staker))).toBigInt()).to.be.equal(nominal); - expect((await helper.api!.rpc.unique.totalStaked()).toBigInt()).to.be.equal(totalStakedBefore + nominal); - - await waitNewBlocks(helper.api!, 1); + await expect(helper.staking.stake(staker, 2n * nominal)).to.be.eventually.fulfilled; + expect(await helper.staking.getTotalStakingLocked({Substrate: staker.address})).to.be.equal(3n * nominal); - - await expect(helper.signTransaction(staker, helper.api!.tx.promotion.stake(2n * nominal))).to.be.eventually.fulfilled; - expect((await helper.api!.rpc.unique.totalStakingLocked(normalizeAccountId(staker))).toBigInt()).to.be.equal(3n * nominal); - - const stakedPerBlock = (await helper.api!.rpc.unique.totalStakedPerBlock(normalizeAccountId(staker))).map(([block, amount]) => [block.toBigInt(), amount.toBigInt()]); + const stakedPerBlock = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address}); expect(stakedPerBlock.map((x) => x[1])).to.be.deep.equal([nominal, 2n * nominal]); }); }); --- a/tests/src/util/playgrounds/unique.ts +++ b/tests/src/util/playgrounds/unique.ts @@ -1979,7 +1979,38 @@ } } +class StakingGroup extends HelperGroup { + /** + * Stake tokens for App Promotion + * @param signer keyring of signer + * @param amount amount of tokens to stake + * @param label extra label for log + */ + async stake(signer: TSigner, amount: bigint, label?: string): Promise { + if(typeof label === 'undefined') label = `${signer.address} amount: ${amount}`; + const stakeResult = await this.helper.executeExtrinsic( + signer, + 'api.tx.promotion.stake', [amount], + true, `stake failed for ${label}`, + ); + // TODO extract info from events + return true; + } + async getTotalStaked(address?: ICrossAccountId): Promise { + if (address) return (await this.helper.callRpc('api.rpc.unique.totalStaked', [address])).toBigInt(); + return (await this.helper.callRpc('api.rpc.unique.totalStaked')).toBigInt(); + } + + async getTotalStakingLocked(address: ICrossAccountId): Promise { + return (await this.helper.callRpc('api.rpc.unique.totalStakingLocked', [address])).toBigInt(); + } + + async getTotalStakedPerBlock(address: ICrossAccountId): Promise { + return (await this.helper.callRpc('api.rpc.unique.totalStakedPerBlock', [address])).map(([block, amount]: any[]) => [block.toBigInt(), amount.toBigInt()]); + } +} + export class UniqueHelper extends ChainHelperBase { chain: ChainGroup; balance: BalanceGroup; @@ -1988,6 +2019,7 @@ nft: NFTGroup; rft: RFTGroup; ft: FTGroup; + staking: StakingGroup; constructor(logger?: ILogger) { super(logger); @@ -1998,6 +2030,7 @@ this.nft = new NFTGroup(this); this.rft = new RFTGroup(this); this.ft = new FTGroup(this); + this.staking = new StakingGroup(this); } }