From 994aeb14c5bbeaf08b07085abaa0b7f67fffcbed Mon Sep 17 00:00:00 2001 From: Max Andreev Date: Wed, 21 Dec 2022 10:46:27 +0000 Subject: [PATCH] Add checks to mintCross tests --- --- 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}) => { --- a/tests/src/eth/nonFungible.test.ts +++ b/tests/src/eth/nonFungible.test.ts @@ -175,55 +175,82 @@ // expect(tokenUri).to.be.equal(`https://offchain-service.local/token-info/${nextTokenId}`); }); - itEth('Can perform mintCross()', async ({helper}) => { - const caller = await helper.eth.createAccountWithBalance(donor); - const receiverCross = helper.ethCrossAccount.fromKeyringPair(bob); - const properties = Array(5).fill(0).map((_, i) => { return {key: `key_${i}`, value: Buffer.from(`value_${i}`)}; }); - const permissions: ITokenPropertyPermission[] = properties - .map(p => { - return { - key: p.key, permission: { - tokenOwner: true, - collectionAdmin: true, - mutable: true, - }, - }; - }); + // TODO combine all minting tests in one place + [ + 'substrate' as const, + 'ethereum' as const, + ].map(testCase => { + itEth(`Can perform mintCross() for ${testCase} address`, async ({helper}) => { + const collectionAdmin = await helper.eth.createAccountWithBalance(donor); + + const receiverEth = helper.eth.createAccount(); + const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth); + const receiverSub = bob; + const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(receiverSub); + + // const receiverCross = helper.ethCrossAccount.fromKeyringPair(bob); + const properties = Array(5).fill(0).map((_, i) => { return {key: `key_${i}`, value: Buffer.from(`value_${i}`)}; }); + const permissions: ITokenPropertyPermission[] = properties + .map(p => { + return { + key: p.key, permission: { + tokenOwner: true, + collectionAdmin: true, + mutable: true, + }, + }; + }); - const collection = await helper.nft.mintCollection(minter, { - tokenPrefix: 'ethp', - tokenPropertyPermissions: permissions, - }); - await collection.addAdmin(minter, {Ethereum: caller}); + const collection = await helper.nft.mintCollection(minter, { + tokenPrefix: 'ethp', + tokenPropertyPermissions: permissions, + }); + await collection.addAdmin(minter, {Ethereum: collectionAdmin}); - const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId); - const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', caller, true); - let expectedTokenId = await contract.methods.nextTokenId().call(); - let result = await contract.methods.mintCross(receiverCross, []).send(); - let tokenId = result.events.Transfer.returnValues.tokenId; - expect(tokenId).to.be.equal(expectedTokenId); + const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId); + const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', collectionAdmin, true); + let expectedTokenId = await contract.methods.nextTokenId().call(); + let result = await contract.methods.mintCross(testCase === 'ethereum' ? receiverCrossEth : receiverCrossSub, []).send(); + let tokenId = result.events.Transfer.returnValues.tokenId; + expect(tokenId).to.be.equal(expectedTokenId); - let event = result.events.Transfer; - expect(event.address).to.be.equal(collectionAddress); - expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000'); - expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(bob.address)); - expect(await contract.methods.properties(tokenId, []).call()).to.be.like([]); + let event = result.events.Transfer; + expect(event.address).to.be.equal(collectionAddress); + expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000'); + expect(event.returnValues.to).to.be.equal(testCase === 'ethereum' ? receiverEth : helper.address.substrateToEth(bob.address)); + expect(await contract.methods.properties(tokenId, []).call()).to.be.like([]); - expectedTokenId = await contract.methods.nextTokenId().call(); - result = await contract.methods.mintCross(receiverCross, properties).send(); - event = result.events.Transfer; - expect(event.address).to.be.equal(collectionAddress); - expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000'); - expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(bob.address)); - expect(await contract.methods.properties(tokenId, []).call()).to.be.like([]); + expectedTokenId = await contract.methods.nextTokenId().call(); + result = await contract.methods.mintCross(testCase === 'ethereum' ? receiverCrossEth : receiverCrossSub, properties).send(); + event = result.events.Transfer; + expect(event.address).to.be.equal(collectionAddress); + expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000'); + expect(event.returnValues.to).to.be.equal(testCase === 'ethereum' ? receiverEth : helper.address.substrateToEth(bob.address)); + expect(await contract.methods.properties(tokenId, []).call()).to.be.like([]); - tokenId = result.events.Transfer.returnValues.tokenId; + tokenId = result.events.Transfer.returnValues.tokenId; - expect(tokenId).to.be.equal(expectedTokenId); + expect(tokenId).to.be.equal(expectedTokenId); - expect(await contract.methods.properties(tokenId, []).call()).to.be.like(properties - .map(p => { return helper.ethProperty.property(p.key, p.value.toString()); })); + expect(await contract.methods.properties(tokenId, []).call()).to.be.like(properties + .map(p => { return helper.ethProperty.property(p.key, p.value.toString()); })); + + expect(await helper.nft.getTokenOwner(collection.collectionId, tokenId)) + .to.deep.eq(testCase === 'ethereum' ? {Ethereum: receiverEth.toLowerCase()} : {Substrate: receiverSub.address}); + }); + }); + + itEth('Non-owner and non admin cannot mintCross', async ({helper}) => { + const nonOwner = await helper.eth.createAccountWithBalance(donor); + const nonOwnerCross = helper.ethCrossAccount.fromAddress(nonOwner); + + const collection = await helper.nft.mintCollection(minter); + const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId); + const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft'); + + await expect(collectionEvm.methods.mintCross(nonOwnerCross, []).call({from: nonOwner})) + .to.be.rejectedWith('PublicMintingNotAllowed'); }); //TODO: CORE-302 add eth methods --- a/tests/src/eth/reFungible.test.ts +++ b/tests/src/eth/reFungible.test.ts @@ -137,48 +137,61 @@ 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: true, + collectionAdmin: true, + mutable: true}}; }); - 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}) => { -- gitstuff