This Crypto Client Proof of Concept (POC) application is designed to work in conjunction with the crypto-monitor-api-poc project. It fetches cryptocurrency data from Coingecko.com, processes it, and sends it to the crypto-monitor-api-poc service for storage and monitoring purposes.
- Fetch current cryptocurrency data from Coingecko API
- Update historical data for multiple cryptocurrencies
- Parse data into Coin format
- Post processed data to crypto-monitor-api-poc service
- Respect API rate limiting to prevent request throttling
- Robust error handling and retry mechanism
- Efficient data processing
- Comprehensive logging for monitoring and debugging
The Coin object is a crucial component of this client application. It serves as the data model for cryptocurrency information and is used for communication with the crypto-monitor-api-poc service.
The Coin class is defined in the crypto-monitor-api-poc project and is included as a dependency in this client. The client application populates Coin objects with data fetched from Coingecko and sends these objects to the server, which expects this specific format.
Key fields of the Coin object include:
coinId
: Unique identifier for the cryptocurrencycoinName
: Name of the cryptocurrencysymbol
: Symbol of the cryptocurrencypriceUsd
,priceEur
,priceBtc
,priceEth
: Current prices in various currenciesmarketCapUsd
,marketCapEur
,marketCapBtc
,marketCapEth
: Market capitalization datatimestamp
: Time of data retrieval
For a complete list of fields and methods, refer to the Coin.java file in the crypto-monitor-api-poc project.
Before setting up and running the Crypto Client POC, ensure you have the following:
- Java 11 or higher
- Maven 3.6 or higher
- crypto-monitor-api-poc project set up and built:
- Clone the crypto-monitor-api-poc repository
- Navigate to the crypto-monitor-api-poc directory
- Run
mvn clean install
to build the project and install its artifacts in your local Maven repository- This step is crucial as the Crypto Client POC depends on classes (such as the Coin object) from the crypto-monitor-api-poc project
- The crypto-monitor-api-poc service should be running and accessible:
- Start the crypto-monitor-api-poc service (either in a Docker container or as a standalone Java application)
- Ensure the service is accessible at
http://localhost:8080
- This is the default URL the Crypto Client POC will use to communicate with the backend service
- If you need to use a different URL, update the
backendUrl
in theCryptoConfig
class accordingly
Ensuring these prerequisites are met will allow for smooth compilation, execution, and proper functionality of the Crypto Client POC application.
-
Clone the repository:
git clone https://github.com/src-dbgr/crypto-client-poc.git
-
Navigate to the project directory:
cd crypto-client-poc
-
Build the project using Maven:
mvn clean install
The CryptoClient now offers the following main operations for both bulk and individual cryptocurrency data fetching:
updateCurrentData()
: Updates all chosen and backend-enabled cryptos with current price and metadata information.updateCurrentData(CryptoId cryptoId)
: Updates a single cryptocurrency with current price and metadata information.updateHistoricalData()
: Fetches and updates historical data for all cryptocurrencies based on the last valid date from the backend.updateHistoricalData(CryptoId cryptoId)
: Fetches and updates historical data for a single cryptocurrency based on the last valid date from the backend.fetchAllHistoricalData(int timeFrame)
: Updates historical data for all cryptocurrencies for the specified number of days, starting from today and going backwards.fetchAllHistoricalData(CryptoId cryptoId, int timeFrame)
: Updates historical data for a single cryptocurrency for the specified number of days, starting from today and going backwards.
To use the CryptoClient:
- Ensure the crypto-monitor-api-poc is up and running.
- The
CryptoId
enum in thecrypto.config
package now defines all supported cryptocurrencies. Use these enum values when working with individual cryptocurrencies. - In the
main
method ofCryptoClient
, uncomment, or remove comment, or modify the operations you want to execute.The major methods are now:
- client.updateCurrentData()
- client.updateCurrentData({CryptoId})
- client.updateHistoricalData()
- client.updateHistoricalData({CryptoId})
- client.fetchAllHistoricalData({time-frame})
- client.fetchAllHistoricalData({CryptoId}, {time-frame})
- Run the
CryptoClient
as a Java application.
Example usage in main
method:
public static void main(String[] args) {
// ... initialization code ...
CryptoClient client = new CryptoClient(config, dataSource, backendService);
try {
LOG.info("Updating current crypto data for all cryptocurrencies...");
client.updateCurrentData();
LOG.info("Current crypto data update completed for all cryptocurrencies.");
// ...
} catch (Exception e) {
LOG.error("An error occurred", e);
}
}
The application configuration is managed through the CryptoConfig
class. This class contains several important settings:
backendUrl
: The URL of your crypto-monitor-api-poc servicecoingeckoApiUrl
: The base URL for the Coingecko APImaxRetries
: Maximum number of retries for failed requestsrateLimitDelay
: Delay between requests to respect rate limiting
The supported cryptocurrency IDs are now defined in the CryptoId
enum in the crypto.config
package. This enum provides a type-safe way to work with cryptocurrency IDs. For example:
public enum CryptoId {
BITCOIN("bitcoin"),
ETHEREUM("ethereum"),
CARDANO("cardano"),
POLKADOT("polkadot"),
CHAINLINK("chainlink"),
// ... other cryptocurrencies ...
}
To add or remove cryptocurrencies from tracking, modify this enum in the CryptoId.java
file. The string value in each enum constant should match exactly with the identifiers used by the Coingecko API.
When using methods that operate on individual cryptocurrencies, use the enum constants. For example:
client.updateCurrentData(CryptoId.BITCOIN);
client.updateHistoricalData(CryptoId.ETHEREUM);
client.fetchAllHistoricalData(CryptoId.CARDANO, 30);
Please note that Coingecko has recently imposed significant restrictions on their public API. These limitations may result in rate limiting issues when fetching data. To mitigate this, the application implements a RateLimiter
class that enforces delays between API requests:
public class RateLimiter {
private final long delayMs;
// ... implementation ...
}
The delay between requests is configurable via the rateLimitDelay
parameter in CryptoConfig
. Adjust this value if you encounter rate limiting issues:
private final long rateLimitDelay = 5000; // 5 seconds delay
A retry mechanism with growing delays is in place. Despite these precautions, you may still experience limitations when using the Coingecko public API extensively. Consider using their pro services for more reliable and extensive data fetching capabilities in a production environment.
The application uses several configuration constants that can be modified in the CryptoConfig
class:
backendUrl
: The URL of your crypto-monitor-api-poc servicecoingeckoApiUrl
: The base URL for the Coingecko APImaxRetries
: Maximum number of retries for failed requestsrateLimitDelay
: Delay between requests to respect rate limitingcryptoIds
: List of cryptocurrency IDs to fetch data for
Project Link: https://github.com/src-dbgr/crypto-client-poc
This client application is tightly coupled with the crypto-monitor-api-poc project, which stores the data in a PostgreSQL database and serves as the data source for Grafana Monitoring Dashboards.