-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgas.html
251 lines (212 loc) · 8.97 KB
/
gas.html
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
---
layout: default
---
<div class="container">
<div class="p-5 mb-4 bg-light rounded-3" id="startForn">
<div class="container-fluid py-5">
<h1 class="display-5 fw-bold">Start Here</h1>
<p>Get a free API key from <a href="https://explorer.blocknative.com/account">https://explorer.blocknative.com/account</a>.</p>
<label for="apiKey" class="form-label">Blocknative API Key</label>
<input type="text" class="form-control" id="apiKey" value="" required>
<hr/>
<button class="btn btn-primary btn-lg" type="button">Start</button>
</div>
</div>
<div class="row">
<div class="col">
<h2>Base: <strong id="basePrice"></strong> Gwei
<br/>
Max: <strong id="maxPrice"></strong> Gwei</h2>
</div>
<div class="col">
<h3>Txs: <strong id="transactionCount"></strong>
<br/>
Since last: <strong id="msSinceLastBlock"></strong> seconds</h3>
</div>
</div>
<div class="row">
<div class="col">
<div class="alert alert-success" role="alert">
<h2 class="alert-heading">(99 %) Price: <strong id="estPrice"></strong> Gwei</h2>
<p>Priority Fee: <strong id="estPriorityFeePerGas"></strong> Gwei |
Suggested Max Price: <strong id="estMaxFeePerGas"></strong> Gwei</p>
</div>
</div>
<div class="col">
<div class="alert alert-info" role="alert">
<h2 class="alert-heading">(80 %) Price: <strong id="estPrice1"></strong> Gwei</h2>
<p>Priority Fee: <strong id="estPriorityFeePerGas1"></strong> Gwei |
Suggested Max Price: <strong id="estMaxFeePerGas1"></strong> Gwei</p>
</div>
</div>
</div>
<h2>Previous <strong id="nbBlocks"></strong> Blocks + Next Block (<strong id="blockNb"></strong>)</h2>
<p>Average Price: <strong id="avgPrice"></strong> Gwei<br/>
Pending Prices: <strong id="estPending1"></strong> |
<strong id="estPending2"></strong> |
<strong id="estPending3"></strong> |
<strong id="estPending4"></strong> |
<strong id="estPending5"></strong></p>
<canvas id="myChart"></canvas>
<hr/>
<h2>Previous <strong id="nbHours"></strong> Hours</h2>
<table class="table">
<thead>
<tr>
<th scope="col">Date Time</th>
<th scope="col">Avg</th>
<th scope="col">Min</th>
<th scope="col">Max</th>
</tr>
</thead>
<tbody id="myTable"><tr><td>0</td><td>0</td><td>0</td><td>0</td></tr></tbody>
</table>
</div>
{% include jquery.html %}
{% include chartsjs.html %}
<script>
function calculateAverage(array) {
var total = 0;
var count = 0;
array.forEach(function(item, index) {
total += item;
count++;
});
return total / count;
}
const labels = [];
const data = {
labels: labels,
datasets: [{
type: 'line',
label: '99 %',
backgroundColor: 'rgb(15, 81, 50)',
borderColor: 'rgb(15, 81, 50)',
data: [],
},{
type: 'line',
label: '80 %',
backgroundColor: 'rgb(207, 244, 252)',
borderColor: 'rgb(207, 244, 252)',
data: [],
}]
};
const config = {
data: data,
options: {}
};
const myChart = new Chart(
document.getElementById('myChart'),
config
);
$(document).ready(function() {
$("button").click(function() {
var history = [{block: 0}];
var lastBlock = 0;
$("#startForn").hide();
setInterval(function() {
$.ajax({
dataType: "json",
url: "https://api.blocknative.com/gasprices/blockprices?confidenceLevels=99&confidenceLevels=80",
headers: {"Authorization": $("#apiKey").val()},
success: function(result) {
const liveDate = new Date(Date.now());
const maxPrice = result.maxPrice;
const sinceLastBlock = result.msSinceLastBlock;
const blockNb = result.blockPrices[0].blockNumber;
const basePrice = result.blockPrices[0].baseFeePerGas;
const transactionCount = result.blockPrices[0].estimatedTransactionCount
const estPrice = result.blockPrices[0].estimatedPrices[0].price;
const estPriorityFeePerGas = result.blockPrices[0].estimatedPrices[0].maxPriorityFeePerGas;
const estMaxFeePerGas = result.blockPrices[0].estimatedPrices[0].maxFeePerGas;
const estPrice1 = result.blockPrices[0].estimatedPrices[1].price;
const estPriorityFeePerGas1 = result.blockPrices[0].estimatedPrices[1].maxPriorityFeePerGas;
const estMaxFeePerGas1 = result.blockPrices[0].estimatedPrices[1].maxFeePerGas;
const estPending1 = result.estimatedBaseFees[0]["pending+1"][0].baseFee;
const estPending2 = result.estimatedBaseFees[1]["pending+2"][0].baseFee;
const estPending3 = result.estimatedBaseFees[2]["pending+3"][0].baseFee;
const estPending4 = result.estimatedBaseFees[3]["pending+4"][0].baseFee;
const estPending5 = result.estimatedBaseFees[4]["pending+5"][0].baseFee;
console.log("Pending+1 " + estPending1);
console.log("Pending+2 " + estPending2);
console.log("Pending+3 " + estPending3);
console.log("Pending+4 " + estPending4);
console.log("Pending+5 " + estPending5);
if (lastBlock == blockNb) {
myChart.data.labels.pop();
myChart.data.datasets.forEach((dataset) => {
dataset.data.pop();
});
} else {
lastBlock = blockNb;
const lastDate = new Date($("#myTable > tr").first()[0].childNodes[0].innerText);
const lastAvg = $("#myTable > tr").first()[0].childNodes[1].innerText;
const lastMin = $("#myTable > tr").first()[0].childNodes[2].innerText;
const lastMax = $("#myTable > tr").first()[0].childNodes[3].innerText;
const lastPrice = myChart.data.datasets[0].data[myChart.data.datasets[0].data.length-1];
// if (liveDate.getMinutes() == lastDate.getMinutes()) {
if (liveDate.getHours() == lastDate.getHours()) {
if (lastPrice < lastMin || lastMin == 0 ) {
var newMin = lastPrice;
} else {
var newMin = lastMin;
}
if (lastPrice > lastMax ) {
var newMax = lastPrice;
} else {
var newMax = lastMax;
}
$("#myTable > tr:first").html("<td>" + liveDate +
"</td><td>" + Math.round((Number(lastPrice) + Number(lastAvg))/2) +
"</td><td>" + newMin +
"</td><td>" + newMax + "</td>");
} else {
if (lastPrice != null) {
$("#myTable").prepend("<tr><td>" + liveDate +
"</td><td>" + lastPrice +
"</td><td>" + lastPrice +
"</td><td>" + lastPrice + "</td></tr>");
} else {
$("#myTable").html("<tr><td>" + liveDate +
"</td><td>" + 0 +
"</td><td>" + 0 +
"</td><td>" + 0 + "</td></tr>");
}
}
}
if (myChart.data.labels.length == 200) {
myChart.data.labels.shift();
myChart.data.datasets.forEach((dataset) => {
dataset.data.shift();
});
}
myChart.data.labels.push(blockNb);
myChart.data.datasets[0].data.push(estPrice);
myChart.data.datasets[1].data.push(estPrice1);
myChart.update();
$("title").html(estPrice + " + " + estPriorityFeePerGas + " Gwei (" + Math.round(sinceLastBlock/1000) + " s)");
$("#blockNb").html(blockNb);
$("#basePrice").html(Math.round(basePrice));
$("#maxPrice").html(maxPrice);
$("#transactionCount").html(transactionCount);
$("#msSinceLastBlock").html(Math.round(sinceLastBlock/1000));
$("#estPrice").html(estPrice);
$("#estPriorityFeePerGas").html(estPriorityFeePerGas);
$("#estMaxFeePerGas").html(estMaxFeePerGas);
$("#estPrice1").html(estPrice1);
$("#estPriorityFeePerGas1").html(estPriorityFeePerGas1);
$("#estMaxFeePerGas1").html(estMaxFeePerGas1);
$("#estPending1").html(Math.round(estPending1));
$("#estPending2").html(Math.round(estPending2));
$("#estPending3").html(Math.round(estPending3));
$("#estPending4").html(Math.round(estPending4));
$("#estPending5").html(Math.round(estPending5));
$("#nbBlocks").html(myChart.data.labels.length - 1);
$("#avgPrice").html(Math.round(calculateAverage(myChart.data.datasets[0].data)));
$("#nbHours").html($("#myTable > tr").length);
//console.log(history);
}});
}, 6000);
});
});
</script>