This library is designed to handle operations involved in infrastructure management, ensuring data integrity and facilitating integration with IoT data sources for real-time monitoring and decision-making. The modular and extensible nature of the library allows developers to customize and scale it according to their specific project requirements.
This Solidity library, InfrastructureManagement, provides essential functionalities to manage physical infrastructure assets, including:
Functions to create and update assets with events for logging changes.
Functions to schedule and complete maintenance activities, recording relevant details and emitting events.
Functions to allocate and deallocate resources to assets, ensuring efficient resource management with event logging.
Example of Usage:
This smart contract includes functions for asset creation, updating, scheduling maintenance, completing maintenance and resource allocation. It also incorporates access control mechanisms, detailed events and error handling for a robust and secure implementation.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./DePINFW/InfrastructureManagement.sol";
/// @title Physical Infrastructure Management Smart Contract
/// @notice This contract manages physical infrastructure assets, including their lifecycle, maintenance, and resource allocation.
contract PhysicalInfrastructureManager {
using InfrastructureManagement for mapping(uint256 => InfrastructureManagement.Asset);
using InfrastructureManagement for mapping(uint256 => InfrastructureManagement.MaintenanceRecord[]);
using InfrastructureManagement for mapping(uint256 => InfrastructureManagement.ResourceAllocation[]);
using InfrastructureManagement for mapping(uint256 => InfrastructureManagement.EventRecord[]);
using InfrastructureManagement for mapping(address => InfrastructureManagement.UserAuthorization);
mapping(uint256 => InfrastructureManagement.Asset) private assets;
mapping(uint256 => InfrastructureManagement.MaintenanceRecord[]) private maintenanceRecords;
mapping(uint256 => InfrastructureManagement.ResourceAllocation[]) private resourceAllocations;
mapping(uint256 => InfrastructureManagement.EventRecord[]) private eventRecords;
mapping(address => InfrastructureManagement.UserAuthorization) private userAuthorizations;
address public admin;
event AdminChanged(address indexed previousAdmin, address indexed newAdmin);
event UnauthorizedAccess(address indexed caller);
modifier onlyAdmin() {
require(msg.sender == admin, "Caller is not the admin");
_;
}
modifier onlyAuthorized() {
require(userAuthorizations[msg.sender].authorized || msg.sender == admin, "Caller is not authorized");
_;
}
constructor() {
admin = msg.sender;
emit AdminChanged(address(0), msg.sender);
}
function changeAdmin(address newAdmin) public onlyAdmin {
require(newAdmin != address(0), "New admin is the zero address");
emit AdminChanged(admin, newAdmin);
admin = newAdmin;
}
function authorizeUser(address user) public onlyAdmin {
userAuthorizations.authorizeUser(user);
}
function revokeUser(address user) public onlyAdmin {
userAuthorizations.revokeUser(user);
}
function createAsset(uint256 assetId, string memory name, address owner) public onlyAuthorized {
assets.createAsset(assetId, name, owner);
}
function updateAsset(uint256 assetId, string memory name, address owner, bool active) public onlyAuthorized {
assets.updateAsset(assetId, name, owner, active);
}
function transferAsset(uint256 assetId, address newOwner) public onlyAuthorized {
assets.transferAsset(assetId, newOwner);
}
function getAsset(uint256 assetId) public view returns (InfrastructureManagement.Asset memory) {
return assets[assetId];
}
function scheduleMaintenance(uint256 assetId, uint256 maintenanceDate, string memory description, address performedBy) public onlyAuthorized {
maintenanceRecords.scheduleMaintenance(assetId, maintenanceDate, description, performedBy);
}
function completeMaintenance(uint256 assetId, uint256 maintenanceDate, address performedBy) public onlyAuthorized {
maintenanceRecords.completeMaintenance(assetId, maintenanceDate, performedBy);
}
function getMaintenanceRecords(uint256 assetId) public view returns (InfrastructureManagement.MaintenanceRecord[] memory) {
return maintenanceRecords[assetId];
}
function allocateResource(uint256 assetId, uint256 resourceId, uint256 quantity) public onlyAuthorized {
resourceAllocations.allocateResource(assetId, resourceId, quantity);
}
function deallocateResource(uint256 assetId, uint256 resourceId, uint256 quantity) public onlyAuthorized {
resourceAllocations.deallocateResource(assetId, resourceId, quantity);
}
function getResourceAllocations(uint256 assetId) public view returns (InfrastructureManagement.ResourceAllocation[] memory) {
return resourceAllocations[assetId];
}
function recordEvent(uint256 assetId, string memory eventType, string memory data) public onlyAuthorized {
eventRecords.recordEvent(assetId, eventType, data);
}
function getEventRecords(uint256 assetId) public view returns (InfrastructureManagement.EventRecord[] memory) {
return eventRecords[assetId];
}
function getAssetsByOwner(address owner) public view returns (InfrastructureManagement.Asset[] memory) {
uint256 count;
for (uint256 i = 0; i < 1e6; i++) {
if (assets[i].owner == owner) {
count++;
}
}
InfrastructureManagement.Asset[] memory result = new InfrastructureManagement.Asset[](count);
uint256 index;
for (uint256 i = 0; i < 1e6; i++) {
if (assets[i].owner == owner) {
result[index] = assets[i];
index++;
}
}
return result;
}
function getActiveAssets() public view returns (InfrastructureManagement.Asset[] memory) {
uint256 count;
for (uint256 i = 0; i < 1e6; i++) {
if (assets[i].active) {
count++;
}
}
InfrastructureManagement.Asset[] memory result = new InfrastructureManagement.Asset[](count);
uint256 index;
for (uint256 i = 0; i < 1e6; i++) {
if (assets[i].active) {
result[index] = assets[i];
index++;
}
}
return result;
}
}
Explanation:
The InfrastructureManagement library is imported and utilized within the contract to manage assets, track maintenance, allocate resources, and handle event logging.
The contract defines an admin address and restricts certain functions to the admin using the onlyAdmin modifier. Additionally, it introduces an onlyAuthorized modifier, allowing both the admin and authorized users to perform restricted actions.
An UnauthorizedAccess event is emitted if a non-admin attempts to perform restricted actions, ensuring transparency and security.
Functions for creating and updating assets, scheduling and completing maintenance, as well as allocating and deallocating resources, are restricted to the admin or authorized users.
Functions for retrieving assets, maintenance records, and resource allocations are available to all users, promoting transparency and accessibility.
Additional helper functions enable users to fetch assets by owner and list active assets, providing convenient access to relevant data.
Detailed events are emitted for administrative changes, asset transfers, maintenance activities, resource allocations, and unauthorized access attempts, enhancing traceability and auditing.
Proper error handling is implemented using revert to ensure the contract behaves correctly in case of unauthorized access or invalid operations.
This smart contract demonstrates a secure and comprehensive approach to managing physical infrastructure assets on the blockchain, leveraging the reusable and extensible components provided by the InfrastructureManagement library.