-
Notifications
You must be signed in to change notification settings - Fork 3
Feat: [WIP] - API #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
0xpeluche
wants to merge
20
commits into
DefiLlama:master
Choose a base branch
from
0xpeluche:api
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
g1nt0ki
reviewed
Mar 11, 2025
g1nt0ki
reviewed
Mar 11, 2025
g1nt0ki
reviewed
Mar 11, 2025
g1nt0ki
reviewed
Mar 11, 2025
g1nt0ki
reviewed
Mar 11, 2025
g1nt0ki
reviewed
Mar 11, 2025
g1nt0ki
reviewed
Mar 11, 2025
g1nt0ki
reviewed
Mar 11, 2025
g1nt0ki
reviewed
Mar 11, 2025
g1nt0ki
reviewed
Mar 11, 2025
g1nt0ki
reviewed
Mar 11, 2025
g1nt0ki
reviewed
Mar 11, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
API Prototype PR
Overview
This is a prototype PR of an API that manages cryptocurrency metadata, current prices, and timeseries data. The API uses:
Features
PM2 Cluster Setup
The API is designed to run in a PM2 cluster. Instance 0 is responsible for fetching the initial metadata refresh (if enabled) while all instances serve the endpoints.
Local Cache Management
Metadata is cached locally in a file (e.g.,
api/data/metadata.json). The cache is maintained via a dedicated module (cache/metadataCache.js).Note: The metadata refresh cron is now managed externally (e.g., via Coolify) using a separate script.
Normalized PID Mapping
Coin IDs (pids) are normalized using the helper
normalizeCoinId(fromutils/index.js) and a mapping is maintained between the input pid and its normalized value. A maximum of 100 pids per request is allowed.Endpoints
1. Metadata Route
GET
/api/coins/metadata?pid=bitcoin,ethereumDescription:
Retrieves metadata for the specified coin IDs.
Process:
null(no fallback to Elasticsearch is performed).2. Current Price Route
GET
/api/coins/current?pid=bitcoin,ethereum&withMetadata=true&withTTL=trueDescription:
Retrieves the current price of the specified coins from Redis.
Options:
withTTL=true: Includes TTL information.Process:
price_<normalized_pid>.3. Timeseries Route
GET
/api/coins/timeseries?pid=bitcoin,ethereum&startDate=2023-01-01&endDate=2023-01-31&scale=5mor
GET
/api/coins/timeseries?pid=bitcoin,ethereum×tamp=1673000000Description:
Retrieves historical timeseries data for the specified coin IDs.
Modes:
Timestamp mode:
If a
timestamp(in seconds) is provided, the API returns, for each coin, the document whosetsis closest to that timestamp.Range mode:
If
startDateandendDateare provided, the API returns all matching records (grouped by coin).Scale mode:
If a
scale(e.g., "1m", "5m", "1h", "1d") is provided along withstartDateandendDate, the API performs an aggregation using adate_histogram.The response includes, for each coin, an array of objects per interval containing:
timestamp(in seconds)avg_pricemin_pricemax_pricecountNote:
A check is performed to avoid exceeding the maximum number of buckets (e.g., 5,000). If too many buckets would be created, an error is returned.
4. Earliest Timestamp Route
GET
/api/coins/earliest?pid=bitcoin,ethereumDescription:
Retrieves, for each coin, the earliest available timeseries record (i.e., the record with the lowest timestamp).
Process:
ts.5. Percentage Change Route
GET
/api/coins/percentage-change?pid=bitcoin,ethereum×tamp=1656944730&period=3600&lookForward=trueDescription:
Calculates the percentage change in price for each coin between two points in time.
Parameters:
timestamp: A reference timestamp in seconds (t₀).period: The period (in seconds) over which to calculate the change.lookForward:true, the change is measured from t₀ to t₀ + period (i.e., forward in time).falseor not provided, the change is measured from t₀ to t₀ - period (i.e., backward in time).Process:
Setup Instructions
Install dependencies:
Start the PM2 cluster:
Env Dependencies