Skip to content

zeekman/FarmersMarketplace

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

471 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌿 Farmers Marketplace

CI

A minimal MVP marketplace where farmers list products and buyers pay using the Stellar Network (XLM).

Stack

  • Frontend: React + Vite
  • Backend: Node.js + Express
  • Database: SQLite (local dev, default) / PostgreSQL (production)
  • Payments: Stellar Testnet (XLM)

Project Structure

FarmersMarketplace/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ index.js          # Express app entry
β”‚   β”‚   β”œβ”€β”€ stellar.js        # Stellar SDK helpers
β”‚   β”‚   β”œβ”€β”€ middleware/auth.js
β”‚   β”‚   β”œβ”€β”€ db/schema.js      # SQLite schema + connection
β”‚   β”‚   └── routes/
β”‚   β”‚       β”œβ”€β”€ auth.js       # register, login
β”‚   β”‚       β”œβ”€β”€ products.js   # CRUD listings
β”‚   β”‚       β”œβ”€β”€ orders.js     # place order + pay
β”‚   β”‚       └── wallet.js     # balance, transactions, fund
β”‚   └── package.json
└── frontend/
    β”œβ”€β”€ src/
    β”‚   β”œβ”€β”€ App.jsx
    β”‚   β”œβ”€β”€ api/client.js     # API wrapper
    β”‚   β”œβ”€β”€ context/AuthContext.jsx
    β”‚   β”œβ”€β”€ components/Navbar.jsx
    β”‚   └── pages/
    β”‚       β”œβ”€β”€ Auth.jsx      # Login + Register
    β”‚       β”œβ”€β”€ Dashboard.jsx # Farmer: add/view products
    β”‚       β”œβ”€β”€ Marketplace.jsx # Buyer: browse
    β”‚       β”œβ”€β”€ ProductDetail.jsx # Buy flow
    β”‚       └── Wallet.jsx    # Balance + transactions
    └── package.json

Quick Start

1. Backend

cd backend
cp .env.example .env
npm install
npm run dev

Runs on http://localhost:4000

2. Frontend

cd frontend
npm install
npm run dev

Runs on http://localhost:3000

Payment Flow

  1. Register as a buyer and a farmer (two separate accounts)
  2. Go to Wallet β†’ click "Fund with Testnet XLM" (uses Stellar Friendbot, free testnet tokens)
  3. As a farmer, go to Dashboard and list a product priced in XLM
  4. As a buyer, browse the Marketplace, open a product, set quantity, click Buy Now
  5. The backend signs and submits a real Stellar transaction on testnet
  6. View the transaction hash in Wallet β†’ Transaction History or on stellar.expert

API Endpoints

Interactive API documentation is available at http://localhost:4000/api/docs when the backend is running.

Method Path Auth Description
POST /api/auth/register β€” Register user
POST /api/auth/login β€” Login
GET /api/products β€” Browse all products
GET /api/products/:id β€” Product detail
POST /api/products farmer Create listing
GET /api/products/mine/list farmer My listings
DELETE /api/products/:id farmer Remove listing
POST /api/orders buyer Place + pay order
GET /api/orders buyer Order history
GET /api/orders/sales farmer Incoming sales
GET /api/wallet auth Balance
GET /api/wallet/transactions auth TX history
POST /api/wallet/fund auth Fund via Friendbot (testnet)
GET /api/contracts/:contractId/state?prefix= auth View Soroban contract storage entries (JSON: key, val, durability)

Database Migrations

Schema changes are managed through versioned SQL migration files in backend/migrations/.

Running migrations

cd backend
npm run migrate           # apply all pending migrations
npm run migrate:rollback  # revert the last applied migration

Migrations run automatically on app startup β€” no manual step needed for development.

How it works

  • Migration files: backend/migrations/NNN_description.sql
  • Rollback files: backend/migrations/NNN_description.undo.sql (optional)
  • Applied migrations are tracked in a migrations table in the database
  • Running migrate twice is safe β€” already-applied migrations are skipped

Creating a new migration

# Up migration
echo "ALTER TABLE products ADD COLUMN featured INTEGER DEFAULT 0;" \
  > backend/migrations/002_add_featured.sql

# Rollback (optional)
echo "ALTER TABLE products DROP COLUMN IF EXISTS featured;" \
  > backend/migrations/002_add_featured.undo.sql

npm run migrate

Database Backup and Restore

The application includes automated database backup functionality to protect against data loss.

Manual Backup

Create a timestamped backup of the database:

cd backend
npm run backup

This creates a backup file in backend/backups/ with format market-YYYY-MM-DD.db.

Manual Restore

Restore the database from a backup file:

cd backend
npm run restore -- backups/market-2024-01-01.db

Important: Before restoring, the current database is automatically backed up to market.db.backup.

Automated Daily Backups

  • Backups run automatically every day at midnight UTC
  • Only the last 7 backups are retained (older ones are automatically deleted)
  • Backup status and errors are logged using the structured logging system

Backup Location

  • Backup files are stored in: backend/backups/
  • File naming convention: market-YYYY-MM-DD.db
  • Maximum retention: 7 days

Recovery Procedures

  1. Quick Restore: Use npm run restore with the desired backup file
  2. Emergency Recovery: Copy market.db.backup (created before restore) back to market.db
  3. Complete Reset: Delete market.db and restart the application (fresh schema)

PostgreSQL Setup

The backend supports both SQLite (local dev) and PostgreSQL (production), controlled by the DATABASE_URL environment variable.

Local development (SQLite β€” default)

No extra setup needed. SQLite is used automatically when DATABASE_URL is not set.

Production (PostgreSQL)

  1. Add DATABASE_URL to your .env:
    DATABASE_URL=postgresql://user:password@localhost:5432/farmersmarketplace
    
  2. The schema is created automatically on first start.

Docker Compose (PostgreSQL + backend + frontend)

cp backend/.env.example backend/.env
# Edit backend/.env β€” set JWT_SECRET etc.
docker compose up

This starts:

  • postgres β€” PostgreSQL 16 on port 5432
  • backend β€” Express API on port 4000 (connected to postgres)
  • frontend β€” React app on port 3000

Migrate existing SQLite data to PostgreSQL

DATABASE_URL=postgresql://user:pass@host:5432/dbname \
  node backend/scripts/migrate-sqlite-to-pg.js

Contract Testing (Soroban)

Test Soroban contracts against a local Stellar node using the built-in test harness.

Start the local node

docker-compose -f docker-compose.test.yml up -d

This starts a stellar/quickstart node on port 8000 with Soroban RPC enabled.

Run contract tests

cd backend
npm run test:contracts

Test helpers

backend/src/__tests__/helpers/soroban.js exposes:

  • fundAccount(publicKey) β€” fund via local Friendbot
  • deployContract(wasmBuffer, keypair) β€” upload WASM and create contract instance
  • invokeContract(contractId, method, args, keypair) β€” call a contract function

Environment variables (optional)

Variable Default Description
TEST_HORIZON_URL http://localhost:8000 Local Horizon endpoint
TEST_SOROBAN_RPC_URL http://localhost:8000/soroban/rpc Local Soroban RPC
TEST_NETWORK_PASSPHRASE Standalone Network ; February 2017 Local network passphrase
SKIP_CONTRACT_TESTS false Set to true to skip in CI without Docker

Notes

  • Stellar wallets are auto-created on registration
  • All payments use XLM on Stellar Testnet β€” no real money involved
  • SQLite database file (market.db) is created automatically on first run (when DATABASE_URL is not set)
  • To reset SQLite: delete backend/market.db

About

Farmers Payment & Agricultural Marketplace connects farmers with buyers to trade agricultural products easily. Built on the Stellar Network, it enables fast, secure, and low-cost payments while helping farmers access wider markets and receive reliable digital payments.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages