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

difftreelog

source

js-packages/tests/vesting.test.ts7.8 KiBsourcehistory
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 type {IKeyringPair} from '@polkadot/types/types';18import {itSub, usingPlaygrounds, expect} from '@unique/test-utils/util.js';1920describe('Vesting', () => {21  let donor: IKeyringPair;22  let nominal: bigint;2324  before(async () => {25    await usingPlaygrounds(async (helper, privateKey) => {26      donor = await privateKey({url: import.meta.url});27      nominal = helper.balance.getOneTokenNominal();28    });29  });3031  itSub('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 SCHEDULE_1_PERIOD = 6n; // 6 blocks one period36    const SCHEDULE_1_START = currentRelayBlock + 6n; // Block when 1 schedule starts37    const SCHEDULE_2_PERIOD = 12n; // 12 blocks one period38    const SCHEDULE_2_START = currentRelayBlock + 12n; // Block when 2 schedule starts39    const schedule1 = {start: SCHEDULE_1_START, period: SCHEDULE_1_PERIOD, periodCount: 2n, perPeriod: 50n * nominal};40    const schedule2 = {start: SCHEDULE_2_START, period: SCHEDULE_2_PERIOD, periodCount: 2n, perPeriod: 100n * nominal};4142    // act43    await helper.balance.vestedTransfer(sender, recepient.address, schedule1);44    await helper.balance.vestedTransfer(sender, recepient.address, schedule2);45    let schedule = await helper.balance.getVestingSchedules(recepient.address);4647    // check senders balance after vesting:48    let balanceSender = await helper.balance.getSubstrateFull(sender.address);49    expect(balanceSender.free / nominal).to.eq(699n);50    expect(balanceSender.frozen).to.eq(0n);51    expect(balanceSender.reserved).to.eq(0n);5253    // check recepient balance after vesting:54    let balanceRecepient = await helper.balance.getSubstrateFull(recepient.address);55    expect(balanceRecepient.free).to.eq(301n * nominal);56    expect(balanceRecepient.frozen).to.eq(300n * nominal);57    expect(balanceRecepient.reserved).to.eq(0n);5859    // Schedules list correct:60    expect(schedule).to.has.length(2);61    expect(schedule[0]).to.deep.eq(schedule1);62    expect(schedule[1]).to.deep.eq(schedule2);6364    // Wait first part available:65    await helper.wait.forRelayBlockNumber(SCHEDULE_1_START + SCHEDULE_1_PERIOD);66    await helper.balance.claim(recepient);6768    // check recepient balance after claim (50 tokens claimed, 250 left):69    balanceRecepient = await helper.balance.getSubstrateFull(recepient.address);70    expect(balanceRecepient.free / nominal).to.eq(300n);71    expect(balanceRecepient.frozen).to.eq(250n * nominal);72    expect(balanceRecepient.reserved).to.eq(0n);7374    // Wait first schedule ends and first part od second schedule:75    await helper.wait.forRelayBlockNumber(SCHEDULE_2_START + SCHEDULE_2_PERIOD);76    await helper.balance.claim(recepient);7778    // check recepient balance after second claim (150 tokens claimed, 100 left):79    balanceRecepient = await helper.balance.getSubstrateFull(recepient.address);80    expect(balanceRecepient.free / nominal).to.eq(300n);81    expect(balanceRecepient.frozen).to.eq(100n * nominal);82    expect(balanceRecepient.reserved).to.eq(0n);8384    // Schedules list contain 1 vesting:85    schedule = await helper.balance.getVestingSchedules(recepient.address);86    expect(schedule).to.has.length(1);87    expect(schedule[0]).to.deep.eq(schedule2);8889    // Wait 2 schedule ends:90    await helper.wait.forRelayBlockNumber(SCHEDULE_2_START + SCHEDULE_2_PERIOD * 2n);91    await helper.balance.claim(recepient);9293    // check recepient balance after second claim (100 tokens claimed, 0 left):94    balanceRecepient = await helper.balance.getSubstrateFull(recepient.address);95    expect(balanceRecepient.free / nominal).to.eq(300n);96    expect(balanceRecepient.frozen).to.eq(0n);97    expect(balanceRecepient.reserved).to.eq(0n);9899    // check sender balance does not changed:100    balanceSender = await helper.balance.getSubstrateFull(sender.address);101    expect(balanceSender.free / nominal).to.eq(699n);102    expect(balanceSender.frozen).to.eq(0n);103    expect(balanceSender.reserved).to.eq(0n);104  });105106  itSub('cannot send more tokens than have', async ({helper}) => {107    const [sender, receiver] = await helper.arrange.createAccounts([1000n, 1n], donor);108    const schedule = {start: 0n, period: 1n, periodCount: 1n, perPeriod: 100n * nominal};109    const manyPeriodsSchedule = {start: 0n, period: 1n, periodCount: 100n, perPeriod: 10n * nominal};110    const oneBigSumSchedule = {start: 0n, period: 1n, periodCount: 1n, perPeriod: 5000n * nominal};111112    // Sender cannot send vestedTransfer to self or other113    await expect(helper.balance.vestedTransfer(sender, sender.address, manyPeriodsSchedule)).to.be.rejectedWith(/^vesting.InsufficientBalanceToLock$/);114    await expect(helper.balance.vestedTransfer(sender, receiver.address, manyPeriodsSchedule)).to.be.rejectedWith(/^Token: FundsUnavailable$/);115    await expect(helper.balance.vestedTransfer(sender, sender.address, oneBigSumSchedule)).to.be.rejectedWith(/^vesting.InsufficientBalanceToLock$/);116    await expect(helper.balance.vestedTransfer(sender, receiver.address, oneBigSumSchedule)).to.be.rejectedWith(/^Token: FundsUnavailable$/);117118    const balanceSender = await helper.balance.getSubstrateFull(sender.address);119    const balanceReceiver = await helper.balance.getSubstrateFull(receiver.address);120121    // Sender's balance has not changed122    expect(balanceSender.free / nominal).to.eq(999n);123    expect(balanceSender.frozen).to.eq(0n);124    expect(balanceSender.reserved).to.eq(0n);125126    // Receiver's balance has not changed127    expect(balanceReceiver.free).to.be.eq(1n * nominal);128    expect(balanceReceiver.frozen).to.be.eq(0n);129    expect(balanceReceiver.reserved).to.be.eq(0n);130131    // Receiver cannot send vestedTransfer back because of freeze132    await expect(helper.balance.vestedTransfer(receiver, sender.address, schedule)).to.be.rejectedWith(/^Token: FundsUnavailable$/);133  });134135  itSub('cannot send vestedTransfer with incorrect parameters', async ({helper}) => {136    const [sender, receiver] = await helper.arrange.createAccounts([1000n, 1n], donor);137    const incorrectperiodSchedule = {start: 0n, period: 0n, periodCount: 10n, perPeriod: 10n * nominal};138    const incorrectPeriodCountSchedule = {start: 0n, period: 1n, periodCount: 0n, perPeriod: 10n * nominal};139    const incorrectPerPeriodSchedule = {start: 0n, period: 1n, periodCount: 1n, perPeriod: 0n * nominal};140141    await expect(helper.balance.vestedTransfer(sender, sender.address, incorrectperiodSchedule)).to.be.rejectedWith(/vesting.ZeroVestingPeriod/);142    await expect(helper.balance.vestedTransfer(sender, receiver.address, incorrectPeriodCountSchedule)).to.be.rejectedWith(/vesting.ZeroVestingPeriod/);143    await expect(helper.balance.vestedTransfer(sender, receiver.address, incorrectPerPeriodSchedule)).to.be.rejectedWith(/vesting.AmountLow/);144145    const balanceSender = await helper.balance.getSubstrateFull(sender.address);146    // Sender's balance has not changed147    expect(balanceSender.free / nominal).to.eq(999n);148    expect(balanceSender.frozen).to.eq(0n);149    expect(balanceSender.reserved).to.eq(0n);150  });151});