Skip to content

Instantly share code, notes, and snippets.

@Violet-Bora-Lee
Created November 22, 2025 01:27
Show Gist options
  • Select an option

  • Save Violet-Bora-Lee/93bf397bad8504243f67a46e1d81510a to your computer and use it in GitHub Desktop.

Select an option

Save Violet-Bora-Lee/93bf397bad8504243f67a46e1d81510a to your computer and use it in GitHub Desktop.
스마트팜 이력 추적 코드
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.2 <0.9.0;
contract GinsengProductionHistory {
struct Ginseng {
string name;
uint256 plantingDate;
}
struct GrowthRecord {
uint256 temperature;
uint256 humidity;
uint256 timestamp;
}
mapping(uint256 => Ginseng) private ginsengs;
mapping(uint256 => GrowthRecord[]) private growthRecords;
mapping(uint256 => bool) private exists;
/// @notice Register new ginseng with given id and name, plantingDate is set to current timestamp
/// @param id The unique ID of the ginseng
/// @param name The name of the ginseng
function plantGinseng(uint256 id, string calldata name) external {
require(!exists[id], "Ginseng ID already registered");
ginsengs[id] = Ginseng(name, block.timestamp);
exists[id] = true;
}
/// @notice Record new growth info (temperature, humidity) for ginseng with given id
/// @param id The unique ID of the ginseng
/// @param temperature Temperature value to record
/// @param humidity Humidity value to record
function recordGrowth(uint256 id, uint256 temperature, uint256 humidity) external {
require(exists[id], "Ginseng ID not registered");
growthRecords[id].push(GrowthRecord(temperature, humidity, block.timestamp));
}
/// @notice Retrieve ginseng info and all recorded growth data
/// @param id The unique ID of the ginseng
/// @return name The name of the ginseng
/// @return plantingDate The planting date timestamp
/// @return temperatures Array of temperature recordings
/// @return humidities Array of humidity recordings
/// @return timestamps Array of timestamps of recordings
function getTraceability(uint256 id) external view returns (
string memory name,
uint256 plantingDate,
uint256[] memory temperatures,
uint256[] memory humidities,
uint256[] memory timestamps
) {
require(exists[id], "Ginseng ID not registered");
Ginseng storage ginseng = ginsengs[id];
GrowthRecord[] storage records = growthRecords[id];
uint256 len = records.length;
uint256[] memory temps = new uint256[](len);
uint256[] memory humids = new uint256[](len);
uint256[] memory times = new uint256[](len);
for (uint256 i = 0; i < len; i++) {
temps[i] = records[i].temperature;
humids[i] = records[i].humidity;
times[i] = records[i].timestamp;
}
return (ginseng.name, ginseng.plantingDate, temps, humids, times);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment