DefiFundr-Labs/defifundr_backend
Decentralized Payroll applicalication - Backend
DefiFundr - A Decentralized Payroll Platform
๐ Table of Contents
- DefiFundr - A Decentralized Payroll Platform
๐ What is DefiFundr?
DefiFundr is a revolutionary decentralized payroll and invoice management system that bridges the gap between traditional financial systems and blockchain technology. The platform provides a seamless, secure, and transparent solution for businesses to manage employee payments, handle freelancer invoices, and automate salary disbursements across both fiat and cryptocurrency channels.
๐ Features
- Automated Payroll Management: Schedule and automate regular salary payments
- Multi-currency Support: Pay in both fiat and cryptocurrency
- Invoice Processing: Create, manage, and process invoices efficiently
- Secure Authentication: PASETO token-based authentication with robust password hashing
- User Management: Comprehensive user account management with KYC verification
- Transaction History: Detailed tracking of all financial transactions
- Smart Contract Integration: Direct interaction with Ethereum-based smart contracts
- API-First Design: RESTful API architecture for seamless integration
๐๏ธ Architecture
DefiFundr implements a Hexagonal Architecture (also known as Ports and Adapters) to achieve:
- Separation of business logic from external concerns
- Improved testability through clear boundaries
- Greater flexibility in replacing components
- Enhanced maintainability with well-defined interfaces
Key Components:
- Core Domain (internal/core): Business rules and entities
- Ports (internal/core/ports): Interface definitions
- Adapters (internal/adapters): Implementation of interfaces
- Infrastructure (infrastructure/): Cross-cutting concerns
๐ Project Structure
defifundr_backend/
โโโ cmd/ # Application entry points
โ โโโ api/ # API server
โ โ โโโ docs/ # Swagger documentation
โ โ โโโ main.go # API server entry point
โ โโโ seed/ # Database seeding
โโโ config/ # Configuration management
โโโ db/ # Database related code
โ โโโ migrations/ # SQL migrations
โ โโโ query/ # SQL queries
โ โโโ sqlc/ # Generated Go code
โโโ docs/ # Project documentation
โโโ infrastructure/ # Cross-cutting concerns
โ โโโ common/ # Shared utilities
โ โโโ hash/ # Password hashing
โ โโโ middleware/ # HTTP middleware
โโโ internal/ # Application core code
โ โโโ adapters/ # Ports implementation
โ โโโ core/ # Business logic and domain
โ โโโ domain/ # Domain models
โ โโโ ports/ # Interface definitions
โ โโโ services/ # Business logic
โโโ pkg/ # Reusable packages
โโโ scripts/ # Utility scripts
โโโ test/ # Test suites
๐ ๏ธ Technologies
- Go: Main programming language
- PostgreSQL: Primary database
- Docker: Containerization
- SQLC: Type-safe SQL query generation
- Goose: Database migration management
- Swagger: API documentation
- PASETO: Modern security token framework
- Solidity: Smart contract development
๐ Getting Started
Prerequisites
- Go 1.21+
- Docker and Docker Compose
- PostgreSQL 14+
- Make
- Git
Installation
-
Clone the repository:
git clone https://github.com/DefiFundr-Labs/defifundr_backend.git cd defifundr_backend -
Install required tools:
make install-tools
-
Set up the development environment:
make docker-up
-
Set up the database:
make postgres make createdb make migrate-up
-
Generate SQL code:
make sqlc
-
Run the server:
make server
Environment Setup
Create a .env file in the project root:
DB_SOURCE=postgres://root:secret@localhost:5433/defi?sslmode=disable
SERVER_ADDRESS=0.0.0.0:8080
TOKEN_SYMMETRIC_KEY=your-secret-key-at-least-32-bytes-long
ACCESS_TOKEN_DURATION=15m
REFRESH_TOKEN_DURATION=24h
๐ป Development
Running the Application
You can run the application in several ways:
# Standard mode
make server
# Hot reload mode (recommended for development)
make air
# Using Docker
make docker-upAvailable Commands
Run make help to see a list of all available commands. Key commands include:
# Database commands
make postgres # Start PostgreSQL
make createdb # Create the database
make dropdb # Drop the database
# Migration commands
make migrate-up # Apply migrations
make migrate-down # Revert migrations
make migrate-create # Create a new migration
# Development commands
make sqlc # Generate SQL code
make mock # Generate mock code
make test # Run tests
make lint # Run linter
make swagger # Generate Swagger documentation๐ Run Migration with Shell Commands
# Create a new migration
cd scripts
sh create_migration.sh# Apply all pending migrations
cd scripts
sh migrate_up.sh# Revert the last migration
cd scripts
sh migrate_down.sh# Reset Migrations
cd scripts
sh migrate_reset.sh# Migration Status
cd scripts
sh migrate_status.sh# Run migrations up to the latest version
cd scripts
sh migrate.sh๐ API Documentation
DefiFundr provides comprehensive API documentation using Swagger.
-
Generate the Swagger documentation:
make swagger
-
Access the Swagger UI:
http://localhost:8080/swagger/index.html
The API follows RESTful principles with these main endpoints:
- Authentication:
/v1/auth/*(register, login, refresh, logout) - Users:
/v1/users/*(user management) - Transactions:
/v1/transactions/*(payment operations) - KYC:
/v1/kyc/*(verification processes)
For detailed API specifications, see API_DOCUMENTATION.md.
๐๏ธ Database Management
DefiFundr uses Goose for database migrations and SQLC for type-safe SQL queries.
Creating a New Migration
make migrate-create
# When prompted, enter a descriptive nameRunning Migrations
# Apply all pending migrations
make migrate-up
# Revert the last migration
make migrate-down
# Check migration status
make migrate-statusGenerate SQL Code
After adding or modifying queries in the db/query/ directory:
make sqlcFor more details, see DATABASE.md.
Test Structure
- Unit Tests: Located alongside the code being tested
- Integration Tests: In the
test/integration/directory - End-to-End Tests: In the
test/e2e/directory
For detailed testing information, see TESTING.md.
๐ฅ Contributing
We welcome contributions to DefiFundr! Please review our CONTRIBUTING.md for guidelines on how to contribute.
Development Workflow
- Create a feature branch from
main - Implement your changes with appropriate tests
- Ensure all tests pass with
make test - Create a pull request following our PR guidelines
Contributors
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
DefiFundr - Revolutionizing Payroll with Blockchain Technology