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.tsdiffbeforeafterboth--- a/tests/src/eth/fungible.test.ts
+++ b/tests/src/eth/fungible.test.ts
@@ -79,23 +79,40 @@
expect(event.returnValues.value).to.equal('100');
});
+ [
+ 'substrate' as const,
+ 'ethereum' as const,
+ ].map(testCase => {
+ itEth(`Can perform mintCross() for ${testCase} address`, async ({helper}) => {
+ // 1. Create receiver depending on the test case:
+ const receiverEth = helper.eth.createAccount();
+ const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);
+ const receiverSub = owner;
+ const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(owner);
+
+ const ethOwner = await helper.eth.createAccountWithBalance(donor);
+ const collection = await helper.ft.mintCollection(alice);
+ await collection.addAdmin(alice, {Ethereum: ethOwner});
- itEth('Can perform mintCross()', async ({helper}) => {
- const receiverCross = helper.ethCrossAccount.fromKeyringPair(owner);
- const ethOwner = await helper.eth.createAccountWithBalance(donor);
- const collection = await helper.ft.mintCollection(alice);
- await collection.addAdmin(alice, {Ethereum: ethOwner});
-
- const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);
- const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', ethOwner);
+ const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);
+ const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', ethOwner);
+
+ // 2. Mint tokens:
+ const result = await collectionEvm.methods.mintCross(testCase === 'ethereum' ? receiverCrossEth : receiverCrossSub, 100).send();
+
+ const event = result.events.Transfer;
+ expect(event.address).to.equal(collectionAddress);
+ expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');
+ expect(event.returnValues.to).to.equal(testCase === 'ethereum' ? receiverEth : helper.address.substrateToEth(receiverSub.address));
+ expect(event.returnValues.value).to.equal('100');
- const result = await contract.methods.mintCross(receiverCross, 100).send();
-
- const event = result.events.Transfer;
- expect(event.address).to.equal(collectionAddress);
- expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');
- expect(event.returnValues.to).to.equal(helper.address.substrateToEth(owner.address));
- expect(event.returnValues.value).to.equal('100');
+ // 3. Get balance depending on the test case:
+ let balance;
+ if (testCase === 'ethereum') balance = await collection.getBalance({Ethereum: receiverEth});
+ else if (testCase === 'substrate') balance = await collection.getBalance({Substrate: receiverSub.address});
+ // 3.1 Check balance:
+ expect(balance).to.eq(100n);
+ });
});
itEth('Can perform mintBulk()', async ({helper}) => {
@@ -169,6 +186,68 @@
}
});
+ itEth('Can perform approveCross()', async ({helper}) => {
+ const owner = await helper.eth.createAccountWithBalance(donor);
+ const spender = helper.eth.createAccount();
+ const spenderSub = (await helper.arrange.createAccounts([1n], donor))[0];
+ const spenderCrossEth = helper.ethCrossAccount.fromAddress(spender);
+ const spenderCrossSub = helper.ethCrossAccount.fromKeyringPair(spenderSub);
+
+
+ const collection = await helper.ft.mintCollection(alice);
+ await collection.mint(alice, 200n, {Ethereum: owner});
+
+ const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);
+ const contract = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);
+
+ {
+ const result = await contract.methods.approveCross(spenderCrossEth, 100).send({from: owner});
+ const event = result.events.Approval;
+ expect(event.address).to.be.equal(collectionAddress);
+ expect(event.returnValues.owner).to.be.equal(owner);
+ expect(event.returnValues.spender).to.be.equal(spender);
+ expect(event.returnValues.value).to.be.equal('100');
+ }
+
+ {
+ const allowance = await contract.methods.allowance(owner, spender).call();
+ expect(+allowance).to.equal(100);
+ }
+
+
+ {
+ const result = await contract.methods.approveCross(spenderCrossSub, 100).send({from: owner});
+ const event = result.events.Approval;
+ expect(event.address).to.be.equal(collectionAddress);
+ expect(event.returnValues.owner).to.be.equal(owner);
+ expect(event.returnValues.spender).to.be.equal(helper.address.substrateToEth(spenderSub.address));
+ expect(event.returnValues.value).to.be.equal('100');
+ }
+
+ {
+ const allowance = await collection.getApprovedTokens({Ethereum: owner}, {Substrate: spenderSub.address});
+ expect(allowance).to.equal(100n);
+ }
+
+ {
+ //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.ft.mintCollection(alice, {name: 'A', description: 'B', tokenPrefix: 'C'});
+ await collection.mint(alice, 100n, {Ethereum: owner});
+
+ const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);
+ const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', owner);
+
+ await expect(collectionEvm.methods.approveCross(nonOwnerCross, 20).call({from: nonOwner})).to.be.rejectedWith('CantApproveMoreThanOwned');
+ });
+
+
itEth('Can perform burnFromCross()', async ({helper}) => {
const sender = await helper.eth.createAccountWithBalance(donor, 100n);
tests/src/eth/nonFungible.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 {itEth, usingEthPlaygrounds, expect, EthUniqueHelper} from './util';18import {IKeyringPair} from '@polkadot/types/types';19import {Contract} from 'web3-eth-contract';20import {ITokenPropertyPermission} from '../util/playgrounds/types';212223describe('NFT: Information getting', () => {24 let donor: IKeyringPair;25 let alice: IKeyringPair;2627 before(async function() {28 await usingEthPlaygrounds(async (helper, privateKey) => {29 donor = await privateKey({filename: __filename});30 [alice] = await helper.arrange.createAccounts([10n], donor);31 });32 });3334 itEth('totalSupply', async ({helper}) => {35 const collection = await helper.nft.mintCollection(alice, {});36 await collection.mintToken(alice);3738 const caller = await helper.eth.createAccountWithBalance(donor);3940 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);41 const totalSupply = await contract.methods.totalSupply().call();4243 expect(totalSupply).to.equal('1');44 });4546 itEth('balanceOf', async ({helper}) => {47 const collection = await helper.nft.mintCollection(alice, {});48 const caller = await helper.eth.createAccountWithBalance(donor);4950 await collection.mintToken(alice, {Ethereum: caller});51 await collection.mintToken(alice, {Ethereum: caller});52 await collection.mintToken(alice, {Ethereum: caller});5354 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);55 const balance = await contract.methods.balanceOf(caller).call();5657 expect(balance).to.equal('3');58 });5960 itEth('ownerOf', async ({helper}) => {61 const collection = await helper.nft.mintCollection(alice, {});62 const caller = await helper.eth.createAccountWithBalance(donor);6364 const token = await collection.mintToken(alice, {Ethereum: caller});6566 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);6768 const owner = await contract.methods.ownerOf(token.tokenId).call();6970 expect(owner).to.equal(caller);71 });7273 itEth('name/symbol is available regardless of ERC721Metadata support', async ({helper}) => {74 const collection = await helper.nft.mintCollection(alice, {name: 'test', tokenPrefix: 'TEST'});75 const caller = helper.eth.createAccount();7677 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);7879 expect(await contract.methods.name().call()).to.equal('test');80 expect(await contract.methods.symbol().call()).to.equal('TEST');81 });82});8384describe('Check ERC721 token URI for NFT', () => {85 let donor: IKeyringPair;8687 before(async function() {88 await usingEthPlaygrounds(async (_helper, privateKey) => {89 donor = await privateKey({filename: __filename});90 });91 });9293 async function setup(helper: EthUniqueHelper, baseUri: string, propertyKey?: string, propertyValue?: string): Promise<{contract: Contract, nextTokenId: string}> {94 const owner = await helper.eth.createAccountWithBalance(donor);95 const receiver = helper.eth.createAccount();9697 const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Mint collection', 'a', 'b', baseUri);98 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);99100 const result = await contract.methods.mint(receiver).send();101 const tokenId = result.events.Transfer.returnValues.tokenId;102 expect(tokenId).to.be.equal('1');103104 if (propertyKey && propertyValue) {105 // Set URL or suffix106 await contract.methods.setProperties(tokenId, [{key: propertyKey, value: Buffer.from(propertyValue)}]).send();107 }108109 const event = result.events.Transfer;110 expect(event.address).to.be.equal(collectionAddress);111 expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');112 expect(event.returnValues.to).to.be.equal(receiver);113 expect(event.returnValues.tokenId).to.be.equal(tokenId);114115 return {contract, nextTokenId: tokenId};116 }117118 itEth('Empty tokenURI', async ({helper}) => {119 const {contract, nextTokenId} = await setup(helper, '');120 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('');121 });122123 itEth('TokenURI from url', async ({helper}) => {124 const {contract, nextTokenId} = await setup(helper, 'BaseURI_', 'URI', 'Token URI');125 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Token URI');126 });127128 itEth('TokenURI from baseURI', async ({helper}) => {129 const {contract, nextTokenId} = await setup(helper, 'BaseURI_');130 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_');131 });132133 itEth('TokenURI from baseURI + suffix', async ({helper}) => {134 const suffix = '/some/suffix';135 const {contract, nextTokenId} = await setup(helper, 'BaseURI_', 'URISuffix', suffix);136 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_' + suffix);137 });138});139140describe('NFT: Plain calls', () => {141 let donor: IKeyringPair;142 let minter: IKeyringPair;143 let bob: IKeyringPair;144 let charlie: IKeyringPair;145146 before(async function() {147 await usingEthPlaygrounds(async (helper, privateKey) => {148 donor = await privateKey({filename: __filename});149 [minter, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);150 });151 });152153 itEth('Can perform mint() & get crossOwner()', async ({helper}) => {154 const owner = await helper.eth.createAccountWithBalance(donor);155 const receiver = helper.eth.createAccount();156157 const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Mint collection', '6', '6', '');158 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);159160 const result = await contract.methods.mintWithTokenURI(receiver, 'Test URI').send();161 const tokenId = result.events.Transfer.returnValues.tokenId;162 expect(tokenId).to.be.equal('1');163164 const event = result.events.Transfer;165 expect(event.address).to.be.equal(collectionAddress);166 expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');167 expect(event.returnValues.to).to.be.equal(receiver);168169 expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');170 console.log(await contract.methods.crossOwnerOf(tokenId).call());171 expect(await contract.methods.crossOwnerOf(tokenId).call()).to.be.like([receiver, '0']);172 // TODO: this wont work right now, need release 919000 first173 // await helper.methods.setOffchainSchema(collectionIdAddress, 'https://offchain-service.local/token-info/{id}').send();174 // const tokenUri = await contract.methods.tokenURI(nextTokenId).call();175 // expect(tokenUri).to.be.equal(`https://offchain-service.local/token-info/${nextTokenId}`);176 });177 178 itEth('Can perform mintCross()', async ({helper}) => {179 const caller = await helper.eth.createAccountWithBalance(donor);180 const receiverCross = helper.ethCrossAccount.fromKeyringPair(bob);181 const properties = Array(5).fill(0).map((_, i) => { return {key: `key_${i}`, value: Buffer.from(`value_${i}`)}; });182 const permissions: ITokenPropertyPermission[] = properties183 .map(p => {184 return {185 key: p.key, permission: {186 tokenOwner: true,187 collectionAdmin: true,188 mutable: true,189 },190 };191 });192 193 194 const collection = await helper.nft.mintCollection(minter, {195 tokenPrefix: 'ethp',196 tokenPropertyPermissions: permissions,197 });198 await collection.addAdmin(minter, {Ethereum: caller});199 200 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);201 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', caller, true);202 let expectedTokenId = await contract.methods.nextTokenId().call();203 let result = await contract.methods.mintCross(receiverCross, []).send();204 let tokenId = result.events.Transfer.returnValues.tokenId;205 expect(tokenId).to.be.equal(expectedTokenId);206207 let event = result.events.Transfer;208 expect(event.address).to.be.equal(collectionAddress);209 expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');210 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(bob.address));211 expect(await contract.methods.properties(tokenId, []).call()).to.be.like([]);212 213 expectedTokenId = await contract.methods.nextTokenId().call();214 result = await contract.methods.mintCross(receiverCross, properties).send();215 event = result.events.Transfer;216 expect(event.address).to.be.equal(collectionAddress);217 expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');218 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(bob.address));219 expect(await contract.methods.properties(tokenId, []).call()).to.be.like([]);220 221 tokenId = result.events.Transfer.returnValues.tokenId;222 223 expect(tokenId).to.be.equal(expectedTokenId);224225 expect(await contract.methods.properties(tokenId, []).call()).to.be.like(properties226 .map(p => { return helper.ethProperty.property(p.key, p.value.toString()); }));227 });228 229 //TODO: CORE-302 add eth methods230 itEth.skip('Can perform mintBulk()', async ({helper}) => {231 const caller = await helper.eth.createAccountWithBalance(donor);232 const receiver = helper.eth.createAccount();233234 const collection = await helper.nft.mintCollection(minter);235 await collection.addAdmin(minter, {Ethereum: caller});236237 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);238 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', caller);239 {240 const bulkSize = 3;241 const nextTokenId = await contract.methods.nextTokenId().call();242 expect(nextTokenId).to.be.equal('1');243 const result = await contract.methods.mintBulkWithTokenURI(244 receiver,245 Array.from({length: bulkSize}, (_, i) => (246 [+nextTokenId + i, `Test URI ${i}`]247 )),248 ).send({from: caller});249250 const events = result.events.Transfer.sort((a: any, b: any) => +a.returnValues.tokenId - b.returnValues.tokenId);251 for (let i = 0; i < bulkSize; i++) {252 const event = events[i];253 expect(event.address).to.equal(collectionAddress);254 expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');255 expect(event.returnValues.to).to.equal(receiver);256 expect(event.returnValues.tokenId).to.equal(`${+nextTokenId+i}`);257258 expect(await contract.methods.tokenURI(+nextTokenId + i).call()).to.be.equal(`Test URI ${i}`);259 }260 }261 });262263 itEth('Can perform burn()', async ({helper}) => {264 const caller = await helper.eth.createAccountWithBalance(donor);265266 const collection = await helper.nft.mintCollection(minter, {});267 const {tokenId} = await collection.mintToken(minter, {Ethereum: caller});268269 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);270 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', caller);271272 {273 const result = await contract.methods.burn(tokenId).send({from: caller});274275 const event = result.events.Transfer;276 expect(event.address).to.be.equal(collectionAddress);277 expect(event.returnValues.from).to.be.equal(caller);278 expect(event.returnValues.to).to.be.equal('0x0000000000000000000000000000000000000000');279 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);280 }281 });282283 itEth('Can perform approve()', async ({helper}) => {284 const owner = await helper.eth.createAccountWithBalance(donor);285 const spender = helper.eth.createAccount();286287 const collection = await helper.nft.mintCollection(minter, {});288 const {tokenId} = await collection.mintToken(minter, {Ethereum: owner});289290 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);291 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);292293 {294 const result = await contract.methods.approve(spender, tokenId).send({from: owner});295296 const event = result.events.Approval;297 expect(event.address).to.be.equal(collectionAddress);298 expect(event.returnValues.owner).to.be.equal(owner);299 expect(event.returnValues.approved).to.be.equal(spender);300 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);301 }302 });303304 itEth('Can perform setApprovalForAll()', async ({helper}) => {305 const owner = await helper.eth.createAccountWithBalance(donor);306 const operator = helper.eth.createAccount();307308 const collection = await helper.nft.mintCollection(minter, {});309310 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);311 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);312313 const approvedBefore = await contract.methods.isApprovedForAll(owner, operator).call();314 expect(approvedBefore).to.be.equal(false);315316 {317 const result = await contract.methods.setApprovalForAll(operator, true).send({from: owner});318319 expect(result.events.ApprovalForAll).to.be.like({320 address: collectionAddress,321 event: 'ApprovalForAll',322 returnValues: {323 owner,324 operator,325 approved: true,326 },327 });328329 const approvedAfter = await contract.methods.isApprovedForAll(owner, operator).call();330 expect(approvedAfter).to.be.equal(true);331 }332333 {334 const result = await contract.methods.setApprovalForAll(operator, false).send({from: owner});335336 expect(result.events.ApprovalForAll).to.be.like({337 address: collectionAddress,338 event: 'ApprovalForAll',339 returnValues: {340 owner,341 operator,342 approved: false,343 },344 });345346 const approvedAfter = await contract.methods.isApprovedForAll(owner, operator).call();347 expect(approvedAfter).to.be.equal(false);348 }349 });350351 itEth('Can perform burn with ApprovalForAll', async ({helper}) => {352 const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});353354 const owner = await helper.eth.createAccountWithBalance(donor);355 const operator = await helper.eth.createAccountWithBalance(donor, 100n);356357 const token = await collection.mintToken(minter, {Ethereum: owner});358359 const address = helper.ethAddress.fromCollectionId(collection.collectionId);360 const contract = helper.ethNativeContract.collection(address, 'nft');361362 {363 await contract.methods.setApprovalForAll(operator, true).send({from: owner});364 const ownerCross = helper.ethCrossAccount.fromAddress(owner);365 const result = await contract.methods.burnFromCross(ownerCross, token.tokenId).send({from: operator});366 const events = result.events.Transfer;367368 expect(events).to.be.like({369 address,370 event: 'Transfer',371 returnValues: {372 from: owner,373 to: '0x0000000000000000000000000000000000000000',374 tokenId: token.tokenId.toString(),375 },376 });377 }378 });379 380 itEth('Can perform transfer with ApprovalForAll', async ({helper}) => {381 const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});382383 const owner = await helper.eth.createAccountWithBalance(donor);384 const operator = await helper.eth.createAccountWithBalance(donor);385 const receiver = charlie;386387 const token = await collection.mintToken(minter, {Ethereum: owner});388389 const address = helper.ethAddress.fromCollectionId(collection.collectionId);390 const contract = helper.ethNativeContract.collection(address, 'nft');391392 {393 await contract.methods.setApprovalForAll(operator, true).send({from: owner});394 const ownerCross = helper.ethCrossAccount.fromAddress(owner);395 const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);396 const result = await contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: operator});397 const event = result.events.Transfer;398 expect(event).to.be.like({399 address: helper.ethAddress.fromCollectionId(collection.collectionId),400 event: 'Transfer',401 returnValues: {402 from: owner,403 to: helper.address.substrateToEth(receiver.address),404 tokenId: token.tokenId.toString(),405 },406 });407 }408409 expect(await token.getOwner()).to.be.like({Substrate: receiver.address});410 });411412 itEth('Can perform burnFromCross()', async ({helper}) => {413 const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});414 const ownerSub = bob;415 const ownerCrossSub = helper.ethCrossAccount.fromKeyringPair(ownerSub);416 const ownerEth = await helper.eth.createAccountWithBalance(donor, 100n);417 const ownerCrossEth = helper.ethCrossAccount.fromAddress(ownerEth);418419 const burnerEth = await helper.eth.createAccountWithBalance(donor, 100n);420 const burnerCrossEth = helper.ethCrossAccount.fromAddress(burnerEth);421422 const token1 = await collection.mintToken(minter, {Substrate: ownerSub.address});423 const token2 = await collection.mintToken(minter, {Ethereum: ownerEth});424425 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);426 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft');427428 // Approve tokens from substrate and ethereum:429 await token1.approve(ownerSub, {Ethereum: burnerEth});430 await collectionEvm.methods.approveCross(burnerCrossEth, token2.tokenId).send({from: ownerEth});431432 // can burnFromCross:433 const result1 = await collectionEvm.methods.burnFromCross(ownerCrossSub, token1.tokenId).send({from: burnerEth});434 const result2 = await collectionEvm.methods.burnFromCross(ownerCrossEth, token2.tokenId).send({from: burnerEth});435 const events1 = result1.events.Transfer;436 const events2 = result2.events.Transfer;437438 // Check events for burnFromCross (substrate and ethereum):439 [440 [events1, token1, helper.address.substrateToEth(ownerSub.address)], 441 [events2, token2, ownerEth],442 ].map(burnData => {443 expect(burnData[0]).to.be.like({444 address: collectionAddress,445 event: 'Transfer',446 returnValues: {447 from: burnData[2],448 to: '0x0000000000000000000000000000000000000000',449 tokenId: burnData[1].tokenId.toString(),450 },451 });452 });453454 expect(await token1.doesExist()).to.be.false;455 expect(await token2.doesExist()).to.be.false;456 });457458 itEth('Can perform approveCross()', async ({helper}) => {459 // arrange: create accounts460 const owner = await helper.eth.createAccountWithBalance(donor, 100n);461 const ownerCross = helper.ethCrossAccount.fromAddress(owner);462 const receiverSub = charlie;463 const recieverCrossSub = helper.ethCrossAccount.fromKeyringPair(receiverSub);464 const receiverEth = await helper.eth.createAccountWithBalance(donor, 100n);465 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);466467 // arrange: create collection and tokens:468 const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});469 const token1 = await collection.mintToken(minter, {Ethereum: owner});470 const token2 = await collection.mintToken(minter, {Ethereum: owner});471472 const collectionEvm = helper.ethNativeContract.collection(helper.ethAddress.fromCollectionId(collection.collectionId), 'nft');473474 // Can approveCross substrate and ethereum address:475 const resultSub = await collectionEvm.methods.approveCross(recieverCrossSub, token1.tokenId).send({from: owner});476 const resultEth = await collectionEvm.methods.approveCross(receiverCrossEth, token2.tokenId).send({from: owner});477 const eventSub = resultSub.events.Approval;478 const eventEth = resultEth.events.Approval;479 expect(eventSub).to.be.like({480 address: helper.ethAddress.fromCollectionId(collection.collectionId),481 event: 'Approval',482 returnValues: {483 owner,484 approved: helper.address.substrateToEth(receiverSub.address),485 tokenId: token1.tokenId.toString(),486 },487 });488 expect(eventEth).to.be.like({489 address: helper.ethAddress.fromCollectionId(collection.collectionId),490 event: 'Approval',491 returnValues: {492 owner,493 approved: receiverEth,494 tokenId: token2.tokenId.toString(),495 },496 });497498 // Substrate address can transferFrom approved tokens:499 await helper.nft.transferTokenFrom(receiverSub, collection.collectionId, token1.tokenId, {Ethereum: owner}, {Substrate: receiverSub.address});500 expect(await helper.nft.getTokenOwner(collection.collectionId, token1.tokenId)).to.deep.eq({Substrate: receiverSub.address});501 // Ethereum address can transferFromCross approved tokens:502 await collectionEvm.methods.transferFromCross(ownerCross, receiverCrossEth, token2.tokenId).send({from: receiverEth});503 expect(await helper.nft.getTokenOwner(collection.collectionId, token2.tokenId)).to.deep.eq({Ethereum: receiverEth.toLowerCase()});504 });505506 itEth('Can reaffirm approved address', async ({helper}) => {507 const owner = await helper.eth.createAccountWithBalance(donor, 100n);508 const ownerCrossEth = helper.ethCrossAccount.fromAddress(owner);509 const [receiver1, receiver2] = await helper.arrange.createAccounts([100n, 100n], donor);510 const receiver1Cross = helper.ethCrossAccount.fromKeyringPair(receiver1);511 const receiver2Cross = helper.ethCrossAccount.fromKeyringPair(receiver2);512 const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});513 const token1 = await collection.mintToken(minter, {Ethereum: owner});514 const token2 = await collection.mintToken(minter, {Ethereum: owner});515 const collectionEvm = helper.ethNativeContract.collection(helper.ethAddress.fromCollectionId(collection.collectionId), 'nft');516517 // Can approve and reaffirm approved address:518 await collectionEvm.methods.approveCross(receiver1Cross, token1.tokenId).send({from: owner});519 await collectionEvm.methods.approveCross(receiver2Cross, token1.tokenId).send({from: owner});520521 // receiver1 cannot transferFrom:522 await expect(helper.nft.transferTokenFrom(receiver1, collection.collectionId, token1.tokenId, {Ethereum: owner}, {Substrate: receiver1.address})).to.be.rejected;523 // receiver2 can transferFrom:524 await helper.nft.transferTokenFrom(receiver2, collection.collectionId, token1.tokenId, {Ethereum: owner}, {Substrate: receiver2.address});525526 // can set approved address to self address to remove approval:527 await collectionEvm.methods.approveCross(receiver1Cross, token2.tokenId).send({from: owner});528 await collectionEvm.methods.approveCross(ownerCrossEth, token2.tokenId).send({from: owner});529530 // receiver1 cannot transfer token anymore:531 await expect(helper.nft.transferTokenFrom(receiver1, collection.collectionId, token2.tokenId, {Ethereum: owner}, {Substrate: receiver1.address})).to.be.rejected;532 });533534 itEth('Can perform transferFrom()', async ({helper}) => {535 const owner = await helper.eth.createAccountWithBalance(donor);536 const spender = await helper.eth.createAccountWithBalance(donor);537 const receiver = helper.eth.createAccount();538539 const collection = await helper.nft.mintCollection(minter, {});540 const {tokenId} = await collection.mintToken(minter, {Ethereum: owner});541542 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);543 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);544545 await contract.methods.approve(spender, tokenId).send({from: owner});546547 {548 const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({from: spender});549550 const event = result.events.Transfer;551 expect(event.address).to.be.equal(collectionAddress);552 expect(event.returnValues.from).to.be.equal(owner);553 expect(event.returnValues.to).to.be.equal(receiver);554 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);555 }556557 {558 const balance = await contract.methods.balanceOf(receiver).call();559 expect(+balance).to.equal(1);560 }561562 {563 const balance = await contract.methods.balanceOf(owner).call();564 expect(+balance).to.equal(0);565 }566 });567568 itEth('Can perform transferFromCross()', async ({helper}) => {569 const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});570571 const [owner, receiver] = await helper.arrange.createAccounts([100n, 100n], donor);572 const spender = await helper.eth.createAccountWithBalance(donor);573574 const token = await collection.mintToken(minter, {Substrate: owner.address});575576 const address = helper.ethAddress.fromCollectionId(collection.collectionId);577 const contract = helper.ethNativeContract.collection(address, 'nft');578579 await token.approve(owner, {Ethereum: spender});580581 {582 const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);583 const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);584 const result = await contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: spender});585 const event = result.events.Transfer;586 expect(event).to.be.like({587 address: helper.ethAddress.fromCollectionId(collection.collectionId),588 event: 'Transfer',589 returnValues: {590 from: helper.address.substrateToEth(owner.address),591 to: helper.address.substrateToEth(receiver.address),592 tokenId: token.tokenId.toString(),593 },594 });595 }596597 expect(await token.getOwner()).to.be.like({Substrate: receiver.address});598 });599600 itEth('Can perform transfer()', async ({helper}) => {601 const collection = await helper.nft.mintCollection(minter, {});602 const owner = await helper.eth.createAccountWithBalance(donor);603 const receiver = helper.eth.createAccount();604605 const {tokenId} = await collection.mintToken(minter, {Ethereum: owner});606607 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);608 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);609610 {611 const result = await contract.methods.transfer(receiver, tokenId).send({from: owner});612613 const event = result.events.Transfer;614 expect(event.address).to.be.equal(collectionAddress);615 expect(event.returnValues.from).to.be.equal(owner);616 expect(event.returnValues.to).to.be.equal(receiver);617 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);618 }619620 {621 const balance = await contract.methods.balanceOf(owner).call();622 expect(+balance).to.equal(0);623 }624625 {626 const balance = await contract.methods.balanceOf(receiver).call();627 expect(+balance).to.equal(1);628 }629 });630 631 itEth('Can perform transferCross()', async ({helper}) => {632 const collection = await helper.nft.mintCollection(minter, {});633 const owner = await helper.eth.createAccountWithBalance(donor);634 const receiverEth = await helper.eth.createAccountWithBalance(donor);635 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);636 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(minter);637 638 const {tokenId} = await collection.mintToken(minter, {Ethereum: owner});639640 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);641 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);642643 {644 // Can transferCross to ethereum address:645 const result = await collectionEvm.methods.transferCross(receiverCrossEth, tokenId).send({from: owner});646 // Check events:647 const event = result.events.Transfer;648 expect(event.address).to.be.equal(collectionAddress);649 expect(event.returnValues.from).to.be.equal(owner);650 expect(event.returnValues.to).to.be.equal(receiverEth);651 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);652 653 // owner has balance = 0:654 const ownerBalance = await collectionEvm.methods.balanceOf(owner).call();655 expect(+ownerBalance).to.equal(0);656 // receiver owns token:657 const receiverBalance = await collectionEvm.methods.balanceOf(receiverEth).call();658 expect(+receiverBalance).to.equal(1);659 expect(await helper.nft.getTokenOwner(collection.collectionId, tokenId)).to.deep.eq({Ethereum: receiverEth.toLowerCase()});660 }661 662 {663 // Can transferCross to substrate address:664 const substrateResult = await collectionEvm.methods.transferCross(receiverCrossSub, tokenId).send({from: receiverEth});665 // Check events:666 const event = substrateResult.events.Transfer;667 expect(event.address).to.be.equal(collectionAddress);668 expect(event.returnValues.from).to.be.equal(receiverEth);669 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(minter.address));670 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);671 672 // owner has balance = 0:673 const ownerBalance = await collectionEvm.methods.balanceOf(receiverEth).call();674 expect(+ownerBalance).to.equal(0);675 // receiver owns token:676 const receiverBalance = await helper.nft.getTokensByAddress(collection.collectionId, {Substrate: minter.address});677 expect(receiverBalance).to.contain(tokenId);678 }679 });680681 ['transfer', 'transferCross'].map(testCase => itEth(`Cannot ${testCase} non-owned token`, async ({helper}) => {682 const sender = await helper.eth.createAccountWithBalance(donor);683 const tokenOwner = await helper.eth.createAccountWithBalance(donor);684 const receiverSub = minter;685 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(minter);686687 const collection = await helper.nft.mintCollection(minter, {});688 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);689 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', sender);690691 await collection.mintToken(minter, {Ethereum: sender});692 const nonSendersToken = await collection.mintToken(minter, {Ethereum: tokenOwner});693694 // Cannot transferCross someone else's token:695 const receiver = testCase === 'transfer' ? helper.address.substrateToEth(receiverSub.address) : receiverCrossSub;696 await expect(collectionEvm.methods[testCase](receiver, nonSendersToken.tokenId).send({from: sender})).to.be.rejected;697 // Cannot transfer token if it does not exist:698 await expect(collectionEvm.methods[testCase](receiver, 999999).send({from: sender})).to.be.rejected;699 }));700});701702describe('NFT: Fees', () => {703 let donor: IKeyringPair;704 let alice: IKeyringPair;705 let bob: IKeyringPair;706 let charlie: IKeyringPair;707708 before(async function() {709 await usingEthPlaygrounds(async (helper, privateKey) => {710 donor = await privateKey({filename: __filename});711 [alice, bob, charlie] = await helper.arrange.createAccounts([10n, 10n, 10n], donor);712 });713 });714715 itEth('approve() call fee is less than 0.2UNQ', async ({helper}) => {716 const owner = await helper.eth.createAccountWithBalance(donor);717 const spender = helper.eth.createAccount();718719 const collection = await helper.nft.mintCollection(alice, {});720 const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});721722 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);723724 const cost = await helper.eth.recordCallFee(owner, () => contract.methods.approve(spender, tokenId).send({from: owner}));725 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));726 });727728 itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {729 const owner = await helper.eth.createAccountWithBalance(donor);730 const spender = await helper.eth.createAccountWithBalance(donor);731732 const collection = await helper.nft.mintCollection(alice, {});733 const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});734735 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);736737 await contract.methods.approve(spender, tokenId).send({from: owner});738739 const cost = await helper.eth.recordCallFee(spender, () => contract.methods.transferFrom(owner, spender, tokenId).send({from: spender}));740 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));741 });742743 itEth('Can perform transferFromCross()', async ({helper}) => {744 const collectionMinter = alice;745 const owner = bob;746 const receiver = charlie;747 const collection = await helper.nft.mintCollection(collectionMinter, {name: 'A', description: 'B', tokenPrefix: 'C'});748749 const spender = await helper.eth.createAccountWithBalance(donor, 100n);750751 const token = await collection.mintToken(collectionMinter, {Substrate: owner.address});752753 const address = helper.ethAddress.fromCollectionId(collection.collectionId);754 const contract = helper.ethNativeContract.collection(address, 'nft');755756 await token.approve(owner, {Ethereum: spender});757758 {759 const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);760 const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);761 const result = await contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: spender});762 const event = result.events.Transfer;763 expect(event).to.be.like({764 address: helper.ethAddress.fromCollectionId(collection.collectionId),765 event: 'Transfer',766 returnValues: {767 from: helper.address.substrateToEth(owner.address),768 to: helper.address.substrateToEth(receiver.address),769 tokenId: token.tokenId.toString(),770 },771 });772 }773774 expect(await token.getOwner()).to.be.like({Substrate: receiver.address});775 });776777 itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {778 const owner = await helper.eth.createAccountWithBalance(donor);779 const receiver = helper.eth.createAccount();780781 const collection = await helper.nft.mintCollection(alice, {});782 const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});783784 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);785786 const cost = await helper.eth.recordCallFee(owner, () => contract.methods.transfer(receiver, tokenId).send({from: owner}));787 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));788 });789});790791describe('NFT: Substrate calls', () => {792 let donor: IKeyringPair;793 let alice: IKeyringPair;794795 before(async function() {796 await usingEthPlaygrounds(async (helper, privateKey) => {797 donor = await privateKey({filename: __filename});798 [alice] = await helper.arrange.createAccounts([20n], donor);799 });800 });801802 itEth('Events emitted for mint()', async ({helper}) => {803 const collection = await helper.nft.mintCollection(alice, {});804 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);805 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');806807 const events: any = [];808 contract.events.allEvents((_: any, event: any) => {809 events.push(event);810 });811812 const {tokenId} = await collection.mintToken(alice);813 if (events.length == 0) await helper.wait.newBlocks(1);814 const event = events[0];815816 expect(event.event).to.be.equal('Transfer');817 expect(event.address).to.be.equal(collectionAddress);818 expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');819 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(alice.address));820 expect(event.returnValues.tokenId).to.be.equal(tokenId.toString());821 });822823 itEth('Events emitted for burn()', async ({helper}) => {824 const collection = await helper.nft.mintCollection(alice, {});825 const token = await collection.mintToken(alice);826827 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);828 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');829830 const events: any = [];831 contract.events.allEvents((_: any, event: any) => {832 events.push(event);833 });834835 await token.burn(alice);836 if (events.length == 0) await helper.wait.newBlocks(1);837 const event = events[0];838839 expect(event.event).to.be.equal('Transfer');840 expect(event.address).to.be.equal(collectionAddress);841 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));842 expect(event.returnValues.to).to.be.equal('0x0000000000000000000000000000000000000000');843 expect(event.returnValues.tokenId).to.be.equal(token.tokenId.toString());844 });845846 itEth('Events emitted for approve()', async ({helper}) => {847 const receiver = helper.eth.createAccount();848849 const collection = await helper.nft.mintCollection(alice, {});850 const token = await collection.mintToken(alice);851852 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);853 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');854855 const events: any = [];856 contract.events.allEvents((_: any, event: any) => {857 events.push(event);858 });859860 await token.approve(alice, {Ethereum: receiver});861 if (events.length == 0) await helper.wait.newBlocks(1);862 const event = events[0];863864 expect(event.event).to.be.equal('Approval');865 expect(event.address).to.be.equal(collectionAddress);866 expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));867 expect(event.returnValues.approved).to.be.equal(receiver);868 expect(event.returnValues.tokenId).to.be.equal(token.tokenId.toString());869 });870871 itEth('Events emitted for transferFrom()', async ({helper}) => {872 const [bob] = await helper.arrange.createAccounts([10n], donor);873 const receiver = helper.eth.createAccount();874875 const collection = await helper.nft.mintCollection(alice, {});876 const token = await collection.mintToken(alice);877 await token.approve(alice, {Substrate: bob.address});878879 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);880 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');881882 const events: any = [];883 contract.events.allEvents((_: any, event: any) => {884 events.push(event);885 });886887 await token.transferFrom(bob, {Substrate: alice.address}, {Ethereum: receiver});888889 if (events.length == 0) await helper.wait.newBlocks(1);890 const event = events[0];891892 expect(event.address).to.be.equal(collectionAddress);893 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));894 expect(event.returnValues.to).to.be.equal(receiver);895 expect(event.returnValues.tokenId).to.be.equal(`${token.tokenId}`);896 });897898 itEth('Events emitted for transfer()', async ({helper}) => {899 const receiver = helper.eth.createAccount();900901 const collection = await helper.nft.mintCollection(alice, {});902 const token = await collection.mintToken(alice);903904 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);905 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');906907 const events: any = [];908 contract.events.allEvents((_: any, event: any) => {909 events.push(event);910 });911912 await token.transfer(alice, {Ethereum: receiver});913914 if (events.length == 0) await helper.wait.newBlocks(1);915 const event = events[0];916917 expect(event.address).to.be.equal(collectionAddress);918 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));919 expect(event.returnValues.to).to.be.equal(receiver);920 expect(event.returnValues.tokenId).to.be.equal(`${token.tokenId}`);921 });922});923924describe('Common metadata', () => {925 let donor: IKeyringPair;926 let alice: IKeyringPair;927928 before(async function() {929 await usingEthPlaygrounds(async (helper, privateKey) => {930 donor = await privateKey({filename: __filename});931 [alice] = await helper.arrange.createAccounts([20n], donor);932 });933 });934935 itEth('Returns collection name', async ({helper}) => {936 const caller = await helper.eth.createAccountWithBalance(donor);937 const tokenPropertyPermissions = [{938 key: 'URI',939 permission: {940 mutable: true,941 collectionAdmin: true,942 tokenOwner: false,943 },944 }];945 const collection = await helper.nft.mintCollection(946 alice,947 {948 name: 'oh River',949 tokenPrefix: 'CHANGE',950 properties: [{key: 'ERC721Metadata', value: '1'}],951 tokenPropertyPermissions,952 },953 );954955 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);956 const name = await contract.methods.name().call();957 expect(name).to.equal('oh River');958 });959960 itEth('Returns symbol name', async ({helper}) => {961 const caller = await helper.eth.createAccountWithBalance(donor);962 const tokenPropertyPermissions = [{963 key: 'URI',964 permission: {965 mutable: true,966 collectionAdmin: true,967 tokenOwner: false,968 },969 }];970 const collection = await helper.nft.mintCollection(971 alice,972 {973 name: 'oh River',974 tokenPrefix: 'CHANGE',975 properties: [{key: 'ERC721Metadata', value: '1'}],976 tokenPropertyPermissions,977 },978 );979980 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);981 const symbol = await contract.methods.symbol().call();982 expect(symbol).to.equal('CHANGE');983 });984});985986describe('Negative tests', () => {987 let donor: IKeyringPair;988 let minter: IKeyringPair;989 let alice: IKeyringPair;990991 before(async function() {992 await usingEthPlaygrounds(async (helper, privateKey) => {993 donor = await privateKey({filename: __filename});994 [minter, alice] = await helper.arrange.createAccounts([100n, 100n], donor);995 });996 });997998 itEth('[negative] Cant perform burn without approval', async ({helper}) => {999 const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});10001001 const owner = await helper.eth.createAccountWithBalance(donor, 100n);1002 const spender = await helper.eth.createAccountWithBalance(donor, 100n);10031004 const token = await collection.mintToken(minter, {Ethereum: owner});10051006 const address = helper.ethAddress.fromCollectionId(collection.collectionId);1007 const contract = helper.ethNativeContract.collection(address, 'nft');10081009 const ownerCross = helper.ethCrossAccount.fromAddress(owner);1010 await expect(contract.methods.burnFromCross(ownerCross, token.tokenId).send({from: spender})).to.be.rejected;10111012 await contract.methods.setApprovalForAll(spender, true).send({from: owner});1013 await contract.methods.setApprovalForAll(spender, false).send({from: owner});10141015 await expect(contract.methods.burnFromCross(ownerCross, token.tokenId).send({from: spender})).to.be.rejected;1016 });10171018 itEth('[negative] Cant perform transfer without approval', async ({helper}) => {1019 const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});1020 const receiver = alice;10211022 const owner = await helper.eth.createAccountWithBalance(donor, 100n);1023 const spender = await helper.eth.createAccountWithBalance(donor, 100n);10241025 const token = await collection.mintToken(minter, {Ethereum: owner});10261027 const address = helper.ethAddress.fromCollectionId(collection.collectionId);1028 const contract = helper.ethNativeContract.collection(address, 'nft');10291030 const ownerCross = helper.ethCrossAccount.fromAddress(owner);1031 const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);10321033 await expect(contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: spender})).to.be.rejected;10341035 await contract.methods.setApprovalForAll(spender, true).send({from: owner});1036 await contract.methods.setApprovalForAll(spender, false).send({from: owner});1037 1038 await expect(contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: spender})).to.be.rejected;1039 });1040});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 {itEth, usingEthPlaygrounds, expect, EthUniqueHelper} from './util';18import {IKeyringPair} from '@polkadot/types/types';19import {Contract} from 'web3-eth-contract';20import {ITokenPropertyPermission} from '../util/playgrounds/types';212223describe('NFT: Information getting', () => {24 let donor: IKeyringPair;25 let alice: IKeyringPair;2627 before(async function() {28 await usingEthPlaygrounds(async (helper, privateKey) => {29 donor = await privateKey({filename: __filename});30 [alice] = await helper.arrange.createAccounts([10n], donor);31 });32 });3334 itEth('totalSupply', async ({helper}) => {35 const collection = await helper.nft.mintCollection(alice, {});36 await collection.mintToken(alice);3738 const caller = await helper.eth.createAccountWithBalance(donor);3940 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);41 const totalSupply = await contract.methods.totalSupply().call();4243 expect(totalSupply).to.equal('1');44 });4546 itEth('balanceOf', async ({helper}) => {47 const collection = await helper.nft.mintCollection(alice, {});48 const caller = await helper.eth.createAccountWithBalance(donor);4950 await collection.mintToken(alice, {Ethereum: caller});51 await collection.mintToken(alice, {Ethereum: caller});52 await collection.mintToken(alice, {Ethereum: caller});5354 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);55 const balance = await contract.methods.balanceOf(caller).call();5657 expect(balance).to.equal('3');58 });5960 itEth('ownerOf', async ({helper}) => {61 const collection = await helper.nft.mintCollection(alice, {});62 const caller = await helper.eth.createAccountWithBalance(donor);6364 const token = await collection.mintToken(alice, {Ethereum: caller});6566 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);6768 const owner = await contract.methods.ownerOf(token.tokenId).call();6970 expect(owner).to.equal(caller);71 });7273 itEth('name/symbol is available regardless of ERC721Metadata support', async ({helper}) => {74 const collection = await helper.nft.mintCollection(alice, {name: 'test', tokenPrefix: 'TEST'});75 const caller = helper.eth.createAccount();7677 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);7879 expect(await contract.methods.name().call()).to.equal('test');80 expect(await contract.methods.symbol().call()).to.equal('TEST');81 });82});8384describe('Check ERC721 token URI for NFT', () => {85 let donor: IKeyringPair;8687 before(async function() {88 await usingEthPlaygrounds(async (_helper, privateKey) => {89 donor = await privateKey({filename: __filename});90 });91 });9293 async function setup(helper: EthUniqueHelper, baseUri: string, propertyKey?: string, propertyValue?: string): Promise<{contract: Contract, nextTokenId: string}> {94 const owner = await helper.eth.createAccountWithBalance(donor);95 const receiver = helper.eth.createAccount();9697 const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Mint collection', 'a', 'b', baseUri);98 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);99100 const result = await contract.methods.mint(receiver).send();101 const tokenId = result.events.Transfer.returnValues.tokenId;102 expect(tokenId).to.be.equal('1');103104 if (propertyKey && propertyValue) {105 // Set URL or suffix106 await contract.methods.setProperties(tokenId, [{key: propertyKey, value: Buffer.from(propertyValue)}]).send();107 }108109 const event = result.events.Transfer;110 expect(event.address).to.be.equal(collectionAddress);111 expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');112 expect(event.returnValues.to).to.be.equal(receiver);113 expect(event.returnValues.tokenId).to.be.equal(tokenId);114115 return {contract, nextTokenId: tokenId};116 }117118 itEth('Empty tokenURI', async ({helper}) => {119 const {contract, nextTokenId} = await setup(helper, '');120 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('');121 });122123 itEth('TokenURI from url', async ({helper}) => {124 const {contract, nextTokenId} = await setup(helper, 'BaseURI_', 'URI', 'Token URI');125 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Token URI');126 });127128 itEth('TokenURI from baseURI', async ({helper}) => {129 const {contract, nextTokenId} = await setup(helper, 'BaseURI_');130 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_');131 });132133 itEth('TokenURI from baseURI + suffix', async ({helper}) => {134 const suffix = '/some/suffix';135 const {contract, nextTokenId} = await setup(helper, 'BaseURI_', 'URISuffix', suffix);136 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_' + suffix);137 });138});139140describe('NFT: Plain calls', () => {141 let donor: IKeyringPair;142 let minter: IKeyringPair;143 let bob: IKeyringPair;144 let charlie: IKeyringPair;145146 before(async function() {147 await usingEthPlaygrounds(async (helper, privateKey) => {148 donor = await privateKey({filename: __filename});149 [minter, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);150 });151 });152153 itEth('Can perform mint() & get crossOwner()', async ({helper}) => {154 const owner = await helper.eth.createAccountWithBalance(donor);155 const receiver = helper.eth.createAccount();156157 const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Mint collection', '6', '6', '');158 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);159160 const result = await contract.methods.mintWithTokenURI(receiver, 'Test URI').send();161 const tokenId = result.events.Transfer.returnValues.tokenId;162 expect(tokenId).to.be.equal('1');163164 const event = result.events.Transfer;165 expect(event.address).to.be.equal(collectionAddress);166 expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');167 expect(event.returnValues.to).to.be.equal(receiver);168169 expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');170 console.log(await contract.methods.crossOwnerOf(tokenId).call());171 expect(await contract.methods.crossOwnerOf(tokenId).call()).to.be.like([receiver, '0']);172 // TODO: this wont work right now, need release 919000 first173 // await helper.methods.setOffchainSchema(collectionIdAddress, 'https://offchain-service.local/token-info/{id}').send();174 // const tokenUri = await contract.methods.tokenURI(nextTokenId).call();175 // expect(tokenUri).to.be.equal(`https://offchain-service.local/token-info/${nextTokenId}`);176 });177 178 // TODO combine all minting tests in one place179 [180 'substrate' as const,181 'ethereum' as const,182 ].map(testCase => {183 itEth(`Can perform mintCross() for ${testCase} address`, async ({helper}) => {184 const collectionAdmin = await helper.eth.createAccountWithBalance(donor);185186 const receiverEth = helper.eth.createAccount();187 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);188 const receiverSub = bob;189 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(receiverSub);190191 // const receiverCross = helper.ethCrossAccount.fromKeyringPair(bob);192 const properties = Array(5).fill(0).map((_, i) => { return {key: `key_${i}`, value: Buffer.from(`value_${i}`)}; });193 const permissions: ITokenPropertyPermission[] = properties194 .map(p => {195 return {196 key: p.key, permission: {197 tokenOwner: false,198 collectionAdmin: true,199 mutable: false,200 },201 };202 });203 204 205 const collection = await helper.nft.mintCollection(minter, {206 tokenPrefix: 'ethp',207 tokenPropertyPermissions: permissions,208 });209 await collection.addAdmin(minter, {Ethereum: collectionAdmin});210 211 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);212 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', collectionAdmin, true);213 let expectedTokenId = await contract.methods.nextTokenId().call();214 let result = await contract.methods.mintCross(testCase === 'ethereum' ? receiverCrossEth : receiverCrossSub, []).send();215 let tokenId = result.events.Transfer.returnValues.tokenId;216 expect(tokenId).to.be.equal(expectedTokenId);217218 let event = result.events.Transfer;219 expect(event.address).to.be.equal(collectionAddress);220 expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');221 expect(event.returnValues.to).to.be.equal(testCase === 'ethereum' ? receiverEth : helper.address.substrateToEth(bob.address));222 expect(await contract.methods.properties(tokenId, []).call()).to.be.like([]);223 224 expectedTokenId = await contract.methods.nextTokenId().call();225 result = await contract.methods.mintCross(testCase === 'ethereum' ? receiverCrossEth : receiverCrossSub, properties).send();226 event = result.events.Transfer;227 expect(event.address).to.be.equal(collectionAddress);228 expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');229 expect(event.returnValues.to).to.be.equal(testCase === 'ethereum' ? receiverEth : helper.address.substrateToEth(bob.address));230 expect(await contract.methods.properties(tokenId, []).call()).to.be.like([]);231 232 tokenId = result.events.Transfer.returnValues.tokenId;233 234 expect(tokenId).to.be.equal(expectedTokenId);235236 expect(await contract.methods.properties(tokenId, []).call()).to.be.like(properties237 .map(p => { return helper.ethProperty.property(p.key, p.value.toString()); }));238 239 expect(await helper.nft.getTokenOwner(collection.collectionId, tokenId))240 .to.deep.eq(testCase === 'ethereum' ? {Ethereum: receiverEth.toLowerCase()} : {Substrate: receiverSub.address});241 });242 });243244 itEth('Non-owner and non admin cannot mintCross', async ({helper}) => {245 const nonOwner = await helper.eth.createAccountWithBalance(donor);246 const nonOwnerCross = helper.ethCrossAccount.fromAddress(nonOwner);247248 const collection = await helper.nft.mintCollection(minter);249 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);250 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft');251252 await expect(collectionEvm.methods.mintCross(nonOwnerCross, []).call({from: nonOwner}))253 .to.be.rejectedWith('PublicMintingNotAllowed');254 });255 256 //TODO: CORE-302 add eth methods257 itEth.skip('Can perform mintBulk()', async ({helper}) => {258 const caller = await helper.eth.createAccountWithBalance(donor);259 const receiver = helper.eth.createAccount();260261 const collection = await helper.nft.mintCollection(minter);262 await collection.addAdmin(minter, {Ethereum: caller});263264 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);265 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', caller);266 {267 const bulkSize = 3;268 const nextTokenId = await contract.methods.nextTokenId().call();269 expect(nextTokenId).to.be.equal('1');270 const result = await contract.methods.mintBulkWithTokenURI(271 receiver,272 Array.from({length: bulkSize}, (_, i) => (273 [+nextTokenId + i, `Test URI ${i}`]274 )),275 ).send({from: caller});276277 const events = result.events.Transfer.sort((a: any, b: any) => +a.returnValues.tokenId - b.returnValues.tokenId);278 for (let i = 0; i < bulkSize; i++) {279 const event = events[i];280 expect(event.address).to.equal(collectionAddress);281 expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');282 expect(event.returnValues.to).to.equal(receiver);283 expect(event.returnValues.tokenId).to.equal(`${+nextTokenId+i}`);284285 expect(await contract.methods.tokenURI(+nextTokenId + i).call()).to.be.equal(`Test URI ${i}`);286 }287 }288 });289290 itEth('Can perform burn()', async ({helper}) => {291 const caller = await helper.eth.createAccountWithBalance(donor);292293 const collection = await helper.nft.mintCollection(minter, {});294 const {tokenId} = await collection.mintToken(minter, {Ethereum: caller});295296 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);297 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', caller);298299 {300 const result = await contract.methods.burn(tokenId).send({from: caller});301302 const event = result.events.Transfer;303 expect(event.address).to.be.equal(collectionAddress);304 expect(event.returnValues.from).to.be.equal(caller);305 expect(event.returnValues.to).to.be.equal('0x0000000000000000000000000000000000000000');306 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);307 }308 });309310 itEth('Can perform approve()', async ({helper}) => {311 const owner = await helper.eth.createAccountWithBalance(donor);312 const spender = helper.eth.createAccount();313314 const collection = await helper.nft.mintCollection(minter, {});315 const {tokenId} = await collection.mintToken(minter, {Ethereum: owner});316317 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);318 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);319320 {321 const result = await contract.methods.approve(spender, tokenId).send({from: owner});322323 const event = result.events.Approval;324 expect(event.address).to.be.equal(collectionAddress);325 expect(event.returnValues.owner).to.be.equal(owner);326 expect(event.returnValues.approved).to.be.equal(spender);327 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);328 }329 });330331 itEth('Can perform setApprovalForAll()', async ({helper}) => {332 const owner = await helper.eth.createAccountWithBalance(donor);333 const operator = helper.eth.createAccount();334335 const collection = await helper.nft.mintCollection(minter, {});336337 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);338 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);339340 const approvedBefore = await contract.methods.isApprovedForAll(owner, operator).call();341 expect(approvedBefore).to.be.equal(false);342343 {344 const result = await contract.methods.setApprovalForAll(operator, true).send({from: owner});345346 expect(result.events.ApprovalForAll).to.be.like({347 address: collectionAddress,348 event: 'ApprovalForAll',349 returnValues: {350 owner,351 operator,352 approved: true,353 },354 });355356 const approvedAfter = await contract.methods.isApprovedForAll(owner, operator).call();357 expect(approvedAfter).to.be.equal(true);358 }359360 {361 const result = await contract.methods.setApprovalForAll(operator, false).send({from: owner});362363 expect(result.events.ApprovalForAll).to.be.like({364 address: collectionAddress,365 event: 'ApprovalForAll',366 returnValues: {367 owner,368 operator,369 approved: false,370 },371 });372373 const approvedAfter = await contract.methods.isApprovedForAll(owner, operator).call();374 expect(approvedAfter).to.be.equal(false);375 }376 });377378 itEth('Can perform burn with ApprovalForAll', async ({helper}) => {379 const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});380381 const owner = await helper.eth.createAccountWithBalance(donor);382 const operator = await helper.eth.createAccountWithBalance(donor, 100n);383384 const token = await collection.mintToken(minter, {Ethereum: owner});385386 const address = helper.ethAddress.fromCollectionId(collection.collectionId);387 const contract = helper.ethNativeContract.collection(address, 'nft');388389 {390 await contract.methods.setApprovalForAll(operator, true).send({from: owner});391 const ownerCross = helper.ethCrossAccount.fromAddress(owner);392 const result = await contract.methods.burnFromCross(ownerCross, token.tokenId).send({from: operator});393 const events = result.events.Transfer;394395 expect(events).to.be.like({396 address,397 event: 'Transfer',398 returnValues: {399 from: owner,400 to: '0x0000000000000000000000000000000000000000',401 tokenId: token.tokenId.toString(),402 },403 });404 }405406 expect(await helper.nft.doesTokenExist(collection.collectionId, token.tokenId)).to.be.false;407 });408 409 itEth('Can perform transfer with ApprovalForAll', async ({helper}) => {410 const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});411412 const owner = await helper.eth.createAccountWithBalance(donor);413 const operator = await helper.eth.createAccountWithBalance(donor);414 const receiver = charlie;415416 const token = await collection.mintToken(minter, {Ethereum: owner});417418 const address = helper.ethAddress.fromCollectionId(collection.collectionId);419 const contract = helper.ethNativeContract.collection(address, 'nft');420421 {422 await contract.methods.setApprovalForAll(operator, true).send({from: owner});423 const ownerCross = helper.ethCrossAccount.fromAddress(owner);424 const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);425 const result = await contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: operator});426 const event = result.events.Transfer;427 expect(event).to.be.like({428 address: helper.ethAddress.fromCollectionId(collection.collectionId),429 event: 'Transfer',430 returnValues: {431 from: owner,432 to: helper.address.substrateToEth(receiver.address),433 tokenId: token.tokenId.toString(),434 },435 });436 }437438 expect(await token.getOwner()).to.be.like({Substrate: receiver.address});439 });440441 itEth('Can perform burnFromCross()', async ({helper}) => {442 const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});443 const ownerSub = bob;444 const ownerCrossSub = helper.ethCrossAccount.fromKeyringPair(ownerSub);445 const ownerEth = await helper.eth.createAccountWithBalance(donor, 100n);446 const ownerCrossEth = helper.ethCrossAccount.fromAddress(ownerEth);447448 const burnerEth = await helper.eth.createAccountWithBalance(donor, 100n);449 const burnerCrossEth = helper.ethCrossAccount.fromAddress(burnerEth);450451 const token1 = await collection.mintToken(minter, {Substrate: ownerSub.address});452 const token2 = await collection.mintToken(minter, {Ethereum: ownerEth});453454 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);455 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft');456457 // Approve tokens from substrate and ethereum:458 await token1.approve(ownerSub, {Ethereum: burnerEth});459 await collectionEvm.methods.approveCross(burnerCrossEth, token2.tokenId).send({from: ownerEth});460461 // can burnFromCross:462 const result1 = await collectionEvm.methods.burnFromCross(ownerCrossSub, token1.tokenId).send({from: burnerEth});463 const result2 = await collectionEvm.methods.burnFromCross(ownerCrossEth, token2.tokenId).send({from: burnerEth});464 const events1 = result1.events.Transfer;465 const events2 = result2.events.Transfer;466467 // Check events for burnFromCross (substrate and ethereum):468 [469 [events1, token1, helper.address.substrateToEth(ownerSub.address)], 470 [events2, token2, ownerEth],471 ].map(burnData => {472 expect(burnData[0]).to.be.like({473 address: collectionAddress,474 event: 'Transfer',475 returnValues: {476 from: burnData[2],477 to: '0x0000000000000000000000000000000000000000',478 tokenId: burnData[1].tokenId.toString(),479 },480 });481 });482483 expect(await token1.doesExist()).to.be.false;484 expect(await token2.doesExist()).to.be.false;485 });486487 // TODO combine all approve tests in one place488 itEth('Can perform approveCross()', async ({helper}) => {489 // arrange: create accounts490 const owner = await helper.eth.createAccountWithBalance(donor, 100n);491 const ownerCross = helper.ethCrossAccount.fromAddress(owner);492 const receiverSub = charlie;493 const recieverCrossSub = helper.ethCrossAccount.fromKeyringPair(receiverSub);494 const receiverEth = await helper.eth.createAccountWithBalance(donor, 100n);495 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);496497 // arrange: create collection and tokens:498 const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});499 const token1 = await collection.mintToken(minter, {Ethereum: owner});500 const token2 = await collection.mintToken(minter, {Ethereum: owner});501502 const collectionEvm = helper.ethNativeContract.collection(helper.ethAddress.fromCollectionId(collection.collectionId), 'nft');503504 // Can approveCross substrate and ethereum address:505 const resultSub = await collectionEvm.methods.approveCross(recieverCrossSub, token1.tokenId).send({from: owner});506 const resultEth = await collectionEvm.methods.approveCross(receiverCrossEth, token2.tokenId).send({from: owner});507 const eventSub = resultSub.events.Approval;508 const eventEth = resultEth.events.Approval;509 expect(eventSub).to.be.like({510 address: helper.ethAddress.fromCollectionId(collection.collectionId),511 event: 'Approval',512 returnValues: {513 owner,514 approved: helper.address.substrateToEth(receiverSub.address),515 tokenId: token1.tokenId.toString(),516 },517 });518 expect(eventEth).to.be.like({519 address: helper.ethAddress.fromCollectionId(collection.collectionId),520 event: 'Approval',521 returnValues: {522 owner,523 approved: receiverEth,524 tokenId: token2.tokenId.toString(),525 },526 });527528 // Substrate address can transferFrom approved tokens:529 await helper.nft.transferTokenFrom(receiverSub, collection.collectionId, token1.tokenId, {Ethereum: owner}, {Substrate: receiverSub.address});530 expect(await helper.nft.getTokenOwner(collection.collectionId, token1.tokenId)).to.deep.eq({Substrate: receiverSub.address});531 // Ethereum address can transferFromCross approved tokens:532 await collectionEvm.methods.transferFromCross(ownerCross, receiverCrossEth, token2.tokenId).send({from: receiverEth});533 expect(await helper.nft.getTokenOwner(collection.collectionId, token2.tokenId)).to.deep.eq({Ethereum: receiverEth.toLowerCase()});534 });535536 itEth('Non-owner and non admin cannot approveCross', async ({helper}) => {537 const nonOwner = await helper.eth.createAccountWithBalance(donor);538 const nonOwnerCross = helper.ethCrossAccount.fromAddress(nonOwner);539 const owner = await helper.eth.createAccountWithBalance(donor);540 const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});541 const collectionEvm = helper.ethNativeContract.collection(helper.ethAddress.fromCollectionId(collection.collectionId), 'nft');542 const token = await collection.mintToken(minter, {Ethereum: owner});543544 await expect(collectionEvm.methods.approveCross(nonOwnerCross, token.tokenId).call({from: nonOwner})).to.be.rejectedWith('CantApproveMoreThanOwned');545 });546547 itEth('Can reaffirm approved address', async ({helper}) => {548 const owner = await helper.eth.createAccountWithBalance(donor, 100n);549 const ownerCrossEth = helper.ethCrossAccount.fromAddress(owner);550 const [receiver1, receiver2] = await helper.arrange.createAccounts([100n, 100n], donor);551 const receiver1Cross = helper.ethCrossAccount.fromKeyringPair(receiver1);552 const receiver2Cross = helper.ethCrossAccount.fromKeyringPair(receiver2);553 const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});554 const token1 = await collection.mintToken(minter, {Ethereum: owner});555 const token2 = await collection.mintToken(minter, {Ethereum: owner});556 const collectionEvm = helper.ethNativeContract.collection(helper.ethAddress.fromCollectionId(collection.collectionId), 'nft');557558 // Can approve and reaffirm approved address:559 await collectionEvm.methods.approveCross(receiver1Cross, token1.tokenId).send({from: owner});560 await collectionEvm.methods.approveCross(receiver2Cross, token1.tokenId).send({from: owner});561562 // receiver1 cannot transferFrom:563 await expect(helper.nft.transferTokenFrom(receiver1, collection.collectionId, token1.tokenId, {Ethereum: owner}, {Substrate: receiver1.address})).to.be.rejected;564 // receiver2 can transferFrom:565 await helper.nft.transferTokenFrom(receiver2, collection.collectionId, token1.tokenId, {Ethereum: owner}, {Substrate: receiver2.address});566567 // can set approved address to self address to remove approval:568 await collectionEvm.methods.approveCross(receiver1Cross, token2.tokenId).send({from: owner});569 await collectionEvm.methods.approveCross(ownerCrossEth, token2.tokenId).send({from: owner});570571 // receiver1 cannot transfer token anymore:572 await expect(helper.nft.transferTokenFrom(receiver1, collection.collectionId, token2.tokenId, {Ethereum: owner}, {Substrate: receiver1.address})).to.be.rejected;573 });574575 itEth('Can perform transferFrom()', async ({helper}) => {576 const owner = await helper.eth.createAccountWithBalance(donor);577 const spender = await helper.eth.createAccountWithBalance(donor);578 const receiver = helper.eth.createAccount();579580 const collection = await helper.nft.mintCollection(minter, {});581 const {tokenId} = await collection.mintToken(minter, {Ethereum: owner});582583 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);584 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);585586 await contract.methods.approve(spender, tokenId).send({from: owner});587588 {589 const result = await contract.methods.transferFrom(owner, receiver, tokenId).send({from: spender});590591 const event = result.events.Transfer;592 expect(event.address).to.be.equal(collectionAddress);593 expect(event.returnValues.from).to.be.equal(owner);594 expect(event.returnValues.to).to.be.equal(receiver);595 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);596 }597598 {599 const balance = await contract.methods.balanceOf(receiver).call();600 expect(+balance).to.equal(1);601 }602603 {604 const balance = await contract.methods.balanceOf(owner).call();605 expect(+balance).to.equal(0);606 }607 });608609 itEth('Can perform transferFromCross()', async ({helper}) => {610 const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});611612 const [owner, receiver] = await helper.arrange.createAccounts([100n, 100n], donor);613 const spender = await helper.eth.createAccountWithBalance(donor);614615 const token = await collection.mintToken(minter, {Substrate: owner.address});616617 const address = helper.ethAddress.fromCollectionId(collection.collectionId);618 const contract = helper.ethNativeContract.collection(address, 'nft');619620 await token.approve(owner, {Ethereum: spender});621622 {623 const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);624 const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);625 const result = await contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: spender});626 const event = result.events.Transfer;627 expect(event).to.be.like({628 address: helper.ethAddress.fromCollectionId(collection.collectionId),629 event: 'Transfer',630 returnValues: {631 from: helper.address.substrateToEth(owner.address),632 to: helper.address.substrateToEth(receiver.address),633 tokenId: token.tokenId.toString(),634 },635 });636 }637638 expect(await token.getOwner()).to.be.like({Substrate: receiver.address});639 });640641 itEth('Can perform transfer()', async ({helper}) => {642 const collection = await helper.nft.mintCollection(minter, {});643 const owner = await helper.eth.createAccountWithBalance(donor);644 const receiver = helper.eth.createAccount();645646 const {tokenId} = await collection.mintToken(minter, {Ethereum: owner});647648 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);649 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);650651 {652 const result = await contract.methods.transfer(receiver, tokenId).send({from: owner});653654 const event = result.events.Transfer;655 expect(event.address).to.be.equal(collectionAddress);656 expect(event.returnValues.from).to.be.equal(owner);657 expect(event.returnValues.to).to.be.equal(receiver);658 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);659 }660661 {662 const balance = await contract.methods.balanceOf(owner).call();663 expect(+balance).to.equal(0);664 }665666 {667 const balance = await contract.methods.balanceOf(receiver).call();668 expect(+balance).to.equal(1);669 }670 });671 672 itEth('Can perform transferCross()', async ({helper}) => {673 const collection = await helper.nft.mintCollection(minter, {});674 const owner = await helper.eth.createAccountWithBalance(donor);675 const receiverEth = await helper.eth.createAccountWithBalance(donor);676 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);677 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(minter);678 679 const {tokenId} = await collection.mintToken(minter, {Ethereum: owner});680681 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);682 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);683684 {685 // Can transferCross to ethereum address:686 const result = await collectionEvm.methods.transferCross(receiverCrossEth, tokenId).send({from: owner});687 // Check events:688 const event = result.events.Transfer;689 expect(event.address).to.be.equal(collectionAddress);690 expect(event.returnValues.from).to.be.equal(owner);691 expect(event.returnValues.to).to.be.equal(receiverEth);692 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);693 694 // owner has balance = 0:695 const ownerBalance = await collectionEvm.methods.balanceOf(owner).call();696 expect(+ownerBalance).to.equal(0);697 // receiver owns token:698 const receiverBalance = await collectionEvm.methods.balanceOf(receiverEth).call();699 expect(+receiverBalance).to.equal(1);700 expect(await helper.nft.getTokenOwner(collection.collectionId, tokenId)).to.deep.eq({Ethereum: receiverEth.toLowerCase()});701 }702 703 {704 // Can transferCross to substrate address:705 const substrateResult = await collectionEvm.methods.transferCross(receiverCrossSub, tokenId).send({from: receiverEth});706 // Check events:707 const event = substrateResult.events.Transfer;708 expect(event.address).to.be.equal(collectionAddress);709 expect(event.returnValues.from).to.be.equal(receiverEth);710 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(minter.address));711 expect(event.returnValues.tokenId).to.be.equal(`${tokenId}`);712 713 // owner has balance = 0:714 const ownerBalance = await collectionEvm.methods.balanceOf(receiverEth).call();715 expect(+ownerBalance).to.equal(0);716 // receiver owns token:717 const receiverBalance = await helper.nft.getTokensByAddress(collection.collectionId, {Substrate: minter.address});718 expect(receiverBalance).to.contain(tokenId);719 }720 });721722 ['transfer', 'transferCross'].map(testCase => itEth(`Cannot ${testCase} non-owned token`, async ({helper}) => {723 const sender = await helper.eth.createAccountWithBalance(donor);724 const tokenOwner = await helper.eth.createAccountWithBalance(donor);725 const receiverSub = minter;726 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(minter);727728 const collection = await helper.nft.mintCollection(minter, {});729 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);730 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', sender);731732 await collection.mintToken(minter, {Ethereum: sender});733 const nonSendersToken = await collection.mintToken(minter, {Ethereum: tokenOwner});734735 // Cannot transferCross someone else's token:736 const receiver = testCase === 'transfer' ? helper.address.substrateToEth(receiverSub.address) : receiverCrossSub;737 await expect(collectionEvm.methods[testCase](receiver, nonSendersToken.tokenId).send({from: sender})).to.be.rejected;738 // Cannot transfer token if it does not exist:739 await expect(collectionEvm.methods[testCase](receiver, 999999).send({from: sender})).to.be.rejected;740 }));741});742743describe('NFT: Fees', () => {744 let donor: IKeyringPair;745 let alice: IKeyringPair;746 let bob: IKeyringPair;747 let charlie: IKeyringPair;748749 before(async function() {750 await usingEthPlaygrounds(async (helper, privateKey) => {751 donor = await privateKey({filename: __filename});752 [alice, bob, charlie] = await helper.arrange.createAccounts([10n, 10n, 10n], donor);753 });754 });755756 itEth('approve() call fee is less than 0.2UNQ', async ({helper}) => {757 const owner = await helper.eth.createAccountWithBalance(donor);758 const spender = helper.eth.createAccount();759760 const collection = await helper.nft.mintCollection(alice, {});761 const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});762763 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);764765 const cost = await helper.eth.recordCallFee(owner, () => contract.methods.approve(spender, tokenId).send({from: owner}));766 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));767 });768769 itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {770 const owner = await helper.eth.createAccountWithBalance(donor);771 const spender = await helper.eth.createAccountWithBalance(donor);772773 const collection = await helper.nft.mintCollection(alice, {});774 const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});775776 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);777778 await contract.methods.approve(spender, tokenId).send({from: owner});779780 const cost = await helper.eth.recordCallFee(spender, () => contract.methods.transferFrom(owner, spender, tokenId).send({from: spender}));781 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));782 });783784 itEth('Can perform transferFromCross()', async ({helper}) => {785 const collectionMinter = alice;786 const owner = bob;787 const receiver = charlie;788 const collection = await helper.nft.mintCollection(collectionMinter, {name: 'A', description: 'B', tokenPrefix: 'C'});789790 const spender = await helper.eth.createAccountWithBalance(donor, 100n);791792 const token = await collection.mintToken(collectionMinter, {Substrate: owner.address});793794 const address = helper.ethAddress.fromCollectionId(collection.collectionId);795 const contract = helper.ethNativeContract.collection(address, 'nft');796797 await token.approve(owner, {Ethereum: spender});798799 {800 const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);801 const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);802 const result = await contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: spender});803 const event = result.events.Transfer;804 expect(event).to.be.like({805 address: helper.ethAddress.fromCollectionId(collection.collectionId),806 event: 'Transfer',807 returnValues: {808 from: helper.address.substrateToEth(owner.address),809 to: helper.address.substrateToEth(receiver.address),810 tokenId: token.tokenId.toString(),811 },812 });813 }814815 expect(await token.getOwner()).to.be.like({Substrate: receiver.address});816 });817818 itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {819 const owner = await helper.eth.createAccountWithBalance(donor);820 const receiver = helper.eth.createAccount();821822 const collection = await helper.nft.mintCollection(alice, {});823 const {tokenId} = await collection.mintToken(alice, {Ethereum: owner});824825 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', owner);826827 const cost = await helper.eth.recordCallFee(owner, () => contract.methods.transfer(receiver, tokenId).send({from: owner}));828 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));829 });830});831832describe('NFT: Substrate calls', () => {833 let donor: IKeyringPair;834 let alice: IKeyringPair;835836 before(async function() {837 await usingEthPlaygrounds(async (helper, privateKey) => {838 donor = await privateKey({filename: __filename});839 [alice] = await helper.arrange.createAccounts([20n], donor);840 });841 });842843 itEth('Events emitted for mint()', async ({helper}) => {844 const collection = await helper.nft.mintCollection(alice, {});845 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);846 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');847848 const events: any = [];849 contract.events.allEvents((_: any, event: any) => {850 events.push(event);851 });852853 const {tokenId} = await collection.mintToken(alice);854 if (events.length == 0) await helper.wait.newBlocks(1);855 const event = events[0];856857 expect(event.event).to.be.equal('Transfer');858 expect(event.address).to.be.equal(collectionAddress);859 expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000');860 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(alice.address));861 expect(event.returnValues.tokenId).to.be.equal(tokenId.toString());862 });863864 itEth('Events emitted for burn()', async ({helper}) => {865 const collection = await helper.nft.mintCollection(alice, {});866 const token = await collection.mintToken(alice);867868 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);869 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');870871 const events: any = [];872 contract.events.allEvents((_: any, event: any) => {873 events.push(event);874 });875876 await token.burn(alice);877 if (events.length == 0) await helper.wait.newBlocks(1);878 const event = events[0];879880 expect(event.event).to.be.equal('Transfer');881 expect(event.address).to.be.equal(collectionAddress);882 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));883 expect(event.returnValues.to).to.be.equal('0x0000000000000000000000000000000000000000');884 expect(event.returnValues.tokenId).to.be.equal(token.tokenId.toString());885 });886887 itEth('Events emitted for approve()', async ({helper}) => {888 const receiver = helper.eth.createAccount();889890 const collection = await helper.nft.mintCollection(alice, {});891 const token = await collection.mintToken(alice);892893 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);894 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');895896 const events: any = [];897 contract.events.allEvents((_: any, event: any) => {898 events.push(event);899 });900901 await token.approve(alice, {Ethereum: receiver});902 if (events.length == 0) await helper.wait.newBlocks(1);903 const event = events[0];904905 expect(event.event).to.be.equal('Approval');906 expect(event.address).to.be.equal(collectionAddress);907 expect(event.returnValues.owner).to.be.equal(helper.address.substrateToEth(alice.address));908 expect(event.returnValues.approved).to.be.equal(receiver);909 expect(event.returnValues.tokenId).to.be.equal(token.tokenId.toString());910 });911912 itEth('Events emitted for transferFrom()', async ({helper}) => {913 const [bob] = await helper.arrange.createAccounts([10n], donor);914 const receiver = helper.eth.createAccount();915916 const collection = await helper.nft.mintCollection(alice, {});917 const token = await collection.mintToken(alice);918 await token.approve(alice, {Substrate: bob.address});919920 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);921 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');922923 const events: any = [];924 contract.events.allEvents((_: any, event: any) => {925 events.push(event);926 });927928 await token.transferFrom(bob, {Substrate: alice.address}, {Ethereum: receiver});929930 if (events.length == 0) await helper.wait.newBlocks(1);931 const event = events[0];932933 expect(event.address).to.be.equal(collectionAddress);934 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));935 expect(event.returnValues.to).to.be.equal(receiver);936 expect(event.returnValues.tokenId).to.be.equal(`${token.tokenId}`);937 });938939 itEth('Events emitted for transfer()', async ({helper}) => {940 const receiver = helper.eth.createAccount();941942 const collection = await helper.nft.mintCollection(alice, {});943 const token = await collection.mintToken(alice);944945 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);946 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');947948 const events: any = [];949 contract.events.allEvents((_: any, event: any) => {950 events.push(event);951 });952953 await token.transfer(alice, {Ethereum: receiver});954955 if (events.length == 0) await helper.wait.newBlocks(1);956 const event = events[0];957958 expect(event.address).to.be.equal(collectionAddress);959 expect(event.returnValues.from).to.be.equal(helper.address.substrateToEth(alice.address));960 expect(event.returnValues.to).to.be.equal(receiver);961 expect(event.returnValues.tokenId).to.be.equal(`${token.tokenId}`);962 });963});964965describe('Common metadata', () => {966 let donor: IKeyringPair;967 let alice: IKeyringPair;968969 before(async function() {970 await usingEthPlaygrounds(async (helper, privateKey) => {971 donor = await privateKey({filename: __filename});972 [alice] = await helper.arrange.createAccounts([20n], donor);973 });974 });975976 itEth('Returns collection name', async ({helper}) => {977 const caller = await helper.eth.createAccountWithBalance(donor);978 const tokenPropertyPermissions = [{979 key: 'URI',980 permission: {981 mutable: true,982 collectionAdmin: true,983 tokenOwner: false,984 },985 }];986 const collection = await helper.nft.mintCollection(987 alice,988 {989 name: 'oh River',990 tokenPrefix: 'CHANGE',991 properties: [{key: 'ERC721Metadata', value: '1'}],992 tokenPropertyPermissions,993 },994 );995996 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);997 const name = await contract.methods.name().call();998 expect(name).to.equal('oh River');999 });10001001 itEth('Returns symbol name', async ({helper}) => {1002 const caller = await helper.eth.createAccountWithBalance(donor);1003 const tokenPropertyPermissions = [{1004 key: 'URI',1005 permission: {1006 mutable: true,1007 collectionAdmin: true,1008 tokenOwner: false,1009 },1010 }];1011 const collection = await helper.nft.mintCollection(1012 alice,1013 {1014 name: 'oh River',1015 tokenPrefix: 'CHANGE',1016 properties: [{key: 'ERC721Metadata', value: '1'}],1017 tokenPropertyPermissions,1018 },1019 );10201021 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller);1022 const symbol = await contract.methods.symbol().call();1023 expect(symbol).to.equal('CHANGE');1024 });1025});10261027describe('Negative tests', () => {1028 let donor: IKeyringPair;1029 let minter: IKeyringPair;1030 let alice: IKeyringPair;10311032 before(async function() {1033 await usingEthPlaygrounds(async (helper, privateKey) => {1034 donor = await privateKey({filename: __filename});1035 [minter, alice] = await helper.arrange.createAccounts([100n, 100n], donor);1036 });1037 });10381039 itEth('[negative] Cant perform burn without approval', async ({helper}) => {1040 const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});10411042 const owner = await helper.eth.createAccountWithBalance(donor, 100n);1043 const spender = await helper.eth.createAccountWithBalance(donor, 100n);10441045 const token = await collection.mintToken(minter, {Ethereum: owner});10461047 const address = helper.ethAddress.fromCollectionId(collection.collectionId);1048 const contract = helper.ethNativeContract.collection(address, 'nft');10491050 const ownerCross = helper.ethCrossAccount.fromAddress(owner);1051 await expect(contract.methods.burnFromCross(ownerCross, token.tokenId).send({from: spender})).to.be.rejected;10521053 await contract.methods.setApprovalForAll(spender, true).send({from: owner});1054 await contract.methods.setApprovalForAll(spender, false).send({from: owner});10551056 await expect(contract.methods.burnFromCross(ownerCross, token.tokenId).send({from: spender})).to.be.rejected;1057 });10581059 itEth('[negative] Cant perform transfer without approval', async ({helper}) => {1060 const collection = await helper.nft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});1061 const receiver = alice;10621063 const owner = await helper.eth.createAccountWithBalance(donor, 100n);1064 const spender = await helper.eth.createAccountWithBalance(donor, 100n);10651066 const token = await collection.mintToken(minter, {Ethereum: owner});10671068 const address = helper.ethAddress.fromCollectionId(collection.collectionId);1069 const contract = helper.ethNativeContract.collection(address, 'nft');10701071 const ownerCross = helper.ethCrossAccount.fromAddress(owner);1072 const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);10731074 await expect(contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: spender})).to.be.rejected;10751076 await contract.methods.setApprovalForAll(spender, true).send({from: owner});1077 await contract.methods.setApprovalForAll(spender, false).send({from: owner});1078 1079 await expect(contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: spender})).to.be.rejected;1080 });1081});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"