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.tsdiffbeforeafterboth--- 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<boolean> {
+ 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<bigint> {
+ 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<bigint> {
+ return (await this.helper.callRpc('api.rpc.unique.totalStakingLocked', [address])).toBigInt();
+ }
+
+ async getTotalStakedPerBlock(address: ICrossAccountId): Promise<bigint[][]> {
+ 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);
}
}