Supplier Reputation System

What it does:
Tracks supplier performance, ratings, and reputation on-chain, allowing buyers and partners to assess reliability transparently.

Why it matters:
Encourages good business practices, reduces fraud, improves trust in supply chains, and provides verifiable supplier history for all stakeholders.

How it works:

  • Buyers submit ratings or feedback after transactions

  • Smart contract calculates and updates supplier reputation scores automatically

  • Weighted scoring can account for transaction size, frequency, or verified milestones

  • Reputation is immutable and auditable, preventing tampering or fake reviews

  • Integrates with Cross-Border Trade Settlement, Supply Chain Traceability, and marketplaces

  • Public dashboards show top-rated suppliers, historical ratings, and trends

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

import "@openzeppelin/contracts/access/Ownable.sol";

/**
 * @title SupplierReputationSystem
 * @author Nam
 * @notice Tracks supplier ratings and calculates reputation scores on-chain
 */
contract SupplierReputationSystem is Ownable {

    struct Supplier {
        string name;
        uint256 totalScore;
        uint256 ratingCount;
        bool registered;
    }

    mapping(address => Supplier) public suppliers;
    mapping(address => bool) public authorizedReviewers;

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

    event SupplierRegistered(address supplier, string name);
    event ReviewerApproved(address reviewer);
    event ReviewSubmitted(address supplier, address reviewer, uint8 rating);
    event ReputationUpdated(address supplier, uint256 newScore);

    // -------------------- REVIEWER MANAGEMENT --------------------

    function approveReviewer(address _reviewer) external onlyOwner {
        authorizedReviewers[_reviewer] = true;
        emit ReviewerApproved(_reviewer);
    }

    function revokeReviewer(address _reviewer) external onlyOwner {
        authorizedReviewers[_reviewer] = false;
    }

    modifier onlyReviewer() {
        require(authorizedReviewers[msg.sender], "Not an authorized reviewer");
        _;
    }

    // -------------------- SUPPLIER MANAGEMENT --------------------

    function registerSupplier(address _supplier, string calldata _name) external onlyOwner {
        require(!suppliers[_supplier].registered, "Already registered");

        suppliers[_supplier] = Supplier({
            name: _name,
            totalScore: 0,
            ratingCount: 0,
            registered: true
        });

        emit SupplierRegistered(_supplier, _name);
    }

    // -------------------- RATING & REPUTATION --------------------

    function submitRating(address _supplier, uint8 _rating) external onlyReviewer {
        require(_rating >= 1 && _rating <= 5, "Rating must be 1-5");
        Supplier storage s = suppliers[_supplier];
        require(s.registered, "Supplier not registered");

        s.totalScore += _rating;
        s.ratingCount += 1;

        emit ReviewSubmitted(_supplier, msg.sender, _rating);
        emit ReputationUpdated(_supplier, getReputationScore(_supplier));
    }

    function getReputationScore(address _supplier) public view returns (uint256) {
        Supplier storage s = suppliers[_supplier];
        if (s.ratingCount == 0) return 0;
        return s.totalScore / s.ratingCount;
    }
}