Roles 角色合约(主要为后面的保险公司 C 提供环境)
// SPDX-License-Identifier: 3.0 pragma solidity ^0.8.20; library Roles { struct Role { mapping (address => bool) bearer; } function has(Role storage role, address account) internal view returns (bool) { require(account != address(0), "Roles: account is the zero address"); return role.bearer[account]; }//查询添加的账户是否为空 function add(Role storage role, address account) internal { require(!has(role, account), "Roles: account already has role"); role.bearer[account] = true; }//增加账户 function remove(Role storage role, address account) internal { require(has(role, account), "Roles: account does not have role"); role.bearer[account] = false; }//删除账户}
复制代码
.AirlineV.sol 航空公司合约首先声明两个事件(增删账户)
// SPDX-License-Identifier: 3.0 pragma solidity ^0.8.20; import "./Roles.sol"; contract AirlineV { using Roles for Roles.Role; Roles.Role private _airlineV;
复制代码
首先声明两个事件(增删账户)
event airlineVAdded(address account); event airlineVRemoved(address account);
复制代码
初始化合约(构造函数)
constructor (address airlineV) { _addairlineV(airlineV);}
复制代码
创建修饰器 onlyairlineV(限制访问权限时调用)
modifier onlyairlineV() {
require( isairlineV(msg.sender),"airlineVRole: caller does not have the airlineV role");
_;
}
复制代码
构造 isairlineV 函数判断是否具有 airlineV 角色
function isairlineV(address account)public view returns(bool){ return _airlineV.has(account); }
复制代码
构造两个添加航空公司的函数,用于合约内部调用(_addairlineV)和外部调用(addairlineV)
function _addairlineV(address account) internal { _airlineV.add(account); emit airlineVAdded(account); }
复制代码
构造一个合约内部删除函数(_removeairlineV)和用户自行退出函(renounceairlineV)
function addairlineV(address account) public onlyairlineV{ _addairlineV(account); } function _removeairlineV(address account) internal { // _ai,.rlineV.remove(account); emit airlineVRemoved(account); } function renounceairlineV() public { _removeairlineV(msg.sender); } }
复制代码
演示:
输入账号,点击 transact
使用部署的账号进行增加操作
如果使用 oxAb8...则会报错
isairlineV 进行判断账号是否为航空公司身份
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/2302_77339802/article/details/131834020
文章来源:CSDN 博主「发呆...」
文章原标题:《solidity 案例详解(三)飞机管理合约》
旨在传播区块链相关技术,如有侵权请与我们联系删除。
评论