This project provides the backend implementation for a crypto-based website, allowing users to deposit funds and earn boosted liquidity for an upcoming project.
- User Deposits: Users can deposit cryptocurrencies and select a lock period.
- Asset Swap: Deposited assets are swapped to USDC via Uniswap.
- Transaction Monitoring: Backend monitors transactions and stores them in the database.
- API Endpoints: Provides RESTful API endpoints for transactions.
- Testing: Includes a comprehensive test suite using
pytest
.
- Project README
Before running the application, ensure you have the following installed:
- Python 3.9 or higher
- Docker and Docker Compose
- Git
git clone https://github.com/yourusername/yourproject.git
cd yourproject
It's recommended to use a Python virtual environment:
python3 -m venv venv
source venv/bin/activate
Install the required Python packages:
pip install -r requirements.txt
Create a .env
file in the project root directory with the following content:
FLASK_APP=api.app
FLASK_ENV=development
DATABASE_URL=postgresql://user:password@db:5432/yourdb
TEST_DATABASE_URL=sqlite:///:memory:
ETHERSCAN_API_KEY=your_etherscan_api_key
GNOSIS_SAFE_ADDRESS=your_gnosis_safe_address
INFURA_URL=https://mainnet.infura.io/v3/your_infura_project_id
AUTH_TOKEN=mysecrettoken
- Note: Replace placeholder values with actual credentials.
- Important: Do not commit
.env
to version control.
The application is containerized using Docker. Follow these steps to run it:
-
Build and Run the Containers
docker-compose up --build
This command:
- Builds the Docker images.
- Starts the following services:
- web: Flask API server.
- db: PostgreSQL database.
- transaction_monitor: Simulates transaction monitoring.
-
Check the Services
- API Endpoint: The API should be running at
http://localhost:5001
. - Database: PostgreSQL is running inside a Docker container.
- API Endpoint: The API should be running at
-
Verify Database Initialization
- The database tables are automatically created on startup using Flask-Migrate.
To run unit tests using pytest
:
-
Activate the Virtual Environment
source venv/bin/activate
-
Install Test Dependencies
Ensure all packages are installed:
pip install -r requirements.txt
-
Run Tests
pytest
- This command discovers and runs all tests in the
api/tests
directory.
- This command discovers and runs all tests in the
You can test the API endpoints using curl
.
Export the AUTH_TOKEN
(must match the one in your .env
file):
export AUTH_TOKEN=mysecrettoken
curl -X POST http://localhost:5001/api/transactions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AUTH_TOKEN" \
-d '{
"user_address": "0x1234567890abcdef1234567890abcdef12345678",
"original_asset": "ETH",
"original_amount": 1.5,
"usdc_amount": 3000,
"lock_duration_weeks": 12,
"transaction_hash": "0xabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef"
}'
- Response: Should return a
201 Created
status with the transaction details.
curl http://localhost:5001/api/transactions
- Response: Returns a list of all transactions.
curl http://localhost:5001/api/transactions/0xabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef
- Response: Returns the details of the specified transaction.
curl http://localhost:5001/api/users/0x1234567890abcdef1234567890abcdef12345678/transactions
- Response: Returns all transactions for the specified user.
yourproject/
├── api/
│ ├── __init__.py
│ ├── app.py # Main Flask application
│ ├── config.py # Configuration settings
│ └── tests/ # Unit tests
│ ├── __init__.py
│ └── test_app.py
├── scripts/
│ ├── transaction_monitor.py # Simulates transaction monitoring
│ └── database_setup.py # Database initialization script
├── .env # Environment variables (not in version control)
├── .gitignore # Files to ignore in Git
├── Dockerfile # Docker image instructions
├── docker-compose.yml # Docker Compose configuration
├── requirements.txt # Python dependencies
├── run_transaction_monitor.sh # Script to run transaction monitor
└── README.md # Project documentation
This project is licensed under the MIT License.