git.delta.rocks / unique-network / refs/commits / 9efe15d9b711

difftreelog

Add vesting e2e test and helpers

Max Andreev2022-11-24parent: #e8d45f9.patch.diff
in: master

2 files changed

modifiedtests/src/util/playgrounds/unique.tsdiffbeforeafterboth
--- a/tests/src/util/playgrounds/unique.ts
+++ b/tests/src/util/playgrounds/unique.ts
@@ -2146,6 +2146,15 @@
   }
 
   /**
+   * Get latest relay block
+   * @returns {number} relay block
+   */
+  async getRelayBlockNumber(): Promise<bigint> {
+    const blockNumber = (await this.helper.callRpc('api.query.parachainSystem.validationData')).toJSON().relayParentNumber;
+    return BigInt(blockNumber);
+  }
+
+  /**
    * Get account nonce
    * @param address substrate address
    * @example getNonce("5GrwvaEF5zXb26Fz...");
@@ -2326,6 +2335,52 @@
     isSuccess = isSuccess && BigInt(amount) === transfer.amount;
     return isSuccess;
   }
+
+  /**
+   * Transfer tokens with the unlock period
+   * @param signer signers Keyring
+   * @param address Substrate address of recipient
+   * @param schedule Schedule params
+   * @example vestedTransfer(signer, recepient.address, 20000, 100, 10, 50 * nominal); // total amount of vested tokens will be 100 * 50 = 5000
+   */
+  async vestedTransfer(signer: TSigner, address: TSubstrateAccount, schedule: {startRelayBlock: bigint, periodBlocks: bigint, periodCount: bigint, perPeriod: bigint}): Promise<void> {
+    const result = await this.helper.executeExtrinsic(signer, 'api.tx.vesting.vestedTransfer', [address, {start: schedule.startRelayBlock, period: schedule.periodBlocks, periodCount: schedule.periodCount, perPeriod: schedule.perPeriod}]);
+    const event = result.result.events
+      .find(e => e.event.section === 'vesting' &&
+            e.event.method === 'VestingScheduleAdded' &&
+            e.event.data[0].toHuman() === signer.address);
+    if (!event) throw Error('Cannot find transfer in events');
+  }
+
+  /**
+   * Get schedule for recepient of vested transfer
+   * @param address Substrate address of recipient
+   * @returns 
+   */
+  async getVestingSchedules(address: TSubstrateAccount): Promise<{startRelayBlock: bigint, periodBlocks: bigint, periodCount: bigint, perPeriod: bigint}[]> {
+    const schedule = (await this.helper.callRpc('api.query.vesting.vestingSchedules', [address])).toJSON();
+    return schedule.map((schedule: any) => {
+      return {
+        startRelayBlock: BigInt(schedule.start),
+        periodBlocks: BigInt(schedule.period),
+        periodCount: BigInt(schedule.periodCount),
+        perPeriod: BigInt(schedule.perPeriod),
+      };
+    });
+  }
+
+  /**
+   * Claim vested tokens
+   * @param signer signers Keyring
+   */
+  async claim(signer: TSigner) {
+    const result = await this.helper.executeExtrinsic(signer, 'api.tx.vesting.claim', []);
+    const event = result.result.events
+      .find(e => e.event.section === 'vesting' &&
+            e.event.method === 'Claimed' &&
+            e.event.data[0].toHuman() === signer.address);
+    if (!event) throw Error('Cannot find claim in events');
+  }
 }
 
 class AddressGroup extends HelperGroup<ChainHelperBase> {
addedtests/src/vesting.test.tsdiffbeforeafterboth
after · tests/src/vesting.test.ts
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {IKeyringPair} from '@polkadot/types/types';18import {itSub, usingPlaygrounds, expect} from './util';1920describe('Vesting', () => {21  let donor: IKeyringPair;22  let nominal: bigint;2324  before(async () => {25    await usingPlaygrounds(async (helper, privateKey) => {26      donor = await privateKey({filename: __filename});27      nominal = helper.balance.getOneTokenNominal();28    });29  });3031  itSub.only('can perform vestedTransfer and claim tokens', async ({helper}) => {32    // arrange33    const [sender, recepient] = await helper.arrange.createAccounts([1000n, 1n], donor);34    const currentRelayBlock = await helper.chain.getRelayBlockNumber();35    const schedule1 = {startRelayBlock: currentRelayBlock + 4n, periodBlocks: 4n, periodCount: 2n, perPeriod: 50n * nominal};36    const schedule2 = {startRelayBlock: currentRelayBlock + 8n, periodBlocks: 8n, periodCount: 2n, perPeriod: 100n * nominal};3738    // act39    await helper.balance.vestedTransfer(sender, recepient.address, schedule1);40    await helper.balance.vestedTransfer(sender, recepient.address, schedule2);41    let schedule = await helper.balance.getVestingSchedules(recepient.address);4243    // check senders balance after vesting:44    let balanceSender = await helper.balance.getSubstrateFull(sender.address);45    expect(balanceSender.free / nominal).to.eq(699n);46    expect(balanceSender.feeFrozen).to.eq(0n);47    expect(balanceSender.miscFrozen).to.eq(0n);48    expect(balanceSender.reserved).to.eq(0n);4950    // check recepient balance after vesting:51    let balanceRecepient = await helper.balance.getSubstrateFull(recepient.address);52    expect(balanceRecepient.free).to.eq(301n * nominal);53    expect(balanceRecepient.feeFrozen).to.eq(300n * nominal);54    expect(balanceRecepient.miscFrozen).to.eq(300n * nominal);55    expect(balanceRecepient.reserved).to.eq(0n);5657    // Schedules list correct:58    expect(schedule).to.has.length(2);59    expect(schedule[0]).to.deep.eq(schedule1);60    expect(schedule[1]).to.deep.eq(schedule2);6162    await helper.wait.forRelayBlockNumber(currentRelayBlock + 8n);63    await helper.balance.claim(recepient);6465    // check recepient balance after claim (50 tokens claimed):66    balanceRecepient = await helper.balance.getSubstrateFull(recepient.address);67    expect(balanceRecepient.free / nominal).to.eq(300n);68    expect(balanceRecepient.feeFrozen).to.eq(250n * nominal);69    expect(balanceRecepient.miscFrozen).to.eq(250n * nominal);70    expect(balanceRecepient.reserved).to.eq(0n);71    72    await helper.wait.forRelayBlockNumber(currentRelayBlock + 16n);73    await helper.balance.claim(recepient);7475    // check recepient balance after second claim (150 tokens claimed):76    balanceRecepient = await helper.balance.getSubstrateFull(recepient.address);77    expect(balanceRecepient.free / nominal).to.eq(300n);78    expect(balanceRecepient.feeFrozen).to.eq(100n * nominal);79    expect(balanceRecepient.miscFrozen).to.eq(100n * nominal);80    expect(balanceRecepient.reserved).to.eq(0n);81    82    // Schedules list contain 1 vesting:83    schedule = await helper.balance.getVestingSchedules(recepient.address);84    expect(schedule).to.has.length(1);85    expect(schedule[0]).to.deep.eq(schedule2);8687    await helper.wait.forRelayBlockNumber(currentRelayBlock + 24n);88    await helper.balance.claim(recepient);8990    // check recepient balance after second claim (100 tokens claimed):91    balanceRecepient = await helper.balance.getSubstrateFull(recepient.address);92    expect(balanceRecepient.free / nominal).to.eq(300n);93    expect(balanceRecepient.feeFrozen).to.eq(0n);94    expect(balanceRecepient.miscFrozen).to.eq(0n);95    expect(balanceRecepient.reserved).to.eq(0n);96    97    // check sender balance does not changed:98    balanceSender = await helper.balance.getSubstrateFull(sender.address);99    expect(balanceSender.free / nominal).to.eq(699n);100    expect(balanceSender.feeFrozen).to.eq(0n);101    expect(balanceSender.miscFrozen).to.eq(0n);102    expect(balanceSender.reserved).to.eq(0n);103  });104});