-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapis.go
461 lines (376 loc) · 12.7 KB
/
apis.go
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package main
import (
"encoding/json"
"errors"
"math"
"strconv"
"time"
"github.com/extism/go-pdk"
)
const mempoolspaceAPI string = "https://mempool.space/api"
const bitnodesAPI string = "https://bitnodes.io/api/v1"
const coincapAPI string = "https://api.coincap.io/v2"
const satsPerBTC = 100000000
const bytesPerMegabyte = 1000000
type MarketData struct {
Supply float64 `json:"supply"`
SupplyPercent float64 `json:"supplyPercent"`
Price float64 `json:"price"`
PriceChangePercent24h float64 `json:"priceChange24hPercent"`
MoscowTime int64 `json:"moscowTime"`
MarketCap float64 `json:"marketCap"`
}
type CoincapResponse struct {
Data struct {
Supply string `json:"supply"`
MaxSupply string `json:"maxSupply"`
MarketCapUsd string `json:"marketCapUsd"`
PriceUsd string `json:"priceUsd"`
ChangePercent24Hr string `json:"changePercent24Hr"`
} `json:"data"`
Timestamp int64 `json:"timestamp"`
}
func GetMarketData() (interface{}, error) {
const endpoint string = "/assets/bitcoin"
req := pdk.NewHTTPRequest(pdk.MethodGet, coincapAPI+endpoint)
res := req.Send()
if res.Status() != 200 {
errMsg := "CoinCap.IO API request returned status: " + strconv.Itoa(int(res.Status()))
return MarketData{}, errors.New(errMsg)
}
data := CoincapResponse{}
err := json.Unmarshal(res.Body(), &data)
if err != nil {
return MarketData{}, err
}
sup, err := strconv.ParseFloat(data.Data.Supply, 64)
if err != nil {
return MarketData{}, err
}
maxSup, err := strconv.ParseFloat(data.Data.MaxSupply, 64)
if err != nil {
return MarketData{}, err
}
priceUSD, err := strconv.ParseFloat(data.Data.PriceUsd, 64)
if err != nil {
return MarketData{}, err
}
changePercent24h, err := strconv.ParseFloat(data.Data.ChangePercent24Hr, 64)
if err != nil {
return MarketData{}, err
}
marketCapUSD, err := strconv.ParseFloat(data.Data.MarketCapUsd, 64)
if err != nil {
return MarketData{}, err
}
var marketData MarketData
marketData.Supply = float64(sup)
marketData.SupplyPercent = roundDec((sup/maxSup)*100, 2)
marketData.Price = roundDec(float64(priceUSD), 2)
marketData.PriceChangePercent24h = roundDec(changePercent24h, 2)
marketData.MoscowTime = int64(float64(satsPerBTC) / priceUSD)
marketData.MarketCap = roundDec(float64(marketCapUSD), 2)
return marketData, nil
}
type DiffAdjData struct {
DifficultyChangePercent float64
RemainingBlocks int
EstimatedRetargetDate string
}
type DiffAdjResponse struct {
DifficultyChange float64 `json:"difficultyChange"`
EstimatedRetargetDate int64 `json:"estimatedRetargetDate"`
RemainingBlocks int `json:"remainingBlocks"`
}
func getDifficultyAdjustmentData() (DiffAdjData, error) {
const endpoint string = "/v1/difficulty-adjustment"
req := pdk.NewHTTPRequest(pdk.MethodGet, mempoolspaceAPI+endpoint)
res := req.Send()
if res.Status() != 200 {
errMsg := "Mempool.Space API request returned status: " + strconv.Itoa(int(res.Status()))
return DiffAdjData{}, errors.New(errMsg)
}
data := DiffAdjResponse{}
err := json.Unmarshal(res.Body(), &data)
if err != nil {
return DiffAdjData{}, err
}
var diffAdj DiffAdjData
diffAdj.DifficultyChangePercent = roundDec(data.DifficultyChange, 2)
diffAdj.RemainingBlocks = data.RemainingBlocks
diffAdj.EstimatedRetargetDate = time.UnixMilli(data.EstimatedRetargetDate).Format(time.RFC1123)
return diffAdj, nil
}
type HRAndDiff struct {
Hashrate float64
Difficulty float64
}
type HRResponse struct {
CurrentHashrate float64 `json:"currentHashrate"`
CurrentDifficulty float64 `json:"currentDifficulty"`
}
func getHashrateAndDifficulty() (HRAndDiff, error) {
const endpoint string = "/v1/mining/hashrate/1s"
req := pdk.NewHTTPRequest(pdk.MethodGet, mempoolspaceAPI+endpoint)
res := req.Send()
if res.Status() != 200 {
errMsg := "Mempool.Space API request returned status: " + strconv.Itoa(int(res.Status()))
return HRAndDiff{}, errors.New(errMsg)
}
var data HRResponse
err := json.Unmarshal(res.Body(), &data)
if err != nil {
return HRAndDiff{}, err
}
var hashrateAndDifficulty HRAndDiff
hashrateAndDifficulty.Hashrate = data.CurrentHashrate
hashrateAndDifficulty.Difficulty = data.CurrentDifficulty
return hashrateAndDifficulty, nil
}
type MiningData struct {
Hashrate float64 `json:"hashrate"`
Difficulty float64 `json:"difficulty"`
RetargetDifficultyChange float64 `json:"retargetDifficultyChangePercent"`
RetargetRemainingBlocks int `json:"retargetRemainingBlocks"`
RetargetEstimatedDate string `json:"retargetEstimatedDate"`
}
func GetMiningData() (interface{}, error) {
var err error
var da DiffAdjData
da, err = getDifficultyAdjustmentData()
if err != nil {
return MiningData{}, err
}
var hd HRAndDiff
hd, err = getHashrateAndDifficulty()
if err != nil {
return MiningData{}, err
}
var mining MiningData
mining.Hashrate = hd.Hashrate
mining.Difficulty = hd.Difficulty
mining.RetargetDifficultyChange = da.DifficultyChangePercent
mining.RetargetRemainingBlocks = da.RemainingBlocks
mining.RetargetEstimatedDate = da.EstimatedRetargetDate
return mining, nil
}
type MempoolStats struct {
UnconfirmedTXs int64 `json:"unconfirmedTXs"`
VSize float64 `json:"vSize"`
PendingFees float64 `json:"pendingFees"`
BlocksToClear int `json:"blocksToClear"`
}
type MempoolResponse struct {
Count int64 `json:"count"`
Vsize int64 `json:"vsize"`
TotalFee int64 `json:"total_fee"`
}
func GetMempoolStats() (interface{}, error) {
const endpoint string = "/mempool"
const maxBlockVSize int64 = 1000000 // maxBlockWeight = 4_000_000 (vSize = weight / 4)
req := pdk.NewHTTPRequest(pdk.MethodGet, mempoolspaceAPI+endpoint)
res := req.Send()
if res.Status() != 200 {
errMsg := "Mempool.Space API request returned status: " + strconv.Itoa(int(res.Status()))
return MempoolStats{}, errors.New(errMsg)
}
var data MempoolResponse
err := json.Unmarshal(res.Body(), &data)
if err != nil {
return MempoolStats{}, err
}
var mempoolStats MempoolStats
mempoolStats.UnconfirmedTXs = data.Count
mempoolStats.VSize = roundDec(float64(data.Vsize)/bytesPerMegabyte, 2)
mempoolStats.PendingFees = roundDec(float64(data.TotalFee)/satsPerBTC, 3)
mempoolStats.BlocksToClear = int((data.Vsize / maxBlockVSize) + 1)
return mempoolStats, nil
}
type NodeStats struct {
TotalNodes int `json:"totalNodes"`
TorNodes int `json:"torNodes"`
TorNodesPercent float64 `json:"torNodesPercent"`
}
type BitnodesResponse struct {
Timestamp int64 `json:"timestamp"`
TotalNodes int `json:"total_nodes"`
LatestHeight int `json:"latest_height"`
Nodes map[string][]interface{} `json:"nodes"`
}
func GetBitcoinNodeStats() (interface{}, error) {
const endpoint string = "/snapshots/latest"
req := pdk.NewHTTPRequest(pdk.MethodGet, bitnodesAPI+endpoint)
res := req.Send()
if res.Status() != 200 {
errMsg := "BitNodes.IO API request returned status: " + strconv.Itoa(int(res.Status()))
return NodeStats{}, errors.New(errMsg)
}
var data BitnodesResponse
err := json.Unmarshal(res.Body(), &data)
if err != nil {
return NodeStats{}, err
}
var nodeStats NodeStats
nodeStats.TotalNodes = data.TotalNodes
torNodes := 0
for _, details := range data.Nodes {
if details[11] == "TOR" {
torNodes++
}
}
nodeStats.TorNodes = torNodes
nodeStats.TorNodesPercent = roundDec((float64(torNodes)/float64(data.TotalNodes))*100, 2)
return nodeStats, nil
}
type Fees struct {
Fastest int `json:"fastest"`
HalfHour int `json:"halfHour"`
Hour int `json:"hour"`
Economy int `json:"economy"`
Minimum int `json:"minimum"`
}
type FeeResponse struct {
FastestFee int `json:"fastestFee"`
HalfHourFee int `json:"halfHourFee"`
HourFee int `json:"hourFee"`
EconomyFee int `json:"economyFee"`
MinimumFee int `json:"minimumFee"`
}
func GetFeeRecommendation() (interface{}, error) {
const endpoint string = "/v1/fees/recommended"
req := pdk.NewHTTPRequest(pdk.MethodGet, mempoolspaceAPI+endpoint)
res := req.Send()
if res.Status() != 200 {
errMsg := "Mempool.Space API request returned status: " + strconv.Itoa(int(res.Status()))
return Fees{}, errors.New(errMsg)
}
var data FeeResponse
err := json.Unmarshal(res.Body(), &data)
if err != nil {
return Fees{}, err
}
var fees Fees
fees.Fastest = data.FastestFee
fees.HalfHour = data.HalfHourFee
fees.Hour = data.HourFee
fees.Economy = data.EconomyFee
fees.Minimum = data.MinimumFee
return fees, nil
}
type LNStats struct {
TotalNodes int `json:"totalNodes"`
TorNodes int `json:"torNodes"`
ClearnetNodes int `json:"clearnetNodes"`
ClearnetTorNodes int `json:"clearnetTorNodes"`
UnannouncedNodes int `json:"unannouncedNodes"`
Channels int `json:"channels"`
TotalCapacity float64 `json:"totalCapacity"`
AverageChannelCapacity float64 `json:"averageChannelCapacity"`
MedianChannelCapacity float64 `json:"medianChannelCapacity"`
}
type LNResponse struct {
Latest struct {
ChannelCount int `json:"channel_count"`
NodeCount int `json:"node_count"`
TotalCapacity int64 `json:"total_capacity"`
TorNodes int `json:"tor_nodes"`
ClearnetNodes int `json:"clearnet_nodes"`
UnannouncedNodes int `json:"unannounced_nodes"`
AverageCapacity int64 `json:"avg_capacity"`
MedianCapacity int64 `json:"med_capacity"`
ClearnetTorNodes int `json:"clearnet_tor_nodes"`
} `json:"latest"`
}
func GetLightningStats() (interface{}, error) {
const endpoint string = "/v1/lightning/statistics/latest"
req := pdk.NewHTTPRequest(pdk.MethodGet, mempoolspaceAPI+endpoint)
res := req.Send()
if res.Status() != 200 {
errMsg := "Mempool.Space API request returned status: " + strconv.Itoa(int(res.Status()))
return LNStats{}, errors.New(errMsg)
}
var data LNResponse
err := json.Unmarshal(res.Body(), &data)
if err != nil {
return LNStats{}, err
}
var lnStats LNStats
lnStats.TotalNodes = data.Latest.NodeCount
lnStats.TorNodes = data.Latest.TorNodes
lnStats.ClearnetNodes = data.Latest.ClearnetNodes
lnStats.ClearnetTorNodes = data.Latest.ClearnetTorNodes
lnStats.UnannouncedNodes = data.Latest.UnannouncedNodes
lnStats.Channels = data.Latest.ChannelCount
lnStats.TotalCapacity = roundDec(float64(data.Latest.TotalCapacity)/satsPerBTC, 3)
lnStats.AverageChannelCapacity = roundDec(float64(data.Latest.AverageCapacity)/satsPerBTC, 3)
lnStats.MedianChannelCapacity = roundDec(float64(data.Latest.MedianCapacity)/satsPerBTC, 3)
return lnStats, nil
}
type BlockInfo struct {
Height int `json:"height"`
Timestamp string `json:"timestamp"`
TXs int64 `json:"transactions"`
Size float64 `json:"size"`
Reward float64 `json:"totalReward"`
TotalFees float64 `json:"totalFees"`
FeeRate float64 `json:"medianFeeRate"`
Miner string `json:"miner"`
}
type BlockResponse struct {
Height int `json:"height"`
Timestamp int64 `json:"timestamp"`
TXCount int64 `json:"tx_count"`
Size int64 `json:"size"`
Extras struct {
Reward int64 `json:"reward"`
TotalFees int64 `json:"totalFees"`
FeeRate float64 `json:"medianFee"`
Pool struct {
Name string `json:"name"`
} `json:"pool"`
} `json:"extras"`
}
func GetLatestBlockInfo() (interface{}, error) {
height, err := getTipHeight()
if err != nil {
return BlockInfo{}, err
}
endpoint := "/v1/blocks/" + string(height)
req := pdk.NewHTTPRequest(pdk.MethodGet, mempoolspaceAPI+endpoint)
res := req.Send()
if res.Status() != 200 {
errMsg := "Mempool.Space API request returned status: " + strconv.Itoa(int(res.Status()))
return BlockInfo{}, errors.New(errMsg)
}
var dataArray []BlockResponse
err = json.Unmarshal(res.Body(), &dataArray)
if err != nil {
return BlockInfo{}, err
}
data := dataArray[0]
var block BlockInfo
block.Height = data.Height
block.Timestamp = time.Unix(data.Timestamp, 0).Format(time.RFC1123)
block.TXs = data.TXCount
block.Size = roundDec(float64(data.Size)/bytesPerMegabyte, 2)
block.Reward = roundDec(float64(data.Extras.Reward)/satsPerBTC, 3)
block.TotalFees = roundDec(float64(data.Extras.TotalFees)/satsPerBTC, 3)
block.FeeRate = roundDec(data.Extras.FeeRate, 1)
block.Miner = data.Extras.Pool.Name
return block, nil
}
func getTipHeight() (string, error) {
const endpoint string = "/blocks/tip/height"
req := pdk.NewHTTPRequest(pdk.MethodGet, mempoolspaceAPI+endpoint)
res := req.Send()
if res.Status() != 200 {
errMsg := "Mempool.Space API request returned status: " + strconv.Itoa(int(res.Status()))
return "", errors.New(errMsg)
}
height := string(res.Body())
return height, nil
}
func roundDec(x float64, d int) float64 {
fac := math.Pow(10, float64(d))
return float64(math.Round(x*fac) / fac)
}