fixed minting permissions and added tests
This commit is contained in:
parent
5a5b6dd695
commit
9ef259d559
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
|
@ -1,4 +1,4 @@
|
|||
{
|
||||
"_format": "hh-sol-dbg-1",
|
||||
"buildInfo": "../../build-info/b939a90cc764b581362b954e5d4c1a71.json"
|
||||
"buildInfo": "../../build-info/61ddbb77dee8171ab2d4d43d1019f3d7.json"
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -2,8 +2,8 @@
|
|||
"_format": "hh-sol-cache-2",
|
||||
"files": {
|
||||
"/Users/migue/Documents/psychedelic/sites_nfts/contracts/SitesNFTs.sol": {
|
||||
"lastModificationDate": 1665514025103,
|
||||
"contentHash": "28e17cde1dc2d621c69ac3d29df60e12",
|
||||
"lastModificationDate": 1665602082122,
|
||||
"contentHash": "81aa3743aebd845356c61c22e8bdaca5",
|
||||
"sourceName": "contracts/SitesNFTs.sol",
|
||||
"solcConfig": {
|
||||
"version": "0.8.7",
|
||||
|
|
|
|||
|
|
@ -12,7 +12,13 @@ contract SitesNFTs is ERC721URIStorage, AccessControl {
|
|||
Counters.Counter private _tokenIds;
|
||||
string private baseURI;
|
||||
|
||||
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
|
||||
bytes32 public constant MINTER_ROLE = 0x4d494e5445525f524f4c45000000000000000000000000000000000000000000; // "MINTER_ROLE"
|
||||
|
||||
modifier canMint() {
|
||||
bool isMinterOrAdmin = hasRole(MINTER_ROLE, msg.sender) || hasRole(DEFAULT_ADMIN_ROLE, msg.sender);
|
||||
require(isMinterOrAdmin, "Caller has no permission to mint.");
|
||||
_;
|
||||
}
|
||||
|
||||
constructor(string memory name, string memory symbol) ERC721(name, symbol) {
|
||||
baseURI = "data:application/json;base64,";
|
||||
|
|
@ -20,9 +26,9 @@ contract SitesNFTs is ERC721URIStorage, AccessControl {
|
|||
}
|
||||
|
||||
// Token uri is the Base64 encoded json metadata
|
||||
function mintNFT(string memory _tokenURI) public onlyRole(MINTER_ROLE) returns (uint256) {
|
||||
function mint(string memory _tokenURI, address account) public canMint() returns (uint256) {
|
||||
uint256 newItemId = _tokenIds.current();
|
||||
_safeMint(msg.sender, newItemId);
|
||||
_safeMint(account, newItemId);
|
||||
_setTokenURI(newItemId, _tokenURI);
|
||||
|
||||
_tokenIds.increment();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
async function main() {
|
||||
const [deployer] = await ethers.getSigners();
|
||||
|
||||
console.log("Deploying contracts with the account:", deployer.address);
|
||||
|
||||
console.log("Account balance:", (await deployer.getBalance()).toString());
|
||||
|
||||
const SitesNFTs = await ethers.getContractFactory("SitesNFTs");
|
||||
const sitesNFTs = await SitesNFTs.deploy("Sites NFTs", "SNFT");
|
||||
|
||||
console.log("SitesNFTs address:", sitesNFTs.address);
|
||||
}
|
||||
|
||||
main()
|
||||
.then(() => process.exit(0))
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
|
@ -63,6 +63,25 @@ describe("SitesNFTs contract", function () {
|
|||
expect(hasMinterRole).to.equal(true);
|
||||
});
|
||||
|
||||
it("User with DEFAULT_ADMIN_ROLE should be able to assign MINTER_ROLE to himself and still have DEFAULT_ADMIN_ROLE", async () => {
|
||||
const [owner] = await ethers.getSigners();
|
||||
|
||||
const SitesNFTs = await ethers.getContractFactory("SitesNFTs");
|
||||
|
||||
const hardhatSitesNFTs = await SitesNFTs.deploy("Sites NFTs", "SNFT");
|
||||
|
||||
const MINTER_ROLE = "MINTER_ROLE";
|
||||
const DEFAULT_ADMIN_ROLE = "";
|
||||
|
||||
await hardhatSitesNFTs.grantRole(ethers.utils.formatBytes32String(MINTER_ROLE), await owner.getAddress());
|
||||
|
||||
const hasMinterRole = await hardhatSitesNFTs.hasRole(ethers.utils.formatBytes32String(MINTER_ROLE), await owner.getAddress());
|
||||
const hasAdminRole = await hardhatSitesNFTs.hasRole(ethers.utils.formatBytes32String(DEFAULT_ADMIN_ROLE), await owner.getAddress());
|
||||
|
||||
expect(hasMinterRole).to.equal(true);
|
||||
expect(hasAdminRole).to.equal(true);
|
||||
});
|
||||
|
||||
it("User with DEFAULT_ADMIN_ROLE should be able to assign DEFAULT_ADMIN_ROLE to another user", async () => {
|
||||
const [owner, address1] = await ethers.getSigners();
|
||||
|
||||
|
|
@ -79,6 +98,26 @@ describe("SitesNFTs contract", function () {
|
|||
expect(hasAdminRole).to.equal(true);
|
||||
});
|
||||
|
||||
it("User with DEFAULT_ADMIN_ROLE should be able to assign DEFAULT_ADMIN_ROLE to another user and still have DEFAULT_ADMIN_ROLE", async () => {
|
||||
const [owner, address1] = await ethers.getSigners();
|
||||
|
||||
const SitesNFTs = await ethers.getContractFactory("SitesNFTs");
|
||||
|
||||
const hardhatSitesNFTs = await SitesNFTs.deploy("Sites NFTs", "SNFT");
|
||||
|
||||
const DEFAULT_ADMIN_ROLE = "";
|
||||
|
||||
await hardhatSitesNFTs.grantRole(ethers.utils.formatBytes32String(DEFAULT_ADMIN_ROLE), await address1.getAddress());
|
||||
|
||||
let hasAdminRole = await hardhatSitesNFTs.hasRole(ethers.utils.formatBytes32String(DEFAULT_ADMIN_ROLE), await address1.getAddress());
|
||||
|
||||
expect(hasAdminRole).to.equal(true);
|
||||
|
||||
hasAdminRole = await hardhatSitesNFTs.hasRole(ethers.utils.formatBytes32String(DEFAULT_ADMIN_ROLE), await owner.getAddress());
|
||||
|
||||
expect(hasAdminRole).to.equal(true);
|
||||
});
|
||||
|
||||
it("User without DEFAULT_ADMIN_ROLE shouldnt be able to assign DEFAULT_ADMIN_ROLE to another user", async () => {
|
||||
const [owner, address1, address2] = await ethers.getSigners();
|
||||
|
||||
|
|
@ -110,13 +149,68 @@ describe("SitesNFTs contract", function () {
|
|||
|
||||
try {
|
||||
await hardhatSitesNFTs.connect(address1).grantRole(ethers.utils.formatBytes32String(MINTER_ROLE), await address2.getAddress());
|
||||
} catch (e) {
|
||||
|
||||
}
|
||||
} catch (e) {}
|
||||
|
||||
const hasMinterRole = await hardhatSitesNFTs.hasRole(ethers.utils.formatBytes32String(MINTER_ROLE), await address2.getAddress());
|
||||
|
||||
expect(hasMinterRole).to.equal(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Minting", () => {
|
||||
it("User with DEFAULT_ADMIN_ROLE should be able to mint", async () => {
|
||||
const [owner, address1] = await ethers.getSigners();
|
||||
|
||||
const SitesNFTs = await ethers.getContractFactory("SitesNFTs");
|
||||
|
||||
const hardhatSitesNFTs = await SitesNFTs.deploy("Sites NFTs", "SNFT");
|
||||
|
||||
const tokenURI = "tokenURI";
|
||||
|
||||
await hardhatSitesNFTs.mint(tokenURI, await address1.getAddress());
|
||||
|
||||
const balance = await hardhatSitesNFTs.balanceOf(await address1.getAddress());
|
||||
|
||||
expect(balance).to.equal(1);
|
||||
});
|
||||
|
||||
it("User with MINTER_ROLE should be able to mint", async () => {
|
||||
const [owner, address1, address2] = await ethers.getSigners();
|
||||
|
||||
const SitesNFTs = await ethers.getContractFactory("SitesNFTs");
|
||||
|
||||
const hardhatSitesNFTs = await SitesNFTs.deploy("Sites NFTs", "SNFT");
|
||||
|
||||
const MINTER_ROLE = "MINTER_ROLE";
|
||||
const tokenURI = "tokenURI";
|
||||
|
||||
await hardhatSitesNFTs.grantRole(ethers.utils.formatBytes32String(MINTER_ROLE), await address1.getAddress());
|
||||
|
||||
await hardhatSitesNFTs.hasRole(ethers.utils.formatBytes32String(MINTER_ROLE), await address1.getAddress());
|
||||
|
||||
await hardhatSitesNFTs.connect(address1).mint(tokenURI, await address2.getAddress());
|
||||
|
||||
const balance = await hardhatSitesNFTs.balanceOf(await address2.getAddress());
|
||||
|
||||
expect(balance).to.equal(1);
|
||||
});
|
||||
|
||||
it("User without MINTER_ROLE or DEFAULT_ADMIN_ROLE shouldnt be able to mint", async () => {
|
||||
const [owner, address1, address2] = await ethers.getSigners();
|
||||
|
||||
const SitesNFTs = await ethers.getContractFactory("SitesNFTs");
|
||||
|
||||
const hardhatSitesNFTs = await SitesNFTs.deploy("Sites NFTs", "SNFT");
|
||||
|
||||
const tokenURI = "tokenURI";
|
||||
|
||||
try {
|
||||
await hardhatSitesNFTs.connect(address1).mint(tokenURI, await address2.getAddress());
|
||||
} catch(e) {}
|
||||
|
||||
const balance = await hardhatSitesNFTs.balanceOf(await address2.getAddress());
|
||||
|
||||
expect(balance).to.equal(0);
|
||||
});
|
||||
})
|
||||
});
|
||||
Loading…
Reference in New Issue