Skip to content
This repository was archived by the owner on Mar 27, 2026. It is now read-only.
This repository was archived by the owner on Mar 27, 2026. It is now read-only.

Ownership #32

@changeschung

Description

@changeschung

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

// Import OpenZeppelin libraries
import "@openzeppelin/contracts/access/AccessControl.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";

contract OwnershipNFT is ERC721URIStorage, AccessControl {
// Define roles
bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");

// Token ID tracker
uint256 private _tokenIdCounter;

// Constructor: Initializes the contract, sets up roles, and deploys the NFT with a name and symbol
constructor() ERC721("OwnershipNFT", "ONFT") {
    // Grant deployer the default admin role and admin role
    _setupRole(DEFAULT_ADMIN_ROLE, msg.sender); // DEFAULT_ADMIN_ROLE is required for role management
    _setupRole(ADMIN_ROLE, msg.sender);         // Assign ADMIN_ROLE to deployer
}

// Modifier to restrict minting only to admins
modifier onlyAdmin() {
    require(hasRole(ADMIN_ROLE, msg.sender), "Permission denied: Not an admin");
    _;
}

// Function to mint a new NFT
function mintNFT(address to, string memory uri) public onlyAdmin {
    _tokenIdCounter++;
    uint256 tokenId = _tokenIdCounter;

    _mint(to, tokenId);       // Mint the token to the recipient's address
    _setTokenURI(tokenId, uri); // Set metadata URI for the token
}

// Function to transfer admin role (ownership) to a new address
function transferAdminRole(address newAdmin) public {
    require(hasRole(ADMIN_ROLE, msg.sender), "Permission denied: Not an admin");
    require(newAdmin != address(0), "New admin cannot be the zero address");

    // Revoke admin role from current admin
    _revokeRole(ADMIN_ROLE, msg.sender);

    // Grant admin role to the new admin
    _grantRole(ADMIN_ROLE, newAdmin);
}

// Function to revoke admin privileges from an address (only by DEFAULT_ADMIN_ROLE)
function revokeAdminRole(address account) public {
    require(hasRole(DEFAULT_ADMIN_ROLE, msg.sender), "Permission denied: Not a default admin");
    require(account != address(0), "Cannot revoke from zero address");

    _revokeRole(ADMIN_ROLE, account);
}

// Function to renounce admin privileges (self-action)
function renounceAdminRole() public {
    require(hasRole(ADMIN_ROLE, msg.sender), "You are not an admin");
    _revokeRole(ADMIN_ROLE, msg.sender);
}

// Internal function to set base URI (for metadata)
function _baseURI() internal pure override returns (string memory) {
    return "https://example.com/metadata/";
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions