student-loan/contracts/LoanToken.sol
2024-08-27 22:01:25 +03:00

62 lines
1.8 KiB
Solidity

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";
contract LoanToken is ERC20, AccessControl {
// Define roles
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
bytes32 public constant BURNER_ROLE = keccak256("BURNER_ROLE");
// Events
event TokensMinted(address indexed to, uint256 amount);
event TokensBurned(address indexed from, uint256 amount);
// Constructor
constructor(string memory name, string memory symbol) ERC20(name, symbol) {
_grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
}
// Modifier for minter role
modifier onlyMinter() {
require(hasRole(MINTER_ROLE, msg.sender), "Caller is not a minter");
_;
}
// Modifier for burner role
modifier onlyBurner() {
require(hasRole(BURNER_ROLE, msg.sender), "Caller is not a burner");
_;
}
// Mint new tokens
function mint(address to, uint256 amount) external onlyMinter {
_mint(to, amount);
emit TokensMinted(to, amount);
}
// Burn tokens
function burn(uint256 amount) external onlyBurner {
_burn(msg.sender, amount);
emit TokensBurned(msg.sender, amount);
}
// Allow admin to add or remove roles
function addMinter(address account) external onlyRole(DEFAULT_ADMIN_ROLE) {
grantRole(MINTER_ROLE, account);
}
function removeMinter(address account) external onlyRole(DEFAULT_ADMIN_ROLE) {
revokeRole(MINTER_ROLE, account);
}
function addBurner(address account) external onlyRole(DEFAULT_ADMIN_ROLE) {
grantRole(BURNER_ROLE, account);
}
function removeBurner(address account) external onlyRole(DEFAULT_ADMIN_ROLE) {
revokeRole(BURNER_ROLE, account);
}
}