This repository contains a set of smart contracts developed in Solidity for a blockchain-based fruit tree management system. The system leverages the Ethereum blockchain to manage the lifecycle of fruit trees, including planting, treatment, production, and distribution, while ensuring transparency and traceability.
The fruit tree management system includes several key components:
- NFTFruit.sol: Manages the creation and ownership of NFTs representing individual fruit trees.
- MainTree.sol: Handles the planting, treatment, and production of fruit trees, integrating with the NFT contract.
- Variedad.sol: Defines the different varieties of fruit trees and their thresholds.
- Distributor.sol: Manages the distribution of fruit tree products.
- ProductionTokenERC20.sol: ERC20 token contract for managing production-related tokens.
- DateTime.sol: Utility library for date and time calculations.
This contract manages NFTs representing individual fruit trees. Each tree is minted as an NFT when planted, ensuring unique ownership and traceability.
This contract manages the core lifecycle of fruit trees, including planting, treatment, and production. Key features include:
- Planting Trees: Plant a new tree by providing location details and variety. This action mints an NFT for the tree.
- Adding Treatments: Record treatments applied to trees.
- Recording Production: Track production details for each tree.
This contract defines the different varieties of fruit trees and manages thresholds and updates for each variety.
This contract manages the distribution of fruit tree products. Distributors can acquire production, list it for sale, and handle transactions with buyers.
This contract is an ERC20 token contract for managing production-related tokens. Distributors can mint and transfer tokens representing tree production.
This utility library provides functions for date and time calculations, which are used across the system to handle timestamps and date-based logic.
To deploy the system, follow these steps:
- Deploy the
NFTFruit.sol
contract. - Deploy the
MainTree.sol
contract, passing the address of the deployedNFTFruit
contract. - Deploy the
Variedad.sol
contract. - Deploy the
ProductionTokenERC20.sol
contract. - Deploy the
Distributor.sol
contract, passing the addresses of theMainTree
andProductionTokenERC20
contracts.
To ensure proper functioning, roles must be assigned using the grantRole
function. Here’s how to do it:
-
Admin Setup: The admin has the default admin role and can grant other roles.
mainTreeContract.grantRole(DEFAULT_ADMIN_ROLE, adminAddress);
-
Farmer Role: Required for planting trees and adding treatments.
mainTreeContract.grantRole(FARMER_ROLE, farmerAddress);
-
Transporter Role: Required for transporting production.
mainTreeContract.grantRole(TRANSPORTER_ROLE, transporterAddress);
-
Storage Role: Required for storing production.
mainTreeContract.grantRole(STORAGE_ROLE, storageAddress);
-
Production Manager Role: Required for managing production quantities.
mainTreeContract.grantRole(PRODUCTION_MANAGER_ROLE, productionManagerAddress);
-
NFTFruit Contract Trust: Ensure the NFTFruit contract trusts the MainTree contract for minting NFTs.
nftFruitContract.grantRole(MINTER_ROLE, mainTreeContractAddress);
After deploying the contracts and assigning roles, you can interact with them using a web3 interface (such as Remix or a custom dApp). Key interactions include:
-
Planting a Tree: Call
plantTree
on theMainTree
contract, providing the necessary details and paying the required fee.mainTreeContract.plantTree(latitude, longitude, pol, parcela, plot, municipality, class, variety, { value: plantingFee });
Ensure the caller has the
FARMER_ROLE
. -
Adding a Treatment: Call
addTreatment
on theMainTree
contract, providing treatment details.mainTreeContract.addTreatment(treeId, treatment);
Ensure the caller has the
FARMER_ROLE
and owns the tree. -
Recording Production: Call
addProduction
on theMainTree
contract, providing production details.mainTreeContract.addProduction(treeId, quantity);
Ensure the caller has the
FARMER_ROLE
and owns the tree. -
Distributing Products: Call
acquireProduction
andlistProductionForSale
on theDistributor
contract to manage distribution.distributorContract.acquireProduction(treeId, productionId, amount); distributorContract.listProductionForSale(inventoryIndex, pricePerUnit);
Contributions are welcome! Please fork the repository and submit pull requests with your improvements.
This project is licensed under the MIT License. See the LICENSE file for details.
This `README.md` provides a comprehensive guide for deploying and using the contracts, including the necessary steps to assign roles and ensure the contracts function correctly.