difftreelog
add playgrounds and fix test
in: master
2 files changed
tests/src/app-promotion.test.tsdiffbeforeafterboth14// You should have received a copy of the GNU General Public License14// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.161617import {default as usingApi, submitTransactionAsync} from './substrate/substrate-api';18import {IKeyringPair, ITuple} from '@polkadot/types/types';17import {IKeyringPair} from '@polkadot/types/types';19import {18import {20 19 21 createMultipleItemsExpectSuccess,22 isTokenExists,23 getLastTokenId,24 getAllowance,25 approve,26 transferFrom,27 createCollection,28 transfer,29 burnItem,30 normalizeAccountId,20 normalizeAccountId,31 CrossAccountId,32 createFungibleItemExpectSuccess,33 U128_MAX,34 burnFromExpectSuccess,35 UNIQUE,36 getModuleNames,21 getModuleNames,37 Pallets,22 Pallets,38 getBlockNumber,39} from './util/helpers';23} from './util/helpers';402441import chai, {use} from 'chai';25import chai from 'chai';42import chaiAsPromised from 'chai-as-promised';26import chaiAsPromised from 'chai-as-promised';43import {usingPlaygrounds} from './util/playgrounds';27import {usingPlaygrounds} from './util/playgrounds';44import {default as waitNewBlocks} from './substrate/wait-new-blocks';28import {default as waitNewBlocks} from './substrate/wait-new-blocks';452946import {encodeAddress, hdEthereum, mnemonicGenerate} from '@polkadot/util-crypto';30import {encodeAddress, mnemonicGenerate} from '@polkadot/util-crypto';47import {stringToU8a} from '@polkadot/util';31import {stringToU8a} from '@polkadot/util';48import {UniqueHelper} from './util/playgrounds/unique';32import {UniqueHelper} from './util/playgrounds/unique';49import {ApiPromise} from '@polkadot/api';33import {ApiPromise} from '@polkadot/api';81 // assert: query appPromotion.staked(Alice) equal [100, 200]65 // assert: query appPromotion.staked(Alice) equal [100, 200]82 // assert: query appPromotion.totalStaked() increased by 20066 // assert: query appPromotion.totalStaked() increased by 20083 67 84 await usingPlaygrounds(async (helper, privateKeyWrapper) => {68 await usingPlaygrounds(async (helper) => {85 const totalStakedBefore = (await helper.api!.rpc.unique.totalStaked()).toBigInt();69 const totalStakedBefore = await helper.staking.getTotalStaked();86 const staker = await createUser();70 const staker = await createUser();87 71 88 89 90 await expect(helper.signTransaction(staker, helper.api!.tx.promotion.stake(nominal / 2n))).to.be.eventually.rejected;72 await expect(helper.staking.stake(staker, nominal - 1n)).to.be.eventually.rejected;91 await expect(helper.signTransaction(staker, helper.api!.tx.promotion.stake(1n * nominal))).to.be.eventually.fulfilled;73 await expect(helper.staking.stake(staker, nominal * 1n)).to.be.eventually.fulfilled;92 expect((await helper.api!.rpc.unique.totalStakingLocked(normalizeAccountId(staker))).toBigInt()).to.be.equal(nominal);74 expect(await helper.staking.getTotalStakingLocked({Substrate: staker.address})).to.be.equal(nominal);7576 // TODO add helpers to assert bigints. Check balance close to 1093 expect(9n * nominal - await helper.balance.getSubstrate(staker.address) <= nominal / 2n).to.be.true;77 expect(await helper.balance.getSubstrate(staker.address) - 9n * nominal >= (nominal / 2n)).to.be.true;94 expect((await helper.api!.rpc.unique.totalStaked(normalizeAccountId(staker))).toBigInt()).to.be.equal(nominal);78 expect(await helper.staking.getTotalStaked({Substrate: staker.address})).to.be.equal(nominal);95 expect((await helper.api!.rpc.unique.totalStaked()).toBigInt()).to.be.equal(totalStakedBefore + nominal);79 expect(await helper.staking.getTotalStaked()).to.be.equal(totalStakedBefore + nominal);96 8097 await waitNewBlocks(helper.api!, 1);81 await helper.arrange.waitNewBlocks(1);98 82 99 100 await expect(helper.signTransaction(staker, helper.api!.tx.promotion.stake(2n * nominal))).to.be.eventually.fulfilled;83 await expect(helper.staking.stake(staker, 2n * nominal)).to.be.eventually.fulfilled;101 expect((await helper.api!.rpc.unique.totalStakingLocked(normalizeAccountId(staker))).toBigInt()).to.be.equal(3n * nominal);84 expect(await helper.staking.getTotalStakingLocked({Substrate: staker.address})).to.be.equal(3n * nominal);102 85 103 const stakedPerBlock = (await helper.api!.rpc.unique.totalStakedPerBlock(normalizeAccountId(staker))).map(([block, amount]) => [block.toBigInt(), amount.toBigInt()]);86 const stakedPerBlock = await helper.staking.getTotalStakedPerBlock({Substrate: staker.address});104 expect(stakedPerBlock.map((x) => x[1])).to.be.deep.equal([nominal, 2n * nominal]);87 expect(stakedPerBlock.map((x) => x[1])).to.be.deep.equal([nominal, 2n * nominal]);105 });88 });106 });89 });tests/src/util/playgrounds/unique.tsdiffbeforeafterboth1979 }1979 }1980}1980}198119811982class StakingGroup extends HelperGroup {1983 /**1984 * Stake tokens for App Promotion1985 * @param signer keyring of signer1986 * @param amount amount of tokens to stake1987 * @param label extra label for log1988 */1989 async stake(signer: TSigner, amount: bigint, label?: string): Promise<boolean> {1990 if(typeof label === 'undefined') label = `${signer.address} amount: ${amount}`;1991 const stakeResult = await this.helper.executeExtrinsic(1992 signer,1993 'api.tx.promotion.stake', [amount],1994 true, `stake failed for ${label}`,1995 );1996 // TODO extract info from events1997 return true;1998 }19992000 async getTotalStaked(address?: ICrossAccountId): Promise<bigint> {2001 if (address) return (await this.helper.callRpc('api.rpc.unique.totalStaked', [address])).toBigInt();2002 return (await this.helper.callRpc('api.rpc.unique.totalStaked')).toBigInt();2003 }20042005 async getTotalStakingLocked(address: ICrossAccountId): Promise<bigint> {2006 return (await this.helper.callRpc('api.rpc.unique.totalStakingLocked', [address])).toBigInt();2007 }20082009 async getTotalStakedPerBlock(address: ICrossAccountId): Promise<bigint[][]> {2010 return (await this.helper.callRpc('api.rpc.unique.totalStakedPerBlock', [address])).map(([block, amount]: any[]) => [block.toBigInt(), amount.toBigInt()]);2011 }2012}198220131983export class UniqueHelper extends ChainHelperBase {2014export class UniqueHelper extends ChainHelperBase {1984 chain: ChainGroup;2015 chain: ChainGroup;1988 nft: NFTGroup;2019 nft: NFTGroup;1989 rft: RFTGroup;2020 rft: RFTGroup;1990 ft: FTGroup;2021 ft: FTGroup;2022 staking: StakingGroup;199120231992 constructor(logger?: ILogger) {2024 constructor(logger?: ILogger) {1993 super(logger);2025 super(logger);1998 this.nft = new NFTGroup(this);2030 this.nft = new NFTGroup(this);1999 this.rft = new RFTGroup(this);2031 this.rft = new RFTGroup(this);2000 this.ft = new FTGroup(this);2032 this.ft = new FTGroup(this);2033 this.staking = new StakingGroup(this);2001 } 2034 } 2002}2035}20032036