🧞♂️ReviewManager.sol
ReviewManager Contract
The ReviewManager contract's primary purpose is to manage the review system, integrate Chainlink oracles for external data fetching, and facilitate interactions with the AdvertisingToken contract to incentivize companies.
Key Functions and Interactions:
Review Storage: Stores reviews on-chain, allowing public access and querying.
Chainlink Integration: Uses Chainlink oracles to fetch external data, such as additional company information.
Incentives with AdvertisingToken: Issues
AdvertisingTokento companies as incentives for addressing reviews, manages booking of advertisement slots, and handles token transfers and redemptions.
Detailed Contract Code and Explanation
ReviewManager.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./AdvertisingToken.sol";
import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract ReviewManager is ChainlinkClient, Ownable {
using Chainlink for Chainlink.Request;
struct Review {
address reviewer;
string content;
uint256 timestamp;
}
mapping(string => Review[]) public reviews;
AdvertisingToken public token;
address private oracle;
bytes32 private jobId;
uint256 private fee;
event ReviewAdded(string indexed company, address indexed reviewer, string content, uint256 timestamp);
constructor(address tokenAddress, address _oracle, string memory _jobId, uint256 _fee, address _link) {
setChainlinkToken(_link);
token = AdvertisingToken(tokenAddress);
oracle = _oracle;
jobId = stringToBytes32(_jobId);
fee = _fee;
}
function addReview(string memory company, string memory content) public {
Review memory review = Review({
reviewer: msg.sender,
content: content,
timestamp: block.timestamp
});
reviews[company].push(review);
emit ReviewAdded(company, msg.sender, content, block.timestamp);
// Mint tokens as incentive
token.mint(msg.sender, 100 * 10**18); // Mint 100 tokens
// Call Chainlink to fetch additional data
requestCompanyInfo(company);
}
function getReviews(string memory company) public view returns (Review[] memory) {
return reviews[company];
}
function requestCompanyInfo(string memory company) internal returns (bytes32 requestId) {
Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfillCompanyInfo.selector);
request.add("company", company);
return sendChainlinkRequestTo(oracle, request, fee);
}
function fulfillCompanyInfo(bytes32 _requestId, bytes memory _info) public recordChainlinkFulfillment(_requestId) {
// Handle the external data (e.g., update company info or process further)
}
function stringToBytes32(string memory source) internal pure returns (bytes32 result) {
bytes memory tempEmptyStringTest = bytes(source);
if (tempEmptyStringTest.length == 0) {
return 0x0;
}
assembly {
result := mload(add(source, 32))
}
}
}Detailed Explanation
Imports
import "./AdvertisingToken.sol";
import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol";
import "@openzeppelin/contracts/access/Ownable.sol";AdvertisingToken.sol: Imports the advertising token contract.
ChainlinkClient.sol: Provides Chainlink client functionality.
Ownable.sol: Provides ownership functionality.
Contract Declaration
contract ReviewManager is ChainlinkClient, Ownable {
using Chainlink for Chainlink.Request;ReviewManager Contract: Inherits from
ChainlinkClientandOwnable.
Structs and Mappings
struct Review {
address reviewer;
string content;
uint256 timestamp;
}
mapping(string => Review[]) public reviews;Review: Struct to store review details.
reviews: Mapping from company name to an array of reviews.
State Variables
AdvertisingToken public token;
address private oracle;
bytes32 private jobId;
uint256 private fee;
event ReviewAdded(string indexed company, address indexed reviewer, string content, uint256 timestamp);token: Reference to the
AdvertisingTokencontract.oracle, jobId, fee: Variables for Chainlink integration.
ReviewAdded: Event emitted when a review is added.
Constructor
constructor(address tokenAddress, address _oracle, string memory _jobId, uint256 _fee, address _link) {
setChainlinkToken(_link);
token = AdvertisingToken(tokenAddress);
oracle = _oracle;
jobId = stringToBytes32(_jobId);
fee = _fee;
}constructor: Initializes the contract with the token address, oracle address, job ID, fee, and LINK token address.
Add Review Function
function addReview(string memory company, string memory content) public {
Review memory review = Review({
reviewer: msg.sender,
content: content,
timestamp: block.timestamp
});
reviews[company].push(review);
emit ReviewAdded(company, msg.sender, content, block.timestamp);
// Mint tokens as incentive
token.mint(msg.sender, 100 * 10**18); // Mint 100 tokens
// Call Chainlink to fetch additional data
requestCompanyInfo(company);
}addReview: Adds a review, emits an event, mints incentive tokens, and requests additional data via Chainlink.
Get Reviews Function
function getReviews(string memory company) public view returns (Review[] memory) {
return reviews[company];
}getReviews: Returns the reviews for a specified company.
Request Company Info Function
function requestCompanyInfo(string memory company) internal returns (bytes32 requestId) {
Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfillCompanyInfo.selector);
request.add("company", company);
return sendChainlinkRequestTo(oracle, request, fee);
}requestCompanyInfo: Builds and sends a Chainlink request to fetch additional company info.
Fulfill Function
function fulfillCompanyInfo(bytes32 _requestId, bytes memory _info) public recordChainlinkFulfillment(_requestId) {
// Handle the external data (e.g., update company info or process further)
}fulfillCompanyInfo: Callback function to handle the response from the Chainlink oracle.
Helper Function
function stringToBytes32(string memory source) internal pure returns (bytes32 result) {
bytes memory tempEmptyStringTest = bytes(source);
if (tempEmptyStringTest.length == 0) {
return 0x0;
}
assembly {
result := mload(add(source, 32))
}
}
}stringToBytes32: Helper function to convert a string to bytes32 format.
Interaction with AdvertisingToken
Incentives: When a review is added, the
ReviewManagercontract mintsAdvertisingTokento the reviewer.Data Fetching: Uses Chainlink oracles to fetch additional company information, enhancing the review system.
Last updated