DEV Community

Alg0rider for Evire

Posted on

MaintenanceTracking.sol | Contracts

The MaintenanceTracking.sol smart contract is designed to handle maintenance tracking and management for physical assets on the Evire blockchain. By leveraging blockchain technology, it ensures that maintenance records are immutable, transparent, and easily accessible, which is crucial for maintaining the integrity and value of physical assets.

Real-world asset management, especially for physical assets, requires meticulous tracking of maintenance activities. Traditional systems often suffer from issues like data tampering, lack of transparency, and inefficiencies in record-keeping. The MaintenanceTracking.sol smart contract addresses these challenges by providing a decentralized and immutable ledger for maintenance records. This is crucial for sectors like real estate, vehicle fleets, and industrial machinery, where maintenance history significantly impacts asset value and operational efficiency.

Contract's Structure

The MaintenanceTracking.sol contract is structured to handle the core functionalities of maintenance tracking. It includes the following main modules and functions:

  1. State Variables: These are used to store essential data such as asset details, maintenance records, and authorized users.
  2. Modifiers: These ensure that only authorized users can execute certain functions.
  3. Events: These are used to log significant actions such as the addition of a maintenance record or the creation of a new asset.
  4. Functions: These handle the core operations like adding new assets, logging maintenance activities, and querying records.

Explanations

  1. State Variables:

    • assets: A mapping that stores information about each asset.
    • maintenanceRecords: A mapping that keeps track of maintenance activities for each asset.
    • authorizedUsers: A list of users who are authorized to update maintenance records.
  2. Modifiers:

    • onlyAuthorized: Ensures that only users in the authorizedUsers list can call certain functions.
  3. Events:

    • AssetCreated: Emitted when a new asset is registered.
    • MaintenanceLogged: Emitted when a maintenance activity is recorded.
  4. Functions:

    • addAsset: Registers a new asset on the blockchain.
    • logMaintenance: Records maintenance activity for a specific asset.
    • getMaintenanceRecords: Retrieves all maintenance records for a given asset.

Description of the Essential Functions of the Contract

  1. addAsset(string memory _assetId, string memory _details):

    • Registers a new asset with a unique ID and relevant details.
    • Emits AssetCreated event upon successful addition.
  2. logMaintenance(string memory _assetId, string memory _description, uint256 _date):

    • Records a maintenance activity for an asset identified by _assetId.
    • Emits MaintenanceLogged event after logging the maintenance activity.
  3. getMaintenanceRecords(string memory _assetId):

    • Retrieves all maintenance records for the specified asset.
    • Returns an array of maintenance records associated with the asset.

Key Points

  • The MaintenanceTracking.sol contract is pivotal for maintaining transparent and immutable maintenance records for physical assets.
  • It includes state variables, modifiers, events, and functions that collectively handle the registration of assets, logging of maintenance activities, and retrieval of maintenance records.
  • Key functions such as addAsset, logMaintenance, and getMaintenanceRecords are crucial for the operational integrity of the contract.
  • The contract ensures that only authorized users can update maintenance records, enhancing security and data integrity.

To do

  • Implement more granular access control mechanisms to handle different levels of authorization.

  • Automate maintenance logging by integrating IoT devices that can trigger maintenance records based on real-time data.

  • Develop user interfaces or dashboards to make it easier for users to interact with the contract.

  • Ensure the contract can interoperate with existing asset management systems to provide a more comprehensive solution.

  • Optimize the contract to handle a large number of assets and maintenance records efficiently.

The MaintenanceTracking.sol contract represents a significant step towards efficient and transparent maintenance tracking in the realm of real-world asset management. By leveraging blockchain's inherent properties, it ensures data integrity and security, thereby enhancing the value and operational reliability of physical assets.

For the full contract implementation, you can refer to the GitHub repository: MaintenanceTracking.sol.

MaintenanceTracking.sol CODE:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "./RWAAsset.sol";

/**
 * @title MaintenanceTracking
 * @dev Contract for tracking maintenance activities of Real World Assets (RWA)
 */
contract MaintenanceTracking is Ownable {
    using Counters for Counters.Counter;

    struct MaintenanceRecord {
        uint256 assetId;
        string description;
        uint256 timestamp;
        address performedBy;
        uint256 cost;
        bool isCompleted;
    }

    RWAAsset public rwaAsset;
    Counters.Counter private _maintenanceIds;
    mapping(uint256 => MaintenanceRecord) public maintenanceRecords;
    mapping(uint256 => uint256[]) public assetMaintenanceHistory;

    event MaintenanceScheduled(uint256 indexed maintenanceId, uint256 indexed assetId, string description);
    event MaintenanceCompleted(uint256 indexed maintenanceId, uint256 indexed assetId, address performedBy, uint256 cost);

    constructor(address _rwaAssetAddress) {
        rwaAsset = RWAAsset(_rwaAssetAddress);
    }

    /**
     * @dev Schedule a maintenance activity for an asset
     * @param _assetId The ID of the asset requiring maintenance
     * @param _description A description of the maintenance activity
     */
    function scheduleMaintenance(uint256 _assetId, string memory _description) external onlyOwner {
        require(rwaAsset.ownerOf(_assetId) != address(0), "Asset does not exist");

        _maintenanceIds.increment();
        uint256 newMaintenanceId = _maintenanceIds.current();

        maintenanceRecords[newMaintenanceId] = MaintenanceRecord({
            assetId: _assetId,
            description: _description,
            timestamp: block.timestamp,
            performedBy: address(0),
            cost: 0,
            isCompleted: false
        });

        assetMaintenanceHistory[_assetId].push(newMaintenanceId);

        emit MaintenanceScheduled(newMaintenanceId, _assetId, _description);
    }

    /**
     * @dev Mark a maintenance activity as completed
     * @param _maintenanceId The ID of the maintenance activity
     * @param _cost The cost of the maintenance activity
     */
    function completeMaintenance(uint256 _maintenanceId, uint256 _cost) external {
        MaintenanceRecord storage record = maintenanceRecords[_maintenanceId];
        require(!record.isCompleted, "Maintenance already completed");
        require(rwaAsset.ownerOf(record.assetId) == msg.sender, "Only asset owner can complete maintenance");

        record.isCompleted = true;
        record.performedBy = msg.sender;
        record.cost = _cost;
        record.timestamp = block.timestamp;

        emit MaintenanceCompleted(_maintenanceId, record.assetId, msg.sender, _cost);
    }

    /**
     * @dev Get the maintenance history for an asset
     * @param _assetId The ID of the asset
     * @return An array of maintenance record IDs for the asset
     */
    function getAssetMaintenanceHistory(uint256 _assetId) external view returns (uint256[] memory) {
        return assetMaintenanceHistory[_assetId];
    }

    /**
     * @dev Get details of a specific maintenance record
     * @param _maintenanceId The ID of the maintenance record
     * @return The maintenance record details
     */
    function getMaintenanceDetails(uint256 _maintenanceId) external view returns (MaintenanceRecord memory) {
        return maintenanceRecords[_maintenanceId];
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)