// SPDX-License-Identifier: MIT
pragma solidity >= 0.6.0;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "hardhat/console.sol";
contract MyAccessControl is Ownable,AccessControl,ERC20{
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
bytes32 public constant BURN_ROLE = keccak256("BURN_ROLE");
//name,symbol
constructor() ERC20("MyToken","AAA"){
_setupRole(DEFAULT_ADMIN_ROLE,msg.sender);
}
function mint(address addr,uint256 amount) public{
bool ret = hasRole(MINTER_ROLE,msg.sender);
console.logBool(ret);
require(hasRole(MINTER_ROLE,msg.sender),"need miner role");
_mint(addr,amount);
}
function burn(address addr,uint256 amount) public{
require(hasRole(BURN_ROLE,msg.sender),"need burn role");
_burn(addr,amount);
}
}
const { ethers } = require("hardhat")
const web3 = require("web3")
contractName = "MyAccessControl";
describe(contractName, function () {
it("test ", async function () {
const Contract = await ethers.getContractFactory(contractName);
const contract = await Contract.deploy();
console.log("owner:",await contract.owner());
[owner1, owner2,owner3] = await ethers.getSigners();
//owner1是管理员,同样没有mint和burn的权限,但它可以给其它地址加权限
await contract.connect(owner1).grantRole(web3.utils.soliditySha3("MINTER_ROLE"),owner2.address);
//给owner3添加mint权限
await contract.connect(owner2).mint(owner3.address,100);
console.log("after mint,owner3 balance:",await contract.balanceOf(owner3.address));
//给owner3添加burn权限
await contract.connect(owner1).grantRole(web3.utils.soliditySha3("BURN_ROLE"),owner3.address);
await contract.connect(owner3).burn(owner3.address,10);
console.log("after burn,owner3 balance:",await contract.balanceOf(owner3.address));
//修改owner
await contract.connect(owner1).transferOwnership(owner2.address);
console.log("new owner:",await contract.owner());
});
});
MyAccessControl
owner: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
true
after mint,owner3 balance: BigNumber { value: "100" }
after burn,owner3 balance: BigNumber { value: "90" }
new owner: 0x70997970C51812dc3A010C7d01b50e0d17dc79C8
分享到:
相关推荐
**AccessControl 门禁管理软件详解** 门禁系统在现代安全管理中扮演着至关重要的角色,而中控ZKT AccessControl门禁管理软件是一款专为此设计的高效解决方案。这款轻量级软件提供了丰富的功能,旨在满足各类机构...
**Cisco Secure Access Control System 5.2 知识点详解** Cisco Secure Access Control System (ACS) 是Cisco公司推出的一种强大的身份认证、授权和审计(AAA)解决方案,它为网络访问提供了安全控制。版本5.2是该...
基于角色的访问控制(Role-Based Access Control,简称 RBAC)是一种广泛应用于现代信息系统中的安全机制,其核心思想是通过定义不同的角色来实现对用户权限的有效管理和控制。这种机制不仅能够简化系统的权限配置,...
全球平台安全元素访问控制规范(Global Platform Secure Element Access Control)是全球平台组织(GlobalPlatform Inc.)于2012年发布的,旨在为安全元件(Secure Element,SE)的访问控制提供标准化指导。...
thinking in java 4 access control
cisco secure access control system镜像1。 cisco secure access control system镜像1。
cisco secure access control system 镜像2 cisco secure access control system 镜像2
《Python库AccessControl详解》 在Python编程领域,库扮演着至关重要的角色,它们提供了丰富的功能,让开发者能够高效地实现各种复杂任务。本文将详细探讨AccessControl这一库,它是Python开发中的一个重要组件,...
2. MAC层(Medium Access Control Layer):在OSI七层模型中,MAC层位于数据链路层的第二层,负责管理设备如何接入无线媒介,确保数据包在无线网络上有效传输。包括CSMA/CA(Carrier Sense Multiple Access with ...
setup access control in oracle
标题中的"AccessControl-4.1-cp36-cp36m-win_amd64.whl.zip"是一个Python库的发行文件,主要用于权限管理。这个文件是为Python 3.6版本设计的,适用于Windows操作系统,且是64位架构。"AccessControl"是库的名称,...
标题中的"AccessControl-4.0b4-cp36-cp36m-win32.whl.zip"是一个Python库的发行版本,其中包含了“AccessControl”模块的源代码或预编译版本。这个文件是为Python 3.6编译的,并且是针对Windows 32位系统的。"4.0b4...
Access_Control_System_V3.5.4.1_EN.exe DatabaseBackupRestore.rar IGK Software Operation Guide.pdf IgkDatabase_V1.6_EN.exe 安装顺序:先裝SQL2000 再裝IgkDatabase_V1.6_EN最後才裝Access_Control_System_V...
cisco secure access control system 镜像5 cisco secure access control system 镜像5
在本文中,作者提出了一个支持协作的基于属性访问控制(Cooperative Attribute-Based Access Control, 简称Coop-ABAC)机制。该机制将用户划分为不同的群体,并且每个用户都与一组属性相关联。通过基于属性的访问...
cisco secure access control system 镜像4 cisco secure access control system 镜像4
cisco secure access control system 镜像3 cisco secure access control system 镜像2
标题“Part11:Wireless LAN Medium Access Control (MAC) and Physical Layer (PHY) Specifications”明确指出了文档的核心内容,即IEEE 802.11标准中的无线局域网(Wireless Local Area Network, WLAN)介质访问...
在IT领域,尤其是在无线通信与网络技术中,IEEE 802.11标准作为无线局域网(Wireless Local Area Network,WLAN)的媒介访问控制(Medium Access Control,MAC)协议,其性能表现是业界关注的重点。本文将深入探讨...
通过深入阅读《IEEE Std 802.11ax-2021 Wireless LAN Medium Access Control (MAC) and Physical Layer (PHY) Specifications - Amendment 1:Enhancements for High-Efficiency WLAN》这份文档,可以全面理解这一...