git.delta.rocks / unique-network / refs/commits / 425a1544a345

difftreelog

Check isOwnerOrAdminCross for rft, nft and ft

Max Andreev2022-12-22parent: #a1111d4.patch.diff
in: master
+ Remove only for vesting test

2 files changed

modifiedtests/src/eth/collectionAdmin.test.tsdiffbeforeafterboth
--- a/tests/src/eth/collectionAdmin.test.ts
+++ b/tests/src/eth/collectionAdmin.test.ts
@@ -15,6 +15,7 @@
 
 import {IKeyringPair} from '@polkadot/types/types';
 import {expect} from 'chai';
+import {Pallets} from '../util';
 import {IEthCrossAccountId} from '../util/playgrounds/types';
 import {usingEthPlaygrounds, itEth} from './util';
 import {EthUniqueHelper} from './util/playgrounds/unique.dev';
@@ -39,36 +40,52 @@
     });
   });
 
-  itEth('can add account admin by owner', async ({helper, privateKey}) => {
-    // arrange
-    const owner = await helper.eth.createAccountWithBalance(donor);
-    const adminSub = await privateKey('//admin2');
-    const adminEth = helper.eth.createAccount().toLowerCase();
-
-    const adminDeprecated = helper.eth.createAccount().toLowerCase();
-    const adminCrossSub = helper.ethCrossAccount.fromKeyringPair(adminSub);
-    const adminCrossEth = helper.ethCrossAccount.fromAddress(adminEth);
-        
-    const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'A', 'B', 'C');
-    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);
-
-    // Soft-deprecated: can addCollectionAdmin 
-    await collectionEvm.methods.addCollectionAdmin(adminDeprecated).send();
-    // Can addCollectionAdminCross for substrate and ethereum address
-    await collectionEvm.methods.addCollectionAdminCross(adminCrossSub).send();
-    await collectionEvm.methods.addCollectionAdminCross(adminCrossEth).send();
+  [
+    {mode: 'nft' as const, requiredPallets: []},
+    {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},
+    {mode: 'ft' as const, requiredPallets: []},
+  ].map(testCase => {
+    itEth.ifWithPallets(`can add account admin by owner for ${testCase.mode}`, testCase.requiredPallets, async ({helper, privateKey}) => {
+      // arrange
+      const owner = await helper.eth.createAccountWithBalance(donor);
+      const adminSub = await privateKey('//admin2');
+      const adminEth = helper.eth.createAccount().toLowerCase();
+  
+      const adminDeprecated = helper.eth.createAccount().toLowerCase();
+      const adminCrossSub = helper.ethCrossAccount.fromKeyringPair(adminSub);
+      const adminCrossEth = helper.ethCrossAccount.fromAddress(adminEth);
+      
+      const {collectionAddress, collectionId} = await helper.eth.createCollection(testCase.mode, owner, 'A', 'B', 'C');
+      const collectionEvm = helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner, true);
 
-    // 1. Expect api.rpc.unique.adminlist returns admins:
-    const adminListRpc = await helper.collection.getAdmins(collectionId);
-    expect(adminListRpc).to.has.length(3);
-    expect(adminListRpc).to.be.deep.contain.members([{Substrate: adminSub.address}, {Ethereum: adminEth}, {Ethereum: adminDeprecated}]);
+      // Check isOwnerOrAdminCross returns false:
+      expect(await collectionEvm.methods.isOwnerOrAdminCross(adminCrossSub).call()).to.be.false;
+      expect(await collectionEvm.methods.isOwnerOrAdminCross(adminCrossEth).call()).to.be.false;
+      expect(await collectionEvm.methods.isOwnerOrAdminCross(helper.ethCrossAccount.fromAddress(adminDeprecated)).call()).to.be.false;
+      
+      // Soft-deprecated: can addCollectionAdmin 
+      await collectionEvm.methods.addCollectionAdmin(adminDeprecated).send();
+      // Can addCollectionAdminCross for substrate and ethereum address
+      await collectionEvm.methods.addCollectionAdminCross(adminCrossSub).send();
+      await collectionEvm.methods.addCollectionAdminCross(adminCrossEth).send();
+  
+      // 1. Expect api.rpc.unique.adminlist returns admins:
+      const adminListRpc = await helper.collection.getAdmins(collectionId);
+      expect(adminListRpc).to.has.length(3);
+      expect(adminListRpc).to.be.deep.contain.members([{Substrate: adminSub.address}, {Ethereum: adminEth}, {Ethereum: adminDeprecated}]);
+  
+      // 2. Expect methods.collectionAdmins == api.rpc.unique.adminlist
+      let adminListEth = await collectionEvm.methods.collectionAdmins().call();
+      adminListEth = adminListEth.map((element: IEthCrossAccountId) => {
+        return helper.address.convertCrossAccountFromEthCrossAccount(element);
+      });
+      expect(adminListRpc).to.be.like(adminListEth);
 
-    // 2. Expect methods.collectionAdmins == api.rpc.unique.adminlist
-    let adminListEth = await collectionEvm.methods.collectionAdmins().call();
-    adminListEth = adminListEth.map((element: IEthCrossAccountId) => {
-      return helper.address.convertCrossAccountFromEthCrossAccount(element);
+      // 3. check isOwnerOrAdminCross returns true:
+      expect(await collectionEvm.methods.isOwnerOrAdminCross(adminCrossSub).call()).to.be.true;
+      expect(await collectionEvm.methods.isOwnerOrAdminCross(adminCrossEth).call()).to.be.true;
+      expect(await collectionEvm.methods.isOwnerOrAdminCross(helper.ethCrossAccount.fromAddress(adminDeprecated)).call()).to.be.true;
     });
-    expect(adminListRpc).to.be.like(adminListEth);
   });
 
   itEth('cross account admin can mint', async ({helper}) => {
modifiedtests/src/vesting.test.tsdiffbeforeafterboth
before · 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 SCHEDULE_1_PERIOD = 4n; // 6 blocks one period36    const SCHEDULE_1_START = currentRelayBlock + 6n; // Block when 1 schedule starts37    const SCHEDULE_2_PERIOD = 8n; // 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.feeFrozen).to.eq(0n);51    expect(balanceSender.miscFrozen).to.eq(0n);52    expect(balanceSender.reserved).to.eq(0n);5354    // check recepient balance after vesting:55    let balanceRecepient = await helper.balance.getSubstrateFull(recepient.address);56    expect(balanceRecepient.free).to.eq(301n * nominal);57    expect(balanceRecepient.feeFrozen).to.eq(300n * nominal);58    expect(balanceRecepient.miscFrozen).to.eq(300n * nominal);59    expect(balanceRecepient.reserved).to.eq(0n);6061    // Schedules list correct:62    expect(schedule).to.has.length(2);63    expect(schedule[0]).to.deep.eq(schedule1);64    expect(schedule[1]).to.deep.eq(schedule2);6566    // Wait first part available:67    await helper.wait.forRelayBlockNumber(SCHEDULE_1_START + SCHEDULE_1_PERIOD);68    await helper.balance.claim(recepient);6970    // check recepient balance after claim (50 tokens claimed, 250 left):71    balanceRecepient = await helper.balance.getSubstrateFull(recepient.address);72    expect(balanceRecepient.free / nominal).to.eq(300n);73    expect(balanceRecepient.feeFrozen).to.eq(250n * nominal);74    expect(balanceRecepient.miscFrozen).to.eq(250n * nominal);75    expect(balanceRecepient.reserved).to.eq(0n);76    77    // Wait first schedule ends and first part od second schedule:78    await helper.wait.forRelayBlockNumber(SCHEDULE_2_START + SCHEDULE_2_PERIOD);79    await helper.balance.claim(recepient);8081    // check recepient balance after second claim (150 tokens claimed, 100 left):82    balanceRecepient = await helper.balance.getSubstrateFull(recepient.address);83    expect(balanceRecepient.free / nominal).to.eq(300n);84    expect(balanceRecepient.feeFrozen).to.eq(100n * nominal);85    expect(balanceRecepient.miscFrozen).to.eq(100n * nominal);86    expect(balanceRecepient.reserved).to.eq(0n);87    88    // Schedules list contain 1 vesting:89    schedule = await helper.balance.getVestingSchedules(recepient.address);90    expect(schedule).to.has.length(1);91    expect(schedule[0]).to.deep.eq(schedule2);9293    // Wait 2 schedule ends:94    await helper.wait.forRelayBlockNumber(SCHEDULE_2_START + SCHEDULE_2_PERIOD * 2n);95    await helper.balance.claim(recepient);9697    // check recepient balance after second claim (100 tokens claimed, 0 left):98    balanceRecepient = await helper.balance.getSubstrateFull(recepient.address);99    expect(balanceRecepient.free / nominal).to.eq(300n);100    expect(balanceRecepient.feeFrozen).to.eq(0n);101    expect(balanceRecepient.miscFrozen).to.eq(0n);102    expect(balanceRecepient.reserved).to.eq(0n);103    104    // check sender balance does not changed:105    balanceSender = await helper.balance.getSubstrateFull(sender.address);106    expect(balanceSender.free / nominal).to.eq(699n);107    expect(balanceSender.feeFrozen).to.eq(0n);108    expect(balanceSender.miscFrozen).to.eq(0n);109    expect(balanceSender.reserved).to.eq(0n);110  });111112  itSub('cannot send more tokens than have', async ({helper}) => {113    const [sender, receiver] = await helper.arrange.createAccounts([1000n, 1n], donor);114    const schedule = {start: 0n, period: 1n, periodCount: 1n, perPeriod: 100n * nominal};115    const manyPeriodsSchedule = {start: 0n, period: 1n, periodCount: 100n, perPeriod: 10n * nominal};116    const oneBigSumSchedule = {start: 0n, period: 1n, periodCount: 1n, perPeriod: 5000n * nominal};117118    // Sender cannot send vestedTransfer to self or other119    await expect(helper.balance.vestedTransfer(sender, sender.address, manyPeriodsSchedule)).to.be.rejectedWith(/InsufficientBalance/);120    await expect(helper.balance.vestedTransfer(sender, receiver.address, manyPeriodsSchedule)).to.be.rejectedWith(/InsufficientBalance/);121    await expect(helper.balance.vestedTransfer(sender, sender.address, oneBigSumSchedule)).to.be.rejectedWith(/InsufficientBalance/);122    await expect(helper.balance.vestedTransfer(sender, receiver.address, oneBigSumSchedule)).to.be.rejectedWith(/InsufficientBalance/);123124    const balanceSender = await helper.balance.getSubstrateFull(sender.address);125    const balanceReceiver = await helper.balance.getSubstrateFull(receiver.address);126127    // Sender's balance has not changed128    expect(balanceSender.free / nominal).to.eq(999n);129    expect(balanceSender.feeFrozen).to.eq(0n);130    expect(balanceSender.miscFrozen).to.eq(0n);131    expect(balanceSender.reserved).to.eq(0n);132133    // Receiver's balance has not changed134    expect(balanceReceiver.free).to.be.eq(1n * nominal);135    expect(balanceReceiver.feeFrozen).to.be.eq(0n);136    expect(balanceReceiver.miscFrozen).to.be.eq(0n);137    expect(balanceReceiver.reserved).to.be.eq(0n);138139    // Receiver cannot send vestedTransfer back because of freeze140    await expect(helper.balance.vestedTransfer(receiver, sender.address, schedule)).to.be.rejectedWith(/InsufficientBalance/);141  });142143  itSub('cannot send vestedTransfer with incorrect parameters', async ({helper}) => {144    const [sender, receiver] = await helper.arrange.createAccounts([1000n, 1n], donor);145    const incorrectperiodSchedule = {start: 0n, period: 0n, periodCount: 10n, perPeriod: 10n * nominal};146    const incorrectPeriodCountSchedule = {start: 0n, period: 1n, periodCount: 0n, perPeriod: 10n * nominal};147    const incorrectPerPeriodSchedule = {start: 0n, period: 1n, periodCount: 1n, perPeriod: 0n * nominal};148149    await expect(helper.balance.vestedTransfer(sender, sender.address, incorrectperiodSchedule)).to.be.rejectedWith(/vesting.ZeroVestingPeriod/);150    await expect(helper.balance.vestedTransfer(sender, receiver.address, incorrectPeriodCountSchedule)).to.be.rejectedWith(/vesting.ZeroVestingPeriod/);151    await expect(helper.balance.vestedTransfer(sender, receiver.address, incorrectPerPeriodSchedule)).to.be.rejectedWith(/vesting.AmountLow/);152153    const balanceSender = await helper.balance.getSubstrateFull(sender.address);154    // Sender's balance has not changed155    expect(balanceSender.free / nominal).to.eq(999n);156    expect(balanceSender.feeFrozen).to.eq(0n);157    expect(balanceSender.miscFrozen).to.eq(0n);158    expect(balanceSender.reserved).to.eq(0n);159  });160});