I am having trouble testing this smart contract. When I call isAdmin, I get the role error.
Is msg.sender different every time? Is there a way to specify a “from” address? I’ve tried a few different ways of testing but not really getting anywhere. My code is below.
EstateAgent.sol
pragma solidity ^0.5.0;
import "@openzeppelin/contracts-ethereum-package/contracts/access/Roles.sol";
import "@openzeppelin/upgrades/contracts/Initializable.sol";
contract EstateAgent is Initializable {
using Roles for Roles.Role;
Roles.Role private _admins;
function initialize() public initializer {
_admins.add(msg.sender);
}
function isAdmin() public view returns (bool){
require(_admins.has(msg.sender), "NOT_ADMIN__ROLE");
return true;
}
}
this way of deployment not calling initialize() method, so your code _admins.add(msg.sender); has not been reached at all.
If you want to create a proxy with initializer - please read the docs on how to do it. Here you can find a sample of code: https://docs.openzeppelin.com/sdk/2.6/zos-lib
Also, always clearly define who calls the method or creating deployment with “{from: }”
I am now having another issue with testing an ERC721 token. @abcoathup
I am trying to allow the initial EstateAgent admin to mine Deed tokens but when I call balanceOf in the test, it returns 0. Is there something wrong with how they’re interacting with each other?
Or should I be using the test helpers to check events?
Also I read I should be using something like await this.deed.methods['initialize(address)'](admin); for initialising the token but I’m not sure entirely where to place it as I can’t get it to work I think
Deed.sol
pragma solidity ^0.5.0;
import '@openzeppelin/upgrades/contracts/Initializable.sol';
import '@openzeppelin/contracts-ethereum-package/contracts/token/ERC721/ERC721Full.sol';
import '@openzeppelin/contracts-ethereum-package/contracts/token/ERC721/ERC721Mintable.sol';
import "@openzeppelin/contracts-ethereum-package/contracts/drafts/Counters.sol";
contract Deed is Initializable, ERC721Full, ERC721Mintable {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
function initialize(address agent) public initializer {
ERC721.initialize();
ERC721Enumerable.initialize();
ERC721Metadata.initialize("Deed", "DEED");
ERC721Mintable.initialize(agent);
}
function createDeed(address to, string memory tokenURI) public returns (uint256){
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(to, newItemId);
_setTokenURI(newItemId, tokenURI);
return newItemId;
}
}
methods calls and transaction options are documented in the web3 docs. With these options you also can set, for example, amount of gas that will be sent with transaction, or amount of ether
The issue with the test was using call rather than send for sending the createDeed transaction. We also need to specify who the transaction is from and the gas limit.
I suggest for new questions that you create a new, standalone topic (unless they are tightly related). This makes it easier for other community members to potentially answer.
As an aside, I suggest having a test file per contract, as in this case there were tests for two contracts in the one file.
I posted it in here as I assumed the problem was to do with it being upgradeable but now I understand, it’s like oz cli where you have to do call or send-tx.
Yep, I will definitely be splitting it up, this is more or less code that will be changed anyway as I was trying to get a minimal token test.
Thanks a lot for your help. Definitely helped me figure out whats going on in the testing.