Monitoring prices of Cryptocurrency with Java and Spring Boot

This is just a simple application developed with the simple, yet useful purpose of monitoring the prices of your favorite crypto-coins, if they are
listed on https://coinmarketcap.com.

There is not much complexity here, if you already know a little about Java and the fantastic Spring Boot framework, it will be quite simple, if you still need to know more about Spring Boot, I leave below a suggestion to start your learning.

Starting with Spring Boot:
https://dzone.com/articles/newbies-intro-booting-with-spring

Packages that separate application functions:
- rescontroller — where is the class that provides our endpoint;
- entity — where we create our object of price alert;
- service — where we create our class responsible for the rules of the application;
- output — responsible for return objects;
- repository — where we create our class that can access database to store our settings, in this example we will only simulate the values;
- utils — classes SendEmail.java and SchedulingTasks.java.
These classes are responsible for sending email alerts and also for monitoring the time interval in which we will consult the api that provides us the prices.

Our simple application has two functions:

1 — Request information about cryptocurrency (for example: price, market cap, symbol, etc);
2 — Create alert for change price of some crypto especified.

Hint: For init your project Spring Boot, use this website, https://start.spring.io/

Let’s create the class main, responsible for starting our application by tomcat built-in and also with some necessary settings.

Let’s create our endpoint:

In the above code, we are making an api available to the world.
The {id} attribute is the name of the digital currency that you want to obtain information or alert, such as bitcoin or ripple.

let’s go ahead, now we’ve created our class service, responsible for organizing the rules of our application.

Let’s understand what’s happening in the code above.

  • getInformation() method:
    It accesses the class restClient that calls the api of the site www.coinmarketcap.com with all the information of the chosen digital currency.
  • enableDisableAlert() method:
    Informs if the price alert will be enabled or disabled. If enabled it will call the price tracking method.
  • initMonitoringOfPrice():
    If the enable price alert option is enabled, this method is called at the endpoint request and will also be called by our monitoring class, which will call it at a given time interval. We will see this in more detail soon.
  • alertUser() method:
    If the tracking validations are correct, we call this method to send an email with the new price alert reached.

For all this to work, we still need to create the helper classes with their specific functions.

Return Objects:

Enable or disable alert:

All informations of your cripto:

Class Builder for Output informations (util in the tests units):

Client class that searches information from CoinmarketCap.
This class makes use of RestTemplate available by the Spring Framework and assists us in the calls to external URL’s to our application.

Entity Class. This class can represent a possible database, in this example application, the data will be simulated and so this class does not represent a real database table. Feel free to refine this.

Repository class.
This class is respossible for accessing simulated data from a “database”.

The getValueAlert method is very important in this example, since we are not using a real database, this method is “storing” our information. It is in it that you will decide which crypto you want to monitor and which alert price.

Utilitarian classes.
Here we will send the email if the alert conditions are satisfied and the SchedulingTasks class, responsible for calling the external URL with pricing information in a given period.
Here again Spring makes task scheduling something simple to implement.

Send mail with Java mail:

Scheduling:

This is the basis of our application, the coinmarketcap.com website, provides several APIs so we can work with information related to digital currencies.

https://coinmarketcap.com/api/

From the Spring Boot framework and the Java platform, we have been able to create a simple and efficient web application that uses the information in this API to create useful rules for our purpose, in this example, to show information about a digital currency and, price change alert.

You can still use this application and you can certainly improve this code to work with other information and rules, if you find it necessary, you can also make this application even better with a mobile application where non-technical users can enjoy the features.

Note: sending the email can be blocked for security by email services like gmail. You can optionally create a test specific account.
If this block occurs, simply change the configuration of your email service to allow less secure applications to use your account.

If you want the complete code, follow in Github:

https://github.com/vinicius-pereiradeoliveira/cripto-prices

Thank you.

Software Developer || Technology Researcher || Very interested in economics and general aspects of society