A decentralized application that combines blockchain technology with AI to provide farmers with instant crop disease diagnosis and treatment recommendations.
- Web3 Integration: MetaMask wallet connection and smart contract payments
- AI-Powered Analysis: Google Gemini AI for accurate crop disease detection
- Gasless Transactions: Nitrolite SDK for improved user experience
- Secure Payments: Smart contract-based payment system (0.001 ETH per analysis)
- Database Storage: PostgreSQL for analysis history
- Mobile-First Design: Responsive interface optimized for farmers
- Frontend: React + TypeScript + Tailwind CSS + Vite
- Backend: Node.js + Express + PostgreSQL
- Blockchain: Ethereum + Solidity + Truffle
- AI: Google Gemini API
- Web3: Web3.js + MetaMask integration
Before running this application locally, ensure you have the following installed:
- Node.js (v18.0.0 or higher)
- npm (v8.0.0 or higher)
- PostgreSQL (v13.0 or higher)
- Git (for cloning the repository)
- MetaMask Extension installed in your browser
- Modern browser with Web3 support (Chrome, Firefox, Edge)
- Google AI Studio Account (for Gemini API key)
- Infura Account (for Ethereum network access)
- Ethereum Testnet Setup (Sepolia recommended)
git clone <repository-url>
cd crop-advisor-web3npm installcd server
npm install
cd ..cp .env.example .envUpdate .env with your values:
# Ethereum Configuration
VITE_CONTRACT_ADDRESS=0x... # Your deployed contract address
VITE_NETWORK_ID=11155111 # Sepolia testnet
# API Configuration
VITE_API_URL=http://localhost:3001cp server/.env.example server/.envUpdate server/.env with your values:
# Database Configuration
DB_USER=postgres
DB_HOST=localhost
DB_NAME=crop_advisor
DB_PASSWORD=your_password
DB_PORT=5432
# Gemini AI Configuration
GEMINI_API_KEY=your_gemini_api_key_here
# Server Configuration
PORT=3001
NODE_ENV=development
# Ethereum Configuration (for backend contract interaction)
CONTRACT_ADDRESS=0x...
PRIVATE_KEY=your_private_key_here
INFURA_PROJECT_ID=your_infura_project_id# Connect to PostgreSQL
psql -U postgres
# Create database
CREATE DATABASE crop_advisor;
# Create user (optional)
CREATE USER crop_user WITH PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE crop_advisor TO crop_user;
# Exit PostgreSQL
\qThe application will automatically create the required tables on first run.
npm install -g trufflenpm run compile# Start Ganache CLI or Ganache GUI first
npm run migrate# Make sure you have MNEMONIC and INFURA_PROJECT_ID in your environment
npm run migrate -- --network sepoliaAfter deployment, update the contract address in:
src/contracts/contractConfig.ts.envfileserver/.envfile
cd server
npm startThe backend will run on http://localhost:3001
# In a new terminal, from the root directory
npm run devThe frontend will run on http://localhost:5173
{
"dependencies": {
"react": "^18.3.1",
"react-dom": "^18.3.1",
"web3": "^4.3.0",
"@metamask/detect-provider": "^2.0.0",
"axios": "^1.6.2",
"lucide-react": "^0.344.0"
},
"devDependencies": {
"@vitejs/plugin-react": "^4.3.1",
"typescript": "^5.5.3",
"tailwindcss": "^3.4.1",
"vite": "^5.4.2",
"truffle": "^5.11.5",
"@truffle/hdwallet-provider": "^2.1.15"
}
}{
"dependencies": {
"express": "^4.18.2",
"cors": "^2.8.5",
"multer": "^1.4.5-lts.1",
"@google/generative-ai": "^0.2.1",
"pg": "^8.11.3",
"dotenv": "^16.3.1"
},
"devDependencies": {
"nodemon": "^3.0.2"
}
}- Visit Google AI Studio
- Create a new API key
- Add it to
server/.envasGEMINI_API_KEY
- Create account at Infura
- Create a new project
- Copy the Project ID
- Add it to your environment variables
- Install MetaMask browser extension
- Create or import a wallet
- Add Sepolia testnet to MetaMask:
- Network Name: Sepolia
- RPC URL:
https://sepolia.infura.io/v3/YOUR_PROJECT_ID - Chain ID: 11155111
- Currency Symbol: ETH
- Block Explorer: https://sepolia.etherscan.io
npm run test# Health check
curl http://localhost:3001/api/health
# Test image analysis (requires running server)
curl -X POST -F "image=@test-image.jpg" -F "analysisId=test123" http://localhost:3001/api/analyze- Connect Wallet: Click "Connect MetaMask" and approve the connection
- Upload Image: Drag & drop or select a crop image (JPG, PNG, WEBP)
- Pay for Analysis: Click "Pay & Get AI Analysis" (0.001 ETH)
- Get Results: AI analyzes the image and provides diagnosis and treatment advice
- View History: Access previous analyses through your connected wallet
# Frontend
npm run dev # Start development server
npm run build # Build for production
npm run preview # Preview production build
# Backend
npm run server # Start backend server
cd server && npm run dev # Start with nodemon
# Smart Contracts
npm run compile # Compile contracts
npm run migrate # Deploy contracts
npm run test # Run contract tests- Local Development: Ganache (localhost:8545)
- Testnet: Sepolia (Chain ID: 11155111)
- Mainnet: Ethereum (Chain ID: 1) - for production
- Analysis request: ~300,000 gas
- Contract deployment: ~2,000,000 gas
- Recommended gas price: 20 Gwei (testnet)
- Smart contract ownership controls
- Input validation and sanitization
- Secure file handling with size limits (10MB)
- Error handling and recovery mechanisms
- Gas optimization to prevent attacks
- Image hash verification
# Clear browser cache and cookies
# Disable other wallet extensions
# Ensure MetaMask is unlocked# Check PostgreSQL is running
sudo service postgresql status
# Verify database exists
psql -U postgres -l# Check network configuration in truffle-config.js
# Ensure sufficient ETH balance for gas
# Verify Infura project ID is correct# Verify Gemini API key is valid
# Check image file size (max 10MB)
# Ensure supported image format (JPG, PNG, WEBP)-
GEMINI_API_KEY- Valid Google AI Studio API key -
DB_PASSWORD- PostgreSQL database password -
VITE_CONTRACT_ADDRESS- Deployed contract address -
INFURA_PROJECT_ID- Infura project ID -
PRIVATE_KEY- Ethereum private key (for deployment)
- Image Processing: Images are compressed to 800px max width
- Database: Indexed queries for farmer addresses
- Caching: API responses cached for 5 minutes
- File Cleanup: Uploaded images automatically deleted after processing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
For support and questions:
- Create an issue in the GitHub repository
- Check the troubleshooting section above
- Verify all environment variables are correctly set
- v1.0.0 - Initial MVP release with Web3 integration and AI analysis
- v0.9.0 - Beta release with smart contract deployment
- v0.8.0 - Alpha release with basic functionality