difftreelog
Merge pull request #799 from UniqueNetwork/tests/eth-helpers
in: master
14 files changed
.envdiffbeforeafterboth--- a/.env
+++ b/.env
@@ -3,21 +3,21 @@
POLKADOT_MAINNET_BRANCH=release-v0.9.30
STATEMINT_BUILD_BRANCH=release-parachains-v9320
-ACALA_BUILD_BRANCH=2.10.1
+ACALA_BUILD_BRANCH=2.11.0
MOONBEAM_BUILD_BRANCH=runtime-1901
-UNIQUE_MAINNET_BRANCH=v930033
+UNIQUE_MAINNET_BRANCH=release-v930033
UNIQUE_REPLICA_FROM=wss://eu-ws.unique.network:443
-KUSAMA_MAINNET_BRANCH=release-v0.9.34
-STATEMINE_BUILD_BRANCH=release-parachains-v9320
-KARURA_BUILD_BRANCH=release-karura-2.10.0
-MOONRIVER_BUILD_BRANCH=runtime-1901
-QUARTZ_MAINNET_BRANCH=v930033
+KUSAMA_MAINNET_BRANCH=release-v0.9.35
+STATEMINE_BUILD_BRANCH=release-parachains-v9330
+KARURA_BUILD_BRANCH=release-karura-2.11.0
+MOONRIVER_BUILD_BRANCH=runtime-2000
+QUARTZ_MAINNET_BRANCH=release-v930034
QUARTZ_REPLICA_FROM=wss://eu-ws-quartz.unique.network:443
UNQWND_MAINNET_BRANCH=release-v0.9.30
WESTMINT_BUILD_BRANCH=parachains-v9330
-OPAL_MAINNET_BRANCH=v930032
+OPAL_MAINNET_BRANCH=release-v930034
OPAL_REPLICA_FROM=wss://eu-ws-opal.unique.network:443
POLKADOT_LAUNCH_BRANCH=unique-network
tests/package.jsondiffbeforeafterboth--- a/tests/package.json
+++ b/tests/package.json
@@ -8,11 +8,13 @@
"@types/chai": "^4.3.3",
"@types/chai-as-promised": "^7.1.5",
"@types/chai-like": "^1.1.1",
+ "@types/chai-subset": "^1.3.3",
"@types/mocha": "^10.0.0",
"@types/node": "^18.11.2",
"@typescript-eslint/eslint-plugin": "^5.40.1",
"@typescript-eslint/parser": "^5.40.1",
"chai": "^4.3.6",
+ "chai-subset": "^1.6.0",
"eslint": "^8.25.0",
"eslint-plugin-mocha": "^10.1.0",
"mocha": "^10.1.0",
tests/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}) => {
tests/src/eth/collectionProperties.test.tsdiffbeforeafterboth--- a/tests/src/eth/collectionProperties.test.ts
+++ b/tests/src/eth/collectionProperties.test.ts
@@ -26,27 +26,36 @@
before(async function() {
await usingEthPlaygrounds(async (_helper, privateKey) => {
donor = await privateKey({filename: __filename});
- [alice] = await _helper.arrange.createAccounts([20n], donor);
+ [alice] = await _helper.arrange.createAccounts([50n], donor);
});
});
// Soft-deprecated: setCollectionProperty
[
- {method: 'setCollectionProperties', methodParams: [[{key: 'testKey1', value: Buffer.from('testValue1')}, {key: 'testKey2', value: Buffer.from('testValue2')}]], expectedProps: [{key: 'testKey1', value: 'testValue1'}, {key: 'testKey2', value: 'testValue2'}]},
- {method: 'setCollectionProperty', methodParams: ['testKey', Buffer.from('testValue')], expectedProps: [{key: 'testKey', value: 'testValue'}]},
+ {method: 'setCollectionProperties', mode: 'nft' as const, methodParams: [[{key: 'testKey1', value: Buffer.from('testValue1')}, {key: 'testKey2', value: Buffer.from('testValue2')}]], expectedProps: [{key: 'testKey1', value: 'testValue1'}, {key: 'testKey2', value: 'testValue2'}]},
+ {method: 'setCollectionProperties', mode: 'rft' as const, methodParams: [[{key: 'testKey1', value: Buffer.from('testValue1')}, {key: 'testKey2', value: Buffer.from('testValue2')}]], expectedProps: [{key: 'testKey1', value: 'testValue1'}, {key: 'testKey2', value: 'testValue2'}]},
+ {method: 'setCollectionProperties', mode: 'ft' as const, methodParams: [[{key: 'testKey1', value: Buffer.from('testValue1')}, {key: 'testKey2', value: Buffer.from('testValue2')}]], expectedProps: [{key: 'testKey1', value: 'testValue1'}, {key: 'testKey2', value: 'testValue2'}]},
+ {method: 'setCollectionProperty', mode: 'nft' as const, methodParams: ['testKey', Buffer.from('testValue')], expectedProps: [{key: 'testKey', value: 'testValue'}]},
].map(testCase =>
- itEth(`Collection properties can be set: ${testCase.method}`, async({helper}) => {
+ itEth.ifWithPallets(`Collection properties can be set: ${testCase.method}() for ${testCase.mode}`, testCase.mode === 'rft' ? [Pallets.ReFungible] : [], async({helper}) => {
const caller = await helper.eth.createAccountWithBalance(donor);
- const collection = await helper.nft.mintCollection(alice, {name: 'name', description: 'test', tokenPrefix: 'test', properties: []});
+ const collection = await helper[testCase.mode].mintCollection(alice, {name: 'name', description: 'test', tokenPrefix: 'test', properties: []});
await collection.addAdmin(alice, {Ethereum: caller});
-
+
const address = helper.ethAddress.fromCollectionId(collection.collectionId);
- const contract = helper.ethNativeContract.collection(address, 'nft', caller, testCase.method === 'setCollectionProperty');
+ const collectionEvm = helper.ethNativeContract.collection(address, 'nft', caller, testCase.method === 'setCollectionProperty');
- await contract.methods[testCase.method](...testCase.methodParams).send({from: caller});
+ // collectionProperties returns an empty array if no properties:
+ expect(await collectionEvm.methods.collectionProperties([]).call()).to.be.like([]);
+ expect(await collectionEvm.methods.collectionProperties(['NonExistingKey']).call()).to.be.like([]);
+
+ await collectionEvm.methods[testCase.method](...testCase.methodParams).send({from: caller});
const raw = (await collection.getData())?.raw;
expect(raw.properties).to.deep.equal(testCase.expectedProps);
+
+ // collectionProperties returns properties:
+ expect(await collectionEvm.methods.collectionProperties([]).call()).to.be.like(testCase.expectedProps.map(prop => helper.ethProperty.property(prop.key, prop.value)));
}));
itEth('Cannot set invalid properties', async({helper}) => {
@@ -68,16 +77,18 @@
// Soft-deprecated: deleteCollectionProperty
[
- {method: 'deleteCollectionProperties', methodParams: [['testKey1', 'testKey2']], expectedProps: [{key: 'testKey3', value: 'testValue3'}]},
- {method: 'deleteCollectionProperty', methodParams: ['testKey1'], expectedProps: [{key: 'testKey2', value: 'testValue2'}, {key: 'testKey3', value: 'testValue3'}]},
+ {method: 'deleteCollectionProperties', mode: 'nft' as const, methodParams: [['testKey1', 'testKey2']], expectedProps: [{key: 'testKey3', value: 'testValue3'}]},
+ {method: 'deleteCollectionProperties', mode: 'rft' as const, methodParams: [['testKey1', 'testKey2']], expectedProps: [{key: 'testKey3', value: 'testValue3'}]},
+ {method: 'deleteCollectionProperties', mode: 'ft' as const, methodParams: [['testKey1', 'testKey2']], expectedProps: [{key: 'testKey3', value: 'testValue3'}]},
+ {method: 'deleteCollectionProperty', mode: 'nft' as const, methodParams: ['testKey1'], expectedProps: [{key: 'testKey2', value: 'testValue2'}, {key: 'testKey3', value: 'testValue3'}]},
].map(testCase =>
- itEth(`Collection properties can be deleted: ${testCase.method}()`, async({helper}) => {
+ itEth.ifWithPallets(`Collection properties can be deleted: ${testCase.method}() for ${testCase.mode}`, testCase.mode === 'rft' ? [Pallets.ReFungible] : [], async({helper}) => {
const properties = [
{key: 'testKey1', value: 'testValue1'},
{key: 'testKey2', value: 'testValue2'},
{key: 'testKey3', value: 'testValue3'}];
const caller = await helper.eth.createAccountWithBalance(donor);
- const collection = await helper.nft.mintCollection(alice, {name: 'name', description: 'test', tokenPrefix: 'test', properties});
+ const collection = await helper[testCase.mode].mintCollection(alice, {name: 'name', description: 'test', tokenPrefix: 'test', properties});
await collection.addAdmin(alice, {Ethereum: caller});
@@ -92,7 +103,6 @@
expect(raw.properties).to.deep.equal(testCase.expectedProps);
}));
-
[
{method: 'deleteCollectionProperties', methodParams: [['testKey2']]},
{method: 'deleteCollectionProperty', methodParams: ['testKey2']},
@@ -207,82 +217,5 @@
await contract.methods.setProperties(tokenId2, [{key: 'URISuffix', value: Buffer.from(SUFFIX)}]).send();
expect(await contract.methods.tokenURI(tokenId2).call()).to.equal(BASE_URI + SUFFIX);
- }));
-});
-
-describe('EVM collection property', () => {
- let donor: IKeyringPair;
-
- before(async function() {
- await usingEthPlaygrounds(async (_helper, privateKey) => {
- donor = await privateKey({filename: __filename});
- });
- });
-
- [
- {case: 'nft' as const},
- {case: 'rft' as const, requiredPallets: [Pallets.ReFungible]},
- {case: 'ft' as const},
- ].map(testCase =>
- itEth.ifWithPallets(`can set/read properties ${testCase.case}`, testCase.requiredPallets || [], async ({helper}) => {
- const collection = await helper[testCase.case].mintCollection(donor, {name: 'A', description: 'B', tokenPrefix: 'C'});
-
- const sender = await helper.eth.createAccountWithBalance(donor, 100n);
- await collection.addAdmin(donor, {Ethereum: sender});
-
- const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);
- const contract = helper.ethNativeContract.collection(collectionAddress, testCase.case, sender);
-
- const keys = ['key0', 'key1'];
-
- const writeProperties = [
- helper.ethProperty.property(keys[0], 'value0'),
- helper.ethProperty.property(keys[1], 'value1'),
- ];
-
- await contract.methods.setCollectionProperties(writeProperties).send();
- const readProperties = await contract.methods.collectionProperties([keys[0], keys[1]]).call();
- expect(readProperties).to.be.like(writeProperties);
- }));
-
- [
- {case: 'nft' as const},
- {case: 'rft' as const, requiredPallets: [Pallets.ReFungible]},
- {case: 'ft' as const},
- ].map(testCase =>
- itEth.ifWithPallets(`can delete properties ${testCase.case}`, testCase.requiredPallets || [], async ({helper}) => {
- const collection = await helper[testCase.case].mintCollection(donor, {name: 'A', description: 'B', tokenPrefix: 'C'});
-
- const sender = await helper.eth.createAccountWithBalance(donor, 100n);
- await collection.addAdmin(donor, {Ethereum: sender});
-
- const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);
- const contract = helper.ethNativeContract.collection(collectionAddress, testCase.case, sender);
-
- const keys = ['key0', 'key1', 'key2', 'key3'];
-
- {
- const writeProperties = [
- helper.ethProperty.property(keys[0], 'value0'),
- helper.ethProperty.property(keys[1], 'value1'),
- helper.ethProperty.property(keys[2], 'value2'),
- helper.ethProperty.property(keys[3], 'value3'),
- ];
-
- await contract.methods.setCollectionProperties(writeProperties).send();
- const readProperties = await contract.methods.collectionProperties([keys[0], keys[1], keys[2], keys[3]]).call();
- expect(readProperties).to.be.like(writeProperties);
- }
-
- {
- const expectProperties = [
- helper.ethProperty.property(keys[0], 'value0'),
- helper.ethProperty.property(keys[1], 'value1'),
- ];
-
- await contract.methods.deleteCollectionProperties([keys[2], keys[3]]).send();
- const readProperties = await contract.methods.collectionProperties([]).call();
- expect(readProperties).to.be.like(expectProperties);
- }
}));
});
tests/src/eth/contractSponsoring.test.tsdiffbeforeafterboth--- a/tests/src/eth/contractSponsoring.test.ts
+++ b/tests/src/eth/contractSponsoring.test.ts
@@ -22,29 +22,31 @@
describe('Sponsoring EVM contracts', () => {
let donor: IKeyringPair;
+ let nominal: bigint;
before(async () => {
- await usingPlaygrounds(async (_helper, privateKey) => {
+ await usingPlaygrounds(async (helper, privateKey) => {
donor = await privateKey({filename: __filename});
+ nominal = helper.balance.getOneTokenNominal();
});
});
- itEth('Self sponsored can be set by the address that deployed the contract', async ({helper}) => {
+ itEth('Self sponsoring can be set by the address that deployed the contract', async ({helper}) => {
const owner = await helper.eth.createAccountWithBalance(donor);
const flipper = await helper.eth.deployFlipper(owner);
const helpers = helper.ethNativeContract.contractHelpers(owner);
+ // 1. owner can set selfSponsoring:
expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
- await expect(helpers.methods.selfSponsoredEnable(flipper.options.address).send()).to.be.not.rejected;
+ const result = await helpers.methods.selfSponsoredEnable(flipper.options.address).send({from: owner});
expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;
- });
- itEth('Set self sponsored events', async ({helper}) => {
- const owner = await helper.eth.createAccountWithBalance(donor);
- const flipper = await helper.eth.deployFlipper(owner);
- const helpers = helper.ethNativeContract.contractHelpers(owner);
-
- const result = await helpers.methods.selfSponsoredEnable(flipper.options.address).send();
+ // 1.1 Can get sponsor using methods.sponsor:
+ const actualSponsor = await helpers.methods.sponsor(flipper.options.address).call();
+ expect(actualSponsor.eth).to.eq(flipper.options.address);
+ expect(actualSponsor.sub).to.eq('0');
+
+ // 2. Events should be:
const ethEvents = helper.eth.helper.eth.normalizeEvents(result.events);
expect(ethEvents).to.be.deep.equal([
{
@@ -66,7 +68,7 @@
]);
});
- itEth('Self sponsored can not be set by the address that did not deployed the contract', async ({helper}) => {
+ itEth('Self sponsoring cannot be set by the address that did not deployed the contract', async ({helper}) => {
const owner = await helper.eth.createAccountWithBalance(donor);
const notOwner = await helper.eth.createAccountWithBalance(donor);
const helpers = helper.ethNativeContract.contractHelpers(owner);
@@ -83,7 +85,7 @@
const flipper = await helper.eth.deployFlipper(owner);
expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.false;
- await expect(helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner})).to.be.not.rejected;
+ await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});
expect(await helpers.methods.sponsoringEnabled(flipper.options.address).call()).to.be.true;
});
@@ -104,18 +106,12 @@
const helpers = helper.ethNativeContract.contractHelpers(owner);
const flipper = await helper.eth.deployFlipper(owner);
+ // 1. owner can set a sponsor:
expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.false;
- await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;
- expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.true;
- });
-
- itEth('Set sponsor event', async ({helper}) => {
- const owner = await helper.eth.createAccountWithBalance(donor);
- const sponsor = await helper.eth.createAccountWithBalance(donor);
- const helpers = helper.ethNativeContract.contractHelpers(owner);
- const flipper = await helper.eth.deployFlipper(owner);
-
const result = await helpers.methods.setSponsor(flipper.options.address, sponsor).send();
+ expect(await helpers.methods.hasPendingSponsor(flipper.options.address).call()).to.be.true;
+
+ // 2. Events should be:
const events = helper.eth.normalizeEvents(result.events);
expect(events).to.be.deep.equal([
{
@@ -129,7 +125,7 @@
]);
});
- itEth('Sponsor can not be set by the address that did not deployed the contract', async ({helper}) => {
+ itEth('Sponsor cannot be set by the address that did not deployed the contract', async ({helper}) => {
const owner = await helper.eth.createAccountWithBalance(donor);
const sponsor = await helper.eth.createAccountWithBalance(donor);
const notOwner = await helper.eth.createAccountWithBalance(donor);
@@ -148,19 +144,18 @@
const flipper = await helper.eth.deployFlipper(owner);
expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
- await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;
- await expect(helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor})).to.be.not.rejected;
+ await helpers.methods.setSponsor(flipper.options.address, sponsor).send();
+
+ // 1. sponsor can confirm sponsorship:
+ const result = await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});
expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;
- });
- itEth('Confirm sponsorship event', async ({helper}) => {
- const owner = await helper.eth.createAccountWithBalance(donor);
- const sponsor = await helper.eth.createAccountWithBalance(donor);
- const helpers = helper.ethNativeContract.contractHelpers(owner);
- const flipper = await helper.eth.deployFlipper(owner);
+ // 1.1 Can get sponsor using methods.sponsor:
+ const actualSponsor = await helpers.methods.sponsor(flipper.options.address).call();
+ expect(actualSponsor.eth).to.eq(sponsor);
+ expect(actualSponsor.sub).to.eq('0');
- await expect(helpers.methods.setSponsor(flipper.options.address, sponsor).send()).to.be.not.rejected;
- const result = await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});
+ // 2. Events should be:
const events = helper.eth.normalizeEvents(result.events);
expect(events).to.be.deep.equal([
{
@@ -196,34 +191,6 @@
expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
await expect(helpers.methods.confirmSponsorship(flipper.options.address).call({from: notSponsor})).to.be.rejectedWith('NoPendingSponsor');
expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
- });
-
- itEth('Get self sponsored sponsor', async ({helper}) => {
- const owner = await helper.eth.createAccountWithBalance(donor);
- const helpers = helper.ethNativeContract.contractHelpers(owner);
- const flipper = await helper.eth.deployFlipper(owner);
-
- await helpers.methods.selfSponsoredEnable(flipper.options.address).send();
-
- const result = await helpers.methods.sponsor(flipper.options.address).call();
-
- expect(result[0]).to.be.eq(flipper.options.address);
- expect(result[1]).to.be.eq('0');
- });
-
- itEth('Get confirmed sponsor', async ({helper}) => {
- const owner = await helper.eth.createAccountWithBalance(donor);
- const sponsor = await helper.eth.createAccountWithBalance(donor);
- const helpers = helper.ethNativeContract.contractHelpers(owner);
- const flipper = await helper.eth.deployFlipper(owner);
-
- await helpers.methods.setSponsor(flipper.options.address, sponsor).send();
- await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});
-
- const result = await helpers.methods.sponsor(flipper.options.address).call();
-
- expect(result[0]).to.be.eq(sponsor);
- expect(result[1]).to.be.eq('0');
});
itEth('Sponsor can be removed by the address that deployed the contract', async ({helper}) => {
@@ -236,21 +203,11 @@
await helpers.methods.setSponsor(flipper.options.address, sponsor).send();
await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});
expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;
-
- await helpers.methods.removeSponsor(flipper.options.address).send();
+ // 1. Can remove sponsor:
+ const result = await helpers.methods.removeSponsor(flipper.options.address).send();
expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.false;
- });
- itEth('Remove sponsor event', async ({helper}) => {
- const owner = await helper.eth.createAccountWithBalance(donor);
- const sponsor = await helper.eth.createAccountWithBalance(donor);
- const helpers = helper.ethNativeContract.contractHelpers(owner);
- const flipper = await helper.eth.deployFlipper(owner);
-
- await helpers.methods.setSponsor(flipper.options.address, sponsor).send();
- await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});
-
- const result = await helpers.methods.removeSponsor(flipper.options.address).send();
+ // 2. Events should be:
const events = helper.eth.normalizeEvents(result.events);
expect(events).to.be.deep.equal([
{
@@ -261,6 +218,11 @@
},
},
]);
+
+ // TODO: why call method reverts?
+ // const actualSponsor = await helpers.methods.sponsor(flipper.options.address).call();
+ // expect(actualSponsor.eth).to.eq(sponsor);
+ // expect(actualSponsor.sub).to.eq('0');
});
itEth('Sponsor can not be removed by the address that did not deployed the contract', async ({helper}) => {
@@ -276,6 +238,7 @@
expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;
await expect(helpers.methods.removeSponsor(flipper.options.address).call({from: notOwner})).to.be.rejectedWith('NoPermission');
+ await expect(helpers.methods.removeSponsor(flipper.options.address).send({from: notOwner})).to.be.rejected;
expect(await helpers.methods.hasSponsor(flipper.options.address).call()).to.be.true;
});
@@ -292,15 +255,15 @@
await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Generous).send({from: owner});
await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});
- const sponsorBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));
- const callerBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));
+ const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));
+ const callerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(caller));
await flipper.methods.flip().send({from: caller});
expect(await flipper.methods.getValue().call()).to.be.true;
// Balance should be taken from sponsor instead of caller
- const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));
- const callerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));
+ const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));
+ const callerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(caller));
expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;
expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);
});
@@ -331,82 +294,67 @@
expect(callerBalanceAfter).to.be.eq(callerBalanceBefore);
});
- itEth('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should decrease (allowlisted)', async ({helper}) => {
- const owner = await helper.eth.createAccountWithBalance(donor);
- const sponsor = await helper.eth.createAccountWithBalance(donor);
- const caller = helper.eth.createAccount();
- const helpers = helper.ethNativeContract.contractHelpers(owner);
- const flipper = await helper.eth.deployFlipper(owner);
-
- await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});
- await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});
-
- await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});
- await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});
-
- await helpers.methods.setSponsor(flipper.options.address, sponsor).send();
- await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});
-
- const sponsorBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));
- expect(sponsorBalanceBefore).to.be.not.equal('0');
-
- await flipper.methods.flip().send({from: caller});
- expect(await flipper.methods.getValue().call()).to.be.true;
-
- // Balance should be taken from flipper instead of caller
- const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));
- expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;
+ [
+ {balance: 0n, label: '0'},
+ {balance: 10n, label: '10'},
+ ].map(testCase => {
+ itEth(`Allow-listed address that has ${testCase.label} UNQ can call a contract. Sponsor balance should decrease`, async ({helper}) => {
+ const owner = await helper.eth.createAccountWithBalance(donor);
+ const sponsor = await helper.eth.createAccountWithBalance(donor);
+ const caller = helper.eth.createAccount();
+ await helper.eth.transferBalanceFromSubstrate(donor, caller, testCase.balance);
+ const helpers = helper.ethNativeContract.contractHelpers(owner);
+ const flipper = await helper.eth.deployFlipper(owner);
+
+ await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});
+ await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});
+
+ await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});
+ await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});
+
+ await helpers.methods.setSponsor(flipper.options.address, sponsor).send();
+ await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});
+
+ const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));
+ expect(sponsorBalanceBefore > 0n).to.be.true;
+
+ await flipper.methods.flip().send({from: caller});
+ expect(await flipper.methods.getValue().call()).to.be.true;
+
+ // Balance should be taken from flipper instead of caller
+ const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));
+ expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;
+ // Caller's balance does not change:
+ const callerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(caller));
+ expect(callerBalanceAfter).to.eq(testCase.balance * nominal);
+ });
});
- itEth('Sponsoring is set, an address that has no UNQ can send a transaction and it works. Sponsor balance should not decrease (non-allowlisted)', async ({helper}) => {
+ itEth('Non-allow-listed address can call a contract. Sponsor balance should not decrease', async ({helper}) => {
const owner = await helper.eth.createAccountWithBalance(donor);
- const caller = await helper.eth.createAccount();
- const helpers = helper.ethNativeContract.contractHelpers(owner);
- const flipper = await helper.eth.deployFlipper(owner);
-
- await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});
- await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});
+ const caller = helper.eth.createAccount();
+ const contractHelpers = helper.ethNativeContract.contractHelpers(owner);
+ // Deploy flipper and send some tokens:
+ const flipper = await helper.eth.deployFlipper(owner);
await helper.eth.transferBalanceFromSubstrate(donor, flipper.options.address);
-
+ expect(await flipper.methods.getValue().call()).to.be.false;
+ // flipper address has some tokens:
const originalFlipperBalance = await helper.balance.getEthereum(flipper.options.address);
- expect(originalFlipperBalance).to.be.not.equal('0');
+ expect(originalFlipperBalance > 0n).to.be.true;
+
+ // Set Allowlisted sponsoring mode. caller is not in allow list:
+ await contractHelpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});
+ await contractHelpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});
+ await contractHelpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});
+ // 1. Caller has no UNQ and is not in allow list. So he cannot flip:
await expect(flipper.methods.flip().send({from: caller})).to.be.rejectedWith(/Returned error: insufficient funds for gas \* price \+ value/);
expect(await flipper.methods.getValue().call()).to.be.false;
- // Balance should be taken from flipper instead of caller
- // FIXME the comment is wrong! What check should be here?
+ // Flipper's balance does not change:
const balanceAfter = await helper.balance.getEthereum(flipper.options.address);
expect(balanceAfter).to.be.equal(originalFlipperBalance);
- });
-
- itEth('Sponsoring is set, an address that has UNQ can send a transaction and it works. User balance should not change', async ({helper}) => {
- const owner = await helper.eth.createAccountWithBalance(donor);
- const sponsor = await helper.eth.createAccountWithBalance(donor);
- const caller = await helper.eth.createAccountWithBalance(donor);
- const helpers = helper.ethNativeContract.contractHelpers(owner);
- const flipper = await helper.eth.deployFlipper(owner);
-
- await helpers.methods.toggleAllowlist(flipper.options.address, true).send({from: owner});
- await helpers.methods.toggleAllowed(flipper.options.address, caller, true).send({from: owner});
-
- await helpers.methods.setSponsoringMode(flipper.options.address, SponsoringMode.Allowlisted).send({from: owner});
- await helpers.methods.setSponsoringRateLimit(flipper.options.address, 0).send({from: owner});
-
- await helpers.methods.setSponsor(flipper.options.address, sponsor).send();
- await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});
-
- const sponsorBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));
- const callerBalanceBefore = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));
-
- await flipper.methods.flip().send({from: caller});
- expect(await flipper.methods.getValue().call()).to.be.true;
-
- const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));
- const callerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(caller));
- expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;
- expect(callerBalanceAfter).to.be.equal(callerBalanceBefore);
});
itEth('Sponsoring is limited, with setContractRateLimit. The limitation is working if transactions are sent more often, the sender pays the commission.', async ({helper}) => {
@@ -427,7 +375,7 @@
await helpers.methods.confirmSponsorship(flipper.options.address).send({from: sponsor});
const originalFlipperBalance = await helper.balance.getEthereum(sponsor);
- expect(originalFlipperBalance).to.be.not.equal('0');
+ expect(originalFlipperBalance > 0n).to.be.true;
await flipper.methods.flip().send({from: caller});
expect(await flipper.methods.getValue().call()).to.be.true;
tests/src/eth/events.test.tsdiffbeforeafterboth--- a/tests/src/eth/events.test.ts
+++ b/tests/src/eth/events.test.ts
@@ -40,7 +40,7 @@
const {collectionAddress, events: ethEvents} = await helper.eth.createCollection(mode, owner, 'A', 'B', 'C');
await helper.wait.newBlocks(1);
{
- expect(ethEvents).to.be.like([
+ expect(ethEvents).to.containSubset([
{
event: 'CollectionCreated',
args: {
@@ -49,21 +49,21 @@
},
},
]);
- expect(subEvents).to.be.like([{method: 'CollectionCreated'}]);
+ expect(subEvents).to.containSubset([{method: 'CollectionCreated'}]);
clearEvents(ethEvents, subEvents);
}
{
const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);
const result = await collectionHelper.methods.destroyCollection(collectionAddress).send({from:owner});
await helper.wait.newBlocks(1);
- expect(result.events).to.be.like({
+ expect(result.events).to.containSubset({
CollectionDestroyed: {
returnValues: {
collectionId: collectionAddress,
},
},
});
- expect(subEvents).to.be.like([{method: 'CollectionDestroyed'}]);
+ expect(subEvents).to.containSubset([{method: 'CollectionDestroyed'}]);
}
unsubscribe();
}
@@ -71,7 +71,7 @@
async function testCollectionPropertySetAndDeleted(helper: EthUniqueHelper, mode: TCollectionMode) {
const owner = await helper.eth.createAccountWithBalance(donor);
const {collectionAddress} = await helper.eth.createCollection(mode, owner, 'A', 'B', 'C');
- const collection = await helper.ethNativeContract.collection(collectionAddress, mode, owner);
+ const collection = helper.ethNativeContract.collection(collectionAddress, mode, owner);
const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);
const ethEvents: any = [];
@@ -82,7 +82,7 @@
{
await collection.methods.setCollectionProperties([{key: 'A', value: [0,1,2,3]}]).send({from:owner});
await helper.wait.newBlocks(1);
- expect(ethEvents).to.be.like([
+ expect(ethEvents).to.containSubset([
{
event: 'CollectionChanged',
returnValues: {
@@ -90,13 +90,13 @@
},
},
]);
- expect(subEvents).to.be.like([{method: 'CollectionPropertySet'}]);
+ expect(subEvents).to.containSubset([{method: 'CollectionPropertySet'}]);
clearEvents(ethEvents, subEvents);
}
{
await collection.methods.deleteCollectionProperties(['A']).send({from:owner});
await helper.wait.newBlocks(1);
- expect(ethEvents).to.be.like([
+ expect(ethEvents).to.containSubset([
{
event: 'CollectionChanged',
returnValues: {
@@ -104,7 +104,7 @@
},
},
]);
- expect(subEvents).to.be.like([{method: 'CollectionPropertyDeleted'}]);
+ expect(subEvents).to.containSubset([{method: 'CollectionPropertyDeleted'}]);
}
unsubscribe();
}
@@ -112,7 +112,7 @@
async function testPropertyPermissionSet(helper: EthUniqueHelper, mode: TCollectionMode) {
const owner = await helper.eth.createAccountWithBalance(donor);
const {collectionAddress} = await helper.eth.createCollection(mode, owner, 'A', 'B', 'C');
- const collection = await helper.ethNativeContract.collection(collectionAddress, mode, owner);
+ const collection = helper.ethNativeContract.collection(collectionAddress, mode, owner);
const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);
const ethEvents: any = [];
collectionHelper.events.allEvents((_: any, event: any) => {
@@ -127,7 +127,7 @@
],
]).send({from: owner});
await helper.wait.newBlocks(1);
- expect(ethEvents).to.be.like([
+ expect(ethEvents).to.containSubset([
{
event: 'CollectionChanged',
returnValues: {
@@ -135,7 +135,7 @@
},
},
]);
- expect(subEvents).to.be.like([{method: 'PropertyPermissionSet'}]);
+ expect(subEvents).to.containSubset([{method: 'PropertyPermissionSet'}]);
unsubscribe();
}
@@ -143,7 +143,7 @@
const owner = await helper.eth.createAccountWithBalance(donor);
const user = helper.ethCrossAccount.createAccount();
const {collectionAddress} = await helper.eth.createCollection(mode, owner, 'A', 'B', 'C');
- const collection = await helper.ethNativeContract.collection(collectionAddress, mode, owner);
+ const collection = helper.ethNativeContract.collection(collectionAddress, mode, owner);
const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);
const ethEvents: any[] = [];
collectionHelper.events.allEvents((_: any, event: any) => {
@@ -154,7 +154,7 @@
{
await collection.methods.addToCollectionAllowListCross(user).send({from: owner});
await helper.wait.newBlocks(1);
- expect(ethEvents).to.be.like([
+ expect(ethEvents).to.containSubset([
{
event: 'CollectionChanged',
returnValues: {
@@ -162,14 +162,13 @@
},
},
]);
- expect(subEvents).to.be.like([{method: 'AllowListAddressAdded'}]);
+ expect(subEvents).to.containSubset([{method: 'AllowListAddressAdded'}]);
clearEvents(ethEvents, subEvents);
}
{
await collection.methods.removeFromCollectionAllowListCross(user).send({from: owner});
await helper.wait.newBlocks(1);
- expect(ethEvents.length).to.be.eq(1);
- expect(ethEvents).to.be.like([
+ expect(ethEvents).to.containSubset([
{
event: 'CollectionChanged',
returnValues: {
@@ -177,7 +176,7 @@
},
},
]);
- expect(subEvents).to.be.like([{method: 'AllowListAddressRemoved'}]);
+ expect(subEvents).to.containSubset([{method: 'AllowListAddressRemoved'}]);
}
unsubscribe();
}
@@ -186,7 +185,7 @@
const owner = await helper.eth.createAccountWithBalance(donor);
const user = helper.ethCrossAccount.createAccount();
const {collectionAddress} = await helper.eth.createCollection(mode, owner, 'A', 'B', 'C');
- const collection = await helper.ethNativeContract.collection(collectionAddress, mode, owner);
+ const collection = helper.ethNativeContract.collection(collectionAddress, mode, owner);
const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);
const ethEvents: any = [];
collectionHelper.events.allEvents((_: any, event: any) => {
@@ -196,7 +195,7 @@
{
await collection.methods.addCollectionAdminCross(user).send({from: owner});
await helper.wait.newBlocks(1);
- expect(ethEvents).to.be.like([
+ expect(ethEvents).to.containSubset([
{
event: 'CollectionChanged',
returnValues: {
@@ -204,13 +203,13 @@
},
},
]);
- expect(subEvents).to.be.like([{method: 'CollectionAdminAdded'}]);
+ expect(subEvents).to.containSubset([{method: 'CollectionAdminAdded'}]);
clearEvents(ethEvents, subEvents);
}
{
await collection.methods.removeCollectionAdminCross(user).send({from: owner});
await helper.wait.newBlocks(1);
- expect(ethEvents).to.be.like([
+ expect(ethEvents).to.containSubset([
{
event: 'CollectionChanged',
returnValues: {
@@ -218,7 +217,7 @@
},
},
]);
- expect(subEvents).to.be.like([{method: 'CollectionAdminRemoved'}]);
+ expect(subEvents).to.containSubset([{method: 'CollectionAdminRemoved'}]);
}
unsubscribe();
}
@@ -226,7 +225,7 @@
async function testCollectionLimitSet(helper: EthUniqueHelper, mode: TCollectionMode) {
const owner = await helper.eth.createAccountWithBalance(donor);
const {collectionAddress} = await helper.eth.createCollection(mode, owner, 'A', 'B', 'C');
- const collection = await helper.ethNativeContract.collection(collectionAddress, mode, owner);
+ const collection = helper.ethNativeContract.collection(collectionAddress, mode, owner);
const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);
const ethEvents: any = [];
collectionHelper.events.allEvents((_: any, event: any) => {
@@ -236,7 +235,7 @@
{
await collection.methods.setCollectionLimit(CollectionLimits.OwnerCanTransfer, true, 0).send({from: owner});
await helper.wait.newBlocks(1);
- expect(ethEvents).to.be.like([
+ expect(ethEvents).to.containSubset([
{
event: 'CollectionChanged',
returnValues: {
@@ -244,7 +243,7 @@
},
},
]);
- expect(subEvents).to.be.like([{method: 'CollectionLimitSet'}]);
+ expect(subEvents).to.containSubset([{method: 'CollectionLimitSet'}]);
}
unsubscribe();
}
@@ -253,7 +252,7 @@
const owner = await helper.eth.createAccountWithBalance(donor);
const newOwner = helper.ethCrossAccount.createAccount();
const {collectionAddress} = await helper.eth.createCollection(mode, owner, 'A', 'B', 'C');
- const collection = await helper.ethNativeContract.collection(collectionAddress, mode, owner);
+ const collection = helper.ethNativeContract.collection(collectionAddress, mode, owner);
const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);
const ethEvents: any = [];
collectionHelper.events.allEvents((_: any, event: any) => {
@@ -263,7 +262,7 @@
{
await collection.methods.changeCollectionOwnerCross(newOwner).send({from: owner});
await helper.wait.newBlocks(1);
- expect(ethEvents).to.be.like([
+ expect(ethEvents).to.containSubset([
{
event: 'CollectionChanged',
returnValues: {
@@ -271,7 +270,7 @@
},
},
]);
- expect(subEvents).to.be.like([{method: 'CollectionOwnerChanged'}]);
+ expect(subEvents).to.containSubset([{method: 'CollectionOwnerChanged'}]);
}
unsubscribe();
}
@@ -279,7 +278,7 @@
async function testCollectionPermissionSet(helper: EthUniqueHelper, mode: TCollectionMode) {
const owner = await helper.eth.createAccountWithBalance(donor);
const {collectionAddress} = await helper.eth.createCollection(mode, owner, 'A', 'B', 'C');
- const collection = await helper.ethNativeContract.collection(collectionAddress, mode, owner);
+ const collection = helper.ethNativeContract.collection(collectionAddress, mode, owner);
const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);
const ethEvents: any = [];
collectionHelper.events.allEvents((_: any, event: any) => {
@@ -289,7 +288,7 @@
{
await collection.methods.setCollectionMintMode(true).send({from: owner});
await helper.wait.newBlocks(1);
- expect(ethEvents).to.be.like([
+ expect(ethEvents).to.containSubset([
{
event: 'CollectionChanged',
returnValues: {
@@ -297,13 +296,13 @@
},
},
]);
- expect(subEvents).to.be.like([{method: 'CollectionPermissionSet'}]);
+ expect(subEvents).to.containSubset([{method: 'CollectionPermissionSet'}]);
clearEvents(ethEvents, subEvents);
}
{
await collection.methods.setCollectionAccess(1).send({from: owner});
await helper.wait.newBlocks(1);
- expect(ethEvents).to.be.like([
+ expect(ethEvents).to.containSubset([
{
event: 'CollectionChanged',
returnValues: {
@@ -311,7 +310,7 @@
},
},
]);
- expect(subEvents).to.be.like([{method: 'CollectionPermissionSet'}]);
+ expect(subEvents).to.containSubset([{method: 'CollectionPermissionSet'}]);
}
unsubscribe();
}
@@ -320,7 +319,7 @@
const owner = await helper.eth.createAccountWithBalance(donor);
const sponsor = await helper.ethCrossAccount.createAccountWithBalance(donor);
const {collectionAddress} = await helper.eth.createCollection(mode, owner, 'A', 'B', 'C');
- const collection = await helper.ethNativeContract.collection(collectionAddress, mode, owner);
+ const collection = helper.ethNativeContract.collection(collectionAddress, mode, owner);
const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);
const ethEvents: any = [];
collectionHelper.events.allEvents((_: any, event: any) => {
@@ -332,21 +331,19 @@
{
await collection.methods.setCollectionSponsorCross(sponsor).send({from: owner});
await helper.wait.newBlocks(1);
- expect(ethEvents).to.be.like([
- {
- event: 'CollectionChanged',
- returnValues: {
- collectionId: collectionAddress,
- },
+ expect(ethEvents).to.containSubset([{
+ event: 'CollectionChanged',
+ returnValues: {
+ collectionId: collectionAddress,
},
- ]);
- expect(subEvents).to.be.like([{method: 'CollectionSponsorSet'}]);
+ }]);
+ expect(subEvents).to.containSubset([{method: 'CollectionSponsorSet'}]);
clearEvents(ethEvents, subEvents);
}
{
await collection.methods.confirmCollectionSponsorship().send({from: sponsor.eth});
await helper.wait.newBlocks(1);
- expect(ethEvents).to.be.like([
+ expect(ethEvents).to.containSubset([
{
event: 'CollectionChanged',
returnValues: {
@@ -354,13 +351,13 @@
},
},
]);
- expect(subEvents).to.be.like([{method: 'SponsorshipConfirmed'}]);
+ expect(subEvents).to.containSubset([{method: 'SponsorshipConfirmed'}]);
clearEvents(ethEvents, subEvents);
}
{
await collection.methods.removeCollectionSponsor().send({from: owner});
await helper.wait.newBlocks(1);
- expect(ethEvents).to.be.like([
+ expect(ethEvents).to.containSubset([
{
event: 'CollectionChanged',
returnValues: {
@@ -368,7 +365,7 @@
},
},
]);
- expect(subEvents).to.be.like([{method: 'CollectionSponsorRemoved'}]);
+ expect(subEvents).to.containSubset([{method: 'CollectionSponsorRemoved'}]);
}
unsubscribe();
}
@@ -376,7 +373,7 @@
async function testTokenPropertySetAndDeleted(helper: EthUniqueHelper, mode: TCollectionMode) {
const owner = await helper.eth.createAccountWithBalance(donor);
const {collectionAddress} = await helper.eth.createCollection(mode, owner, 'A', 'B', 'C');
- const collection = await helper.ethNativeContract.collection(collectionAddress, mode, owner);
+ const collection = helper.ethNativeContract.collection(collectionAddress, mode, owner);
const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);
const result = await collection.methods.mint(owner).send({from: owner});
const tokenId = result.events.Transfer.returnValues.tokenId;
@@ -397,7 +394,7 @@
{
await collection.methods.setProperties(tokenId, [{key: 'A', value: [1,2,3]}]).send({from: owner});
await helper.wait.newBlocks(1);
- expect(ethEvents).to.be.like([
+ expect(ethEvents).to.containSubset([
{
event: 'TokenChanged',
returnValues: {
@@ -405,13 +402,13 @@
},
},
]);
- expect(subEvents).to.be.like([{method: 'TokenPropertySet'}]);
+ expect(subEvents).to.containSubset([{method: 'TokenPropertySet'}]);
clearEvents(ethEvents, subEvents);
}
{
await collection.methods.deleteProperties(tokenId, ['A']).send({from: owner});
await helper.wait.newBlocks(1);
- expect(ethEvents).to.be.like([
+ expect(ethEvents).to.containSubset([
{
event: 'TokenChanged',
returnValues: {
@@ -419,7 +416,7 @@
},
},
]);
- expect(subEvents).to.be.like([{method: 'TokenPropertyDeleted'}]);
+ expect(subEvents).to.containSubset([{method: 'TokenPropertyDeleted'}]);
}
unsubscribe();
}
tests/src/eth/fungible.test.tsdiffbeforeafterboth1// 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 {expect, itEth, usingEthPlaygrounds} from './util';18import {IKeyringPair} from '@polkadot/types/types';1920describe('Fungible: Information getting', () => {21 let donor: IKeyringPair;22 let alice: IKeyringPair;2324 before(async function() {25 await usingEthPlaygrounds(async (helper, privateKey) => {26 donor = await privateKey({filename: __filename});27 [alice] = await helper.arrange.createAccounts([20n], donor);28 });29 });3031 itEth('totalSupply', async ({helper}) => {32 const caller = await helper.eth.createAccountWithBalance(donor);33 const collection = await helper.ft.mintCollection(alice);34 await collection.mint(alice, 200n);3536 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'ft', caller);37 const totalSupply = await contract.methods.totalSupply().call();38 expect(totalSupply).to.equal('200');39 });4041 itEth('balanceOf', async ({helper}) => {42 const caller = await helper.eth.createAccountWithBalance(donor);43 const collection = await helper.ft.mintCollection(alice);44 await collection.mint(alice, 200n, {Ethereum: caller});4546 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'ft', caller);47 const balance = await contract.methods.balanceOf(caller).call();48 expect(balance).to.equal('200');49 });50});5152describe('Fungible: Plain calls', () => {53 let donor: IKeyringPair;54 let alice: IKeyringPair;55 let owner: IKeyringPair;5657 before(async function() {58 await usingEthPlaygrounds(async (helper, privateKey) => {59 donor = await privateKey({filename: __filename});60 [alice, owner] = await helper.arrange.createAccounts([30n, 20n], donor);61 });62 });6364 itEth('Can perform mint()', async ({helper}) => {65 const owner = await helper.eth.createAccountWithBalance(donor);66 const receiver = helper.eth.createAccount();67 const collection = await helper.ft.mintCollection(alice);68 await collection.addAdmin(alice, {Ethereum: owner});6970 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);71 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);7273 const result = await contract.methods.mint(receiver, 100).send();74 75 const event = result.events.Transfer;76 expect(event.address).to.equal(collectionAddress);77 expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');78 expect(event.returnValues.to).to.equal(receiver);79 expect(event.returnValues.value).to.equal('100');80 });81 82 83 itEth('Can perform mintCross()', async ({helper}) => {84 const receiverCross = helper.ethCrossAccount.fromKeyringPair(owner);85 const ethOwner = await helper.eth.createAccountWithBalance(donor);86 const collection = await helper.ft.mintCollection(alice);87 await collection.addAdmin(alice, {Ethereum: ethOwner});8889 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);90 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', ethOwner);9192 const result = await contract.methods.mintCross(receiverCross, 100).send();93 94 const event = result.events.Transfer;95 expect(event.address).to.equal(collectionAddress);96 expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');97 expect(event.returnValues.to).to.equal(helper.address.substrateToEth(owner.address));98 expect(event.returnValues.value).to.equal('100');99 });100101 itEth('Can perform mintBulk()', async ({helper}) => {102 const owner = await helper.eth.createAccountWithBalance(donor);103 const bulkSize = 3;104 const receivers = [...new Array(bulkSize)].map(() => helper.eth.createAccount());105 const collection = await helper.ft.mintCollection(alice);106 await collection.addAdmin(alice, {Ethereum: owner});107108 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);109 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);110111 const result = await contract.methods.mintBulk(Array.from({length: bulkSize}, (_, i) => (112 [receivers[i], (i + 1) * 10]113 ))).send();114 const events = result.events.Transfer.sort((a: any, b: any) => +a.returnValues.value - b.returnValues.value);115 for (let i = 0; i < bulkSize; i++) {116 const event = events[i];117 expect(event.address).to.equal(collectionAddress);118 expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');119 expect(event.returnValues.to).to.equal(receivers[i]);120 expect(event.returnValues.value).to.equal(String(10 * (i + 1)));121 }122 });123124 // Soft-deprecated125 itEth('Can perform burn()', async ({helper}) => {126 const owner = await helper.eth.createAccountWithBalance(donor);127 const receiver = await helper.eth.createAccountWithBalance(donor);128 const collection = await helper.ft.mintCollection(alice);129 await collection.addAdmin(alice, {Ethereum: owner});130131 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);132 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner, true);133 await contract.methods.mint(receiver, 100).send();134135 const result = await contract.methods.burnFrom(receiver, 49).send({from: receiver});136 137 const event = result.events.Transfer;138 expect(event.address).to.equal(collectionAddress);139 expect(event.returnValues.from).to.equal(receiver);140 expect(event.returnValues.to).to.equal('0x0000000000000000000000000000000000000000');141 expect(event.returnValues.value).to.equal('49');142143 const balance = await contract.methods.balanceOf(receiver).call();144 expect(balance).to.equal('51');145 });146147 itEth('Can perform approve()', async ({helper}) => {148 const owner = await helper.eth.createAccountWithBalance(donor);149 const spender = helper.eth.createAccount();150 const collection = await helper.ft.mintCollection(alice);151 await collection.mint(alice, 200n, {Ethereum: owner});152153 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);154 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);155156 {157 const result = await contract.methods.approve(spender, 100).send({from: owner});158159 const event = result.events.Approval;160 expect(event.address).to.be.equal(collectionAddress);161 expect(event.returnValues.owner).to.be.equal(owner);162 expect(event.returnValues.spender).to.be.equal(spender);163 expect(event.returnValues.value).to.be.equal('100');164 }165166 {167 const allowance = await contract.methods.allowance(owner, spender).call();168 expect(+allowance).to.equal(100);169 }170 });171172 itEth('Can perform burnFromCross()', async ({helper}) => {173 const sender = await helper.eth.createAccountWithBalance(donor, 100n);174175 const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);176177 await collection.mint(owner, 200n, {Substrate: owner.address});178 await collection.approveTokens(owner, {Ethereum: sender}, 100n);179180 const address = helper.ethAddress.fromCollectionId(collection.collectionId);181 const contract = helper.ethNativeContract.collection(address, 'ft');182183 const fromBalanceBefore = await collection.getBalance({Substrate: owner.address});184 185 const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);186 const result = await contract.methods.burnFromCross(ownerCross, 49).send({from: sender});187 const events = result.events;188189 expect(events).to.be.like({190 Transfer: {191 address: helper.ethAddress.fromCollectionId(collection.collectionId),192 event: 'Transfer',193 returnValues: {194 from: helper.address.substrateToEth(owner.address),195 to: '0x0000000000000000000000000000000000000000',196 value: '49',197 },198 },199 Approval: {200 address: helper.ethAddress.fromCollectionId(collection.collectionId),201 returnValues: {202 owner: helper.address.substrateToEth(owner.address),203 spender: sender,204 value: '51',205 },206 event: 'Approval',207 },208 });209210 const fromBalanceAfter = await collection.getBalance({Substrate: owner.address});211 expect(fromBalanceBefore - fromBalanceAfter).to.be.eq(49n);212 });213214 itEth('Can perform transferFrom()', async ({helper}) => {215 const owner = await helper.eth.createAccountWithBalance(donor);216 const spender = await helper.eth.createAccountWithBalance(donor);217 const receiver = helper.eth.createAccount();218 const collection = await helper.ft.mintCollection(alice);219 await collection.mint(alice, 200n, {Ethereum: owner});220221 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);222 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);223224 await contract.methods.approve(spender, 100).send();225226 {227 const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: spender});228 229 let event = result.events.Transfer;230 expect(event.address).to.be.equal(collectionAddress);231 expect(event.returnValues.from).to.be.equal(owner);232 expect(event.returnValues.to).to.be.equal(receiver);233 expect(event.returnValues.value).to.be.equal('49');234235 event = result.events.Approval;236 expect(event.address).to.be.equal(collectionAddress);237 expect(event.returnValues.owner).to.be.equal(owner);238 expect(event.returnValues.spender).to.be.equal(spender);239 expect(event.returnValues.value).to.be.equal('51');240 }241242 {243 const balance = await contract.methods.balanceOf(receiver).call();244 expect(+balance).to.equal(49);245 }246247 {248 const balance = await contract.methods.balanceOf(owner).call();249 expect(+balance).to.equal(151);250 }251 });252253 itEth('Can perform transferCross()', async ({helper}) => {254 const sender = await helper.eth.createAccountWithBalance(donor);255 const receiverEth = await helper.eth.createAccountWithBalance(donor);256 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);257 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(donor);258 const collection = await helper.ft.mintCollection(alice);259 await collection.mint(alice, 200n, {Ethereum: sender});260261 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);262 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', sender);263264 {265 // Can transferCross to ethereum address:266 const result = await collectionEvm.methods.transferCross(receiverCrossEth, 50).send({from: sender});267 // Check events:268 const event = result.events.Transfer;269 expect(event.address).to.be.equal(collectionAddress);270 expect(event.returnValues.from).to.be.equal(sender);271 expect(event.returnValues.to).to.be.equal(receiverEth);272 expect(event.returnValues.value).to.be.equal('50');273 // Sender's balance decreased:274 const ownerBalance = await collectionEvm.methods.balanceOf(sender).call();275 expect(+ownerBalance).to.equal(150);276 // Receiver's balance increased:277 const receiverBalance = await collectionEvm.methods.balanceOf(receiverEth).call();278 expect(+receiverBalance).to.equal(50);279 }280 281 {282 // Can transferCross to substrate address:283 const result = await collectionEvm.methods.transferCross(receiverCrossSub, 50).send({from: sender});284 // Check events:285 const event = result.events.Transfer;286 expect(event.address).to.be.equal(collectionAddress);287 expect(event.returnValues.from).to.be.equal(sender);288 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(donor.address));289 expect(event.returnValues.value).to.be.equal('50');290 // Sender's balance decreased:291 const senderBalance = await collection.getBalance({Ethereum: sender});292 expect(senderBalance).to.equal(100n);293 // Receiver's balance increased:294 const balance = await collection.getBalance({Substrate: donor.address});295 expect(balance).to.equal(50n);296 }297 });298299 ['transfer', 'transferCross'].map(testCase => itEth(`Cannot ${testCase} incorrect amount`, async ({helper}) => {300 const sender = await helper.eth.createAccountWithBalance(donor);301 const receiverEth = await helper.eth.createAccountWithBalance(donor);302 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);303 const BALANCE = 200n;304 const BALANCE_TO_TRANSFER = BALANCE + 100n;305306 const collection = await helper.ft.mintCollection(alice);307 await collection.mint(alice, BALANCE, {Ethereum: sender});308 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);309 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', sender);310311 // 1. Cannot transfer more than have312 const receiver = testCase === 'transfer' ? receiverEth : receiverCrossEth;313 await expect(collectionEvm.methods[testCase](receiver, BALANCE_TO_TRANSFER).send({from: sender})).to.be.rejected;314 // 2. Zero transfer allowed (EIP-20):315 await collectionEvm.methods[testCase](receiver, 0n).send({from: sender});316 }));317 318 319 itEth('Can perform transfer()', async ({helper}) => {320 const owner = await helper.eth.createAccountWithBalance(donor);321 const receiver = await helper.eth.createAccountWithBalance(donor);322 const collection = await helper.ft.mintCollection(alice);323 await collection.mint(alice, 200n, {Ethereum: owner});324325 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);326 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);327328 {329 const result = await contract.methods.transfer(receiver, 50).send({from: owner});330 331 const event = result.events.Transfer;332 expect(event.address).to.be.equal(collectionAddress);333 expect(event.returnValues.from).to.be.equal(owner);334 expect(event.returnValues.to).to.be.equal(receiver);335 expect(event.returnValues.value).to.be.equal('50');336 }337338 {339 const balance = await contract.methods.balanceOf(owner).call();340 expect(+balance).to.equal(150);341 }342343 {344 const balance = await contract.methods.balanceOf(receiver).call();345 expect(+balance).to.equal(50);346 }347 });348349 itEth('Can perform transferFromCross()', async ({helper}) => {350 const sender = await helper.eth.createAccountWithBalance(donor, 100n);351352 const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);353354 const receiver = helper.eth.createAccount();355356 await collection.mint(owner, 200n, {Substrate: owner.address});357 await collection.approveTokens(owner, {Ethereum: sender}, 100n);358359 const address = helper.ethAddress.fromCollectionId(collection.collectionId);360 const contract = helper.ethNativeContract.collection(address, 'ft');361362 const from = helper.ethCrossAccount.fromKeyringPair(owner);363 const to = helper.ethCrossAccount.fromAddress(receiver);364365 const fromBalanceBefore = await collection.getBalance({Substrate: owner.address});366 const toBalanceBefore = await collection.getBalance({Ethereum: receiver});367 368 const result = await contract.methods.transferFromCross(from, to, 51).send({from: sender});369370 expect(result.events).to.be.like({371 Transfer: {372 address,373 event: 'Transfer',374 returnValues: {375 from: helper.address.substrateToEth(owner.address),376 to: receiver,377 value: '51',378 },379 },380 Approval: {381 address,382 event: 'Approval',383 returnValues: {384 owner: helper.address.substrateToEth(owner.address),385 spender: sender,386 value: '49',387 },388 }});389390 const fromBalanceAfter = await collection.getBalance({Substrate: owner.address});391 expect(fromBalanceBefore - fromBalanceAfter).to.be.eq(51n);392 const toBalanceAfter = await collection.getBalance({Ethereum: receiver});393 expect(toBalanceAfter - toBalanceBefore).to.be.eq(51n);394 });395});396397describe('Fungible: Fees', () => {398 let donor: IKeyringPair;399 let alice: IKeyringPair;400401 before(async function() {402 await usingEthPlaygrounds(async (helper, privateKey) => {403 donor = await privateKey({filename: __filename});404 [alice] = await helper.arrange.createAccounts([20n], donor);405 });406 });407 408 itEth('approve() call fee is less than 0.2UNQ', async ({helper}) => {409 const owner = await helper.eth.createAccountWithBalance(donor);410 const spender = helper.eth.createAccount();411 const collection = await helper.ft.mintCollection(alice);412 await collection.mint(alice, 200n, {Ethereum: owner});413414 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);415 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);416417 const cost = await helper.eth.recordCallFee(owner, () => contract.methods.approve(spender, 100).send({from: owner}));418 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));419 });420421 itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {422 const owner = await helper.eth.createAccountWithBalance(donor);423 const spender = await helper.eth.createAccountWithBalance(donor);424 const collection = await helper.ft.mintCollection(alice);425 await collection.mint(alice, 200n, {Ethereum: owner});426427 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);428 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);429430 await contract.methods.approve(spender, 100).send({from: owner});431432 const cost = await helper.eth.recordCallFee(spender, () => contract.methods.transferFrom(owner, spender, 100).send({from: spender}));433 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));434 });435436 itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {437 const owner = await helper.eth.createAccountWithBalance(donor);438 const receiver = helper.eth.createAccount();439 const collection = await helper.ft.mintCollection(alice);440 await collection.mint(alice, 200n, {Ethereum: owner});441442 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);443 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);444445 const cost = await helper.eth.recordCallFee(owner, () => contract.methods.transfer(receiver, 100).send({from: owner}));446 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));447 });448});449450describe('Fungible: Substrate calls', () => {451 let donor: IKeyringPair;452 let alice: IKeyringPair;453 let owner: IKeyringPair;454455 before(async function() {456 await usingEthPlaygrounds(async (helper, privateKey) => {457 donor = await privateKey({filename: __filename});458 [alice, owner] = await helper.arrange.createAccounts([20n, 20n], donor);459 });460 });461462 itEth('Events emitted for approve()', async ({helper}) => {463 const receiver = helper.eth.createAccount();464 const collection = await helper.ft.mintCollection(alice);465 await collection.mint(alice, 200n);466467 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);468 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');469470 const events: any = [];471 contract.events.allEvents((_: any, event: any) => {472 events.push(event);473 });474 475 await collection.approveTokens(alice, {Ethereum: receiver}, 100n);476 if (events.length == 0) await helper.wait.newBlocks(1);477 const event = events[0];478479 expect(event.event).to.be.equal('Approval');480 expect(event.address).to.be.equal(collectionAddress);481 expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));482 expect(event.returnValues.spender).to.be.equal(receiver);483 expect(event.returnValues.value).to.be.equal('100');484 });485486 itEth('Events emitted for transferFrom()', async ({helper}) => {487 const [bob] = await helper.arrange.createAccounts([10n], donor);488 const receiver = helper.eth.createAccount();489 const collection = await helper.ft.mintCollection(alice);490 await collection.mint(alice, 200n);491 await collection.approveTokens(alice, {Substrate: bob.address}, 100n);492493 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);494 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');495496 const events: any = [];497 contract.events.allEvents((_: any, event: any) => {498 events.push(event);499 });500501 await collection.transferFrom(bob, {Substrate: alice.address}, {Ethereum: receiver}, 51n);502 if (events.length == 0) await helper.wait.newBlocks(1);503 let event = events[0];504505 expect(event.event).to.be.equal('Transfer');506 expect(event.address).to.be.equal(collectionAddress);507 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));508 expect(event.returnValues.to).to.be.equal(receiver);509 expect(event.returnValues.value).to.be.equal('51');510511 event = events[1];512 expect(event.event).to.be.equal('Approval');513 expect(event.address).to.be.equal(collectionAddress);514 expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));515 expect(event.returnValues.spender).to.be.equal(helper.address.substrateToEth(bob.address));516 expect(event.returnValues.value).to.be.equal('49');517 });518519 itEth('Events emitted for transfer()', async ({helper}) => {520 const receiver = helper.eth.createAccount();521 const collection = await helper.ft.mintCollection(alice);522 await collection.mint(alice, 200n);523524 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);525 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');526527 const events: any = [];528 contract.events.allEvents((_: any, event: any) => {529 events.push(event);530 });531 532 await collection.transfer(alice, {Ethereum:receiver}, 51n);533 if (events.length == 0) await helper.wait.newBlocks(1);534 const event = events[0];535536 expect(event.event).to.be.equal('Transfer');537 expect(event.address).to.be.equal(collectionAddress);538 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));539 expect(event.returnValues.to).to.be.equal(receiver);540 expect(event.returnValues.value).to.be.equal('51');541 });542543 itEth('Events emitted for transferFromCross()', async ({helper}) => {544 const sender = await helper.eth.createAccountWithBalance(donor, 100n);545546 const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);547548 const receiver = helper.eth.createAccount();549550 await collection.mint(owner, 200n, {Substrate: owner.address});551 await collection.approveTokens(owner, {Ethereum: sender}, 100n);552553 const address = helper.ethAddress.fromCollectionId(collection.collectionId);554 const contract = helper.ethNativeContract.collection(address, 'ft');555556 const from = helper.ethCrossAccount.fromKeyringPair(owner);557 const to = helper.ethCrossAccount.fromAddress(receiver);558 559 const result = await contract.methods.transferFromCross(from, to, 51).send({from: sender});560561 expect(result.events).to.be.like({562 Transfer: {563 address,564 event: 'Transfer',565 returnValues: {566 from: helper.address.substrateToEth(owner.address),567 to: receiver,568 value: '51',569 },570 },571 Approval: {572 address,573 event: 'Approval',574 returnValues: {575 owner: helper.address.substrateToEth(owner.address),576 spender: sender,577 value: '49',578 },579 }});580 });581});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 {expect, itEth, usingEthPlaygrounds} from './util';18import {IKeyringPair} from '@polkadot/types/types';1920describe('Fungible: Information getting', () => {21 let donor: IKeyringPair;22 let alice: IKeyringPair;2324 before(async function() {25 await usingEthPlaygrounds(async (helper, privateKey) => {26 donor = await privateKey({filename: __filename});27 [alice] = await helper.arrange.createAccounts([20n], donor);28 });29 });3031 itEth('totalSupply', async ({helper}) => {32 const caller = await helper.eth.createAccountWithBalance(donor);33 const collection = await helper.ft.mintCollection(alice);34 await collection.mint(alice, 200n);3536 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'ft', caller);37 const totalSupply = await contract.methods.totalSupply().call();38 expect(totalSupply).to.equal('200');39 });4041 itEth('balanceOf', async ({helper}) => {42 const caller = await helper.eth.createAccountWithBalance(donor);43 const collection = await helper.ft.mintCollection(alice);44 await collection.mint(alice, 200n, {Ethereum: caller});4546 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'ft', caller);47 const balance = await contract.methods.balanceOf(caller).call();48 expect(balance).to.equal('200');49 });50});5152describe('Fungible: Plain calls', () => {53 let donor: IKeyringPair;54 let alice: IKeyringPair;55 let owner: IKeyringPair;5657 before(async function() {58 await usingEthPlaygrounds(async (helper, privateKey) => {59 donor = await privateKey({filename: __filename});60 [alice, owner] = await helper.arrange.createAccounts([30n, 20n], donor);61 });62 });6364 itEth('Can perform mint()', async ({helper}) => {65 const owner = await helper.eth.createAccountWithBalance(donor);66 const receiver = helper.eth.createAccount();67 const collection = await helper.ft.mintCollection(alice);68 await collection.addAdmin(alice, {Ethereum: owner});6970 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);71 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);7273 const result = await contract.methods.mint(receiver, 100).send();74 75 const event = result.events.Transfer;76 expect(event.address).to.equal(collectionAddress);77 expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');78 expect(event.returnValues.to).to.equal(receiver);79 expect(event.returnValues.value).to.equal('100');80 });81 82 [83 'substrate' as const,84 'ethereum' as const,85 ].map(testCase => {86 itEth(`Can perform mintCross() for ${testCase} address`, async ({helper}) => {87 // 1. Create receiver depending on the test case:88 const receiverEth = helper.eth.createAccount();89 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);90 const receiverSub = owner;91 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(owner);9293 const ethOwner = await helper.eth.createAccountWithBalance(donor);94 const collection = await helper.ft.mintCollection(alice);95 await collection.addAdmin(alice, {Ethereum: ethOwner});96 97 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);98 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', ethOwner);99 100 // 2. Mint tokens:101 const result = await collectionEvm.methods.mintCross(testCase === 'ethereum' ? receiverCrossEth : receiverCrossSub, 100).send();102 103 const event = result.events.Transfer;104 expect(event.address).to.equal(collectionAddress);105 expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');106 expect(event.returnValues.to).to.equal(testCase === 'ethereum' ? receiverEth : helper.address.substrateToEth(receiverSub.address));107 expect(event.returnValues.value).to.equal('100');108109 // 3. Get balance depending on the test case:110 let balance;111 if (testCase === 'ethereum') balance = await collection.getBalance({Ethereum: receiverEth});112 else if (testCase === 'substrate') balance = await collection.getBalance({Substrate: receiverSub.address});113 // 3.1 Check balance:114 expect(balance).to.eq(100n);115 });116 });117118 itEth('Can perform mintBulk()', async ({helper}) => {119 const owner = await helper.eth.createAccountWithBalance(donor);120 const bulkSize = 3;121 const receivers = [...new Array(bulkSize)].map(() => helper.eth.createAccount());122 const collection = await helper.ft.mintCollection(alice);123 await collection.addAdmin(alice, {Ethereum: owner});124125 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);126 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);127128 const result = await contract.methods.mintBulk(Array.from({length: bulkSize}, (_, i) => (129 [receivers[i], (i + 1) * 10]130 ))).send();131 const events = result.events.Transfer.sort((a: any, b: any) => +a.returnValues.value - b.returnValues.value);132 for (let i = 0; i < bulkSize; i++) {133 const event = events[i];134 expect(event.address).to.equal(collectionAddress);135 expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');136 expect(event.returnValues.to).to.equal(receivers[i]);137 expect(event.returnValues.value).to.equal(String(10 * (i + 1)));138 }139 });140141 // Soft-deprecated142 itEth('Can perform burn()', async ({helper}) => {143 const owner = await helper.eth.createAccountWithBalance(donor);144 const receiver = await helper.eth.createAccountWithBalance(donor);145 const collection = await helper.ft.mintCollection(alice);146 await collection.addAdmin(alice, {Ethereum: owner});147148 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);149 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner, true);150 await contract.methods.mint(receiver, 100).send();151152 const result = await contract.methods.burnFrom(receiver, 49).send({from: receiver});153 154 const event = result.events.Transfer;155 expect(event.address).to.equal(collectionAddress);156 expect(event.returnValues.from).to.equal(receiver);157 expect(event.returnValues.to).to.equal('0x0000000000000000000000000000000000000000');158 expect(event.returnValues.value).to.equal('49');159160 const balance = await contract.methods.balanceOf(receiver).call();161 expect(balance).to.equal('51');162 });163164 itEth('Can perform approve()', async ({helper}) => {165 const owner = await helper.eth.createAccountWithBalance(donor);166 const spender = helper.eth.createAccount();167 const collection = await helper.ft.mintCollection(alice);168 await collection.mint(alice, 200n, {Ethereum: owner});169170 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);171 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);172173 {174 const result = await contract.methods.approve(spender, 100).send({from: owner});175176 const event = result.events.Approval;177 expect(event.address).to.be.equal(collectionAddress);178 expect(event.returnValues.owner).to.be.equal(owner);179 expect(event.returnValues.spender).to.be.equal(spender);180 expect(event.returnValues.value).to.be.equal('100');181 }182183 {184 const allowance = await contract.methods.allowance(owner, spender).call();185 expect(+allowance).to.equal(100);186 }187 });188189 itEth('Can perform approveCross()', async ({helper}) => {190 const owner = await helper.eth.createAccountWithBalance(donor);191 const spender = helper.eth.createAccount();192 const spenderSub = (await helper.arrange.createAccounts([1n], donor))[0];193 const spenderCrossEth = helper.ethCrossAccount.fromAddress(spender);194 const spenderCrossSub = helper.ethCrossAccount.fromKeyringPair(spenderSub);195 196197 const collection = await helper.ft.mintCollection(alice);198 await collection.mint(alice, 200n, {Ethereum: owner});199200 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);201 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);202203 {204 const result = await contract.methods.approveCross(spenderCrossEth, 100).send({from: owner});205 const event = result.events.Approval;206 expect(event.address).to.be.equal(collectionAddress);207 expect(event.returnValues.owner).to.be.equal(owner);208 expect(event.returnValues.spender).to.be.equal(spender);209 expect(event.returnValues.value).to.be.equal('100');210 }211212 {213 const allowance = await contract.methods.allowance(owner, spender).call();214 expect(+allowance).to.equal(100);215 }216 217 218 {219 const result = await contract.methods.approveCross(spenderCrossSub, 100).send({from: owner});220 const event = result.events.Approval;221 expect(event.address).to.be.equal(collectionAddress);222 expect(event.returnValues.owner).to.be.equal(owner);223 expect(event.returnValues.spender).to.be.equal(helper.address.substrateToEth(spenderSub.address));224 expect(event.returnValues.value).to.be.equal('100');225 }226227 {228 const allowance = await collection.getApprovedTokens({Ethereum: owner}, {Substrate: spenderSub.address});229 expect(allowance).to.equal(100n);230 }231 232 {233 //TO-DO expect with future allowanceCross(owner, spenderCrossEth).call()234 }235 });236237 itEth('Non-owner and non admin cannot approveCross', async ({helper}) => {238 const nonOwner = await helper.eth.createAccountWithBalance(donor);239 const nonOwnerCross = helper.ethCrossAccount.fromAddress(nonOwner);240 const owner = await helper.eth.createAccountWithBalance(donor);241 const collection = await helper.ft.mintCollection(alice, {name: 'A', description: 'B', tokenPrefix: 'C'});242 await collection.mint(alice, 100n, {Ethereum: owner});243244 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);245 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);246247 await expect(collectionEvm.methods.approveCross(nonOwnerCross, 20).call({from: nonOwner})).to.be.rejectedWith('CantApproveMoreThanOwned');248 });249250251 itEth('Can perform burnFromCross()', async ({helper}) => {252 const sender = await helper.eth.createAccountWithBalance(donor, 100n);253254 const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);255256 await collection.mint(owner, 200n, {Substrate: owner.address});257 await collection.approveTokens(owner, {Ethereum: sender}, 100n);258259 const address = helper.ethAddress.fromCollectionId(collection.collectionId);260 const contract = helper.ethNativeContract.collection(address, 'ft');261262 const fromBalanceBefore = await collection.getBalance({Substrate: owner.address});263 264 const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);265 const result = await contract.methods.burnFromCross(ownerCross, 49).send({from: sender});266 const events = result.events;267268 expect(events).to.be.like({269 Transfer: {270 address: helper.ethAddress.fromCollectionId(collection.collectionId),271 event: 'Transfer',272 returnValues: {273 from: helper.address.substrateToEth(owner.address),274 to: '0x0000000000000000000000000000000000000000',275 value: '49',276 },277 },278 Approval: {279 address: helper.ethAddress.fromCollectionId(collection.collectionId),280 returnValues: {281 owner: helper.address.substrateToEth(owner.address),282 spender: sender,283 value: '51',284 },285 event: 'Approval',286 },287 });288289 const fromBalanceAfter = await collection.getBalance({Substrate: owner.address});290 expect(fromBalanceBefore - fromBalanceAfter).to.be.eq(49n);291 });292293 itEth('Can perform transferFrom()', async ({helper}) => {294 const owner = await helper.eth.createAccountWithBalance(donor);295 const spender = await helper.eth.createAccountWithBalance(donor);296 const receiver = helper.eth.createAccount();297 const collection = await helper.ft.mintCollection(alice);298 await collection.mint(alice, 200n, {Ethereum: owner});299300 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);301 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);302303 await contract.methods.approve(spender, 100).send();304305 {306 const result = await contract.methods.transferFrom(owner, receiver, 49).send({from: spender});307 308 let event = result.events.Transfer;309 expect(event.address).to.be.equal(collectionAddress);310 expect(event.returnValues.from).to.be.equal(owner);311 expect(event.returnValues.to).to.be.equal(receiver);312 expect(event.returnValues.value).to.be.equal('49');313314 event = result.events.Approval;315 expect(event.address).to.be.equal(collectionAddress);316 expect(event.returnValues.owner).to.be.equal(owner);317 expect(event.returnValues.spender).to.be.equal(spender);318 expect(event.returnValues.value).to.be.equal('51');319 }320321 {322 const balance = await contract.methods.balanceOf(receiver).call();323 expect(+balance).to.equal(49);324 }325326 {327 const balance = await contract.methods.balanceOf(owner).call();328 expect(+balance).to.equal(151);329 }330 });331332 itEth('Can perform transferCross()', async ({helper}) => {333 const sender = await helper.eth.createAccountWithBalance(donor);334 const receiverEth = await helper.eth.createAccountWithBalance(donor);335 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);336 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(donor);337 const collection = await helper.ft.mintCollection(alice);338 await collection.mint(alice, 200n, {Ethereum: sender});339340 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);341 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', sender);342343 {344 // Can transferCross to ethereum address:345 const result = await collectionEvm.methods.transferCross(receiverCrossEth, 50).send({from: sender});346 // Check events:347 const event = result.events.Transfer;348 expect(event.address).to.be.equal(collectionAddress);349 expect(event.returnValues.from).to.be.equal(sender);350 expect(event.returnValues.to).to.be.equal(receiverEth);351 expect(event.returnValues.value).to.be.equal('50');352 // Sender's balance decreased:353 const ownerBalance = await collectionEvm.methods.balanceOf(sender).call();354 expect(+ownerBalance).to.equal(150);355 // Receiver's balance increased:356 const receiverBalance = await collectionEvm.methods.balanceOf(receiverEth).call();357 expect(+receiverBalance).to.equal(50);358 }359 360 {361 // Can transferCross to substrate address:362 const result = await collectionEvm.methods.transferCross(receiverCrossSub, 50).send({from: sender});363 // Check events:364 const event = result.events.Transfer;365 expect(event.address).to.be.equal(collectionAddress);366 expect(event.returnValues.from).to.be.equal(sender);367 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(donor.address));368 expect(event.returnValues.value).to.be.equal('50');369 // Sender's balance decreased:370 const senderBalance = await collection.getBalance({Ethereum: sender});371 expect(senderBalance).to.equal(100n);372 // Receiver's balance increased:373 const balance = await collection.getBalance({Substrate: donor.address});374 expect(balance).to.equal(50n);375 }376 });377378 ['transfer', 'transferCross'].map(testCase => itEth(`Cannot ${testCase} incorrect amount`, async ({helper}) => {379 const sender = await helper.eth.createAccountWithBalance(donor);380 const receiverEth = await helper.eth.createAccountWithBalance(donor);381 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);382 const BALANCE = 200n;383 const BALANCE_TO_TRANSFER = BALANCE + 100n;384385 const collection = await helper.ft.mintCollection(alice);386 await collection.mint(alice, BALANCE, {Ethereum: sender});387 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);388 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', sender);389390 // 1. Cannot transfer more than have391 const receiver = testCase === 'transfer' ? receiverEth : receiverCrossEth;392 await expect(collectionEvm.methods[testCase](receiver, BALANCE_TO_TRANSFER).send({from: sender})).to.be.rejected;393 // 2. Zero transfer allowed (EIP-20):394 await collectionEvm.methods[testCase](receiver, 0n).send({from: sender});395 }));396 397 398 itEth('Can perform transfer()', async ({helper}) => {399 const owner = await helper.eth.createAccountWithBalance(donor);400 const receiver = await helper.eth.createAccountWithBalance(donor);401 const collection = await helper.ft.mintCollection(alice);402 await collection.mint(alice, 200n, {Ethereum: owner});403404 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);405 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);406407 {408 const result = await contract.methods.transfer(receiver, 50).send({from: owner});409 410 const event = result.events.Transfer;411 expect(event.address).to.be.equal(collectionAddress);412 expect(event.returnValues.from).to.be.equal(owner);413 expect(event.returnValues.to).to.be.equal(receiver);414 expect(event.returnValues.value).to.be.equal('50');415 }416417 {418 const balance = await contract.methods.balanceOf(owner).call();419 expect(+balance).to.equal(150);420 }421422 {423 const balance = await contract.methods.balanceOf(receiver).call();424 expect(+balance).to.equal(50);425 }426 });427428 itEth('Can perform transferFromCross()', async ({helper}) => {429 const sender = await helper.eth.createAccountWithBalance(donor, 100n);430431 const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);432433 const receiver = helper.eth.createAccount();434435 await collection.mint(owner, 200n, {Substrate: owner.address});436 await collection.approveTokens(owner, {Ethereum: sender}, 100n);437438 const address = helper.ethAddress.fromCollectionId(collection.collectionId);439 const contract = helper.ethNativeContract.collection(address, 'ft');440441 const from = helper.ethCrossAccount.fromKeyringPair(owner);442 const to = helper.ethCrossAccount.fromAddress(receiver);443444 const fromBalanceBefore = await collection.getBalance({Substrate: owner.address});445 const toBalanceBefore = await collection.getBalance({Ethereum: receiver});446 447 const result = await contract.methods.transferFromCross(from, to, 51).send({from: sender});448449 expect(result.events).to.be.like({450 Transfer: {451 address,452 event: 'Transfer',453 returnValues: {454 from: helper.address.substrateToEth(owner.address),455 to: receiver,456 value: '51',457 },458 },459 Approval: {460 address,461 event: 'Approval',462 returnValues: {463 owner: helper.address.substrateToEth(owner.address),464 spender: sender,465 value: '49',466 },467 }});468469 const fromBalanceAfter = await collection.getBalance({Substrate: owner.address});470 expect(fromBalanceBefore - fromBalanceAfter).to.be.eq(51n);471 const toBalanceAfter = await collection.getBalance({Ethereum: receiver});472 expect(toBalanceAfter - toBalanceBefore).to.be.eq(51n);473 });474});475476describe('Fungible: Fees', () => {477 let donor: IKeyringPair;478 let alice: IKeyringPair;479480 before(async function() {481 await usingEthPlaygrounds(async (helper, privateKey) => {482 donor = await privateKey({filename: __filename});483 [alice] = await helper.arrange.createAccounts([20n], donor);484 });485 });486 487 itEth('approve() call fee is less than 0.2UNQ', async ({helper}) => {488 const owner = await helper.eth.createAccountWithBalance(donor);489 const spender = helper.eth.createAccount();490 const collection = await helper.ft.mintCollection(alice);491 await collection.mint(alice, 200n, {Ethereum: owner});492493 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);494 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);495496 const cost = await helper.eth.recordCallFee(owner, () => contract.methods.approve(spender, 100).send({from: owner}));497 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));498 });499500 itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {501 const owner = await helper.eth.createAccountWithBalance(donor);502 const spender = await helper.eth.createAccountWithBalance(donor);503 const collection = await helper.ft.mintCollection(alice);504 await collection.mint(alice, 200n, {Ethereum: owner});505506 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);507 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);508509 await contract.methods.approve(spender, 100).send({from: owner});510511 const cost = await helper.eth.recordCallFee(spender, () => contract.methods.transferFrom(owner, spender, 100).send({from: spender}));512 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));513 });514515 itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {516 const owner = await helper.eth.createAccountWithBalance(donor);517 const receiver = helper.eth.createAccount();518 const collection = await helper.ft.mintCollection(alice);519 await collection.mint(alice, 200n, {Ethereum: owner});520521 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);522 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);523524 const cost = await helper.eth.recordCallFee(owner, () => contract.methods.transfer(receiver, 100).send({from: owner}));525 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));526 });527});528529describe('Fungible: Substrate calls', () => {530 let donor: IKeyringPair;531 let alice: IKeyringPair;532 let owner: IKeyringPair;533534 before(async function() {535 await usingEthPlaygrounds(async (helper, privateKey) => {536 donor = await privateKey({filename: __filename});537 [alice, owner] = await helper.arrange.createAccounts([20n, 20n], donor);538 });539 });540541 itEth('Events emitted for approve()', async ({helper}) => {542 const receiver = helper.eth.createAccount();543 const collection = await helper.ft.mintCollection(alice);544 await collection.mint(alice, 200n);545546 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);547 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');548549 const events: any = [];550 contract.events.allEvents((_: any, event: any) => {551 events.push(event);552 });553 554 await collection.approveTokens(alice, {Ethereum: receiver}, 100n);555 if (events.length == 0) await helper.wait.newBlocks(1);556 const event = events[0];557558 expect(event.event).to.be.equal('Approval');559 expect(event.address).to.be.equal(collectionAddress);560 expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));561 expect(event.returnValues.spender).to.be.equal(receiver);562 expect(event.returnValues.value).to.be.equal('100');563 });564565 itEth('Events emitted for transferFrom()', async ({helper}) => {566 const [bob] = await helper.arrange.createAccounts([10n], donor);567 const receiver = helper.eth.createAccount();568 const collection = await helper.ft.mintCollection(alice);569 await collection.mint(alice, 200n);570 await collection.approveTokens(alice, {Substrate: bob.address}, 100n);571572 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);573 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');574575 const events: any = [];576 contract.events.allEvents((_: any, event: any) => {577 events.push(event);578 });579580 await collection.transferFrom(bob, {Substrate: alice.address}, {Ethereum: receiver}, 51n);581 if (events.length == 0) await helper.wait.newBlocks(1);582 let event = events[0];583584 expect(event.event).to.be.equal('Transfer');585 expect(event.address).to.be.equal(collectionAddress);586 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));587 expect(event.returnValues.to).to.be.equal(receiver);588 expect(event.returnValues.value).to.be.equal('51');589590 event = events[1];591 expect(event.event).to.be.equal('Approval');592 expect(event.address).to.be.equal(collectionAddress);593 expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));594 expect(event.returnValues.spender).to.be.equal(helper.address.substrateToEth(bob.address));595 expect(event.returnValues.value).to.be.equal('49');596 });597598 itEth('Events emitted for transfer()', async ({helper}) => {599 const receiver = helper.eth.createAccount();600 const collection = await helper.ft.mintCollection(alice);601 await collection.mint(alice, 200n);602603 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);604 const contract = helper.ethNativeContract.collection(collectionAddress, 'ft');605606 const events: any = [];607 contract.events.allEvents((_: any, event: any) => {608 events.push(event);609 });610 611 await collection.transfer(alice, {Ethereum:receiver}, 51n);612 if (events.length == 0) await helper.wait.newBlocks(1);613 const event = events[0];614615 expect(event.event).to.be.equal('Transfer');616 expect(event.address).to.be.equal(collectionAddress);617 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));618 expect(event.returnValues.to).to.be.equal(receiver);619 expect(event.returnValues.value).to.be.equal('51');620 });621622 itEth('Events emitted for transferFromCross()', async ({helper}) => {623 const sender = await helper.eth.createAccountWithBalance(donor, 100n);624625 const collection = await helper.ft.mintCollection(owner, {name: 'A', description: 'B', tokenPrefix: 'C'}, 0);626627 const receiver = helper.eth.createAccount();628629 await collection.mint(owner, 200n, {Substrate: owner.address});630 await collection.approveTokens(owner, {Ethereum: sender}, 100n);631632 const address = helper.ethAddress.fromCollectionId(collection.collectionId);633 const contract = helper.ethNativeContract.collection(address, 'ft');634635 const from = helper.ethCrossAccount.fromKeyringPair(owner);636 const to = helper.ethCrossAccount.fromAddress(receiver);637 638 const result = await contract.methods.transferFromCross(from, to, 51).send({from: sender});639640 expect(result.events).to.be.like({641 Transfer: {642 address,643 event: 'Transfer',644 returnValues: {645 from: helper.address.substrateToEth(owner.address),646 to: receiver,647 value: '51',648 },649 },650 Approval: {651 address,652 event: 'Approval',653 returnValues: {654 owner: helper.address.substrateToEth(owner.address),655 spender: sender,656 value: '49',657 },658 }});659 });660});tests/src/eth/nonFungible.test.tsdiffbeforeafterboth--- a/tests/src/eth/nonFungible.test.ts
+++ b/tests/src/eth/nonFungible.test.ts
@@ -175,55 +175,82 @@
// expect(tokenUri).to.be.equal(`https://offchain-service.local/token-info/${nextTokenId}`);
});
- itEth('Can perform mintCross()', async ({helper}) => {
- const caller = await helper.eth.createAccountWithBalance(donor);
- const receiverCross = helper.ethCrossAccount.fromKeyringPair(bob);
- const properties = Array(5).fill(0).map((_, i) => { return {key: `key_${i}`, value: Buffer.from(`value_${i}`)}; });
- const permissions: ITokenPropertyPermission[] = properties
- .map(p => {
- return {
- key: p.key, permission: {
- tokenOwner: true,
- collectionAdmin: true,
- mutable: true,
- },
- };
- });
+ // TODO combine all minting tests in one place
+ [
+ 'substrate' as const,
+ 'ethereum' as const,
+ ].map(testCase => {
+ itEth(`Can perform mintCross() for ${testCase} address`, async ({helper}) => {
+ const collectionAdmin = await helper.eth.createAccountWithBalance(donor);
+
+ const receiverEth = helper.eth.createAccount();
+ const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);
+ const receiverSub = bob;
+ const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(receiverSub);
+
+ // const receiverCross = helper.ethCrossAccount.fromKeyringPair(bob);
+ const properties = Array(5).fill(0).map((_, i) => { return {key: `key_${i}`, value: Buffer.from(`value_${i}`)}; });
+ const permissions: ITokenPropertyPermission[] = properties
+ .map(p => {
+ return {
+ key: p.key, permission: {
+ tokenOwner: false,
+ collectionAdmin: true,
+ mutable: false,
+ },
+ };
+ });
- const collection = await helper.nft.mintCollection(minter, {
- tokenPrefix: 'ethp',
- tokenPropertyPermissions: permissions,
- });
- await collection.addAdmin(minter, {Ethereum: caller});
+ const collection = await helper.nft.mintCollection(minter, {
+ tokenPrefix: 'ethp',
+ tokenPropertyPermissions: permissions,
+ });
+ await collection.addAdmin(minter, {Ethereum: collectionAdmin});
- const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);
- const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', caller, true);
- let expectedTokenId = await contract.methods.nextTokenId().call();
- let result = await contract.methods.mintCross(receiverCross, []).send();
- let tokenId = result.events.Transfer.returnValues.tokenId;
- expect(tokenId).to.be.equal(expectedTokenId);
+ const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);
+ const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', collectionAdmin, true);
+ let expectedTokenId = await contract.methods.nextTokenId().call();
+ let result = await contract.methods.mintCross(testCase === 'ethereum' ? receiverCrossEth : receiverCrossSub, []).send();
+ let tokenId = result.events.Transfer.returnValues.tokenId;
+ expect(tokenId).to.be.equal(expectedTokenId);
- let event = result.events.Transfer;
- expect(event.address).to.be.equal(collectionAddress);
- expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');
- expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(bob.address));
- expect(await contract.methods.properties(tokenId, []).call()).to.be.like([]);
+ let event = result.events.Transfer;
+ expect(event.address).to.be.equal(collectionAddress);
+ expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');
+ expect(event.returnValues.to).to.be.equal(testCase === 'ethereum' ? receiverEth : helper.address.substrateToEth(bob.address));
+ expect(await contract.methods.properties(tokenId, []).call()).to.be.like([]);
- expectedTokenId = await contract.methods.nextTokenId().call();
- result = await contract.methods.mintCross(receiverCross, properties).send();
- event = result.events.Transfer;
- expect(event.address).to.be.equal(collectionAddress);
- expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');
- expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(bob.address));
- expect(await contract.methods.properties(tokenId, []).call()).to.be.like([]);
+ expectedTokenId = await contract.methods.nextTokenId().call();
+ result = await contract.methods.mintCross(testCase === 'ethereum' ? receiverCrossEth : receiverCrossSub, properties).send();
+ event = result.events.Transfer;
+ expect(event.address).to.be.equal(collectionAddress);
+ expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');
+ expect(event.returnValues.to).to.be.equal(testCase === 'ethereum' ? receiverEth : helper.address.substrateToEth(bob.address));
+ expect(await contract.methods.properties(tokenId, []).call()).to.be.like([]);
- tokenId = result.events.Transfer.returnValues.tokenId;
+ tokenId = result.events.Transfer.returnValues.tokenId;
- expect(tokenId).to.be.equal(expectedTokenId);
+ expect(tokenId).to.be.equal(expectedTokenId);
- expect(await contract.methods.properties(tokenId, []).call()).to.be.like(properties
- .map(p => { return helper.ethProperty.property(p.key, p.value.toString()); }));
+ expect(await contract.methods.properties(tokenId, []).call()).to.be.like(properties
+ .map(p => { return helper.ethProperty.property(p.key, p.value.toString()); }));
+
+ expect(await helper.nft.getTokenOwner(collection.collectionId, tokenId))
+ .to.deep.eq(testCase === 'ethereum' ? {Ethereum: receiverEth.toLowerCase()} : {Substrate: receiverSub.address});
+ });
+ });
+
+ itEth('Non-owner and non admin cannot mintCross', async ({helper}) => {
+ const nonOwner = await helper.eth.createAccountWithBalance(donor);
+ const nonOwnerCross = helper.ethCrossAccount.fromAddress(nonOwner);
+
+ const collection = await helper.nft.mintCollection(minter);
+ const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);
+ const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft');
+
+ await expect(collectionEvm.methods.mintCross(nonOwnerCross, []).call({from: nonOwner}))
+ .to.be.rejectedWith('PublicMintingNotAllowed');
});
//TODO: CORE-302 add eth methods
@@ -375,6 +402,8 @@
},
});
}
+
+ expect(await helper.nft.doesTokenExist(collection.collectionId, token.tokenId)).to.be.false;
});
itEth('Can perform transfer with ApprovalForAll', async ({helper}) => {
@@ -455,6 +484,7 @@
expect(await token2.doesExist()).to.be.false;
});
+ // TODO combine all approve tests in one place
itEth('Can perform approveCross()', async ({helper}) => {
// arrange: create accounts
const owner = await helper.eth.createAccountWithBalance(donor, 100n);
@@ -503,6 +533,17 @@
expect(await helper.nft.getTokenOwner(collection.collectionId, token2.tokenId)).to.deep.eq({Ethereum: receiverEth.toLowerCase()});
});
+ itEth('Non-owner and non admin cannot approveCross', async ({helper}) => {
+ const nonOwner = await helper.eth.createAccountWithBalance(donor);
+ const nonOwnerCross = helper.ethCrossAccount.fromAddress(nonOwner);
+ const owner = await helper.eth.createAccountWithBalance(donor);
+ const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});
+ const collectionEvm = helper.ethNativeContract.collection(helper.ethAddress.fromCollectionId(collection.collectionId), 'nft');
+ const token = await collection.mintToken(minter, {Ethereum: owner});
+
+ await expect(collectionEvm.methods.approveCross(nonOwnerCross, token.tokenId).call({from: nonOwner})).to.be.rejectedWith('CantApproveMoreThanOwned');
+ });
+
itEth('Can reaffirm approved address', async ({helper}) => {
const owner = await helper.eth.createAccountWithBalance(donor, 100n);
const ownerCrossEth = helper.ethCrossAccount.fromAddress(owner);
tests/src/eth/reFungible.test.tsdiffbeforeafterboth--- a/tests/src/eth/reFungible.test.ts
+++ b/tests/src/eth/reFungible.test.ts
@@ -137,48 +137,63 @@
expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');
});
- itEth('Can perform mintCross()', async ({helper}) => {
- const caller = await helper.eth.createAccountWithBalance(donor);
- const receiverCross = helper.ethCrossAccount.fromKeyringPair(bob);
- const properties = Array(5).fill(0).map((_, i) => { return {key: `key_${i}`, value: Buffer.from(`value_${i}`)}; });
- const permissions: ITokenPropertyPermission[] = properties.map(p => { return {key: p.key, permission: {tokenOwner: true,
- collectionAdmin: true,
- mutable: true}}; });
+ [
+ 'substrate' as const,
+ 'ethereum' as const,
+ ].map(testCase => {
+ itEth(`Can perform mintCross() for ${testCase} address`, async ({helper}) => {
+ const collectionAdmin = await helper.eth.createAccountWithBalance(donor);
+
+ const receiverEth = helper.eth.createAccount();
+ const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);
+ const receiverSub = bob;
+ const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(receiverSub);
+
+ const properties = Array(5).fill(0).map((_, i) => { return {key: `key_${i}`, value: Buffer.from(`value_${i}`)}; });
+ const permissions: ITokenPropertyPermission[] = properties.map(p => { return {key: p.key, permission: {
+ tokenOwner: false,
+ collectionAdmin: true,
+ mutable: false}};
+ });
- const collection = await helper.rft.mintCollection(minter, {
- tokenPrefix: 'ethp',
- tokenPropertyPermissions: permissions,
- });
- await collection.addAdmin(minter, {Ethereum: caller});
+ const collection = await helper.rft.mintCollection(minter, {
+ tokenPrefix: 'ethp',
+ tokenPropertyPermissions: permissions,
+ });
+ await collection.addAdmin(minter, {Ethereum: collectionAdmin});
- const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);
- const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller, true);
- let expectedTokenId = await contract.methods.nextTokenId().call();
- let result = await contract.methods.mintCross(receiverCross, []).send();
- let tokenId = result.events.Transfer.returnValues.tokenId;
- expect(tokenId).to.be.equal(expectedTokenId);
+ const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);
+ const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', collectionAdmin, true);
+ let expectedTokenId = await contract.methods.nextTokenId().call();
+ let result = await contract.methods.mintCross(testCase === 'ethereum' ? receiverCrossEth : receiverCrossSub, []).send();
+ let tokenId = result.events.Transfer.returnValues.tokenId;
+ expect(tokenId).to.be.equal(expectedTokenId);
- let event = result.events.Transfer;
- expect(event.address).to.be.equal(collectionAddress);
- expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');
- expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(bob.address));
- expect(await contract.methods.properties(tokenId, []).call()).to.be.like([]);
+ let event = result.events.Transfer;
+ expect(event.address).to.be.equal(collectionAddress);
+ expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');
+ expect(event.returnValues.to).to.be.equal(testCase === 'ethereum' ? receiverEth : helper.address.substrateToEth(bob.address));
+ expect(await contract.methods.properties(tokenId, []).call()).to.be.like([]);
- expectedTokenId = await contract.methods.nextTokenId().call();
- result = await contract.methods.mintCross(receiverCross, properties).send();
- event = result.events.Transfer;
- expect(event.address).to.be.equal(collectionAddress);
- expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');
- expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(bob.address));
- expect(await contract.methods.properties(tokenId, []).call()).to.be.like([]);
+ expectedTokenId = await contract.methods.nextTokenId().call();
+ result = await contract.methods.mintCross(testCase === 'ethereum' ? receiverCrossEth : receiverCrossSub, properties).send();
+ event = result.events.Transfer;
+ expect(event.address).to.be.equal(collectionAddress);
+ expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');
+ expect(event.returnValues.to).to.be.equal(testCase === 'ethereum' ? receiverEth : helper.address.substrateToEth(bob.address));
+ expect(await contract.methods.properties(tokenId, []).call()).to.be.like([]);
- tokenId = result.events.Transfer.returnValues.tokenId;
+ tokenId = result.events.Transfer.returnValues.tokenId;
- expect(tokenId).to.be.equal(expectedTokenId);
+ expect(tokenId).to.be.equal(expectedTokenId);
- expect(await contract.methods.properties(tokenId, []).call()).to.be.like(properties
- .map(p => { return helper.ethProperty.property(p.key, p.value.toString()); }));
+ expect(await contract.methods.properties(tokenId, []).call()).to.be.like(properties
+ .map(p => { return helper.ethProperty.property(p.key, p.value.toString()); }));
+
+ expect(await helper.nft.getTokenOwner(collection.collectionId, tokenId))
+ .to.deep.eq(testCase === 'ethereum' ? {Ethereum: receiverEth.toLowerCase()} : {Substrate: receiverSub.address});
+ });
});
itEth.skip('Can perform mintBulk()', async ({helper}) => {
tests/src/eth/reFungibleToken.test.tsdiffbeforeafterboth--- a/tests/src/eth/reFungibleToken.test.ts
+++ b/tests/src/eth/reFungibleToken.test.ts
@@ -207,7 +207,20 @@
//TO-DO expect with future allowanceCross(owner, spenderCrossEth).call()
}
});
-
+
+ itEth('Non-owner and non admin cannot approveCross', async ({helper}) => {
+ const nonOwner = await helper.eth.createAccountWithBalance(donor);
+ const nonOwnerCross = helper.ethCrossAccount.fromAddress(nonOwner);
+ const owner = await helper.eth.createAccountWithBalance(donor);
+ const collection = await helper.rft.mintCollection(alice, {name: 'A', description: 'B', tokenPrefix: 'C'});
+ const token = await collection.mintToken(alice, 100n, {Ethereum: owner});
+
+ const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, token.tokenId);
+ const tokenEvm = helper.ethNativeContract.rftToken(tokenAddress, owner);
+
+ await expect(tokenEvm.methods.approveCross(nonOwnerCross, 20).call({from: nonOwner})).to.be.rejectedWith('CantApproveMoreThanOwned');
+ });
+
[
'transferFrom',
'transferFromCross',
tests/src/eth/tokenProperties.test.tsdiffbeforeafterboth--- a/tests/src/eth/tokenProperties.test.ts
+++ b/tests/src/eth/tokenProperties.test.ts
@@ -37,7 +37,7 @@
{mode: 'nft' as const, requiredPallets: []},
{mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},
].map(testCase =>
- itEth.ifWithPallets(`[${testCase.mode}] Set and get token property permissions`, testCase.requiredPallets, async({helper}) => {
+ itEth.ifWithPallets(`[${testCase.mode}] Can set all possible token property permissions`, testCase.requiredPallets, async({helper}) => {
const owner = await helper.eth.createAccountWithBalance(donor);
const caller = await helper.ethCrossAccount.createAccountWithBalance(donor);
for(const [mutable,collectionAdmin, tokenOwner] of cartesian([], [false, true], [false, true], [false, true])) {
@@ -72,11 +72,11 @@
{mode: 'nft' as const, requiredPallets: []},
{mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},
].map(testCase =>
- itEth.ifWithPallets(`[${testCase.mode}] Set and get multiple token property permissions as owner`, testCase.requiredPallets, async({helper}) => {
+ itEth.ifWithPallets(`[${testCase.mode}] Can set multiple token property permissions as owner`, testCase.requiredPallets, async({helper}) => {
const owner = await helper.eth.createAccountWithBalance(donor);
const {collectionId, collectionAddress} = await helper.eth.createCollection(testCase.mode, owner, 'A', 'B', 'C');
- const collection = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);
+ const collection = helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);
await collection.methods.setTokenPropertyPermissions([
['testKey_0', [
@@ -128,19 +128,18 @@
[EthTokenPermissions.CollectionAdmin.toString(), false]],
],
]);
-
}));
[
{mode: 'nft' as const, requiredPallets: []},
{mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},
].map(testCase =>
- itEth.ifWithPallets(`[${testCase.mode}] Set and get multiple token property permissions as admin`, testCase.requiredPallets, async({helper}) => {
+ itEth.ifWithPallets(`[${testCase.mode}] Can set multiple token property permissions as admin`, testCase.requiredPallets, async({helper}) => {
const owner = await helper.eth.createAccountWithBalance(donor);
const caller = await helper.ethCrossAccount.createAccountWithBalance(donor);
const {collectionId, collectionAddress} = await helper.eth.createCollection(testCase.mode, owner, 'A', 'B', 'C');
- const collection = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);
+ const collection = helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);
await collection.methods.addCollectionAdminCross(caller).send({from: owner});
await collection.methods.setTokenPropertyPermissions([
@@ -452,12 +451,12 @@
{mode: 'nft' as const, requiredPallets: []},
{mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},
].map(testCase =>
- itEth.ifWithPallets(`[${testCase.mode}] Cant set token property permissions as non owner or admin`, testCase.requiredPallets, async({helper}) => {
+ itEth.ifWithPallets(`[${testCase.mode}] Cannot set token property permissions as non owner or admin`, testCase.requiredPallets, async({helper}) => {
const owner = await helper.eth.createAccountWithBalance(donor);
const caller = await helper.eth.createAccountWithBalance(donor);
const {collectionAddress} = await helper.eth.createCollection(testCase.mode, owner, 'A', 'B', 'C');
- const collection = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);
+ const collection = helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);
await expect(collection.methods.setTokenPropertyPermissions([
['testKey_0', [
@@ -472,11 +471,11 @@
{mode: 'nft' as const, requiredPallets: []},
{mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},
].map(testCase =>
- itEth.ifWithPallets(`[${testCase.mode}] Cant set token property permissions with invalid character`, testCase.requiredPallets, async({helper}) => {
+ itEth.ifWithPallets(`[${testCase.mode}] Cannot set token property permissions with invalid character`, testCase.requiredPallets, async({helper}) => {
const owner = await helper.eth.createAccountWithBalance(donor);
const {collectionAddress} = await helper.eth.createCollection(testCase.mode, owner, 'A', 'B', 'C');
- const collection = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);
+ const collection = helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);
await expect(collection.methods.setTokenPropertyPermissions([
// "Space" is invalid character
@@ -487,7 +486,73 @@
],
]).call({from: owner})).to.be.rejectedWith('InvalidCharacterInPropertyKey');
}));
-
+
+ [
+ {mode: 'nft' as const, requiredPallets: []},
+ {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},
+ ].map(testCase =>
+ itEth.ifWithPallets(`[${testCase.mode}] Can reconfigure token property permissions to stricter ones`, testCase.requiredPallets, async({helper}) => {
+ const owner = await helper.eth.createAccountWithBalance(donor);
+
+ const {collectionAddress, collectionId} = await helper.eth.createCollection(testCase.mode, owner, 'A', 'B', 'C');
+ const collection = helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);
+
+ // 1. Owner sets strict property-permissions:
+ await collection.methods.setTokenPropertyPermissions([
+ ['testKey', [
+ [EthTokenPermissions.Mutable, true],
+ [EthTokenPermissions.TokenOwner, true],
+ [EthTokenPermissions.CollectionAdmin, true]],
+ ],
+ ]).send({from: owner});
+
+ // 2. Owner can set stricter property-permissions:
+ for(const values of [[true, true, false], [true, false, false], [false, false, false]]) {
+ await collection.methods.setTokenPropertyPermissions([
+ ['testKey', [
+ [EthTokenPermissions.Mutable, values[0]],
+ [EthTokenPermissions.TokenOwner, values[1]],
+ [EthTokenPermissions.CollectionAdmin, values[2]]],
+ ],
+ ]).send({from: owner});
+ }
+
+ expect(await helper[testCase.mode].getPropertyPermissions(collectionId)).to.be.deep.equal([{
+ key: 'testKey',
+ permission: {mutable: false, collectionAdmin: false, tokenOwner: false},
+ }]);
+ }));
+
+ [
+ {mode: 'nft' as const, requiredPallets: []},
+ {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},
+ ].map(testCase =>
+ itEth.ifWithPallets(`[${testCase.mode}] Cannot reconfigure token property permissions to less strict ones`, testCase.requiredPallets, async({helper}) => {
+ const owner = await helper.eth.createAccountWithBalance(donor);
+
+ const {collectionAddress} = await helper.eth.createCollection(testCase.mode, owner, 'A', 'B', 'C');
+ const collection = helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);
+
+ // 1. Owner sets strict property-permissions:
+ await collection.methods.setTokenPropertyPermissions([
+ ['testKey', [
+ [EthTokenPermissions.Mutable, false],
+ [EthTokenPermissions.TokenOwner, false],
+ [EthTokenPermissions.CollectionAdmin, false]],
+ ],
+ ]).send({from: owner});
+
+ // 2. Owner cannot set less strict property-permissions:
+ for(const values of [[true, false, false], [false, true, false], [false, false, true]]) {
+ await expect(collection.methods.setTokenPropertyPermissions([
+ ['testKey', [
+ [EthTokenPermissions.Mutable, values[0]],
+ [EthTokenPermissions.TokenOwner, values[1]],
+ [EthTokenPermissions.CollectionAdmin, values[2]]],
+ ],
+ ]).call({from: owner})).to.be.rejectedWith('NoPermission');
+ }
+ }));
});
tests/src/util/index.tsdiffbeforeafterboth--- a/tests/src/util/index.ts
+++ b/tests/src/util/index.ts
@@ -6,6 +6,7 @@
import {IKeyringPair} from '@polkadot/types/types/interfaces';
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
+import chaiSubset from 'chai-subset';
import {Context} from 'mocha';
import config from '../config';
import {ChainHelperBase} from './playgrounds/unique';
@@ -13,6 +14,7 @@
import {DevUniqueHelper, SilentLogger, SilentConsole, DevMoonbeamHelper, DevMoonriverHelper, DevAcalaHelper, DevKaruraHelper, DevRelayHelper, DevWestmintHelper} from './playgrounds/unique.dev';
chai.use(chaiAsPromised);
+chai.use(chaiSubset);
export const expect = chai.expect;
const getTestHash = (filename: string) => {
tests/src/vesting.test.tsdiffbeforeafterboth--- a/tests/src/vesting.test.ts
+++ b/tests/src/vesting.test.ts
@@ -32,8 +32,12 @@
// arrange
const [sender, recepient] = await helper.arrange.createAccounts([1000n, 1n], donor);
const currentRelayBlock = await helper.chain.getRelayBlockNumber();
- const schedule1 = {start: currentRelayBlock + 4n, period: 4n, periodCount: 2n, perPeriod: 50n * nominal};
- const schedule2 = {start: currentRelayBlock + 8n, period: 8n, periodCount: 2n, perPeriod: 100n * nominal};
+ const SCHEDULE_1_PERIOD = 4n; // 6 blocks one period
+ const SCHEDULE_1_START = currentRelayBlock + 6n; // Block when 1 schedule starts
+ const SCHEDULE_2_PERIOD = 8n; // 12 blocks one period
+ const SCHEDULE_2_START = currentRelayBlock + 12n; // Block when 2 schedule starts
+ const schedule1 = {start: SCHEDULE_1_START, period: SCHEDULE_1_PERIOD, periodCount: 2n, perPeriod: 50n * nominal};
+ const schedule2 = {start: SCHEDULE_2_START, period: SCHEDULE_2_PERIOD, periodCount: 2n, perPeriod: 100n * nominal};
// act
await helper.balance.vestedTransfer(sender, recepient.address, schedule1);
@@ -59,20 +63,22 @@
expect(schedule[0]).to.deep.eq(schedule1);
expect(schedule[1]).to.deep.eq(schedule2);
- await helper.wait.forRelayBlockNumber(currentRelayBlock + 8n);
+ // Wait first part available:
+ await helper.wait.forRelayBlockNumber(SCHEDULE_1_START + SCHEDULE_1_PERIOD);
await helper.balance.claim(recepient);
- // check recepient balance after claim (50 tokens claimed):
+ // check recepient balance after claim (50 tokens claimed, 250 left):
balanceRecepient = await helper.balance.getSubstrateFull(recepient.address);
expect(balanceRecepient.free / nominal).to.eq(300n);
expect(balanceRecepient.feeFrozen).to.eq(250n * nominal);
expect(balanceRecepient.miscFrozen).to.eq(250n * nominal);
expect(balanceRecepient.reserved).to.eq(0n);
- await helper.wait.forRelayBlockNumber(currentRelayBlock + 16n);
+ // Wait first schedule ends and first part od second schedule:
+ await helper.wait.forRelayBlockNumber(SCHEDULE_2_START + SCHEDULE_2_PERIOD);
await helper.balance.claim(recepient);
- // check recepient balance after second claim (150 tokens claimed):
+ // check recepient balance after second claim (150 tokens claimed, 100 left):
balanceRecepient = await helper.balance.getSubstrateFull(recepient.address);
expect(balanceRecepient.free / nominal).to.eq(300n);
expect(balanceRecepient.feeFrozen).to.eq(100n * nominal);
@@ -84,10 +90,11 @@
expect(schedule).to.has.length(1);
expect(schedule[0]).to.deep.eq(schedule2);
- await helper.wait.forRelayBlockNumber(currentRelayBlock + 24n);
+ // Wait 2 schedule ends:
+ await helper.wait.forRelayBlockNumber(SCHEDULE_2_START + SCHEDULE_2_PERIOD * 2n);
await helper.balance.claim(recepient);
- // check recepient balance after second claim (100 tokens claimed):
+ // check recepient balance after second claim (100 tokens claimed, 0 left):
balanceRecepient = await helper.balance.getSubstrateFull(recepient.address);
expect(balanceRecepient.free / nominal).to.eq(300n);
expect(balanceRecepient.feeFrozen).to.eq(0n);
tests/yarn.lockdiffbeforeafterboth--- a/tests/yarn.lock
+++ b/tests/yarn.lock
@@ -979,6 +979,13 @@
dependencies:
"@types/chai" "*"
+"@types/chai-subset@^1.3.3":
+ version "1.3.3"
+ resolved "https://registry.yarnpkg.com/@types/chai-subset/-/chai-subset-1.3.3.tgz#97893814e92abd2c534de422cb377e0e0bdaac94"
+ integrity sha512-frBecisrNGz+F4T6bcc+NLeolfiojh5FxW2klu669+8BARtyQv2C/GkNW6FUodVe4BroGMP/wER/YDGc7rEllw==
+ dependencies:
+ "@types/chai" "*"
+
"@types/chai@*", "@types/chai@^4.3.3":
version "4.3.4"
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.3.4.tgz#e913e8175db8307d78b4e8fa690408ba6b65dee4"
@@ -1569,6 +1576,11 @@
resolved "https://registry.yarnpkg.com/chai-like/-/chai-like-1.1.1.tgz#8c558a414c34514e814d497c772547ceb7958f64"
integrity sha512-VKa9z/SnhXhkT1zIjtPACFWSoWsqVoaz1Vg+ecrKo5DCKVlgL30F/pEyEvXPBOVwCgLZcWUleCM/C1okaKdTTA==
+chai-subset@^1.6.0:
+ version "1.6.0"
+ resolved "https://registry.yarnpkg.com/chai-subset/-/chai-subset-1.6.0.tgz#a5d0ca14e329a79596ed70058b6646bd6988cfe9"
+ integrity sha512-K3d+KmqdS5XKW5DWPd5sgNffL3uxdDe+6GdnJh3AYPhwnBGRY5urfvfcbRtWIvvpz+KxkL9FeBB6MZewLUNwug==
+
chai@^4.3.6:
version "4.3.7"
resolved "https://registry.yarnpkg.com/chai/-/chai-4.3.7.tgz#ec63f6df01829088e8bf55fca839bcd464a8ec51"