Example: Physical Infrastructure Management Library
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/// @notice This library provides reusable smart contract modules for managing physical infrastructure on the blockchain.
library InfrastructureManagement {
struct Asset {
string name;
uint256 id;
address owner;
uint256 createdAt;
uint256 updatedAt;
bool active;
}
struct MaintenanceRecord {
uint256 assetId;
uint256 maintenanceDate;
string description;
address performedBy;
bool completed;
}
struct ResourceAllocation {
uint256 assetId;
uint256 resourceId;
uint256 quantity;
uint256 allocatedAt;
bool active;
}
struct EventRecord {
uint256 assetId;
uint256 timestamp;
string eventType;
string data;
}
struct UserAuthorization {
address user;
bool authorized;
}
event AssetCreated(uint256 assetId, string name, address owner);
event AssetUpdated(uint256 assetId, string name, address owner, bool active);
event AssetTransferred(uint256 assetId, address from, address to);
event MaintenanceScheduled(uint256 assetId, uint256 maintenanceDate, string description, address performedBy);
event MaintenanceCompleted(uint256 assetId, uint256 maintenanceDate, address performedBy);
event ResourceAllocated(uint256 assetId, uint256 resourceId, uint256 quantity, uint256 allocatedAt);
event ResourceDeallocated(uint256 assetId, uint256 resourceId, uint256 quantity, uint256 deallocatedAt);
event EventRecorded(uint256 assetId, uint256 timestamp, string eventType, string data);
event UserAuthorized(address user);
event UserRevoked(address user);
function createAsset(mapping(uint256 => Asset) storage assets, uint256 assetId, string memory name, address owner) public {
require(assets[assetId].id == 0, "Asset already exists");
assets[assetId] = Asset({
name: name,
id: assetId,
owner: owner,
createdAt: block.timestamp,
updatedAt: block.timestamp,
active: true
});
emit AssetCreated(assetId, name, owner);
}
function updateAsset(mapping(uint256 => Asset) storage assets, uint256 assetId, string memory name, address owner, bool active) public {
require(assets[assetId].id != 0, "Asset does not exist");
Asset storage asset = assets[assetId];
asset.name = name;
asset.owner = owner;
asset.updatedAt = block.timestamp;
asset.active = active;
emit AssetUpdated(assetId, name, owner, active);
}
function transferAsset(mapping(uint256 => Asset) storage assets, uint256 assetId, address newOwner) public {
require(assets[assetId].id != 0, "Asset does not exist");
require(newOwner != address(0), "New owner is the zero address");
Asset storage asset = assets[assetId];
address previousOwner = asset.owner;
asset.owner = newOwner;
asset.updatedAt = block.timestamp;
emit AssetTransferred(assetId, previousOwner, newOwner);
}
function scheduleMaintenance(mapping(uint256 => MaintenanceRecord[]) storage maintenanceRecords, uint256 assetId, uint256 maintenanceDate, string memory description, address performedBy) public {
maintenanceRecords[assetId].push(MaintenanceRecord({
assetId: assetId,
maintenanceDate: maintenanceDate,
description: description,
performedBy: performedBy,
completed: false
}));
emit MaintenanceScheduled(assetId, maintenanceDate, description, performedBy);
}
function completeMaintenance(mapping(uint256 => MaintenanceRecord[]) storage maintenanceRecords, uint256 assetId, uint256 maintenanceDate, address performedBy) public {
MaintenanceRecord[] storage records = maintenanceRecords[assetId];
for (uint256 i = 0; i < records.length; i++) {
if (records[i].maintenanceDate == maintenanceDate && records[i].performedBy == performedBy && !records[i].completed) {
records[i].completed = true;
emit MaintenanceCompleted(assetId, maintenanceDate, performedBy);
break;
}
}
}
function allocateResource(mapping(uint256 => ResourceAllocation[]) storage resourceAllocations, uint256 assetId, uint256 resourceId, uint256 quantity) public {
resourceAllocations[assetId].push(ResourceAllocation({
assetId: assetId,
resourceId: resourceId,
quantity: quantity,
allocatedAt: block.timestamp,
active: true
}));
emit ResourceAllocated(assetId, resourceId, quantity, block.timestamp);
}
function deallocateResource(mapping(uint256 => ResourceAllocation[]) storage resourceAllocations, uint256 assetId, uint256 resourceId, uint256 quantity) public {
ResourceAllocation[] storage allocations = resourceAllocations[assetId];
for (uint256 i = 0; i < allocations.length; i++) {
if (allocations[i].resourceId == resourceId && allocations[i].quantity == quantity && allocations[i].active) {
allocations[i].active = false;
emit ResourceDeallocated(assetId, resourceId, quantity, block.timestamp);
break;
}
}
}
function recordEvent(mapping(uint256 => EventRecord[]) storage eventRecords, uint256 assetId, string memory eventType, string memory data) public {
eventRecords[assetId].push(EventRecord({
assetId: assetId,
timestamp: block.timestamp,
eventType: eventType,
data: data
}));
emit EventRecorded(assetId, block.timestamp, eventType, data);
}
function authorizeUser(mapping(address => UserAuthorization) storage userAuthorizations, address user) public {
require(!userAuthorizations[user].authorized, "User already authorized");
userAuthorizations[user] = UserAuthorization({
user: user,
authorized: true
});
emit UserAuthorized(user);
}
function revokeUser(mapping(address => UserAuthorization) storage userAuthorizations, address user) public {
require(userAuthorizations[user].authorized, "User not authorized");
userAuthorizations[user].authorized = false;
emit UserRevoked(user);
}
function isUserAuthorized(mapping(address => UserAuthorization) storage userAuthorizations, address user) public view returns (bool) {
return userAuthorizations[user].authorized;
}
}
Explanation:
Example of Usage:
Explanation:
Last updated