I am trying to test a very basic contract based on ERC1155.
As a start, the contract has just a single method that calls _mint.
I am trying to write a Solidity test for this method but I get an error because the sender of the call is the test contract itself and it looks like it cannot receive the minted token unless it implements ERC1155Receiver
.
I am trying now to implement ERC1155Receiver
, but I get the following error when I run the test:
1) TestFoo
"before all" hook: prepare suite for "testMint":
Error: Could not find artifacts for @openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol from any sources
at Resolver.require (/home/my/.nvm/versions/node/v16.4.0/lib/node_modules/truffle/build/webpack:/packages/resolver/dist/lib/resolver.js:61:1)
at Context.deployTestDependencies (/home/my/.nvm/versions/node/v16.4.0/lib/node_modules/truffle/build/webpack:/packages/core/lib/testing/SolidityTest.js:195:1)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at Context.<anonymous> (/home/my/.nvm/versions/node/v16.4.0/lib/node_modules/truffle/build/webpack:/packages/core/lib/testing/SolidityTest.js:22:1)
The code of the test is the following:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../contracts/FooContract.sol";
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol";
import "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Receiver.sol";
contract TestFoo is ERC1155Holder {
uint public initialBalance = 100 ether;
function testMint() public {
address payable fooContractAddress = DeployedAddresses.FooContract();
FooContract fooContract = FooContract(fooContractAddress);
fooContract.mintTI();
}
function supportsInterface(bytes4 interfaceId) public view virtual override(ERC1155Receiver) returns (bool) {
return super.supportsInterface(interfaceId);
}
}
The code of the contract is:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol";
contract FooContract is ERC1155, ERC1155Holder {
using Counters for Counters.Counter;
Counters.Counter private nftCounter;
constructor() ERC1155("") {
}
function mintTI() public
{
nftCounter.increment();
uint256 tiID = nftCounter.current();
_mint(msg.sender, tiID, 1, "");
}
function supportsInterface(bytes4 interfaceId) public view virtual override(ERC1155, ERC1155Receiver) returns (bool) {
return super.supportsInterface(interfaceId);
}
}
Before adding the interface ERC1155Receiver I was getting the following error:
1) TestFoo
testMint:
Error: Returned error: VM Exception while processing transaction: revert ERC1155: transfer to non ERC1155Receiver implementer -- Reason given: ERC1155: transfer to non ERC1155Receiver implementer.