A Go backend RESTful API with mini-clean architecture for handling token price data from external APIs.
- Import token prices from Hyperliquid DEX
- Calculate price changes over different time intervals (1h, 4h, 12h, 1d, 7d)
- RESTful API for accessing token data and price changes
- Automatic cleanup of old price data with 7-day retention policy
- Efficient storage with daily historical data points
- Go 1.16 or higher
- PostgreSQL 12 or higher
-
Clone the repository:
git clone https://github.com/purro-wallet/purro-core.git cd purro-core -
Install dependencies:
go mod tidy -
Configure the application: Edit
config/app.iniwith your database and API settings. -
Initialize the database:
psql -U postgres -d your_database_name -f scripts/init_database.sql -
Import tokens from Hyperliquid:
go run scripts/hyperliquid_importer.go import-tokens -
Import initial prices:
go run scripts/hyperliquid_importer.go import-prices
Start the API server:
go run main.go
Run the importer in daemon mode:
go run scripts/hyperliquid_importer.go daemon
-
Edit the service file:
nano scripts/hyperliquid-importer.serviceUpdate the
WorkingDirectorypath to match your installation. -
Install the service:
sudo cp scripts/hyperliquid-importer.service /etc/systemd/system/ sudo systemctl daemon-reload sudo systemctl enable hyperliquid-importer sudo systemctl start hyperliquid-importer
Add a cron job to run the importer every hour:
crontab -e
Add the following line:
0 * * * * cd /path/to/purro-core && /usr/local/go/bin/go run scripts/hyperliquid_importer.go import-prices
To manage database size and performance, the system automatically implements a data retention policy:
- Price data is kept for 7 days by default
- For each token, at least one price point per day is preserved for historical analysis
- Older data is automatically cleaned up after each price update
You can adjust the retention period by modifying the DataRetentionDays constant in price_updater.go.
The API is documented using Swagger/OpenAPI. You can view the interactive documentation by opening docs/swagger/index.html in your web browser.
You can view the Swagger UI documentation by running:
make serve-docsThis will start a local web server on port 8082. Open your browser to http://localhost:8082 to view the Swagger UI. From there, you can:
- Explore the available endpoints, request/response formats, and data models
- Try out API calls directly from the UI
- See example requests and responses
For more details, see the API Documentation README.
GET /api/v1/tokens- Get all tokensPOST /api/v1/tokens- Create a new tokenGET /api/v1/tokens/{id}- Get a token by IDPUT /api/v1/tokens/{id}- Update a token
POST /api/v1/tokens/{id}/prices- Add a price for a tokenGET /api/v1/tokens/{id}/prices/latest- Get the latest price for a tokenGET /api/v1/tokens/{id}/price-changes/{interval}- Get price change for a token over an intervalGET /api/v1/price-changes/{interval}- Get price changes for all tokens over an intervalPOST /api/v1/prices/import- Import token pricesPOST /api/v1/prices/import-from-api- Import token prices from external APIPOST /api/v1/prices/cleanup- Cleanup old price records
GET /api/v1/time-intervals- Get all time intervals
curl -X GET http://localhost:8080/api/v1/price-changes/1d
# First, get the token ID for BTC
curl -X GET http://localhost:8080/api/v1/tokens | jq '.data[] | select(.symbol=="BTC")'
# Then, use the token ID to get the price change
curl -X GET http://localhost:8080/api/v1/tokens/123/price-changes/1h
curl -X POST http://localhost:8080/api/v1/prices/import-from-api
The database schema includes the following tables:
blockchains- Information about blockchainstokens- Information about tokenstoken_prices- Historical price data for tokenstime_intervals- Predefined time intervals for price change calculations
And the following views:
v_tokens- Combined token informationv_latest_token_prices- Latest price for each token