-
Notifications
You must be signed in to change notification settings - Fork 1
/
GasServlet.java
52 lines (42 loc) · 2.06 KB
/
GasServlet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.example.demo;
import java.io.IOException;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.example.demo.api.ApiException;
import com.example.demo.api.Web3Api;
import com.example.demo.models.GasPrediction;
import com.example.demo.models.GasPredictions;
import org.eclipse.microprofile.rest.client.inject.RestClient;
@WebServlet(urlPatterns="/gas")
public class GasServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final long GWEI = 1000000000;
@Inject
@RestClient
Web3Api api;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
GasPredictions gasPredictions;
try {
// Update your API key in the line below
gasPredictions = api.gasPredictions(/* API key */ "UAK000000000000000000000000demo0001", /* Blockchain network */ "ethereum-mainnet").getPayload();
} catch (ApiException e) {
response.getWriter().append("Web3api returned with exception: " + e.getMessage());
return;
}
GasPrediction fast = gasPredictions.getFast();
GasPrediction average = gasPredictions.getAverage();
GasPrediction safeLow = gasPredictions.getSafeLow();
response.getWriter().append("Fast: " + (fast.getGasPrice().longValue() / GWEI) + " Gwei, " + fast.getWait() + " minutes wait\n");
response.getWriter().append("Average: " + (average.getGasPrice().longValue() / GWEI) + " Gwei, " + average.getWait() + " minutes wait\n");
response.getWriter().append("Safe Low: " + (safeLow.getGasPrice().longValue() / GWEI) + " Gwei, " + safeLow.getWait() + " minutes wait\n");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}