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

difftreelog

source

tests/src/vesting.test.ts7.7 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 {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('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 = {start: currentRelayBlock + 4n, period: 4n, periodCount: 2n, perPeriod: 50n * nominal};36    const schedule2 = {start: currentRelayBlock + 8n, period: 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  });104105  itSub('cannot send more tokens than have', async ({helper}) => {106    const [sender, receiver] = await helper.arrange.createAccounts([1000n, 1n], donor);107    const schedule = {start: 0n, period: 1n, periodCount: 1n, perPeriod: 100n * nominal};108    const manyPeriodsSchedule = {start: 0n, period: 1n, periodCount: 100n, perPeriod: 10n * nominal};109    const oneBigSumSchedule = {start: 0n, period: 1n, periodCount: 1n, perPeriod: 5000n * nominal};110111    // Sender cannot send vestedTransfer to self or other112    await expect(helper.balance.vestedTransfer(sender, sender.address, manyPeriodsSchedule)).to.be.rejectedWith(/InsufficientBalance/);113    await expect(helper.balance.vestedTransfer(sender, receiver.address, manyPeriodsSchedule)).to.be.rejectedWith(/InsufficientBalance/);114    await expect(helper.balance.vestedTransfer(sender, sender.address, oneBigSumSchedule)).to.be.rejectedWith(/InsufficientBalance/);115    await expect(helper.balance.vestedTransfer(sender, receiver.address, oneBigSumSchedule)).to.be.rejectedWith(/InsufficientBalance/);116117    const balanceSender = await helper.balance.getSubstrateFull(sender.address);118    const balanceReceiver = await helper.balance.getSubstrateFull(receiver.address);119120    // Sender's balance has not changed121    expect(balanceSender.free / nominal).to.eq(999n);122    expect(balanceSender.feeFrozen).to.eq(0n);123    expect(balanceSender.miscFrozen).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.feeFrozen).to.be.eq(0n);129    expect(balanceReceiver.miscFrozen).to.be.eq(0n);130    expect(balanceReceiver.reserved).to.be.eq(0n);131132    // Receiver cannot send vestedTransfer back because of freeze133    await expect(helper.balance.vestedTransfer(receiver, sender.address, schedule)).to.be.rejectedWith(/InsufficientBalance/);134  });135136  itSub('cannot send vestedTransfer with incorrect parameters', async ({helper}) => {137    const [sender, receiver] = await helper.arrange.createAccounts([1000n, 1n], donor);138    const incorrectperiodSchedule = {start: 0n, period: 0n, periodCount: 10n, perPeriod: 10n * nominal};139    const incorrectPeriodCountSchedule = {start: 0n, period: 1n, periodCount: 0n, perPeriod: 10n * nominal};140    const incorrectPerPeriodSchedule = {start: 0n, period: 1n, periodCount: 1n, perPeriod: 0n * nominal};141142    await expect(helper.balance.vestedTransfer(sender, sender.address, incorrectperiodSchedule)).to.be.rejectedWith(/vesting.ZeroVestingPeriod/);143    await expect(helper.balance.vestedTransfer(sender, receiver.address, incorrectPeriodCountSchedule)).to.be.rejectedWith(/vesting.ZeroVestingPeriod/);144    await expect(helper.balance.vestedTransfer(sender, receiver.address, incorrectPerPeriodSchedule)).to.be.rejectedWith(/vesting.AmountLow/);145146    const balanceSender = await helper.balance.getSubstrateFull(sender.address);147    // Sender's balance has not changed148    expect(balanceSender.free / nominal).to.eq(999n);149    expect(balanceSender.feeFrozen).to.eq(0n);150    expect(balanceSender.miscFrozen).to.eq(0n);151    expect(balanceSender.reserved).to.eq(0n);152  });153});