git.delta.rocks / unique-network / refs/commits / ac1baa6db9e9

difftreelog

add playgrounds and fix test

Max Andreev2022-08-26parent: #982476e.patch.diff
in: master

2 files changed

modifiedtests/src/app-promotion.test.tsdiffbeforeafterboth
--- 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 <http://www.gnu.org/licenses/>.
 
-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]);
     });
   });
modifiedtests/src/util/playgrounds/unique.tsdiffbeforeafterboth
1979 }1979 }
1980}1980}
19811981
1982class StakingGroup extends HelperGroup {
1983 /**
1984 * Stake tokens for App Promotion
1985 * @param signer keyring of signer
1986 * @param amount amount of tokens to stake
1987 * @param label extra label for log
1988 */
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 events
1997 return true;
1998 }
1999
2000 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 }
2004
2005 async getTotalStakingLocked(address: ICrossAccountId): Promise<bigint> {
2006 return (await this.helper.callRpc('api.rpc.unique.totalStakingLocked', [address])).toBigInt();
2007 }
2008
2009 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}
19822013
1983export 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;
19912023
1992 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