Rated Network Coding Challenge
Original repo with task description: https://github.com/rated-network/coding-challenge/
Built with FastAPI best practices in mind using FastAPI production template.
Quick start
git clone https://github.com/ohld/rated-network-coding-challenge
cd rated-network-coding-challenge
cp .env.example .env
docker network create app_main
docker-compose up -d --build
# apply migrations to local db
docker compose exec app migrate
# upload sample data
docker compose exec app python ./tests/upload_tx_data.py
I'd suggest to run the latter command separately to let docker image run for a couple seconds. Otherwise phantom errors like Cannot assign requested address
for localhost may occur. š¤·
What's inside
/stats
(src/stats/router.py)
GET {
"total_transactions_in_db": 5000,
"total_gas_used_gwei": 10492553722.431274,
"total_gas_cost_in_usd": 19219.824439832475
}
/transactions
(src/transactions/router.py)
POST Creates transaction in db, returns nothing (for simplicity). Boilerplate to integrate streams further.
- Stores raw data
- Calculates
gas_used_gwei
on the fly - Background task to calculate
gas_used_usd
using Coingecko API (cached with Redis)
/transactions/{tx_hash}
(src/transactions/router.py)
GET Returns TransactionCompactResponse:
{
"hash": "0x06568e8d3c76f6961c650c3b2c9404c79b3bb14138051313b9a74e6255d02140",
"from_address": "0x7f21c6ab63892ed9db38f5b79d0fadaf4ec79a42",
"to_address": "0x7a097dbacb237d6b6a047368f43514992510bb79",
"block_number": 17818510,
"block_timestamp": "2023-08-01T06:58:35+0000",
"gas_used_gwei": 399397.655979,
"gas_used_usd": 0.7305556709464458
}
Tests
- tx data upload + proper get response with calculated fields (
gas_used_gwei
,gas_used_usd
) - txs data upload from csv file + proper /stats response
I didn't test the Coingecko API integration and didn't write fallbacks in case it will not be available at some point.
Local Development
First Build Only
cp .env.example .env
docker network create app_main
docker-compose up -d --build
Linters
Format the code
docker compose exec app format
Migrations
- Create an automatic migration from changes in
src/database.py
docker compose exec app makemigrations *migration_name*
- Run migrations
docker compose exec app migrate
- Downgrade migrations
docker compose exec app downgrade -1 # or -2 or base or hash of the migration
Tests
All tests are integrational and require DB connection.
One of the choices I've made is to use default database (postgres
), separated from app's app
database.
- Using default database makes it easier to run tests in CI/CD environments, since there is no need to setup additional databases
- Tests are run with
force_rollback=True
, i.e. every transaction made is then reverted
Run tests
docker compose exec app pytest