Tenant Reputation Smart Contract

What it does:
Builds an on-chain reputation profile for tenants based on rental behavior such as payment punctuality, lease completion, and landlord feedback.

Why it matters:
Replaces informal references and opaque credit checks with a transparent, portable reputation system that helps good tenants access better housing and protects landlords from repeat bad actors.

How it works:

  • Each tenant has a reputation profile linked to their wallet address

  • Landlords submit reputation updates after a lease ends

  • Scores increase for on-time payments and completed leases

  • Scores decrease for late payments or early termination

  • Only authorized landlords can submit reviews

  • Reputation data is immutable and publicly verifiable

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

/**
 * @title TenantReputation
 * @author Nam
 * @notice On-chain reputation system for rental tenants
 */
contract TenantReputation {

    // -------------------- ROLES --------------------

    address public admin;

    // -------------------- REPUTATION STRUCT --------------------

    struct Reputation {
        uint256 score;
        uint256 totalLeases;
        uint256 positiveReviews;
        uint256 negativeReviews;
        bool exists;
    }

    // -------------------- STORAGE --------------------

    mapping(address => Reputation) public reputations;
    mapping(address => bool) public approvedLandlords;

    // -------------------- EVENTS --------------------

    event LandlordApproved(address indexed landlord);
    event ReputationUpdated(
        address indexed tenant,
        int256 scoreChange,
        uint256 newScore
    );

    // -------------------- MODIFIERS --------------------

    modifier onlyAdmin() {
        require(msg.sender == admin, "Not admin");
        _;
    }

    modifier onlyLandlord() {
        require(approvedLandlords[msg.sender], "Not approved landlord");
        _;
    }

    // -------------------- CONSTRUCTOR --------------------

    constructor() {
        admin = msg.sender;
    }

    // -------------------- LANDLORD MANAGEMENT --------------------

    /**
     * @notice Approve a landlord to submit reputation data
     */
    function approveLandlord(address _landlord) external onlyAdmin {
        require(_landlord != address(0), "Invalid address");
        approvedLandlords[_landlord] = true;
        emit LandlordApproved(_landlord);
    }

    // -------------------- REPUTATION LOGIC --------------------

    /**
     * @notice Update tenant reputation after lease completion
     * @param _tenant Tenant wallet address
     * @param _positive True for good behavior, false for bad behavior
     */
    function updateReputation(
        address _tenant,
        bool _positive
    ) external onlyLandlord {
        require(_tenant != address(0), "Invalid tenant");

        Reputation storage rep = reputations[_tenant];

        if (!rep.exists) {
            rep.exists = true;
        }

        rep.totalLeases += 1;

        if (_positive) {
            rep.score += 10;
            rep.positiveReviews += 1;
            emit ReputationUpdated(_tenant, 10, rep.score);
        } else {
            if (rep.score >= 5) {
                rep.score -= 5;
            }
            rep.negativeReviews += 1;
            emit ReputationUpdated(_tenant, -5, rep.score);
        }
    }

    // -------------------- VIEW FUNCTIONS --------------------

    /**
     * @notice Get tenant reputation details
     */
    function getReputation(address _tenant)
        external
        view
        returns (
            uint256 score,
            uint256 totalLeases,
            uint256 positiveReviews,
            uint256 negativeReviews
        )
    {
        Reputation memory rep = reputations[_tenant];
        return (
            rep.score,
            rep.totalLeases,
            rep.positiveReviews,
            rep.negativeReviews
        );
    }
}