乐趣区

关于solidity:Solidity案例详解三飞机管理合约

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 案例详解(三)飞机治理合约》旨在流传区块链相干技术,如有侵权请与咱们分割删除。

退出移动版