Ethereum smart contracts for end-to-end honey batch traceability.
# ApiChain Kenya — Blockchain Component
Blockchain-based organic honey traceability system for Kenya's smallholder honey value chain. This component implements Ethereum smart contracts that enable end-to-end traceability of honey batches from hive to distribution.
## Smart Contracts
| Contract | Purpose | Bytecode Size |
|----------|---------|---------------|
| `RoleManager.sol` | Role-based access control (5 roles via OpenZeppelin AccessControl) | 2,127 B (8.7% of 24KB limit) |
| `TraceabilityRegistry.sol` | 6-state deterministic batch lifecycle (CREATED → DISTRIBUTED) | 8,220 B (33.4% of 24KB limit) |
## State Machine
```
CREATED (S0) → HARVESTED (S1) → PROCESSED (S2) → LAB_VERIFIED (S3) → PACKAGED (S4) → DISTRIBUTED (S5)
```
Each transition is role-gated, strictly sequential (no skipping), and anchors a `bytes32` hash on-chain.
## Tech Stack
- **Solidity** ^0.8.20
- **Hardhat** v2 (JavaScript, CommonJS)
- **Ethers.js** v6
- **OpenZeppelin** AccessControl
- **Testing:** Mocha + Chai (118 tests)
## Setup
```bash
# Install dependencies
npm install
# Compile contracts
npx hardhat compile
# Run tests
npx hardhat test
# Run tests with gas report
REPORT_GAS=true npx hardhat test
# Run coverage
npx hardhat coverage
# Start local node + deploy
npx hardhat node
npx hardhat run scripts/deploy.js --network localhost
```
## Environment Variables
Copy `.env.example` to `.env` and fill in your values:
```bash
cp .env.example .env
```
| Variable | Purpose |
|----------|---------|
| `SEPOLIA_RPC_URL` | Sepolia testnet RPC endpoint |
| `PRIVATE_KEY` | Deployer wallet private key |
## Project Structure
```
contracts/
RoleManager.sol — RBAC with 5 roles
TraceabilityRegistry.sol — 6-state batch lifecycle
scripts/
deploy.js — Deployment script (localhost / Sepolia)
test/
RoleManager.test.js — 27 unit tests
TraceabilityRegistry.test.js — 71 unit tests
integration.test.js — 20 integration + gas tests
```