Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ArslanKathia/58624e7da8e6d28177985295a668a8bb to your computer and use it in GitHub Desktop.
Save ArslanKathia/58624e7da8e6d28177985295a668a8bb to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.18+commit.87f61d96.js&optimize=false&runs=200&gist=
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract CropsSupplyChain{
//Define variable for tracking crop batch
struct CropBatch{
string origin;
string cropType;
uint256 quantity;
uint256 dateProduced;
address producer;
address[] handlers;
address distributor;
bool isCertified;
}
//Define variables for tracking certifications
struct Certification{
address certifier;
string standard;
string result;
uint256 dateCertified;
}
//batch ids to crops batach information
mapping(uint256 => CropBatch) public cropsBatches;
//mapping for batch ids to array of certification
mapping(uint256 => Certification[]) public certifications;
//counter for genreating batch ids
uint256 public batchCounter = 0;
//event for when new crop batch is created
event CropBatchCreated(uint256 batchId,
string origin,
string cropType,
uint256 quantity,
uint256 dateProduced,
address producer
);
//event when crop batch is certified
event CropBatchCertified(uint256 batchId,
address certifier,
string standard,
string result,
uint256 dateCertified);
//function to created crop batch
function createCropBatch(string memory _origin,
string memory _cropType,
uint256 _quantity,
uint256 _dateProduced
)
public {
batchCounter++;
CropBatch storage newBatch = cropsBatches[batchCounter];
newBatch.origin = _origin;
newBatch.cropType = _cropType;
newBatch.quantity = _quantity;
newBatch.dateProduced = _dateProduced;
newBatch.producer = msg.sender;
emit CropBatchCreated(batchCounter,_origin,_cropType,_quantity,_dateProduced,msg.sender);
}
//function for adding a handler to crop batch
function addHandler(uint256 _batchId,address _hanlder) public{
CropBatch storage batch = cropsBatches[_batchId];
require(batch.producer == msg.sender || batch.distributor==msg.sender || batch.handlers[batch.handlers.length-1]== msg.sender,"Only Producer,distributor and current handler can add a handler");
batch.handlers.push(_hanlder);
}
//function to certifyin a crop batch
function certifyCropBatch(uint256 _batchId,string memory _standard,string memory _result )
public{
CropBatch storage batch = cropsBatches[_batchId];
require(batch.isCertified == false,"Batch is already certified");
require(batch.distributor == msg.sender,"Only distributor can certify a batch ");
certifications[_batchId].push(Certification(msg.sender,_standard,_result,block.timestamp));
batch.isCertified = true;
emit CropBatchCertified(_batchId,msg.sender,_standard,_result,block.timestamp);
}
//function for getting the certifications for a crop
function getCropBatchCertifications(uint256 _batchId) public view returns(Certification[] memory){
return certifications[_batchId];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment