From e69b17ab5001bc893796b731ba6eec23f4639da2 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Mon, 11 Oct 2021 17:59:13 +0530 Subject: [PATCH 01/43] refactored before block --- test/6_erc1155721.ts | 62 +++++++++++++------------------------------- 1 file changed, 18 insertions(+), 44 deletions(-) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index 1a704bdd..27539497 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -142,21 +142,25 @@ describe('ERC1155ERC721', () => { ); } + async function prepareUtils() { + utils = await UtilsBuilder.create() + .ETHETH() + .buildAsync( + contractERC1155ERC721, + contractVoucherKernel, + contractCashier, + contractBosonRouter + ); + + timestamp = await Utils.getCurrTimestamp(); + return utils; + } + describe('Multi-token contract', function () { describe('Common', () => { before(async () => { await deployContracts(); - - utils = await UtilsBuilder.create() - .ETHETH() - .buildAsync( - contractERC1155ERC721, - contractVoucherKernel, - contractCashier, - contractBosonRouter - ); - - timestamp = await Utils.getCurrTimestamp(); + utils = await prepareUtils(); }); it('Should have correct name on deploy', async () => { @@ -379,17 +383,7 @@ describe('ERC1155ERC721', () => { describe('Negative 1155 Transfers', () => { beforeEach(async () => { await deployContracts(); - - utils = await UtilsBuilder.create() - .ETHETH() - .buildAsync( - contractERC1155ERC721, - contractVoucherKernel, - contractCashier, - contractBosonRouter - ); - - timestamp = await Utils.getCurrTimestamp(); + utils = await prepareUtils(); TOKEN_SUPPLY_ID = await utils.createOrder( users.seller, @@ -499,17 +493,7 @@ describe('ERC1155ERC721', () => { describe('Negative 721 Transfers', () => { beforeEach(async () => { await deployContracts(); - - utils = await UtilsBuilder.create() - .ETHETH() - .buildAsync( - contractERC1155ERC721, - contractVoucherKernel, - contractCashier, - contractBosonRouter - ); - - timestamp = await Utils.getCurrTimestamp(); + utils = await prepareUtils(); TOKEN_SUPPLY_ID = await utils.createOrder( users.seller, @@ -627,17 +611,7 @@ describe('ERC1155ERC721', () => { before(async () => { await deployContracts(); - - utils = await UtilsBuilder.create() - .ETHETH() - .buildAsync( - contractERC1155ERC721, - contractVoucherKernel, - contractCashier, - contractBosonRouter - ); - - timestamp = await Utils.getCurrTimestamp(); + utils = await prepareUtils(); TOKEN_SUPPLY_ID = await utils.createOrder( users.seller, From 9da87fd3f943ef9b9de3178d82d29902d19ebd36 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Mon, 11 Oct 2021 18:21:49 +0530 Subject: [PATCH 02/43] Renaming the it blocks appropriately --- test/6_erc1155721.ts | 64 +++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index 27539497..57d405f3 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -1,10 +1,8 @@ import {ethers} from 'hardhat'; import {Signer, ContractFactory, Contract} from 'ethers'; - import {assert, expect} from 'chai'; import constants from '../testHelpers/constants'; - import Users from '../testHelpers/users'; import Utils from '../testHelpers/utils'; import UtilsBuilder from '../testHelpers/utilsBuilder'; @@ -58,7 +56,7 @@ describe('ERC1155ERC721', () => { contractBSNTokenDeposit: MockERC20Permit, contractTokenRegistry: TokenRegistry; - let timestamp; + let timestamp: number; async function deployContracts() { const sixtySeconds = 60; @@ -163,14 +161,14 @@ describe('ERC1155ERC721', () => { utils = await prepareUtils(); }); - it('Should have correct name on deploy', async () => { + it('[name] Should have correct name on deploy', async () => { const expectedName = 'Boson Smart Voucher'; const actual = await contractERC1155ERC721.name(); assert.equal(actual, expectedName, 'name not set correctly!'); }); - it('Should have correct symbol on deploy', async () => { + it('[symbol] Should have correct symbol on deploy', async () => { const expectedSymbol = 'BSV'; const actual = await contractERC1155ERC721.symbol(); @@ -178,12 +176,6 @@ describe('ERC1155ERC721', () => { assert.equal(actual, expectedSymbol, 'symbol not set correctly!'); }); - it('[NEGATIVE][setApprovalForAll] Should revert if tries to set self as an operator', async () => { - await expect( - contractERC1155ERC721.setApprovalForAll(users.deployer.address, true) - ).to.be.revertedWith(revertReasons.REDUNDANT_CALL); - }); - it('[setApprovalForAll] Should emit ApprovalForAll', async () => { const tx = await contractERC1155ERC721.setApprovalForAll( contractVoucherKernel.address, @@ -212,6 +204,12 @@ describe('ERC1155ERC721', () => { ); }); + it('[NEGATIVE][setApprovalForAll] Should revert if tries to set self as an operator', async () => { + await expect( + contractERC1155ERC721.setApprovalForAll(users.deployer.address, true) + ).to.be.revertedWith(revertReasons.REDUNDANT_CALL); + }); + it('Should emit TransferSingle event', async () => { const txFillOrder = await utils.createOrder( users.seller, @@ -301,7 +299,7 @@ describe('ERC1155ERC721', () => { ); }); - it('Owner should approve transfer of erc721', async () => { + it('[approve] Owner should approve transfer of erc721', async () => { const token721 = await utils.commitToBuy( users.buyer, users.seller, @@ -343,7 +341,7 @@ describe('ERC1155ERC721', () => { ); }); - it('[NEGATIVE] Attacker should not approve transfer of erc721 that does not possess', async () => { + it('[NEGATIVE][approve] Attacker should not approve transfer of erc721 that does not possess', async () => { const token721 = await utils.commitToBuy( users.buyer, users.seller, @@ -361,7 +359,7 @@ describe('ERC1155ERC721', () => { ).to.be.revertedWith(revertReasons.UNAUTHORIZED_APPROVAL); }); - it('[NEGATIVE] Should revert if buyer tries to approve to self', async () => { + it('[NEGATIVE][approve] Should revert if buyer tries to approve to self', async () => { const token721 = await utils.commitToBuy( users.buyer, users.seller, @@ -396,7 +394,7 @@ describe('ERC1155ERC721', () => { ); }); - it('Attacker should not be able to transfer', async () => { + it('[NEGATIVE][safeTransfer1155] Attacker should not be able to transfer', async () => { await expect( utils.safeTransfer1155( users.seller.address, @@ -408,7 +406,7 @@ describe('ERC1155ERC721', () => { ).to.be.revertedWith(revertReasons.UNAUTHORIZED_TRANSFER_1155); }); - it('Seller should not transfer to ZERO address', async () => { + it('[NEGATIVE][safeTransfer1155] Seller should not transfer to ZERO address', async () => { await expect( utils.safeTransfer1155( users.seller.address, @@ -420,7 +418,7 @@ describe('ERC1155ERC721', () => { ).to.be.revertedWith(revertReasons.UNSPECIFIED_ADDRESS); }); - it('Seller should not transfer to contract address', async () => { + it('[NEGATIVE][safeTransfer1155] Seller should not transfer to contract address', async () => { await expect( utils.safeTransfer1155( users.seller.address, @@ -432,7 +430,7 @@ describe('ERC1155ERC721', () => { ).to.be.revertedWith(revertReasons.FN_SELECTOR_NOT_RECOGNIZED); }); - it('Should not be able to transfer batch to ZERO address', async () => { + it('[NEGATIVE][safeBatchTransfer1155] Should not be able to transfer batch to ZERO address', async () => { await expect( utils.safeBatchTransfer1155( users.seller.address, @@ -444,7 +442,7 @@ describe('ERC1155ERC721', () => { ).to.be.revertedWith(revertReasons.UNSPECIFIED_ADDRESS); }); - it('Should revert if array lengths mismatch', async () => { + it('[NEGATIVE][safeBatchTransfer1155] Should revert if array lengths mismatch', async () => { await expect( utils.safeBatchTransfer1155( users.seller.address, @@ -456,7 +454,7 @@ describe('ERC1155ERC721', () => { ).to.be.revertedWith(revertReasons.MISMATCHED_ARRAY_LENGTHS); }); - it('Seller should not transfer batch to contract address', async () => { + it('[NEGATIVE][safeBatchTransfer1155] Seller should not transfer batch to contract address', async () => { await expect( utils.safeBatchTransfer1155( users.seller.address, @@ -468,7 +466,7 @@ describe('ERC1155ERC721', () => { ).to.be.revertedWith(revertReasons.FN_SELECTOR_NOT_RECOGNIZED); }); - it('Should revert if attacker tries to transfer batch', async () => { + it('[NEGATIVE][safeBatchTransfer1155] Should revert if attacker tries to transfer batch', async () => { await expect( utils.safeBatchTransfer1155( users.seller.address, @@ -480,7 +478,7 @@ describe('ERC1155ERC721', () => { ).to.be.revertedWith(revertReasons.UNAUTHORIZED_TRANSFER_BATCH_1155); }); - it('Should revert if balanceOfBatch has been provided with mismatched lengths', async () => { + it('[NEGATIVE][balanceOfBatch] Should revert if balanceOfBatch has been provided with mismatched lengths', async () => { await expect( contractERC1155ERC721.balanceOfBatch( [users.seller.address], @@ -506,7 +504,7 @@ describe('ERC1155ERC721', () => { ); }); - it('[ownerOf] should revert if incorrectId id provided', async () => { + it('[NEGATIVE][ownerOf] should revert if incorrectId id provided', async () => { const sellerInstance = contractERC1155ERC721.connect( users.seller.signer ); @@ -515,7 +513,7 @@ describe('ERC1155ERC721', () => { ); }); - it('[balanceOf] should revert if ZERO address is provided', async () => { + it('[NEGATIVE][balanceOf] should revert if ZERO address is provided', async () => { const balanceOf = contractERC1155ERC721.functions[fnSignatures.balanceOf721]; @@ -524,7 +522,7 @@ describe('ERC1155ERC721', () => { ); }); - it('Should not be able to transfer to contract address', async () => { + it('[NEGATIVE][safeTransfer721] Should not be able to transfer to contract address', async () => { const erc721 = await utils.commitToBuy( users.buyer, users.seller, @@ -543,7 +541,7 @@ describe('ERC1155ERC721', () => { ).to.be.revertedWith(revertReasons.FN_SELECTOR_NOT_RECOGNIZED); }); - it('Attacker should not be able to transfer erc721', async () => { + it('[NEGATIVE][safeTransfer721] Attacker should not be able to transfer erc721', async () => { const erc721 = await utils.commitToBuy( users.buyer, users.seller, @@ -562,7 +560,7 @@ describe('ERC1155ERC721', () => { ).to.be.revertedWith(revertReasons.NOT_OWNER_NOR_APPROVED); }); - it('Should not be able to transfer erc721 to ZERO address', async () => { + it('[NEGATIVE][safeTransfer721] Should not be able to transfer erc721 to ZERO address', async () => { const erc721 = await utils.commitToBuy( users.buyer, users.seller, @@ -581,7 +579,7 @@ describe('ERC1155ERC721', () => { ).to.be.revertedWith(revertReasons.UNSPECIFIED_ADDRESS); }); - it('Should not be able to transfer erc721 if address from is not authorized', async () => { + it('[NEGATIVE][safeTransfer721] Should not be able to transfer erc721 if address from is not authorized', async () => { const erc721 = await utils.commitToBuy( users.buyer, users.seller, @@ -636,12 +634,12 @@ describe('ERC1155ERC721', () => { await contractERC1155ERC721._set721Route(metadata721Route); }); - it('Should return correct url for erc1155', async () => { + it('[uri]Should return correct url for erc1155', async () => { const url = await contractERC1155ERC721.uri(TOKEN_SUPPLY_ID); assert.equal(url, metadataBase + metadata1155Route + TOKEN_SUPPLY_ID); }); - it('Should return correct url for erc721', async () => { + it('[tokenURI] Should return correct url for erc721', async () => { const url = await contractERC1155ERC721.tokenURI(erc721); assert.equal(url, metadataBase + metadata721Route + erc721); @@ -653,7 +651,7 @@ describe('ERC1155ERC721', () => { ).to.be.revertedWith(revertReasons.INVALID_ID); }); - it('[NEGATIVE] Should revert if attacker tries to set metadataBase', async () => { + it('[NEGATIVE][_setMetadataBase] Should revert if attacker tries to set metadataBase', async () => { const attackerInstance = contractERC1155ERC721.connect( users.attacker.signer ); @@ -663,7 +661,7 @@ describe('ERC1155ERC721', () => { ).to.be.revertedWith(revertReasons.UNAUTHORIZED_OWNER); }); - it('[NEGATIVE] Should revert if attacker tries to set metadata1155Route', async () => { + it('[NEGATIVE][_set1155Route] Should revert if attacker tries to set metadata1155Route', async () => { const attackerInstance = contractERC1155ERC721.connect( users.attacker.signer ); @@ -672,7 +670,7 @@ describe('ERC1155ERC721', () => { ).to.be.revertedWith(revertReasons.UNAUTHORIZED_OWNER); }); - it('[NEGATIVE] Should revert if attacker tries to set metadata721Route', async () => { + it('[NEGATIVE][_set721Route] Should revert if attacker tries to set metadata721Route', async () => { const attackerInstance = contractERC1155ERC721.connect( users.attacker.signer ); From 4da846a1a3bcfb3ba250b542b357795f488537fe Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Tue, 12 Oct 2021 09:53:50 +0530 Subject: [PATCH 03/43] unit test of transferFrom --- test/6_erc1155721.ts | 56 ++++++++++++++++++++++++++++--- testHelpers/functionSignatures.ts | 1 + testHelpers/utils.ts | 15 +++++++++ 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index 57d405f3..38c46198 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -378,7 +378,7 @@ describe('ERC1155ERC721', () => { }); }); - describe('Negative 1155 Transfers', () => { + describe('[NEGATIVE] 1155 Transfers', () => { beforeEach(async () => { await deployContracts(); utils = await prepareUtils(); @@ -488,7 +488,7 @@ describe('ERC1155ERC721', () => { }); }); - describe('Negative 721 Transfers', () => { + describe('[Negative] 721 Transfers', () => { beforeEach(async () => { await deployContracts(); utils = await prepareUtils(); @@ -504,7 +504,7 @@ describe('ERC1155ERC721', () => { ); }); - it('[NEGATIVE][ownerOf] should revert if incorrectId id provided', async () => { + it('[NEGATIVE][ownerOf] should revert if incorrect id provided', async () => { const sellerInstance = contractERC1155ERC721.connect( users.seller.signer ); @@ -599,6 +599,54 @@ describe('ERC1155ERC721', () => { revertReasons.TRANSFER_721_ADDRESS_FROM_NOT_AUTHORIZED ); }); + + it('[transferFrom] Should be able to transfer ownership of ERC721 to another address', async () => { + const oldOwner = users.buyer; + const expectedNewOwner = users.other2; + + const erc721 = await utils.commitToBuy( + oldOwner, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ); + + await utils.transfer721( + oldOwner.address, + expectedNewOwner.address, + erc721, + users.buyer.signer + ); + + const owner721Instance = contractERC1155ERC721.connect( + users.buyer.signer + ); + const newTokenOwner = await owner721Instance.ownerOf(erc721); + + assert.equal(newTokenOwner, expectedNewOwner.address); + }); + + it('[NEGATIVE][transferFrom] Should not be able to transfer erc721 if address from is not authorized', async () => { + const erc721 = await utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ); + + await expect( + utils.transfer721( + users.other1.address, + users.other2.address, + erc721, + users.buyer.signer + ) + ).to.be.revertedWith( + revertReasons.TRANSFER_721_ADDRESS_FROM_NOT_AUTHORIZED + ); + }); }); describe('Metadata', () => { @@ -634,7 +682,7 @@ describe('ERC1155ERC721', () => { await contractERC1155ERC721._set721Route(metadata721Route); }); - it('[uri]Should return correct url for erc1155', async () => { + it('[uri] Should return correct url for erc1155', async () => { const url = await contractERC1155ERC721.uri(TOKEN_SUPPLY_ID); assert.equal(url, metadataBase + metadata1155Route + TOKEN_SUPPLY_ID); }); diff --git a/testHelpers/functionSignatures.ts b/testHelpers/functionSignatures.ts index c558821f..2fc8daf6 100644 --- a/testHelpers/functionSignatures.ts +++ b/testHelpers/functionSignatures.ts @@ -19,4 +19,5 @@ export default { 'bytes)', safeTransfer721: 'safeTransferFrom(' + 'address,' + 'address,' + 'uint256,' + 'bytes)', + transfer721: 'transferFrom(' + 'address,' + 'address,' + 'uint256)', }; diff --git a/testHelpers/utils.ts b/testHelpers/utils.ts index 197f997c..e02bdb8f 100644 --- a/testHelpers/utils.ts +++ b/testHelpers/utils.ts @@ -824,6 +824,21 @@ class Utils { ); } + async transfer721( + oldVoucherOwner: string, + newVoucherOwner: string, + voucherID: string, + signer: Signer + ): Promise { + const fromInstance = this.contractERC1155ERC721.connect( + signer + ) as ERC1155ERC721; + + const method = fromInstance.functions[fnSignatures.transfer721]; + + return await method(oldVoucherOwner, newVoucherOwner, voucherID); + } + calcTotalAmountToRecipients( event: DistributionEvent, distributionAmounts: DistributionAmounts, From 983ffb3556bb68704ad9f17d8d147517f1836777 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Tue, 12 Oct 2021 18:14:57 +0530 Subject: [PATCH 04/43] added unit test: getApproved --- test/6_erc1155721.ts | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index 38c46198..24e55877 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -378,7 +378,7 @@ describe('ERC1155ERC721', () => { }); }); - describe('[NEGATIVE] 1155 Transfers', () => { + describe('ERC1155', () => { beforeEach(async () => { await deployContracts(); utils = await prepareUtils(); @@ -488,7 +488,7 @@ describe('ERC1155ERC721', () => { }); }); - describe('[Negative] 721 Transfers', () => { + describe('ERC721', () => { beforeEach(async () => { await deployContracts(); utils = await prepareUtils(); @@ -647,6 +647,40 @@ describe('ERC1155ERC721', () => { revertReasons.TRANSFER_721_ADDRESS_FROM_NOT_AUTHORIZED ); }); + + it('[getApproved] Should return zero address if no address set', async () => { + const erc721 = await utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ); + + const owner721Instance = contractERC1155ERC721.connect( + users.buyer.signer + ); + const approvedAddress = await owner721Instance.getApproved(erc721); + assert.equal(approvedAddress, constants.ZERO_ADDRESS); + }); + + it('[getApproved] Should return the approved address for a token ID', async () => { + const expectedApprovedAddress = users.other1.address; + const erc721 = await utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ); + + const owner721Instance = contractERC1155ERC721.connect( + users.buyer.signer + ); + await owner721Instance.approve(expectedApprovedAddress, erc721); + const approvedAddress = await owner721Instance.getApproved(erc721); + assert.equal(approvedAddress, expectedApprovedAddress); + }); }); describe('Metadata', () => { From 72b1adb90d8201383a1973f86f534808fcd700e9 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Tue, 12 Oct 2021 18:17:27 +0530 Subject: [PATCH 05/43] added unit tests: mint erc721 --- test/6_erc1155721.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index 24e55877..298b0eef 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -681,6 +681,52 @@ describe('ERC1155ERC721', () => { const approvedAddress = await owner721Instance.getApproved(erc721); assert.equal(approvedAddress, expectedApprovedAddress); }); + + it('[mint] Should mint a token', async () => { + await contractERC1155ERC721.setVoucherKernelAddress( + users.deployer.address + ); + + const tokenIdForMint = 123; + const tx = await contractERC1155ERC721.functions[fnSignatures.mint721]( + users.other1.address, + tokenIdForMint + ); + + const txReceipt = await tx.wait(); + + eventUtils.assertEventEmitted( + txReceipt, + ERC1155ERC721_Factory, + eventNames.TRANSFER, + (ev) => { + assert.equal( + ev._from, + constants.ZERO_ADDRESS, + 'ev._from not as expected!' + ); + assert.equal( + ev._to, + users.other1.address, + 'ev._to not as expected!' + ); + assert.equal( + ev._tokenId, + tokenIdForMint, + 'ev._tokenId not as expected!' + ); + } + ); + }); + + it('[NEGATIVE][mint] must fail: unauthorized minting ERC-721', async () => { + await expect( + contractERC1155ERC721.functions[fnSignatures.mint721]( + users.attacker.address, + 666 + ) + ).to.be.revertedWith(revertReasons.UNAUTHORIZED_VK); + }); }); describe('Metadata', () => { From dd65ef08102b9f3b5a9b7331067927ff48f1f3af Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Tue, 12 Oct 2021 18:45:00 +0530 Subject: [PATCH 06/43] adding unit test: approve --- test/6_erc1155721.ts | 58 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index 298b0eef..24fd85da 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -682,6 +682,64 @@ describe('ERC1155ERC721', () => { assert.equal(approvedAddress, expectedApprovedAddress); }); + it('[approve] Should revert when same token owner is again approved', async () => { + const erc721 = await utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ); + + const owner721Instance = contractERC1155ERC721.connect( + users.buyer.signer + ); + + const tx = await owner721Instance.approve(users.other1.address, erc721); + const txReceipt = await tx.wait(); + + eventUtils.assertEventEmitted( + txReceipt, + ERC1155ERC721_Factory, + eventNames.APPROVAL, + (ev) => { + assert.equal( + ev._owner, + users.buyer.address, + 'ev._owner not as expected!' + ); + assert.equal( + ev._approved, + users.other1.address, + 'ev._approved not as expected!' + ); + assert.equal( + ev._tokenId._hex, + erc721._hex, + 'ev._tokenId not as expected!' + ); + } + ); + }); + + it('[NEGATIVE][approve] Should revert when same token owner is again approved', async () => { + const erc721 = await utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ); + + const owner721Instance = contractERC1155ERC721.connect( + users.buyer.signer + ); + + await expect( + owner721Instance.approve(users.buyer.address, erc721) + ).to.be.revertedWith(revertReasons.REDUNDANT_CALL); + }); + it('[mint] Should mint a token', async () => { await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address From c168fd2b024d6bcceb5e41c75b14667dd2f130ed Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Tue, 12 Oct 2021 18:51:21 +0530 Subject: [PATCH 07/43] Revert "adding unit test: approve" This reverts commit cd0815de92625ee7c06d02572c422d39d81e2e66. --- test/6_erc1155721.ts | 58 -------------------------------------------- 1 file changed, 58 deletions(-) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index 24fd85da..298b0eef 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -682,64 +682,6 @@ describe('ERC1155ERC721', () => { assert.equal(approvedAddress, expectedApprovedAddress); }); - it('[approve] Should revert when same token owner is again approved', async () => { - const erc721 = await utils.commitToBuy( - users.buyer, - users.seller, - TOKEN_SUPPLY_ID, - constants.product_price, - constants.buyer_deposit - ); - - const owner721Instance = contractERC1155ERC721.connect( - users.buyer.signer - ); - - const tx = await owner721Instance.approve(users.other1.address, erc721); - const txReceipt = await tx.wait(); - - eventUtils.assertEventEmitted( - txReceipt, - ERC1155ERC721_Factory, - eventNames.APPROVAL, - (ev) => { - assert.equal( - ev._owner, - users.buyer.address, - 'ev._owner not as expected!' - ); - assert.equal( - ev._approved, - users.other1.address, - 'ev._approved not as expected!' - ); - assert.equal( - ev._tokenId._hex, - erc721._hex, - 'ev._tokenId not as expected!' - ); - } - ); - }); - - it('[NEGATIVE][approve] Should revert when same token owner is again approved', async () => { - const erc721 = await utils.commitToBuy( - users.buyer, - users.seller, - TOKEN_SUPPLY_ID, - constants.product_price, - constants.buyer_deposit - ); - - const owner721Instance = contractERC1155ERC721.connect( - users.buyer.signer - ); - - await expect( - owner721Instance.approve(users.buyer.address, erc721) - ).to.be.revertedWith(revertReasons.REDUNDANT_CALL); - }); - it('[mint] Should mint a token', async () => { await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address From 090df56208c38d080a0cd17316da9996796e960f Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Tue, 12 Oct 2021 19:18:14 +0530 Subject: [PATCH 08/43] added unit test: setVoucherKernelAddress --- test/6_erc1155721.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index 298b0eef..2f9fae72 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -376,6 +376,38 @@ describe('ERC1155ERC721', () => { attackerInstance.approve(users.buyer.address, token721) ).to.be.revertedWith(revertReasons.REDUNDANT_CALL); }); + + it('[setVoucherKernelAddress] Should set setVoucherKernelAddress to valid address', async () => { + const tx = await contractERC1155ERC721.setVoucherKernelAddress( + users.other1.address + ); + + const txReceipt = await tx.wait(); + + eventUtils.assertEventEmitted( + txReceipt, + ERC1155ERC721_Factory, + eventNames.LOG_VK_SET, + (ev) => { + assert.equal( + ev._newVoucherKernel, + users.other1.address, + 'ev._newVoucherKernel not as expected!' + ); + assert.equal( + ev._triggeredBy, + users.deployer.address, + 'ev._triggeredBy not as expected!' + ); + } + ); + }); + + it('[NEGATIVE][setVoucherKernelAddress] Should revert for zero address', async () => { + await expect( + contractERC1155ERC721.setVoucherKernelAddress(constants.ZERO_ADDRESS) + ).to.be.revertedWith(revertReasons.ZERO_ADDRESS); + }); }); describe('ERC1155', () => { From 3da4116dc630333d5dd3655bc249cc9fbc139a3f Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Tue, 12 Oct 2021 19:24:46 +0530 Subject: [PATCH 09/43] added unit test: setCashierAddress --- test/6_erc1155721.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index 2f9fae72..ece9487f 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -408,6 +408,38 @@ describe('ERC1155ERC721', () => { contractERC1155ERC721.setVoucherKernelAddress(constants.ZERO_ADDRESS) ).to.be.revertedWith(revertReasons.ZERO_ADDRESS); }); + + it('[setCashierAddress] Should set setVoucherKernelAddress to valid address', async () => { + const tx = await contractERC1155ERC721.setCashierAddress( + contractCashier.address + ); + + const txReceipt = await tx.wait(); + + eventUtils.assertEventEmitted( + txReceipt, + ERC1155ERC721_Factory, + eventNames.LOG_CASHIER_SET, + (ev) => { + assert.equal( + ev._newCashier, + contractCashier.address, + 'ev._newCashier not as expected!' + ); + assert.equal( + ev._triggeredBy, + users.deployer.address, + 'ev._triggeredBy not as expected!' + ); + } + ); + }); + + it('[NEGATIVE][setCashierAddress] Should revert for zero address', async () => { + await expect( + contractERC1155ERC721.setVoucherKernelAddress(constants.ZERO_ADDRESS) + ).to.be.revertedWith(revertReasons.ZERO_ADDRESS); + }); }); describe('ERC1155', () => { From af8157b9150d35c37276f7e767dbb13cc5d91b36 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Tue, 12 Oct 2021 19:41:09 +0530 Subject: [PATCH 10/43] added unit test : ownerOf --- test/6_erc1155721.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index ece9487f..2d9e7ccb 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -568,6 +568,25 @@ describe('ERC1155ERC721', () => { ); }); + it('[ownerOf] should token owner address for valid token', async () => { + const expectedOwner = users.buyer; + + const erc721 = await utils.commitToBuy( + expectedOwner, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ); + + const owner721Instance = contractERC1155ERC721.connect( + users.buyer.signer + ); + const tokenOwner = await owner721Instance.ownerOf(erc721); + + assert.equal(tokenOwner, expectedOwner.address); + }); + it('[NEGATIVE][ownerOf] should revert if incorrect id provided', async () => { const sellerInstance = contractERC1155ERC721.connect( users.seller.signer From e13109c93c539335c17aa0e092625b2e4f1bbfcc Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Wed, 13 Oct 2021 09:32:19 +0530 Subject: [PATCH 11/43] added unit test: isApprovedForAll --- test/6_erc1155721.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index 2d9e7ccb..14dad287 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -440,6 +440,20 @@ describe('ERC1155ERC721', () => { contractERC1155ERC721.setVoucherKernelAddress(constants.ZERO_ADDRESS) ).to.be.revertedWith(revertReasons.ZERO_ADDRESS); }); + + it('[isApprovedForAll] Should return the approval status of an operator for a given account', async () => { + const expectedApprovalStatus = true; + await contractERC1155ERC721.setApprovalForAll( + contractVoucherKernel.address, + expectedApprovalStatus + ); + + const approvalStatus = await contractERC1155ERC721.isApprovedForAll( + users.deployer.address, + contractVoucherKernel.address + ); + assert.equal(approvalStatus, expectedApprovalStatus); + }); }); describe('ERC1155', () => { From 43eea1199c42aa9877c4089b0b21a533478e5f75 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Wed, 13 Oct 2021 09:33:33 +0530 Subject: [PATCH 12/43] added unit test: mint 1155 --- test/6_erc1155721.ts | 55 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index 14dad287..65d553c9 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -564,6 +564,61 @@ describe('ERC1155ERC721', () => { ) ).to.be.revertedWith(revertReasons.MISMATCHED_ARRAY_LENGTHS); }); + + it('[mint] Should mint a desired token', async () => { + await contractERC1155ERC721.setVoucherKernelAddress( + users.deployer.address + ); + + const tokenIdForMint = 123; + const tx = await contractERC1155ERC721.functions[fnSignatures.mint1155]( + users.other1.address, + tokenIdForMint, + constants.QTY_10, + ethers.utils.formatBytes32String('0x0') + ); + + const txReceipt = await tx.wait(); + + eventUtils.assertEventEmitted( + txReceipt, + ERC1155ERC721_Factory, + eventNames.TRANSFER_SINGLE, + (ev) => { + assert.equal( + ev._from, + constants.ZERO_ADDRESS, + 'ev._from not as expected!' + ); + assert.equal( + ev._to, + users.other1.address, + 'ev._to not as expected!' + ); + assert.equal(ev._id, tokenIdForMint, 'ev._id not as expected!'); + assert.equal( + ev._value, + constants.QTY_10, + 'ev._value not as expected!' + ); + } + ); + }); + + it('[NEGATIVE][mint] Should revert when to is a zero address', async () => { + await contractERC1155ERC721.setVoucherKernelAddress( + users.deployer.address + ); + + await expect( + contractERC1155ERC721.functions[fnSignatures.mint1155]( + constants.ZERO_ADDRESS, + 123, + constants.QTY_10, + ethers.utils.formatBytes32String('0x0') + ) + ).to.be.revertedWith(revertReasons.UNSPECIFIED_ADDRESS); + }); }); describe('ERC721', () => { From 7471c070a6a85a6923926310bddedf9187e0d660 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Wed, 13 Oct 2021 11:08:55 +0530 Subject: [PATCH 13/43] added unit test: burn 1155 --- test/6_erc1155721.ts | 53 +++++++++++++++++++++++++++++++ testHelpers/functionSignatures.ts | 1 + 2 files changed, 54 insertions(+) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index 65d553c9..c4b6e4d6 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -619,6 +619,59 @@ describe('ERC1155ERC721', () => { ) ).to.be.revertedWith(revertReasons.UNSPECIFIED_ADDRESS); }); + + it('[burn] Should burn an amount of tokens with the given ID', async () => { + await contractERC1155ERC721.setVoucherKernelAddress( + users.deployer.address + ); + + const tokenIdToBurn = TOKEN_SUPPLY_ID; + const tx = await contractERC1155ERC721.functions[fnSignatures.burn1155]( + users.seller.address, + tokenIdToBurn, + constants.QTY_10 + ); + + const txReceipt = await tx.wait(); + + eventUtils.assertEventEmitted( + txReceipt, + ERC1155ERC721_Factory, + eventNames.TRANSFER_SINGLE, + (ev) => { + assert.equal( + ev._from, + users.seller.address, + 'ev._from not as expected!' + ); + assert.equal( + ev._to, + constants.ZERO_ADDRESS, + 'ev._to not as expected!' + ); + assert.equal(ev._id, tokenIdToBurn, 'ev._id not as expected!'); + assert.equal( + ev._value, + constants.QTY_10, + 'ev._value not as expected!' + ); + } + ); + }); + + it('[NEGATIVE][burn] Should revert when to is a zero address', async () => { + await contractERC1155ERC721.setVoucherKernelAddress( + users.deployer.address + ); + + await expect( + contractERC1155ERC721.functions[fnSignatures.burn1155]( + constants.ZERO_ADDRESS, + TOKEN_SUPPLY_ID, + constants.QTY_10 + ) + ).to.be.revertedWith(revertReasons.UNSPECIFIED_ADDRESS); + }); }); describe('ERC721', () => { diff --git a/testHelpers/functionSignatures.ts b/testHelpers/functionSignatures.ts index 2fc8daf6..1a08fd73 100644 --- a/testHelpers/functionSignatures.ts +++ b/testHelpers/functionSignatures.ts @@ -20,4 +20,5 @@ export default { safeTransfer721: 'safeTransferFrom(' + 'address,' + 'address,' + 'uint256,' + 'bytes)', transfer721: 'transferFrom(' + 'address,' + 'address,' + 'uint256)', + burn1155: 'burn(' + 'address,' + 'uint256,' + 'uint256)', }; From 8ce16cd10c8a8565b029edd885251f0d1f708df8 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Wed, 13 Oct 2021 11:28:23 +0530 Subject: [PATCH 14/43] added unit test : balanceOfBatch --- test/6_erc1155721.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index c4b6e4d6..f7870257 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -556,6 +556,15 @@ describe('ERC1155ERC721', () => { ).to.be.revertedWith(revertReasons.UNAUTHORIZED_TRANSFER_BATCH_1155); }); + it('[balanceOfBatch] Should return balance of account-token pairs', async () => { + const balance = await contractERC1155ERC721.balanceOfBatch( + [users.seller.address], + [TOKEN_SUPPLY_ID] + ); + + assert.equal(balance[0].toString(), constants.QTY_10.toString()); + }); + it('[NEGATIVE][balanceOfBatch] Should revert if balanceOfBatch has been provided with mismatched lengths', async () => { await expect( contractERC1155ERC721.balanceOfBatch( From 03d4851a084f76362895426a25a3a77553fe9560 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Wed, 13 Oct 2021 16:19:03 +0530 Subject: [PATCH 15/43] added unit tests: mintBatch --- test/6_erc1155721.ts | 74 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index f7870257..c76f9ede 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -681,6 +681,80 @@ describe('ERC1155ERC721', () => { ) ).to.be.revertedWith(revertReasons.UNSPECIFIED_ADDRESS); }); + + it('[mintBatch] Should do batch minting of tokens', async () => { + await contractERC1155ERC721.setVoucherKernelAddress( + users.deployer.address + ); + + const tokenIds = TOKEN_SUPPLY_ID; + const tx = await contractERC1155ERC721.mintBatch( + users.seller.address, + [tokenIds], + [constants.QTY_10], + ethers.utils.formatBytes32String('0x0') + ); + + const txReceipt = await tx.wait(); + + eventUtils.assertEventEmitted( + txReceipt, + ERC1155ERC721_Factory, + eventNames.TRANSFER_BATCH, + (ev) => { + assert.equal( + ev._from, + constants.ZERO_ADDRESS, + 'ev._from not as expected!' + ); + assert.equal( + ev._to, + users.seller.address, + 'ev._to not as expected!' + ); + assert.equal( + ev._ids.toString(), + tokenIds.toString(), + 'ev._ids not as expected!' + ); + assert.equal( + ev._values.toString(), + constants.QTY_10.toString(), + 'ev._values not as expected!' + ); + } + ); + }); + + it('[NEGATIVE][mintBatch] Should revert when to is a zero address', async () => { + await contractERC1155ERC721.setVoucherKernelAddress( + users.deployer.address + ); + + await expect( + contractERC1155ERC721.mintBatch( + constants.ZERO_ADDRESS, + [TOKEN_SUPPLY_ID], + [constants.QTY_10], + ethers.utils.formatBytes32String('0x0') + ) + ).to.be.revertedWith(revertReasons.UNSPECIFIED_ADDRESS); + }); + + it('[NEGATIVE][mintBatch] Should revert if array lengths mismatch', async () => { + await contractERC1155ERC721.setVoucherKernelAddress( + users.deployer.address + ); + + await expect( + contractERC1155ERC721.mintBatch( + users.seller.address, + [TOKEN_SUPPLY_ID], + [constants.QTY_10, constants.QTY_1], + ethers.utils.formatBytes32String('0x0') + ) + ).to.be.revertedWith(revertReasons.MISMATCHED_ARRAY_LENGTHS); + }); }); describe('ERC721', () => { From ba1177f97a83d494c1572e33426c5c62d5b4c48e Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Wed, 13 Oct 2021 16:28:36 +0530 Subject: [PATCH 16/43] added unit tests: burnBatch --- test/6_erc1155721.ts | 71 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index c76f9ede..82b009ed 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -755,6 +755,77 @@ describe('ERC1155ERC721', () => { ) ).to.be.revertedWith(revertReasons.MISMATCHED_ARRAY_LENGTHS); }); + + it('[burnBatch] Should do batch minting of tokens', async () => { + await contractERC1155ERC721.setVoucherKernelAddress( + users.deployer.address + ); + + const tokenIds = TOKEN_SUPPLY_ID; + const tx = await contractERC1155ERC721.burnBatch( + users.seller.address, + [tokenIds], + [constants.QTY_10] + ); + + const txReceipt = await tx.wait(); + + eventUtils.assertEventEmitted( + txReceipt, + ERC1155ERC721_Factory, + eventNames.TRANSFER_BATCH, + (ev) => { + assert.equal( + ev._to, + constants.ZERO_ADDRESS, + 'ev._to not as expected!' + ); + assert.equal( + ev._from, + users.seller.address, + 'ev._from not as expected!' + ); + assert.equal( + ev._ids.toString(), + tokenIds.toString(), + 'ev._ids not as expected!' + ); + assert.equal( + ev._values.toString(), + constants.QTY_10.toString(), + 'ev._values not as expected!' + ); + } + ); + }); + + it('[NEGATIVE][burnBatch] Should revert when to is a zero address', async () => { + await contractERC1155ERC721.setVoucherKernelAddress( + users.deployer.address + ); + + await expect( + contractERC1155ERC721.burnBatch( + constants.ZERO_ADDRESS, + [TOKEN_SUPPLY_ID], + [constants.QTY_10] + ) + ).to.be.revertedWith(revertReasons.UNSPECIFIED_ADDRESS); + }); + + it('[NEGATIVE][burnBatch] Should revert if array lengths mismatch', async () => { + await contractERC1155ERC721.setVoucherKernelAddress( + users.deployer.address + ); + + await expect( + contractERC1155ERC721.burnBatch( + users.seller.address, + [TOKEN_SUPPLY_ID], + [constants.QTY_10, constants.QTY_1] + ) + ).to.be.revertedWith(revertReasons.MISMATCHED_ARRAY_LENGTHS); + }); }); describe('ERC721', () => { From 145cc253496935edc47bc2a14006a436b12e71c8 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Wed, 13 Oct 2021 16:43:51 +0530 Subject: [PATCH 17/43] added happy unit test: safeTransfer721 --- test/6_erc1155721.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index 82b009ed..15ca34a6 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -881,6 +881,33 @@ describe('ERC1155ERC721', () => { ); }); + it('[safeTransfer721] Should safely transfer the ownership of a given token ID to another address', async () => { + const oldOwner = users.buyer; + const expectedNewOwner = users.other2; + + const erc721 = await utils.commitToBuy( + oldOwner, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ); + + await utils.safeTransfer721( + oldOwner.address, + expectedNewOwner.address, + erc721, + users.buyer.signer + ); + + const owner721Instance = contractERC1155ERC721.connect( + users.buyer.signer + ); + const newTokenOwner = await owner721Instance.ownerOf(erc721); + + assert.equal(newTokenOwner, expectedNewOwner.address); + }); + it('[NEGATIVE][safeTransfer721] Should not be able to transfer to contract address', async () => { const erc721 = await utils.commitToBuy( users.buyer, From e3113f250615d93321c74908779b70c9d033ce96 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Wed, 13 Oct 2021 17:22:47 +0530 Subject: [PATCH 18/43] added happy unit tests: balanceOf 721 --- test/6_erc1155721.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index 15ca34a6..6e8bc765 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -872,6 +872,35 @@ describe('ERC1155ERC721', () => { ); }); + describe('[balanceOf] should count all NFTs assigned to an owner', async () => { + it('[balanceOf] returns 1 when 1 NFT is assigned to owner', async () => { + await utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ); + const expectedCount = 1; + + const balanceOfBuyer = await contractERC1155ERC721.functions[ + fnSignatures.balanceOf721 + ](users.buyer.address); + + assert.equal(balanceOfBuyer.toString(), expectedCount.toString()); + }); + + it('[balanceOf] returns 0 when no NFTs are assigned to owner', async () => { + const expectedCount = 0; + + const balanceOfBuyer = await contractERC1155ERC721.functions[ + fnSignatures.balanceOf721 + ](users.buyer.address); + + assert.equal(balanceOfBuyer.toString(), expectedCount.toString()); + }); + }); + it('[NEGATIVE][balanceOf] should revert if ZERO address is provided', async () => { const balanceOf = contractERC1155ERC721.functions[fnSignatures.balanceOf721]; From 179dc065b20c7eaef2c7258c90d2c9a020694718 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Wed, 13 Oct 2021 19:39:29 +0530 Subject: [PATCH 19/43] added happy unit test: balanceOf 1155 --- test/6_erc1155721.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index 6e8bc765..05303235 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -472,6 +472,27 @@ describe('ERC1155ERC721', () => { ); }); + it('[balanceOf] Get the balance of tokens of an account', async () => { + const expectedCount = constants.QTY_10; + await contractERC1155ERC721.setVoucherKernelAddress( + users.deployer.address + ); + + await contractERC1155ERC721.functions[fnSignatures.mint1155]( + users.deployer.address, + TOKEN_SUPPLY_ID, + expectedCount, + ethers.utils.formatBytes32String('0x0') + ); + + const balance = await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.deployer.address, + TOKEN_SUPPLY_ID + ); + + assert.equal(balance.toString(), expectedCount.toString()); + }); + it('[NEGATIVE][safeTransfer1155] Attacker should not be able to transfer', async () => { await expect( utils.safeTransfer1155( From e34ac1db32141175684c7f0bb0dd8db98fad160d Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Wed, 13 Oct 2021 19:59:04 +0530 Subject: [PATCH 20/43] lint fix --- test/6_erc1155721.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index 05303235..eae4e7ae 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -485,10 +485,9 @@ describe('ERC1155ERC721', () => { ethers.utils.formatBytes32String('0x0') ); - const balance = await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( - users.deployer.address, - TOKEN_SUPPLY_ID - ); + const balance = await contractERC1155ERC721.functions[ + fnSignatures.balanceOf1155 + ](users.deployer.address, TOKEN_SUPPLY_ID); assert.equal(balance.toString(), expectedCount.toString()); }); From 0dd8ed6e516881350e5dcec14d346ab728b8bc17 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Thu, 14 Oct 2021 17:01:36 +0530 Subject: [PATCH 21/43] review fixes: improved unit tests --- test/6_erc1155721.ts | 508 +++++++++++++++++------------- testHelpers/functionSignatures.ts | 2 + testHelpers/utils.ts | 19 ++ 3 files changed, 305 insertions(+), 224 deletions(-) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index eae4e7ae..4e98cdc1 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -6,6 +6,7 @@ import constants from '../testHelpers/constants'; import Users from '../testHelpers/users'; import Utils from '../testHelpers/utils'; import UtilsBuilder from '../testHelpers/utilsBuilder'; +const BN = ethers.BigNumber.from; import { BosonRouter, @@ -210,180 +211,13 @@ describe('ERC1155ERC721', () => { ).to.be.revertedWith(revertReasons.REDUNDANT_CALL); }); - it('Should emit TransferSingle event', async () => { - const txFillOrder = await utils.createOrder( - users.seller, - timestamp, - timestamp + constants.SECONDS_IN_DAY, - constants.product_price, - constants.seller_deposit, - constants.buyer_deposit, - constants.QTY_10, - true - ); - - const txReceipt = await txFillOrder.wait(); - - eventUtils.assertEventEmitted( - txReceipt, - ERC1155ERC721_Factory, - eventNames.TRANSFER_SINGLE, - (ev) => { - assert.equal( - ev._operator, - contractVoucherKernel.address, - '_operator not expected!' - ); - assert.equal( - ev._from, - constants.ZERO_ADDRESS, - '_from not expected!' - ); - assert.equal(ev._to, users.seller.address, '_to not expected!'); - assert.equal( - ev._value.toString(), - constants.QTY_10, - '_value not expected!' - ); - TOKEN_SUPPLY_ID = ev._id.toString(); - } - ); - }); - - it('Should emit TransferSingle (burn 1155) && Transfer(mint 721)', async () => { - const commitTx = await utils.commitToBuy( - users.buyer, - users.seller, - TOKEN_SUPPLY_ID, - constants.product_price, - constants.buyer_deposit, - true - ); - - const txReceipt = await commitTx.wait(); - - eventUtils.assertEventEmitted( - txReceipt, - ERC1155ERC721_Factory, - eventNames.TRANSFER_SINGLE, - (ev) => { - assert.equal( - ev._operator, - contractVoucherKernel.address, - '_operator not expected!' - ); - assert.equal(ev._from, users.seller.address, '_from not expected!'); - assert.equal(ev._to, constants.ZERO_ADDRESS, '_to not expected!'); - assert.equal( - ev._value.toString(), - constants.QTY_1, - '_value not expected!' - ); - return true; - } - ); - - eventUtils.assertEventEmitted( - txReceipt, - ERC1155ERC721_Factory, - eventNames.TRANSFER, - (ev) => { - assert.equal( - ev._from, - constants.ZERO_ADDRESS, - '_from not expected!' - ); - assert.equal(ev._to, users.buyer.address, '_to not expected!'); - return true; - } - ); - }); - - it('[approve] Owner should approve transfer of erc721', async () => { - const token721 = await utils.commitToBuy( - users.buyer, - users.seller, - TOKEN_SUPPLY_ID, - constants.product_price, - constants.buyer_deposit - ); - - const owner721Instance = contractERC1155ERC721.connect( - users.buyer.signer - ); - const tx = await owner721Instance.approve( - users.other1.address, - token721 - ); - const txReceipt = await tx.wait(); - - eventUtils.assertEventEmitted( - txReceipt, - ERC1155ERC721_Factory, - eventNames.APPROVAL, - (ev) => { - assert.equal( - ev._owner, - users.buyer.address, - 'Owner not as expected!' - ); - assert.equal( - ev._approved, - users.other1.address, - 'Approved not as expected!' - ); - assert.equal( - ev._tokenId.toString(), - token721.toString(), - 'tokenId not as expected!' - ); - } - ); - }); - - it('[NEGATIVE][approve] Attacker should not approve transfer of erc721 that does not possess', async () => { - const token721 = await utils.commitToBuy( - users.buyer, - users.seller, - TOKEN_SUPPLY_ID, - constants.product_price, - constants.buyer_deposit - ); - - const attackerInstance = contractERC1155ERC721.connect( - users.attacker.signer - ); - - await expect( - attackerInstance.approve(users.other1.address, token721) - ).to.be.revertedWith(revertReasons.UNAUTHORIZED_APPROVAL); - }); - - it('[NEGATIVE][approve] Should revert if buyer tries to approve to self', async () => { - const token721 = await utils.commitToBuy( - users.buyer, - users.seller, - TOKEN_SUPPLY_ID, - constants.product_price, - constants.buyer_deposit - ); - - const attackerInstance = contractERC1155ERC721.connect( - users.attacker.signer - ); - - await expect( - attackerInstance.approve(users.buyer.address, token721) - ).to.be.revertedWith(revertReasons.REDUNDANT_CALL); - }); - it('[setVoucherKernelAddress] Should set setVoucherKernelAddress to valid address', async () => { + const expectedVoucherKernelAddress = users.other1.address; const tx = await contractERC1155ERC721.setVoucherKernelAddress( - users.other1.address + expectedVoucherKernelAddress ); const txReceipt = await tx.wait(); - eventUtils.assertEventEmitted( txReceipt, ERC1155ERC721_Factory, @@ -401,6 +235,9 @@ describe('ERC1155ERC721', () => { ); } ); + + const voucherKernelAddress = await contractERC1155ERC721.getVoucherKernelAddress(); + assert.equal(voucherKernelAddress, expectedVoucherKernelAddress); }); it('[NEGATIVE][setVoucherKernelAddress] Should revert for zero address', async () => { @@ -409,9 +246,10 @@ describe('ERC1155ERC721', () => { ).to.be.revertedWith(revertReasons.ZERO_ADDRESS); }); - it('[setCashierAddress] Should set setVoucherKernelAddress to valid address', async () => { + it('[setCashierAddress] Should set setCashierAddress to valid address', async () => { + const expectedCashierAddress = contractCashier.address; const tx = await contractERC1155ERC721.setCashierAddress( - contractCashier.address + expectedCashierAddress ); const txReceipt = await tx.wait(); @@ -433,6 +271,9 @@ describe('ERC1155ERC721', () => { ); } ); + + const cashierAddress = await contractERC1155ERC721.getCashierAddress(); + assert.equal(cashierAddress, expectedCashierAddress); }); it('[NEGATIVE][setCashierAddress] Should revert for zero address', async () => { @@ -516,7 +357,7 @@ describe('ERC1155ERC721', () => { ).to.be.revertedWith(revertReasons.UNSPECIFIED_ADDRESS); }); - it('[NEGATIVE][safeTransfer1155] Seller should not transfer to contract address', async () => { + it('[NEGATIVE][safeTransfer1155] it should not be transferred to a contract that cannot receive it', async () => { await expect( utils.safeTransfer1155( users.seller.address, @@ -632,6 +473,24 @@ describe('ERC1155ERC721', () => { ); } ); + + const expectedBalance = constants.QTY_10; + const balanceOfOwner = await contractERC1155ERC721.functions[ + fnSignatures.balanceOf1155 + ](users.other1.address, tokenIdForMint); + + assert.equal(balanceOfOwner.toString(), expectedBalance.toString()); + }); + + it('[NEGATIVE][mint] must fail: unauthorized minting ERC-1155', async () => { + await expect( + contractERC1155ERC721.functions[fnSignatures.mint1155]( + users.attacker.address, + 666, + constants.QTY_10, + ethers.utils.formatBytes32String('0x0') + ) + ).to.be.revertedWith(revertReasons.UNAUTHORIZED_VK); }); it('[NEGATIVE][mint] Should revert when to is a zero address', async () => { @@ -686,9 +545,16 @@ describe('ERC1155ERC721', () => { ); } ); + + const expectedBalance = 0; + const balanceOfOwner = await contractERC1155ERC721.functions[ + fnSignatures.balanceOf721 + ](users.seller.address); + + assert.equal(balanceOfOwner.toString(), expectedBalance.toString()); }); - it('[NEGATIVE][burn] Should revert when to is a zero address', async () => { + it('[NEGATIVE][burn] Should revert when _account is a zero address', async () => { await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -707,11 +573,12 @@ describe('ERC1155ERC721', () => { users.deployer.address ); - const tokenIds = TOKEN_SUPPLY_ID; + const tokenIds = [BN(123), BN(456), BN(789)]; + const quantities = [BN(constants.QTY_10), BN(constants.QTY_15), BN(constants.QTY_20)]; const tx = await contractERC1155ERC721.mintBatch( users.seller.address, - [tokenIds], - [constants.QTY_10], + tokenIds, + quantities, ethers.utils.formatBytes32String('0x0') ); @@ -738,15 +605,31 @@ describe('ERC1155ERC721', () => { 'ev._ids not as expected!' ); assert.equal( - ev._values.toString(), - constants.QTY_10.toString(), + JSON.stringify(ev._values), + JSON.stringify(quantities), 'ev._values not as expected!' ); } ); + + const balanceOfToken1 = await contractERC1155ERC721.functions[ + fnSignatures.balanceOf1155 + ](users.seller.address, BN(123)); + + const balanceOfToken2 = await contractERC1155ERC721.functions[ + fnSignatures.balanceOf1155 + ](users.seller.address, BN(456)); + + const balanceOfToken3 = await contractERC1155ERC721.functions[ + fnSignatures.balanceOf1155 + ](users.seller.address, BN(789)); + + assert.equal(balanceOfToken1, constants.QTY_10); + assert.equal(balanceOfToken2, constants.QTY_15); + assert.equal(balanceOfToken3, constants.QTY_20); }); - it('[NEGATIVE][mintBatch] Should revert when to is a zero address', async () => { + it('[NEGATIVE][mintBatch] Should revert when _account is a zero address', async () => { await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -819,7 +702,7 @@ describe('ERC1155ERC721', () => { ); }); - it('[NEGATIVE][burnBatch] Should revert when to is a zero address', async () => { + it('[NEGATIVE][burnBatch] Should revert when _account is a zero address', async () => { await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -864,11 +747,9 @@ describe('ERC1155ERC721', () => { ); }); - it('[ownerOf] should token owner address for valid token', async () => { - const expectedOwner = users.buyer; - - const erc721 = await utils.commitToBuy( - expectedOwner, + it('[approve] Owner should approve transfer of erc721', async () => { + const token721 = await utils.commitToBuy( + users.buyer, users.seller, TOKEN_SUPPLY_ID, constants.product_price, @@ -878,7 +759,82 @@ describe('ERC1155ERC721', () => { const owner721Instance = contractERC1155ERC721.connect( users.buyer.signer ); - const tokenOwner = await owner721Instance.ownerOf(erc721); + const tx = await owner721Instance.approve( + users.other1.address, + token721 + ); + const txReceipt = await tx.wait(); + + eventUtils.assertEventEmitted( + txReceipt, + ERC1155ERC721_Factory, + eventNames.APPROVAL, + (ev) => { + assert.equal( + ev._owner, + users.buyer.address, + 'Owner not as expected!' + ); + assert.equal( + ev._approved, + users.other1.address, + 'Approved not as expected!' + ); + assert.equal( + ev._tokenId.toString(), + token721.toString(), + 'tokenId not as expected!' + ); + } + ); + }); + + it('[NEGATIVE][approve] Attacker should not approve transfer of erc721 that does not possess', async () => { + const token721 = await utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ); + + const attackerInstance = contractERC1155ERC721.connect( + users.attacker.signer + ); + + await expect( + attackerInstance.approve(users.other1.address, token721) + ).to.be.revertedWith(revertReasons.UNAUTHORIZED_APPROVAL); + }); + + it('[NEGATIVE][approve] Should revert if buyer tries to approve to self', async () => { + const token721 = await utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ); + + await expect( + contractERC1155ERC721.approve(users.buyer.address, token721) + ).to.be.revertedWith(revertReasons.REDUNDANT_CALL); + }); + + it('[ownerOf] should token owner address for valid token', async () => { + const expectedOwner = users.buyer; + const tokenIdsForMint = 123; + + await contractERC1155ERC721.setVoucherKernelAddress( + users.deployer.address + ); + + await contractERC1155ERC721.functions[fnSignatures.mint721]( + expectedOwner.address, + tokenIdsForMint + ); + + const tokenOwner = await contractERC1155ERC721.ownerOf(tokenIdsForMint); assert.equal(tokenOwner, expectedOwner.address); }); @@ -893,21 +849,26 @@ describe('ERC1155ERC721', () => { }); describe('[balanceOf] should count all NFTs assigned to an owner', async () => { - it('[balanceOf] returns 1 when 1 NFT is assigned to owner', async () => { - await utils.commitToBuy( - users.buyer, - users.seller, - TOKEN_SUPPLY_ID, - constants.product_price, - constants.buyer_deposit + it('[balanceOf] returns 4 when 4 NFTs are assigned to owner', async () => { + await contractERC1155ERC721.setVoucherKernelAddress( + users.deployer.address ); - const expectedCount = 1; - const balanceOfBuyer = await contractERC1155ERC721.functions[ + const tokenIdsForMint = [10, 20, 30, 40]; + + for (var idForMint of tokenIdsForMint) { + await contractERC1155ERC721.functions[fnSignatures.mint721]( + users.other1.address, + idForMint + ); + } + + const expectedCount = tokenIdsForMint.length; + const balanceOfOwner = await contractERC1155ERC721.functions[ fnSignatures.balanceOf721 - ](users.buyer.address); + ](users.other1.address); - assert.equal(balanceOfBuyer.toString(), expectedCount.toString()); + assert.equal(balanceOfOwner.toString(), expectedCount.toString()); }); it('[balanceOf] returns 0 when no NFTs are assigned to owner', async () => { @@ -930,7 +891,7 @@ describe('ERC1155ERC721', () => { ); }); - it('[safeTransfer721] Should safely transfer the ownership of a given token ID to another address', async () => { + it('[safeTransfer721WithNoData] Should safely transfer the ownership of a given token ID to another address', async () => { const oldOwner = users.buyer; const expectedNewOwner = users.other2; @@ -942,17 +903,88 @@ describe('ERC1155ERC721', () => { constants.buyer_deposit ); - await utils.safeTransfer721( + const tx = await utils.safeTransfer721WithNoData( oldOwner.address, expectedNewOwner.address, erc721, users.buyer.signer ); - const owner721Instance = contractERC1155ERC721.connect( + const txReceipt = await tx.wait(); + + eventUtils.assertEventEmitted( + txReceipt, + ERC1155ERC721_Factory, + eventNames.TRANSFER, + (ev) => { + assert.equal( + ev._from, + oldOwner.address, + 'ev._from not as expected!' + ); + assert.equal( + ev._to, + users.other2.address, + 'ev._to not as expected!' + ); + assert.equal( + ev._tokenId.toString(), + erc721.toString(), + 'ev._tokenId not as expected!' + ); + } + ); + + const newTokenOwner = await contractERC1155ERC721.ownerOf(erc721); + + assert.equal(newTokenOwner, expectedNewOwner.address); + }); + + it('[safeTransfer721] Should safely transfer the ownership of a given token ID to another address', async () => { + const oldOwner = users.buyer; + const expectedNewOwner = users.other2; + + const erc721 = await utils.commitToBuy( + oldOwner, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ); + + const tx = await utils.safeTransfer721( + oldOwner.address, + expectedNewOwner.address, + erc721, users.buyer.signer ); - const newTokenOwner = await owner721Instance.ownerOf(erc721); + + const txReceipt = await tx.wait(); + + eventUtils.assertEventEmitted( + txReceipt, + ERC1155ERC721_Factory, + eventNames.TRANSFER, + (ev) => { + assert.equal( + ev._from, + oldOwner.address, + 'ev._from not as expected!' + ); + assert.equal( + ev._to, + users.other2.address, + 'ev._to not as expected!' + ); + assert.equal( + ev._tokenId.toString(), + erc721.toString(), + 'ev._tokenId not as expected!' + ); + } + ); + + const newTokenOwner = await contractERC1155ERC721.ownerOf(erc721); assert.equal(newTokenOwner, expectedNewOwner.address); }); @@ -1047,17 +1079,39 @@ describe('ERC1155ERC721', () => { constants.buyer_deposit ); - await utils.transfer721( + const tx = await utils.transfer721( oldOwner.address, expectedNewOwner.address, erc721, users.buyer.signer ); - const owner721Instance = contractERC1155ERC721.connect( - users.buyer.signer + const txReceipt = await tx.wait(); + + eventUtils.assertEventEmitted( + txReceipt, + ERC1155ERC721_Factory, + eventNames.TRANSFER, + (ev) => { + assert.equal( + ev._from, + oldOwner.address, + 'ev._from not as expected!' + ); + assert.equal( + ev._to, + users.other2.address, + 'ev._to not as expected!' + ); + assert.equal( + ev._tokenId.toString(), + erc721.toString(), + 'ev._tokenId not as expected!' + ); + } ); - const newTokenOwner = await owner721Instance.ownerOf(erc721); + + const newTokenOwner = await contractERC1155ERC721.ownerOf(erc721); assert.equal(newTokenOwner, expectedNewOwner.address); }); @@ -1084,36 +1138,35 @@ describe('ERC1155ERC721', () => { }); it('[getApproved] Should return zero address if no address set', async () => { - const erc721 = await utils.commitToBuy( - users.buyer, - users.seller, - TOKEN_SUPPLY_ID, - constants.product_price, - constants.buyer_deposit + await contractERC1155ERC721.setVoucherKernelAddress( + users.deployer.address ); - const owner721Instance = contractERC1155ERC721.connect( - users.buyer.signer + const tokenIdForMint = 123; + await contractERC1155ERC721.functions[fnSignatures.mint721]( + users.deployer.address, + tokenIdForMint ); - const approvedAddress = await owner721Instance.getApproved(erc721); + + const approvedAddress = await contractERC1155ERC721.getApproved(tokenIdForMint); assert.equal(approvedAddress, constants.ZERO_ADDRESS); }); it('[getApproved] Should return the approved address for a token ID', async () => { const expectedApprovedAddress = users.other1.address; - const erc721 = await utils.commitToBuy( - users.buyer, - users.seller, - TOKEN_SUPPLY_ID, - constants.product_price, - constants.buyer_deposit + + await contractERC1155ERC721.setVoucherKernelAddress( + users.deployer.address ); - const owner721Instance = contractERC1155ERC721.connect( - users.buyer.signer + const tokenIdForMint = 123; + await contractERC1155ERC721.functions[fnSignatures.mint721]( + users.deployer.address, + tokenIdForMint ); - await owner721Instance.approve(expectedApprovedAddress, erc721); - const approvedAddress = await owner721Instance.getApproved(erc721); + + await contractERC1155ERC721.approve(expectedApprovedAddress, tokenIdForMint); + const approvedAddress = await contractERC1155ERC721.getApproved(tokenIdForMint); assert.equal(approvedAddress, expectedApprovedAddress); }); @@ -1152,6 +1205,13 @@ describe('ERC1155ERC721', () => { ); } ); + + const expectedBalance = 1; + const balanceOfBuyer = await contractERC1155ERC721.functions[ + fnSignatures.balanceOf721 + ](users.other1.address); + + assert.equal(balanceOfBuyer.toString(), expectedBalance.toString()); }); it('[NEGATIVE][mint] must fail: unauthorized minting ERC-721', async () => { diff --git a/testHelpers/functionSignatures.ts b/testHelpers/functionSignatures.ts index 1a08fd73..905e68b5 100644 --- a/testHelpers/functionSignatures.ts +++ b/testHelpers/functionSignatures.ts @@ -19,6 +19,8 @@ export default { 'bytes)', safeTransfer721: 'safeTransferFrom(' + 'address,' + 'address,' + 'uint256,' + 'bytes)', + safeTransfer721WithNoData: + 'safeTransferFrom(' + 'address,' + 'address,' + 'uint256)', transfer721: 'transferFrom(' + 'address,' + 'address,' + 'uint256)', burn1155: 'burn(' + 'address,' + 'uint256,' + 'uint256)', }; diff --git a/testHelpers/utils.ts b/testHelpers/utils.ts index e02bdb8f..0cf7f9f3 100644 --- a/testHelpers/utils.ts +++ b/testHelpers/utils.ts @@ -778,6 +778,25 @@ class Utils { ); } + async safeTransfer721WithNoData( + oldVoucherOwner: string, + newVoucherOwner: string, + voucherID: string, + signer: Signer + ): Promise { + const fromInstance = this.contractERC1155ERC721.connect( + signer + ) as ERC1155ERC721; + + const method = fromInstance.functions[fnSignatures.safeTransfer721WithNoData]; + + return await method( + oldVoucherOwner, + newVoucherOwner, + voucherID + ); + } + async safeTransfer1155( oldSupplyOwner: string, newSupplyOwner: string, From eb8ba301d5bbb69611837ee02e65ef775bbaa176 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Thu, 14 Oct 2021 17:02:37 +0530 Subject: [PATCH 22/43] added unit test: supportsInterface --- test/6_erc1155721.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index 4e98cdc1..b70ef2db 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -295,6 +295,29 @@ describe('ERC1155ERC721', () => { ); assert.equal(approvalStatus, expectedApprovalStatus); }); + + describe('[supportsInterface]', () => { + it('Should return True for supported _interfaceId', async () => { + const expectedResult = true; + const supportedInterfaceIds = ['0x01ffc9a7', '0xd9b67a26', '0x80ac58cd', '0x5b5e139f', '0x0e89341c']; + + const randomInterfaceId = supportedInterfaceIds[Math.floor(Math.random() * supportedInterfaceIds.length)]; + const isSupported: boolean = await contractERC1155ERC721.supportsInterface( + randomInterfaceId + ); + assert.equal(isSupported, expectedResult); + }); + + it('Should return False for un-supported _interfaceId', async () => { + const expectedResult = false; + const unSupportedInterfaceId = '0x150b7a02'; + + const isSupported: boolean = await contractERC1155ERC721.supportsInterface( + unSupportedInterfaceId + ); + assert.equal(isSupported, expectedResult); + }); + }); }); describe('ERC1155', () => { From a3881a5627aed2a5d858e22928e5fa48f4fc5958 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Thu, 14 Oct 2021 17:21:18 +0530 Subject: [PATCH 23/43] checking balance of [mintbatch] --- test/6_erc1155721.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index b70ef2db..276190fa 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -35,7 +35,7 @@ let TOKEN_SUPPLY_ID; let users; describe('ERC1155ERC721', () => { - before(async () => { + beforeEach(async () => { const signers: Signer[] = await ethers.getSigners(); users = new Users(signers); @@ -157,7 +157,7 @@ describe('ERC1155ERC721', () => { describe('Multi-token contract', function () { describe('Common', () => { - before(async () => { + beforeEach(async () => { await deployContracts(); utils = await prepareUtils(); }); @@ -637,19 +637,19 @@ describe('ERC1155ERC721', () => { const balanceOfToken1 = await contractERC1155ERC721.functions[ fnSignatures.balanceOf1155 - ](users.seller.address, BN(123)); + ](users.seller.address, tokenIds[0]); const balanceOfToken2 = await contractERC1155ERC721.functions[ fnSignatures.balanceOf1155 - ](users.seller.address, BN(456)); + ](users.seller.address, tokenIds[1]); const balanceOfToken3 = await contractERC1155ERC721.functions[ fnSignatures.balanceOf1155 - ](users.seller.address, BN(789)); + ](users.seller.address, tokenIds[2]); - assert.equal(balanceOfToken1, constants.QTY_10); - assert.equal(balanceOfToken2, constants.QTY_15); - assert.equal(balanceOfToken3, constants.QTY_20); + assert.equal(balanceOfToken1.toString(), quantities[0].toString()); + assert.equal(balanceOfToken2.toString(), quantities[1].toString()); + assert.equal(balanceOfToken3.toString(), quantities[2].toString()); }); it('[NEGATIVE][mintBatch] Should revert when _account is a zero address', async () => { @@ -1253,7 +1253,7 @@ describe('ERC1155ERC721', () => { const metadata1155Route = 'voucher-sets/'; const metadata721Route = 'vouchers/'; - before(async () => { + beforeEach(async () => { await deployContracts(); utils = await prepareUtils(); From ac3f91feffd7ffd4da441b42846a3318093a5679 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Thu, 14 Oct 2021 17:28:55 +0530 Subject: [PATCH 24/43] lint fixes --- test/6_erc1155721.ts | 36 ++++++++++++++++++++++++------- testHelpers/functionSignatures.ts | 2 +- testHelpers/utils.ts | 9 +++----- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index 276190fa..0e2ab459 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -299,9 +299,18 @@ describe('ERC1155ERC721', () => { describe('[supportsInterface]', () => { it('Should return True for supported _interfaceId', async () => { const expectedResult = true; - const supportedInterfaceIds = ['0x01ffc9a7', '0xd9b67a26', '0x80ac58cd', '0x5b5e139f', '0x0e89341c']; - - const randomInterfaceId = supportedInterfaceIds[Math.floor(Math.random() * supportedInterfaceIds.length)]; + const supportedInterfaceIds = [ + '0x01ffc9a7', + '0xd9b67a26', + '0x80ac58cd', + '0x5b5e139f', + '0x0e89341c', + ]; + + const randomInterfaceId = + supportedInterfaceIds[ + Math.floor(Math.random() * supportedInterfaceIds.length) + ]; const isSupported: boolean = await contractERC1155ERC721.supportsInterface( randomInterfaceId ); @@ -597,7 +606,11 @@ describe('ERC1155ERC721', () => { ); const tokenIds = [BN(123), BN(456), BN(789)]; - const quantities = [BN(constants.QTY_10), BN(constants.QTY_15), BN(constants.QTY_20)]; + const quantities = [ + BN(constants.QTY_10), + BN(constants.QTY_15), + BN(constants.QTY_20), + ]; const tx = await contractERC1155ERC721.mintBatch( users.seller.address, tokenIds, @@ -879,7 +892,7 @@ describe('ERC1155ERC721', () => { const tokenIdsForMint = [10, 20, 30, 40]; - for (var idForMint of tokenIdsForMint) { + for (const idForMint of tokenIdsForMint) { await contractERC1155ERC721.functions[fnSignatures.mint721]( users.other1.address, idForMint @@ -1171,7 +1184,9 @@ describe('ERC1155ERC721', () => { tokenIdForMint ); - const approvedAddress = await contractERC1155ERC721.getApproved(tokenIdForMint); + const approvedAddress = await contractERC1155ERC721.getApproved( + tokenIdForMint + ); assert.equal(approvedAddress, constants.ZERO_ADDRESS); }); @@ -1188,8 +1203,13 @@ describe('ERC1155ERC721', () => { tokenIdForMint ); - await contractERC1155ERC721.approve(expectedApprovedAddress, tokenIdForMint); - const approvedAddress = await contractERC1155ERC721.getApproved(tokenIdForMint); + await contractERC1155ERC721.approve( + expectedApprovedAddress, + tokenIdForMint + ); + const approvedAddress = await contractERC1155ERC721.getApproved( + tokenIdForMint + ); assert.equal(approvedAddress, expectedApprovedAddress); }); diff --git a/testHelpers/functionSignatures.ts b/testHelpers/functionSignatures.ts index 905e68b5..18d788b6 100644 --- a/testHelpers/functionSignatures.ts +++ b/testHelpers/functionSignatures.ts @@ -20,7 +20,7 @@ export default { safeTransfer721: 'safeTransferFrom(' + 'address,' + 'address,' + 'uint256,' + 'bytes)', safeTransfer721WithNoData: - 'safeTransferFrom(' + 'address,' + 'address,' + 'uint256)', + 'safeTransferFrom(' + 'address,' + 'address,' + 'uint256)', transfer721: 'transferFrom(' + 'address,' + 'address,' + 'uint256)', burn1155: 'burn(' + 'address,' + 'uint256,' + 'uint256)', }; diff --git a/testHelpers/utils.ts b/testHelpers/utils.ts index 0cf7f9f3..6e0a405f 100644 --- a/testHelpers/utils.ts +++ b/testHelpers/utils.ts @@ -788,13 +788,10 @@ class Utils { signer ) as ERC1155ERC721; - const method = fromInstance.functions[fnSignatures.safeTransfer721WithNoData]; + const method = + fromInstance.functions[fnSignatures.safeTransfer721WithNoData]; - return await method( - oldVoucherOwner, - newVoucherOwner, - voucherID - ); + return await method(oldVoucherOwner, newVoucherOwner, voucherID); } async safeTransfer1155( From d51365b4ff0b404d08ff1cd377e0ac785748f938 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Thu, 14 Oct 2021 18:03:26 +0530 Subject: [PATCH 25/43] added new Mock: MockERC721.sol --- contracts/mocks/MockERC721.sol | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 contracts/mocks/MockERC721.sol diff --git a/contracts/mocks/MockERC721.sol b/contracts/mocks/MockERC721.sol new file mode 100644 index 00000000..b1b672e8 --- /dev/null +++ b/contracts/mocks/MockERC721.sol @@ -0,0 +1,9 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later +pragma solidity 0.7.6; + +import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; + +contract MockERC721 is ERC721 { + constructor() ERC721("MockERC721", "M721") { + } +} From 7cad77d040b29938e98c28f3cda0cc1bb02e6060 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Thu, 14 Oct 2021 18:04:19 +0530 Subject: [PATCH 26/43] updated unit test: minting to ERC721 supporting contract address --- test/6_erc1155721.ts | 56 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index 0e2ab459..a8c619b0 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -15,6 +15,7 @@ import { Cashier, TokenRegistry, MockERC20Permit, + MockERC721, } from '../typechain'; let ERC1155ERC721_Factory: ContractFactory; @@ -23,6 +24,7 @@ let Cashier_Factory: ContractFactory; let BosonRouter_Factory: ContractFactory; let TokenRegistry_Factory: ContractFactory; let MockERC20Permit_Factory: ContractFactory; +let MockERC721_Factory: ContractFactory; import revertReasons from '../testHelpers/revertReasons'; import * as eventUtils from '../testHelpers/events'; @@ -47,6 +49,9 @@ describe('ERC1155ERC721', () => { MockERC20Permit_Factory = await ethers.getContractFactory( 'MockERC20Permit' ); + MockERC721_Factory = await ethers.getContractFactory( + 'MockERC721' + ); }); let contractERC1155ERC721: ERC1155ERC721, @@ -55,7 +60,8 @@ describe('ERC1155ERC721', () => { contractBosonRouter: BosonRouter, contractBSNTokenPrice: MockERC20Permit, contractBSNTokenDeposit: MockERC20Permit, - contractTokenRegistry: TokenRegistry; + contractTokenRegistry: TokenRegistry, + contractMockERC721: MockERC721; let timestamp: number; @@ -87,6 +93,8 @@ describe('ERC1155ERC721', () => { 'BosonTokenDeposit', 'BDEP' )) as Contract & MockERC20Permit; + contractMockERC721 = (await MockERC721_Factory.deploy()) as Contract & + MockERC721; await contractTokenRegistry.deployed(); await contractERC1155ERC721.deployed(); @@ -95,6 +103,7 @@ describe('ERC1155ERC721', () => { await contractBosonRouter.deployed(); await contractBSNTokenPrice.deployed(); await contractBSNTokenDeposit.deployed(); + await contractMockERC721.deployed(); await contractERC1155ERC721.setApprovalForAll( contractVoucherKernel.address, @@ -1257,6 +1266,51 @@ describe('ERC1155ERC721', () => { assert.equal(balanceOfBuyer.toString(), expectedBalance.toString()); }); + it('[mint] Should be able to mint a token to a contract that supports it', async () => { + const supportingContractAddress = contractMockERC721.address; + await contractERC1155ERC721.setVoucherKernelAddress( + users.deployer.address + ); + + const tokenIdForMint = 123; + const tx = await contractERC1155ERC721.functions[fnSignatures.mint721]( + supportingContractAddress, + tokenIdForMint + ); + + const txReceipt = await tx.wait(); + + eventUtils.assertEventEmitted( + txReceipt, + ERC1155ERC721_Factory, + eventNames.TRANSFER, + (ev) => { + assert.equal( + ev._from, + constants.ZERO_ADDRESS, + 'ev._from not as expected!' + ); + assert.equal( + ev._to, + supportingContractAddress, + 'ev._to not as expected!' + ); + assert.equal( + ev._tokenId, + tokenIdForMint, + 'ev._tokenId not as expected!' + ); + } + ); + + const expectedBalance = 1; + const balanceOfBuyer = await contractERC1155ERC721.functions[ + fnSignatures.balanceOf721 + ](supportingContractAddress); + + assert.equal(balanceOfBuyer.toString(), expectedBalance.toString()); + }); + it('[NEGATIVE][mint] must fail: unauthorized minting ERC-721', async () => { await expect( contractERC1155ERC721.functions[fnSignatures.mint721]( From d41b2dd641868e35d0e6f45a8d85942d3cf239f3 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Thu, 14 Oct 2021 19:05:16 +0530 Subject: [PATCH 27/43] Implemented onERC721Received --- contracts/mocks/MockERC721.sol | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/contracts/mocks/MockERC721.sol b/contracts/mocks/MockERC721.sol index b1b672e8..ba74a087 100644 --- a/contracts/mocks/MockERC721.sol +++ b/contracts/mocks/MockERC721.sol @@ -2,8 +2,13 @@ pragma solidity 0.7.6; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; - -contract MockERC721 is ERC721 { +import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol"; + +contract MockERC721 is ERC721, IERC721Receiver { constructor() ERC721("MockERC721", "M721") { } + + function onERC721Received(address, address, uint256, bytes calldata) external override pure returns(bytes4) { + return bytes4(keccak256("onERC721Received(address,address,uint256,bytes)")); + } } From ec0e04ba416096c05f162289a554bbde7f40426e Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Thu, 14 Oct 2021 19:06:37 +0530 Subject: [PATCH 28/43] added new happy unit test: safeTransfer721 --- test/6_erc1155721.ts | 49 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index a8c619b0..d22b0eb0 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -1034,6 +1034,55 @@ describe('ERC1155ERC721', () => { assert.equal(newTokenOwner, expectedNewOwner.address); }); + it('[safeTransfer721] Should safely transfer the ownership of a given token ID to ERC721 supporting contract', async () => { + const oldOwner = users.buyer; + const expectedNewOwnerAddress = contractMockERC721.address; + + const erc721 = await utils.commitToBuy( + oldOwner, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ); + + const tx = await utils.safeTransfer721( + oldOwner.address, + expectedNewOwnerAddress, + erc721, + users.buyer.signer + ); + + const txReceipt = await tx.wait(); + + eventUtils.assertEventEmitted( + txReceipt, + ERC1155ERC721_Factory, + eventNames.TRANSFER, + (ev) => { + assert.equal( + ev._from, + oldOwner.address, + 'ev._from not as expected!' + ); + assert.equal( + ev._to, + expectedNewOwnerAddress, + 'ev._to not as expected!' + ); + assert.equal( + ev._tokenId.toString(), + erc721.toString(), + 'ev._tokenId not as expected!' + ); + } + ); + + const newTokenOwner = await contractERC1155ERC721.ownerOf(erc721); + + assert.equal(newTokenOwner, expectedNewOwnerAddress); + }); + it('[NEGATIVE][safeTransfer721] Should not be able to transfer to contract address', async () => { const erc721 = await utils.commitToBuy( users.buyer, From 080215c48614df9188dd78672f691fb76a086aa9 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Thu, 14 Oct 2021 19:47:57 +0530 Subject: [PATCH 29/43] added new mock: MockERC1155.sol --- contracts/mocks/MockERC1155.sol | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 contracts/mocks/MockERC1155.sol diff --git a/contracts/mocks/MockERC1155.sol b/contracts/mocks/MockERC1155.sol new file mode 100644 index 00000000..ac896d47 --- /dev/null +++ b/contracts/mocks/MockERC1155.sol @@ -0,0 +1,19 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later +pragma solidity 0.7.6; + +import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol"; + +contract MockERC1155 is ERC1155 { + constructor() ERC1155("https://mock.example/{id}.json") { + } + + function onERC1155BatchReceived( + address, + address, + uint256[] calldata, + uint256[] calldata, + bytes calldata + ) external pure returns(bytes4) { + return bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)")); + } +} From 5305b332dd6f14006a00770ef8c3efcfe044a7fd Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Thu, 14 Oct 2021 19:48:58 +0530 Subject: [PATCH 30/43] updated MockERC721 --- contracts/mocks/MockERC721.sol | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/contracts/mocks/MockERC721.sol b/contracts/mocks/MockERC721.sol index ba74a087..f0fffe28 100644 --- a/contracts/mocks/MockERC721.sol +++ b/contracts/mocks/MockERC721.sol @@ -2,13 +2,12 @@ pragma solidity 0.7.6; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; -import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol"; -contract MockERC721 is ERC721, IERC721Receiver { +contract MockERC721 is ERC721 { constructor() ERC721("MockERC721", "M721") { } - function onERC721Received(address, address, uint256, bytes calldata) external override pure returns(bytes4) { + function onERC721Received(address, address, uint256, bytes calldata) external pure returns(bytes4) { return bytes4(keccak256("onERC721Received(address,address,uint256,bytes)")); } } From 83eef617141480d3e1290aa1d29c381e454a371d Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Thu, 14 Oct 2021 19:49:53 +0530 Subject: [PATCH 31/43] added happy unit test: mintBatch for ERC1155 supporting contracts --- test/6_erc1155721.ts | 65 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index d22b0eb0..9738d611 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -16,6 +16,7 @@ import { TokenRegistry, MockERC20Permit, MockERC721, + MockERC1155, } from '../typechain'; let ERC1155ERC721_Factory: ContractFactory; @@ -25,6 +26,7 @@ let BosonRouter_Factory: ContractFactory; let TokenRegistry_Factory: ContractFactory; let MockERC20Permit_Factory: ContractFactory; let MockERC721_Factory: ContractFactory; +let MockERC1155_Factory: ContractFactory; import revertReasons from '../testHelpers/revertReasons'; import * as eventUtils from '../testHelpers/events'; @@ -52,6 +54,9 @@ describe('ERC1155ERC721', () => { MockERC721_Factory = await ethers.getContractFactory( 'MockERC721' ); + MockERC1155_Factory = await ethers.getContractFactory( + 'MockERC1155' + ); }); let contractERC1155ERC721: ERC1155ERC721, @@ -61,7 +66,8 @@ describe('ERC1155ERC721', () => { contractBSNTokenPrice: MockERC20Permit, contractBSNTokenDeposit: MockERC20Permit, contractTokenRegistry: TokenRegistry, - contractMockERC721: MockERC721; + contractMockERC721: MockERC721, + contractMockERC1155: MockERC1155; let timestamp: number; @@ -93,9 +99,13 @@ describe('ERC1155ERC721', () => { 'BosonTokenDeposit', 'BDEP' )) as Contract & MockERC20Permit; + contractMockERC721 = (await MockERC721_Factory.deploy()) as Contract & MockERC721; + contractMockERC1155 = (await MockERC1155_Factory.deploy()) as Contract & + MockERC1155; + await contractTokenRegistry.deployed(); await contractERC1155ERC721.deployed(); await contractVoucherKernel.deployed(); @@ -104,6 +114,7 @@ describe('ERC1155ERC721', () => { await contractBSNTokenPrice.deployed(); await contractBSNTokenDeposit.deployed(); await contractMockERC721.deployed(); + await contractMockERC1155.deployed(); await contractERC1155ERC721.setApprovalForAll( contractVoucherKernel.address, @@ -674,6 +685,58 @@ describe('ERC1155ERC721', () => { assert.equal(balanceOfToken3.toString(), quantities[2].toString()); }); + it('[mintBatch] Should do batch minting of tokens to ERC1155 supporting contract', async () => { + const erc1155supportingContract = contractMockERC1155; + await contractERC1155ERC721.setVoucherKernelAddress( + users.deployer.address + ); + + const tokenIds = [BN(123)]; + const quantities = [BN(constants.QTY_10)]; + const tx = await contractERC1155ERC721.mintBatch( + erc1155supportingContract.address, + tokenIds, + quantities, + ethers.utils.formatBytes32String('0x0') + ); + + const txReceipt = await tx.wait(); + + eventUtils.assertEventEmitted( + txReceipt, + ERC1155ERC721_Factory, + eventNames.TRANSFER_BATCH, + (ev) => { + assert.equal( + ev._from, + constants.ZERO_ADDRESS, + 'ev._from not as expected!' + ); + assert.equal( + ev._to, + erc1155supportingContract.address, + 'ev._to not as expected!' + ); + assert.equal( + ev._ids.toString(), + tokenIds.toString(), + 'ev._ids not as expected!' + ); + assert.equal( + JSON.stringify(ev._values), + JSON.stringify(quantities), + 'ev._values not as expected!' + ); + } + ); + + const balanceOfToken1 = await contractERC1155ERC721.functions[ + fnSignatures.balanceOf1155 + ](erc1155supportingContract.address, tokenIds[0]); + + assert.equal(balanceOfToken1.toString(), quantities[0].toString()); + }); + it('[NEGATIVE][mintBatch] Should revert when _account is a zero address', async () => { await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address From 6b5a3d177ff0f8b23154a211c439481e96e8b20c Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Thu, 14 Oct 2021 19:56:41 +0530 Subject: [PATCH 32/43] added happy unit test: mint to ERC1155 supported contract --- contracts/mocks/MockERC1155.sol | 10 +++++++ test/6_erc1155721.ts | 48 +++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/contracts/mocks/MockERC1155.sol b/contracts/mocks/MockERC1155.sol index ac896d47..1428cd5a 100644 --- a/contracts/mocks/MockERC1155.sol +++ b/contracts/mocks/MockERC1155.sol @@ -16,4 +16,14 @@ contract MockERC1155 is ERC1155 { ) external pure returns(bytes4) { return bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)")); } + + function onERC1155Received( + address, + address, + uint256, + uint256, + bytes calldata + ) external pure returns(bytes4) { + return bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)")); + } } diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index 9738d611..46389e79 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -534,6 +534,54 @@ describe('ERC1155ERC721', () => { assert.equal(balanceOfOwner.toString(), expectedBalance.toString()); }); + it('[mint] Should mint a desired token to ERC1155 supporting contract', async () => { + const erc1155supportingContract = contractMockERC1155; + await contractERC1155ERC721.setVoucherKernelAddress( + users.deployer.address + ); + + const tokenIdForMint = 123; + const tx = await contractERC1155ERC721.functions[fnSignatures.mint1155]( + erc1155supportingContract.address, + tokenIdForMint, + constants.QTY_10, + ethers.utils.formatBytes32String('0x0') + ); + + const txReceipt = await tx.wait(); + + eventUtils.assertEventEmitted( + txReceipt, + ERC1155ERC721_Factory, + eventNames.TRANSFER_SINGLE, + (ev) => { + assert.equal( + ev._from, + constants.ZERO_ADDRESS, + 'ev._from not as expected!' + ); + assert.equal( + ev._to, + erc1155supportingContract.address, + 'ev._to not as expected!' + ); + assert.equal(ev._id, tokenIdForMint, 'ev._id not as expected!'); + assert.equal( + ev._value, + constants.QTY_10, + 'ev._value not as expected!' + ); + } + ); + + const expectedBalance = constants.QTY_10; + const balanceOfOwner = await contractERC1155ERC721.functions[ + fnSignatures.balanceOf1155 + ](erc1155supportingContract.address, tokenIdForMint); + + assert.equal(balanceOfOwner.toString(), expectedBalance.toString()); + }); + it('[NEGATIVE][mint] must fail: unauthorized minting ERC-1155', async () => { await expect( contractERC1155ERC721.functions[fnSignatures.mint1155]( From 20cb3693c8be480784e5f2ab0234c55c3f6d82ad Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Thu, 14 Oct 2021 20:08:00 +0530 Subject: [PATCH 33/43] lint fixes --- contracts/mocks/MockERC1155.sol | 22 ++++++++++++----- contracts/mocks/MockERC721.sol | 20 ++++++++++----- test/6_erc1155721.ts | 44 +++++++++++++++++++++++++++------ 3 files changed, 67 insertions(+), 19 deletions(-) diff --git a/contracts/mocks/MockERC1155.sol b/contracts/mocks/MockERC1155.sol index 1428cd5a..c02d80dd 100644 --- a/contracts/mocks/MockERC1155.sol +++ b/contracts/mocks/MockERC1155.sol @@ -4,8 +4,8 @@ pragma solidity 0.7.6; import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol"; contract MockERC1155 is ERC1155 { - constructor() ERC1155("https://mock.example/{id}.json") { - } + // solhint-disable-next-line no-empty-blocks + constructor() ERC1155("https://mock.example/{id}.json") {} function onERC1155BatchReceived( address, @@ -13,8 +13,13 @@ contract MockERC1155 is ERC1155 { uint256[] calldata, uint256[] calldata, bytes calldata - ) external pure returns(bytes4) { - return bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)")); + ) external pure returns (bytes4) { + return + bytes4( + keccak256( + "onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)" + ) + ); } function onERC1155Received( @@ -23,7 +28,12 @@ contract MockERC1155 is ERC1155 { uint256, uint256, bytes calldata - ) external pure returns(bytes4) { - return bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)")); + ) external pure returns (bytes4) { + return + bytes4( + keccak256( + "onERC1155Received(address,address,uint256,uint256,bytes)" + ) + ); } } diff --git a/contracts/mocks/MockERC721.sol b/contracts/mocks/MockERC721.sol index f0fffe28..36548f3d 100644 --- a/contracts/mocks/MockERC721.sol +++ b/contracts/mocks/MockERC721.sol @@ -1,13 +1,21 @@ // SPDX-License-Identifier: LGPL-3.0-or-later pragma solidity 0.7.6; - + import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; contract MockERC721 is ERC721 { - constructor() ERC721("MockERC721", "M721") { - } + // solhint-disable-next-line no-empty-blocks + constructor() ERC721("MockERC721", "M721") {} - function onERC721Received(address, address, uint256, bytes calldata) external pure returns(bytes4) { - return bytes4(keccak256("onERC721Received(address,address,uint256,bytes)")); - } + function onERC721Received( + address, + address, + uint256, + bytes calldata + ) external pure returns (bytes4) { + return + bytes4( + keccak256("onERC721Received(address,address,uint256,bytes)") + ); + } } diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index 46389e79..152269cf 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -51,12 +51,8 @@ describe('ERC1155ERC721', () => { MockERC20Permit_Factory = await ethers.getContractFactory( 'MockERC20Permit' ); - MockERC721_Factory = await ethers.getContractFactory( - 'MockERC721' - ); - MockERC1155_Factory = await ethers.getContractFactory( - 'MockERC1155' - ); + MockERC721_Factory = await ethers.getContractFactory('MockERC721'); + MockERC1155_Factory = await ethers.getContractFactory('MockERC1155'); }); let contractERC1155ERC721: ERC1155ERC721, @@ -104,7 +100,7 @@ describe('ERC1155ERC721', () => { MockERC721; contractMockERC1155 = (await MockERC1155_Factory.deploy()) as Contract & - MockERC1155; + MockERC1155; await contractTokenRegistry.deployed(); await contractERC1155ERC721.deployed(); @@ -385,6 +381,40 @@ describe('ERC1155ERC721', () => { assert.equal(balance.toString(), expectedCount.toString()); }); + it('[safeTransfer1155] Should be able to safely transfer to contracts that support ERC1155', async () => { + const erc1155supportingContract = contractMockERC1155; + + const transferTx = await utils.safeTransfer1155( + users.seller.address, + erc1155supportingContract.address, + TOKEN_SUPPLY_ID, + constants.QTY_10, + users.seller.signer + ); + + const txReceipt = await transferTx.wait(); + + eventUtils.assertEventEmitted( + txReceipt, + ERC1155ERC721_Factory, + eventNames.TRANSFER_SINGLE, + (ev) => { + ev; + assert.equal(ev._from, users.seller.address); + assert.equal(ev._to, erc1155supportingContract.address); + assert.equal(ev._id.toString(), TOKEN_SUPPLY_ID); + assert.equal(ev._value.toString(), constants.QTY_10); + } + ); + + const expectedBalance = constants.QTY_10; + const balanceOfOwner = await contractERC1155ERC721.functions[ + fnSignatures.balanceOf1155 + ](erc1155supportingContract.address, TOKEN_SUPPLY_ID); + + assert.equal(balanceOfOwner.toString(), expectedBalance.toString()); + }); + it('[NEGATIVE][safeTransfer1155] Attacker should not be able to transfer', async () => { await expect( utils.safeTransfer1155( From 350d4cec2cf5fc79a5c50078df02cfa908381c1f Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Fri, 15 Oct 2021 14:18:04 +0530 Subject: [PATCH 34/43] review fixes --- ...ockERC1155.sol => MockERC1155Receiver.sol} | 12 ++- ...{MockERC721.sol => MockERC721Receiver.sol} | 9 +-- test/6_erc1155721.ts | 79 +++++++++++-------- 3 files changed, 52 insertions(+), 48 deletions(-) rename contracts/mocks/{MockERC1155.sol => MockERC1155Receiver.sol} (71%) rename contracts/mocks/{MockERC721.sol => MockERC721Receiver.sol} (57%) diff --git a/contracts/mocks/MockERC1155.sol b/contracts/mocks/MockERC1155Receiver.sol similarity index 71% rename from contracts/mocks/MockERC1155.sol rename to contracts/mocks/MockERC1155Receiver.sol index c02d80dd..6ec69135 100644 --- a/contracts/mocks/MockERC1155.sol +++ b/contracts/mocks/MockERC1155Receiver.sol @@ -1,19 +1,17 @@ // SPDX-License-Identifier: LGPL-3.0-or-later pragma solidity 0.7.6; -import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol"; - -contract MockERC1155 is ERC1155 { - // solhint-disable-next-line no-empty-blocks - constructor() ERC1155("https://mock.example/{id}.json") {} +import "@openzeppelin/contracts/introspection/ERC165.sol"; +import "@openzeppelin/contracts/token/ERC1155/IERC1155Receiver.sol"; +contract MockERC1155Receiver is ERC165, IERC1155Receiver { function onERC1155BatchReceived( address, address, uint256[] calldata, uint256[] calldata, bytes calldata - ) external pure returns (bytes4) { + ) external pure override returns (bytes4) { return bytes4( keccak256( @@ -28,7 +26,7 @@ contract MockERC1155 is ERC1155 { uint256, uint256, bytes calldata - ) external pure returns (bytes4) { + ) external pure override returns (bytes4) { return bytes4( keccak256( diff --git a/contracts/mocks/MockERC721.sol b/contracts/mocks/MockERC721Receiver.sol similarity index 57% rename from contracts/mocks/MockERC721.sol rename to contracts/mocks/MockERC721Receiver.sol index 36548f3d..7ca12195 100644 --- a/contracts/mocks/MockERC721.sol +++ b/contracts/mocks/MockERC721Receiver.sol @@ -1,18 +1,15 @@ // SPDX-License-Identifier: LGPL-3.0-or-later pragma solidity 0.7.6; -import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; - -contract MockERC721 is ERC721 { - // solhint-disable-next-line no-empty-blocks - constructor() ERC721("MockERC721", "M721") {} +import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol"; +contract MockERC721Receiver is IERC721Receiver { function onERC721Received( address, address, uint256, bytes calldata - ) external pure returns (bytes4) { + ) external pure override returns (bytes4) { return bytes4( keccak256("onERC721Received(address,address,uint256,bytes)") diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index 152269cf..abd81759 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -15,8 +15,8 @@ import { Cashier, TokenRegistry, MockERC20Permit, - MockERC721, - MockERC1155, + MockERC721Receiver, + MockERC1155Receiver, } from '../typechain'; let ERC1155ERC721_Factory: ContractFactory; @@ -25,8 +25,8 @@ let Cashier_Factory: ContractFactory; let BosonRouter_Factory: ContractFactory; let TokenRegistry_Factory: ContractFactory; let MockERC20Permit_Factory: ContractFactory; -let MockERC721_Factory: ContractFactory; -let MockERC1155_Factory: ContractFactory; +let MockERC721Receiver_Factory: ContractFactory; +let MockERC1155Receiver_Factory: ContractFactory; import revertReasons from '../testHelpers/revertReasons'; import * as eventUtils from '../testHelpers/events'; @@ -51,8 +51,12 @@ describe('ERC1155ERC721', () => { MockERC20Permit_Factory = await ethers.getContractFactory( 'MockERC20Permit' ); - MockERC721_Factory = await ethers.getContractFactory('MockERC721'); - MockERC1155_Factory = await ethers.getContractFactory('MockERC1155'); + MockERC721Receiver_Factory = await ethers.getContractFactory( + 'MockERC721Receiver' + ); + MockERC1155Receiver_Factory = await ethers.getContractFactory( + 'MockERC1155Receiver' + ); }); let contractERC1155ERC721: ERC1155ERC721, @@ -62,8 +66,8 @@ describe('ERC1155ERC721', () => { contractBSNTokenPrice: MockERC20Permit, contractBSNTokenDeposit: MockERC20Permit, contractTokenRegistry: TokenRegistry, - contractMockERC721: MockERC721, - contractMockERC1155: MockERC1155; + contractMockERC721Receiver: MockERC721Receiver, + contractMockERC1155Receiver: MockERC1155Receiver; let timestamp: number; @@ -96,11 +100,11 @@ describe('ERC1155ERC721', () => { 'BDEP' )) as Contract & MockERC20Permit; - contractMockERC721 = (await MockERC721_Factory.deploy()) as Contract & - MockERC721; + contractMockERC721Receiver = (await MockERC721Receiver_Factory.deploy()) as Contract & + MockERC721Receiver; - contractMockERC1155 = (await MockERC1155_Factory.deploy()) as Contract & - MockERC1155; + contractMockERC1155Receiver = (await MockERC1155Receiver_Factory.deploy()) as Contract & + MockERC1155Receiver; await contractTokenRegistry.deployed(); await contractERC1155ERC721.deployed(); @@ -109,8 +113,8 @@ describe('ERC1155ERC721', () => { await contractBosonRouter.deployed(); await contractBSNTokenPrice.deployed(); await contractBSNTokenDeposit.deployed(); - await contractMockERC721.deployed(); - await contractMockERC1155.deployed(); + await contractMockERC721Receiver.deployed(); + await contractMockERC1155Receiver.deployed(); await contractERC1155ERC721.setApprovalForAll( contractVoucherKernel.address, @@ -305,16 +309,16 @@ describe('ERC1155ERC721', () => { expectedApprovalStatus ); - const approvalStatus = await contractERC1155ERC721.isApprovedForAll( - users.deployer.address, - contractVoucherKernel.address + assert.isTrue( + await contractERC1155ERC721.isApprovedForAll( + users.deployer.address, + contractVoucherKernel.address + ) ); - assert.equal(approvalStatus, expectedApprovalStatus); }); describe('[supportsInterface]', () => { it('Should return True for supported _interfaceId', async () => { - const expectedResult = true; const supportedInterfaceIds = [ '0x01ffc9a7', '0xd9b67a26', @@ -327,20 +331,20 @@ describe('ERC1155ERC721', () => { supportedInterfaceIds[ Math.floor(Math.random() * supportedInterfaceIds.length) ]; - const isSupported: boolean = await contractERC1155ERC721.supportsInterface( - randomInterfaceId + + assert.isTrue( + await contractERC1155ERC721.supportsInterface(randomInterfaceId) ); - assert.equal(isSupported, expectedResult); }); it('Should return False for un-supported _interfaceId', async () => { - const expectedResult = false; const unSupportedInterfaceId = '0x150b7a02'; - const isSupported: boolean = await contractERC1155ERC721.supportsInterface( - unSupportedInterfaceId + assert.isFalse( + await contractERC1155ERC721.supportsInterface( + unSupportedInterfaceId + ) ); - assert.equal(isSupported, expectedResult); }); }); }); @@ -382,7 +386,7 @@ describe('ERC1155ERC721', () => { }); it('[safeTransfer1155] Should be able to safely transfer to contracts that support ERC1155', async () => { - const erc1155supportingContract = contractMockERC1155; + const erc1155supportingContract = contractMockERC1155Receiver; const transferTx = await utils.safeTransfer1155( users.seller.address, @@ -565,7 +569,7 @@ describe('ERC1155ERC721', () => { }); it('[mint] Should mint a desired token to ERC1155 supporting contract', async () => { - const erc1155supportingContract = contractMockERC1155; + const erc1155supportingContract = contractMockERC1155Receiver; await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -764,7 +768,7 @@ describe('ERC1155ERC721', () => { }); it('[mintBatch] Should do batch minting of tokens to ERC1155 supporting contract', async () => { - const erc1155supportingContract = contractMockERC1155; + const erc1155supportingContract = contractMockERC1155Receiver; await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -973,6 +977,11 @@ describe('ERC1155ERC721', () => { ); } ); + + const approvedAddress = await contractERC1155ERC721.getApproved( + token721 + ); + assert.equal(approvedAddress, users.other1.address); }); it('[NEGATIVE][approve] Attacker should not approve transfer of erc721 that does not possess', async () => { @@ -1007,8 +1016,8 @@ describe('ERC1155ERC721', () => { ).to.be.revertedWith(revertReasons.REDUNDANT_CALL); }); - it('[ownerOf] should token owner address for valid token', async () => { - const expectedOwner = users.buyer; + it('[ownerOf] should return the token owner address for valid token', async () => { + const expectedOwner = users.buyer.address; const tokenIdsForMint = 123; await contractERC1155ERC721.setVoucherKernelAddress( @@ -1016,13 +1025,13 @@ describe('ERC1155ERC721', () => { ); await contractERC1155ERC721.functions[fnSignatures.mint721]( - expectedOwner.address, + expectedOwner, tokenIdsForMint ); const tokenOwner = await contractERC1155ERC721.ownerOf(tokenIdsForMint); - assert.equal(tokenOwner, expectedOwner.address); + assert.equal(tokenOwner, expectedOwner); }); it('[NEGATIVE][ownerOf] should revert if incorrect id provided', async () => { @@ -1177,7 +1186,7 @@ describe('ERC1155ERC721', () => { it('[safeTransfer721] Should safely transfer the ownership of a given token ID to ERC721 supporting contract', async () => { const oldOwner = users.buyer; - const expectedNewOwnerAddress = contractMockERC721.address; + const expectedNewOwnerAddress = contractMockERC721Receiver.address; const erc721 = await utils.commitToBuy( oldOwner, @@ -1457,7 +1466,7 @@ describe('ERC1155ERC721', () => { }); it('[mint] Should be able to mint a token to a contract that supports it', async () => { - const supportingContractAddress = contractMockERC721.address; + const supportingContractAddress = contractMockERC721Receiver.address; await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); From d6bb3bddee5d3a41eba8a8bc7bfc928b5a718236 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Fri, 15 Oct 2021 14:48:22 +0530 Subject: [PATCH 35/43] added new happy test: safeTransfer1155 --- test/6_erc1155721.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index abd81759..cdde3021 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -385,6 +385,38 @@ describe('ERC1155ERC721', () => { assert.equal(balance.toString(), expectedCount.toString()); }); + it('[safeTransfer1155] Should be able to safely transfer to EOA', async () => { + const transferTx = await utils.safeTransfer1155( + users.seller.address, + users.other1.address, + TOKEN_SUPPLY_ID, + constants.QTY_10, + users.seller.signer + ); + + const txReceipt = await transferTx.wait(); + + eventUtils.assertEventEmitted( + txReceipt, + ERC1155ERC721_Factory, + eventNames.TRANSFER_SINGLE, + (ev) => { + ev; + assert.equal(ev._from, users.seller.address); + assert.equal(ev._to, users.other1.address); + assert.equal(ev._id.toString(), TOKEN_SUPPLY_ID); + assert.equal(ev._value.toString(), constants.QTY_10); + } + ); + + const expectedBalance = constants.QTY_10; + const balanceOfOwner = await contractERC1155ERC721.functions[ + fnSignatures.balanceOf1155 + ](users.other1.address, TOKEN_SUPPLY_ID); + + assert.equal(balanceOfOwner.toString(), expectedBalance.toString()); + }); + it('[safeTransfer1155] Should be able to safely transfer to contracts that support ERC1155', async () => { const erc1155supportingContract = contractMockERC1155Receiver; From 699b59c19dc94d50514e5117674db18245833f31 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Fri, 15 Oct 2021 15:18:26 +0530 Subject: [PATCH 36/43] added happy unit test: safeBatchTransfer1155 --- test/6_erc1155721.ts | 149 +++++++++++++++++++++++++++++++++++++++++++ testHelpers/utils.ts | 4 +- 2 files changed, 151 insertions(+), 2 deletions(-) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index cdde3021..da844afe 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -487,6 +487,155 @@ describe('ERC1155ERC721', () => { ).to.be.revertedWith(revertReasons.FN_SELECTOR_NOT_RECOGNIZED); }); + it('[safeBatchTransfer1155] Should be able to safely batch transfer to EOA', async () => { + const tokenIds = [BN(123), BN(456), BN(789)]; + const quantities = [ + BN(constants.QTY_10), + BN(constants.QTY_15), + BN(constants.QTY_20), + ]; + + await contractERC1155ERC721.setVoucherKernelAddress( + users.deployer.address + ); + + await contractERC1155ERC721.mintBatch( + users.deployer.address, + tokenIds, + quantities, + ethers.utils.formatBytes32String('0x0') + ); + + const tx = await utils.safeBatchTransfer1155( + users.deployer.address, + users.other1.address, + tokenIds, + quantities, + users.deployer.signer + ) + + const txReceipt = await tx.wait(); + + eventUtils.assertEventEmitted( + txReceipt, + ERC1155ERC721_Factory, + eventNames.TRANSFER_BATCH, + (ev) => { + assert.equal( + ev._from, + users.deployer.address, + 'ev._from not as expected!' + ); + assert.equal( + ev._to, + users.other1.address, + 'ev._to not as expected!' + ); + assert.equal( + ev._ids.toString(), + tokenIds.toString(), + 'ev._ids not as expected!' + ); + assert.equal( + JSON.stringify(ev._values), + JSON.stringify(quantities), + 'ev._values not as expected!' + ); + } + ); + + const balanceOfToken1 = await contractERC1155ERC721.functions[ + fnSignatures.balanceOf1155 + ](users.other1.address, tokenIds[0]); + + const balanceOfToken2 = await contractERC1155ERC721.functions[ + fnSignatures.balanceOf1155 + ](users.other1.address, tokenIds[1]); + + const balanceOfToken3 = await contractERC1155ERC721.functions[ + fnSignatures.balanceOf1155 + ](users.other1.address, tokenIds[2]); + + assert.equal(balanceOfToken1.toString(), quantities[0].toString()); + assert.equal(balanceOfToken2.toString(), quantities[1].toString()); + assert.equal(balanceOfToken3.toString(), quantities[2].toString()); + }); + + it('[safeBatchTransfer1155] Should be able to safely batch transfer to contracts that support ERC1155', async () => { + const erc1155supportingContract = contractMockERC1155Receiver; + const tokenIds = [BN(123), BN(456), BN(789)]; + const quantities = [ + BN(constants.QTY_10), + BN(constants.QTY_15), + BN(constants.QTY_20), + ]; + + await contractERC1155ERC721.setVoucherKernelAddress( + users.deployer.address + ); + + await contractERC1155ERC721.mintBatch( + users.deployer.address, + tokenIds, + quantities, + ethers.utils.formatBytes32String('0x0') + ); + + const tx = await utils.safeBatchTransfer1155( + users.deployer.address, + erc1155supportingContract.address, + tokenIds, + quantities, + users.deployer.signer + ) + + const txReceipt = await tx.wait(); + + eventUtils.assertEventEmitted( + txReceipt, + ERC1155ERC721_Factory, + eventNames.TRANSFER_BATCH, + (ev) => { + assert.equal( + ev._from, + users.deployer.address, + 'ev._from not as expected!' + ); + assert.equal( + ev._to, + erc1155supportingContract.address, + 'ev._to not as expected!' + ); + assert.equal( + ev._ids.toString(), + tokenIds.toString(), + 'ev._ids not as expected!' + ); + assert.equal( + JSON.stringify(ev._values), + JSON.stringify(quantities), + 'ev._values not as expected!' + ); + } + ); + + const balanceOfToken1 = await contractERC1155ERC721.functions[ + fnSignatures.balanceOf1155 + ](erc1155supportingContract.address, tokenIds[0]); + + const balanceOfToken2 = await contractERC1155ERC721.functions[ + fnSignatures.balanceOf1155 + ](erc1155supportingContract.address, tokenIds[1]); + + const balanceOfToken3 = await contractERC1155ERC721.functions[ + fnSignatures.balanceOf1155 + ](erc1155supportingContract.address, tokenIds[2]); + + assert.equal(balanceOfToken1.toString(), quantities[0].toString()); + assert.equal(balanceOfToken2.toString(), quantities[1].toString()); + assert.equal(balanceOfToken3.toString(), quantities[2].toString()); + }); + it('[NEGATIVE][safeBatchTransfer1155] Should not be able to transfer batch to ZERO address', async () => { await expect( utils.safeBatchTransfer1155( diff --git a/testHelpers/utils.ts b/testHelpers/utils.ts index 6e0a405f..f17e72d6 100644 --- a/testHelpers/utils.ts +++ b/testHelpers/utils.ts @@ -820,8 +820,8 @@ class Utils { async safeBatchTransfer1155( oldSupplyOwner: string, newSupplyOwner: string, - supplyIDs: Array, - values: Array, + supplyIDs: Array, + values: Array, signer: Signer ): Promise { const arbitraryBytes = ethers.utils.formatBytes32String('0x0'); From 6faa5e472b6799f473ae66273de8ea6197b55999 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Fri, 15 Oct 2021 15:23:33 +0530 Subject: [PATCH 37/43] Added Negative unit test: mint1155 --- test/6_erc1155721.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index da844afe..e888d28c 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -797,6 +797,21 @@ describe('ERC1155ERC721', () => { assert.equal(balanceOfOwner.toString(), expectedBalance.toString()); }); + it('[NEGATIVE][mint] Should revert when to is a contract that cannot receive it', async () => { + await contractERC1155ERC721.setVoucherKernelAddress( + users.deployer.address + ); + + await expect( + contractERC1155ERC721.functions[fnSignatures.mint1155]( + contractCashier.address, + 666, + constants.QTY_10, + ethers.utils.formatBytes32String('0x0') + ) + ).to.be.revertedWith(revertReasons.FN_SELECTOR_NOT_RECOGNIZED); + }); + it('[NEGATIVE][mint] must fail: unauthorized minting ERC-1155', async () => { await expect( contractERC1155ERC721.functions[fnSignatures.mint1155]( From 99f782827ee6902d1f31ead220c01092b7bc152c Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Fri, 15 Oct 2021 15:32:05 +0530 Subject: [PATCH 38/43] added negative unit test: mintBatch --- test/6_erc1155721.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index e888d28c..864fe49a 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -1015,6 +1015,21 @@ describe('ERC1155ERC721', () => { assert.equal(balanceOfToken1.toString(), quantities[0].toString()); }); + it('[NEGATIVE][mintBatch] Should revert when to is a contract that cannot receive it', async () => { + await contractERC1155ERC721.setVoucherKernelAddress( + users.deployer.address + ); + + await expect( + contractERC1155ERC721.mintBatch( + contractCashier.address, + [TOKEN_SUPPLY_ID], + [constants.QTY_10], + ethers.utils.formatBytes32String('0x0') + ) + ).to.be.revertedWith(revertReasons.FN_SELECTOR_NOT_RECOGNIZED); + }); + it('[NEGATIVE][mintBatch] Should revert when _account is a zero address', async () => { await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address From 8234dd0c51b1c35b200423d92611f8b3041ac043 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Fri, 15 Oct 2021 16:10:04 +0530 Subject: [PATCH 39/43] Added negative unit test: mint --- test/6_erc1155721.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index 864fe49a..a728e534 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -1729,6 +1729,15 @@ describe('ERC1155ERC721', () => { ) ).to.be.revertedWith(revertReasons.UNAUTHORIZED_VK); }); + + it('[NEGATIVE][mint] Should revert when to is a zero address', async () => { + await expect( + contractERC1155ERC721.functions[fnSignatures.mint721]( + constants.ZERO_ADDRESS, + 666 + ) + ).to.be.revertedWith(revertReasons.UNAUTHORIZED_VK); + }); }); describe('Metadata', () => { From b72ae0d59eab0eff7db80a030a85e02aa5b54892 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Fri, 15 Oct 2021 17:40:46 +0530 Subject: [PATCH 40/43] inherit from the convenience contract ERC1155Holder and ERC721Holder --- contracts/mocks/MockERC1155Receiver.sol | 35 +++---------------------- contracts/mocks/MockERC721Receiver.sol | 16 +++-------- test/6_erc1155721.ts | 4 +-- 3 files changed, 8 insertions(+), 47 deletions(-) diff --git a/contracts/mocks/MockERC1155Receiver.sol b/contracts/mocks/MockERC1155Receiver.sol index 6ec69135..ab280254 100644 --- a/contracts/mocks/MockERC1155Receiver.sol +++ b/contracts/mocks/MockERC1155Receiver.sol @@ -1,37 +1,8 @@ // SPDX-License-Identifier: LGPL-3.0-or-later pragma solidity 0.7.6; -import "@openzeppelin/contracts/introspection/ERC165.sol"; -import "@openzeppelin/contracts/token/ERC1155/IERC1155Receiver.sol"; +import "@openzeppelin/contracts/token/ERC1155/ERC1155Holder.sol"; -contract MockERC1155Receiver is ERC165, IERC1155Receiver { - function onERC1155BatchReceived( - address, - address, - uint256[] calldata, - uint256[] calldata, - bytes calldata - ) external pure override returns (bytes4) { - return - bytes4( - keccak256( - "onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)" - ) - ); - } - - function onERC1155Received( - address, - address, - uint256, - uint256, - bytes calldata - ) external pure override returns (bytes4) { - return - bytes4( - keccak256( - "onERC1155Received(address,address,uint256,uint256,bytes)" - ) - ); - } +// solhint-disable-next-line no-empty-blocks +contract MockERC1155Receiver is ERC1155Holder { } diff --git a/contracts/mocks/MockERC721Receiver.sol b/contracts/mocks/MockERC721Receiver.sol index 7ca12195..e89489ae 100644 --- a/contracts/mocks/MockERC721Receiver.sol +++ b/contracts/mocks/MockERC721Receiver.sol @@ -1,18 +1,8 @@ // SPDX-License-Identifier: LGPL-3.0-or-later pragma solidity 0.7.6; -import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol"; +import "@openzeppelin/contracts/token/ERC721/ERC721Holder.sol"; -contract MockERC721Receiver is IERC721Receiver { - function onERC721Received( - address, - address, - uint256, - bytes calldata - ) external pure override returns (bytes4) { - return - bytes4( - keccak256("onERC721Received(address,address,uint256,bytes)") - ); - } +// solhint-disable-next-line no-empty-blocks +contract MockERC721Receiver is ERC721Holder { } diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index a728e534..bf5159c1 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -512,7 +512,7 @@ describe('ERC1155ERC721', () => { tokenIds, quantities, users.deployer.signer - ) + ); const txReceipt = await tx.wait(); @@ -587,7 +587,7 @@ describe('ERC1155ERC721', () => { tokenIds, quantities, users.deployer.signer - ) + ); const txReceipt = await tx.wait(); From ae73baefd6baa98767f0d0689d9875914faf81ec Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Fri, 15 Oct 2021 17:52:13 +0530 Subject: [PATCH 41/43] lint fixes --- contracts/mocks/MockERC1155Receiver.sol | 1 + contracts/mocks/MockERC721Receiver.sol | 1 + 2 files changed, 2 insertions(+) diff --git a/contracts/mocks/MockERC1155Receiver.sol b/contracts/mocks/MockERC1155Receiver.sol index ab280254..d8c7be7c 100644 --- a/contracts/mocks/MockERC1155Receiver.sol +++ b/contracts/mocks/MockERC1155Receiver.sol @@ -5,4 +5,5 @@ import "@openzeppelin/contracts/token/ERC1155/ERC1155Holder.sol"; // solhint-disable-next-line no-empty-blocks contract MockERC1155Receiver is ERC1155Holder { + } diff --git a/contracts/mocks/MockERC721Receiver.sol b/contracts/mocks/MockERC721Receiver.sol index e89489ae..9e37d8d0 100644 --- a/contracts/mocks/MockERC721Receiver.sol +++ b/contracts/mocks/MockERC721Receiver.sol @@ -5,4 +5,5 @@ import "@openzeppelin/contracts/token/ERC721/ERC721Holder.sol"; // solhint-disable-next-line no-empty-blocks contract MockERC721Receiver is ERC721Holder { + } From b26816c1626b4e42f8270f51c241183ffa7e711a Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Mon, 18 Oct 2021 12:49:47 +0530 Subject: [PATCH 42/43] Unit testing zero address on mint of ERC-721 --- test/6_erc1155721.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index bf5159c1..e508d685 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -1731,12 +1731,16 @@ describe('ERC1155ERC721', () => { }); it('[NEGATIVE][mint] Should revert when to is a zero address', async () => { + await contractERC1155ERC721.setVoucherKernelAddress( + users.deployer.address + ); + await expect( contractERC1155ERC721.functions[fnSignatures.mint721]( constants.ZERO_ADDRESS, 666 ) - ).to.be.revertedWith(revertReasons.UNAUTHORIZED_VK); + ).to.be.revertedWith(revertReasons.UNSPECIFIED_ADDRESS); }); }); From 804cc510641943d744272a60bb5eaf8c5c9bd3e9 Mon Sep 17 00:00:00 2001 From: bidyut-arianelabs Date: Mon, 18 Oct 2021 15:07:42 +0530 Subject: [PATCH 43/43] added comment for setting voucherkerneladdress as deployer --- test/6_erc1155721.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index e508d685..e10c78cd 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -367,6 +367,8 @@ describe('ERC1155ERC721', () => { it('[balanceOf] Get the balance of tokens of an account', async () => { const expectedCount = constants.QTY_10; + + // spoofing the VoucherKernel address here because the function is being called directly instead of via the VoucherKernel contract await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -495,6 +497,7 @@ describe('ERC1155ERC721', () => { BN(constants.QTY_20), ]; + // spoofing the VoucherKernel address here because the function is being called directly instead of via the VoucherKernel contract await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -570,6 +573,7 @@ describe('ERC1155ERC721', () => { BN(constants.QTY_20), ]; + // spoofing the VoucherKernel address here because the function is being called directly instead of via the VoucherKernel contract await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -703,6 +707,7 @@ describe('ERC1155ERC721', () => { }); it('[mint] Should mint a desired token', async () => { + // spoofing the VoucherKernel address here because the function is being called directly instead of via the VoucherKernel contract await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -751,6 +756,8 @@ describe('ERC1155ERC721', () => { it('[mint] Should mint a desired token to ERC1155 supporting contract', async () => { const erc1155supportingContract = contractMockERC1155Receiver; + + // spoofing the VoucherKernel address here because the function is being called directly instead of via the VoucherKernel contract await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -798,6 +805,7 @@ describe('ERC1155ERC721', () => { }); it('[NEGATIVE][mint] Should revert when to is a contract that cannot receive it', async () => { + // spoofing the VoucherKernel address here because the function is being called directly instead of via the VoucherKernel contract await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -824,6 +832,7 @@ describe('ERC1155ERC721', () => { }); it('[NEGATIVE][mint] Should revert when to is a zero address', async () => { + // spoofing the VoucherKernel address here because the function is being called directly instead of via the VoucherKernel contract await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -839,6 +848,7 @@ describe('ERC1155ERC721', () => { }); it('[burn] Should burn an amount of tokens with the given ID', async () => { + // spoofing the VoucherKernel address here because the function is being called directly instead of via the VoucherKernel contract await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -885,6 +895,7 @@ describe('ERC1155ERC721', () => { }); it('[NEGATIVE][burn] Should revert when _account is a zero address', async () => { + // spoofing the VoucherKernel address here because the function is being called directly instead of via the VoucherKernel contract await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -899,6 +910,7 @@ describe('ERC1155ERC721', () => { }); it('[mintBatch] Should do batch minting of tokens', async () => { + // spoofing the VoucherKernel address here because the function is being called directly instead of via the VoucherKernel contract await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -965,6 +977,8 @@ describe('ERC1155ERC721', () => { it('[mintBatch] Should do batch minting of tokens to ERC1155 supporting contract', async () => { const erc1155supportingContract = contractMockERC1155Receiver; + + // spoofing the VoucherKernel address here because the function is being called directly instead of via the VoucherKernel contract await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -1016,6 +1030,7 @@ describe('ERC1155ERC721', () => { }); it('[NEGATIVE][mintBatch] Should revert when to is a contract that cannot receive it', async () => { + // spoofing the VoucherKernel address here because the function is being called directly instead of via the VoucherKernel contract await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -1031,6 +1046,7 @@ describe('ERC1155ERC721', () => { }); it('[NEGATIVE][mintBatch] Should revert when _account is a zero address', async () => { + // spoofing the VoucherKernel address here because the function is being called directly instead of via the VoucherKernel contract await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -1046,6 +1062,7 @@ describe('ERC1155ERC721', () => { }); it('[NEGATIVE][mintBatch] Should revert if array lengths mismatch', async () => { + // spoofing the VoucherKernel address here because the function is being called directly instead of via the VoucherKernel contract await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -1061,6 +1078,7 @@ describe('ERC1155ERC721', () => { }); it('[burnBatch] Should do batch minting of tokens', async () => { + // spoofing the VoucherKernel address here because the function is being called directly instead of via the VoucherKernel contract await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -1104,6 +1122,7 @@ describe('ERC1155ERC721', () => { }); it('[NEGATIVE][burnBatch] Should revert when _account is a zero address', async () => { + // spoofing the VoucherKernel address here because the function is being called directly instead of via the VoucherKernel contract await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -1118,6 +1137,7 @@ describe('ERC1155ERC721', () => { }); it('[NEGATIVE][burnBatch] Should revert if array lengths mismatch', async () => { + // spoofing the VoucherKernel address here because the function is being called directly instead of via the VoucherKernel contract await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -1231,6 +1251,7 @@ describe('ERC1155ERC721', () => { const expectedOwner = users.buyer.address; const tokenIdsForMint = 123; + // spoofing the VoucherKernel address here because the function is being called directly instead of via the VoucherKernel contract await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -1256,6 +1277,7 @@ describe('ERC1155ERC721', () => { describe('[balanceOf] should count all NFTs assigned to an owner', async () => { it('[balanceOf] returns 4 when 4 NFTs are assigned to owner', async () => { + // spoofing the VoucherKernel address here because the function is being called directly instead of via the VoucherKernel contract await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -1593,6 +1615,7 @@ describe('ERC1155ERC721', () => { }); it('[getApproved] Should return zero address if no address set', async () => { + // spoofing the VoucherKernel address here because the function is being called directly instead of via the VoucherKernel contract await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -1612,6 +1635,7 @@ describe('ERC1155ERC721', () => { it('[getApproved] Should return the approved address for a token ID', async () => { const expectedApprovedAddress = users.other1.address; + // spoofing the VoucherKernel address here because the function is being called directly instead of via the VoucherKernel contract await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -1633,6 +1657,7 @@ describe('ERC1155ERC721', () => { }); it('[mint] Should mint a token', async () => { + // spoofing the VoucherKernel address here because the function is being called directly instead of via the VoucherKernel contract await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -1678,6 +1703,8 @@ describe('ERC1155ERC721', () => { it('[mint] Should be able to mint a token to a contract that supports it', async () => { const supportingContractAddress = contractMockERC721Receiver.address; + + // spoofing the VoucherKernel address here because the function is being called directly instead of via the VoucherKernel contract await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address ); @@ -1731,6 +1758,7 @@ describe('ERC1155ERC721', () => { }); it('[NEGATIVE][mint] Should revert when to is a zero address', async () => { + // spoofing the VoucherKernel address here because the function is being called directly instead of via the VoucherKernel contract await contractERC1155ERC721.setVoucherKernelAddress( users.deployer.address );