-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (92 loc) · 2.35 KB
/
main.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
package main
import (
"encoding/json"
"io"
"net/http"
"strconv"
"github.com/gofiber/fiber/v2"
)
type PriceResponse struct {
Time int64 `json:"time"`
USD int64 `json:"USD"`
EUR int64 `json:"EUR"`
GBP int64 `json:"GBP"`
CAD int64 `json:"CAD"`
CHF int64 `json:"CHF"`
AUD int64 `json:"AUD"`
JPY int64 `json:"JPY"`
}
var i int
func get_blockheight() int {
var apiUrl string = "https://mempool.space/api/blocks/tip/height"
resp, err := http.Get(apiUrl)
if err != nil {
return 0
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return 0
}
blockheight, err := strconv.Atoi(string(body))
if err != nil {
return 0
}
return blockheight
}
func get_price() int {
var apiUrl string = "https://mempool.space/api/v1/prices"
resp, err := http.Get(apiUrl)
if err != nil {
return 0
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return 0
}
var priceResponse PriceResponse
if err := json.Unmarshal(body, &priceResponse); err != nil {
return 0
}
return (int(priceResponse.USD))
}
func blockheight_handler(c *fiber.Ctx) error {
var blockheight int = get_blockheight()
if blockheight == 0 {
return c.Status(fiber.StatusInternalServerError).SendString("Error lol")
}
return c.SendString(strconv.Itoa(get_blockheight()))
}
func price_handler(c *fiber.Ctx) error {
var price int = get_price()
if price == 0 {
return c.Status(fiber.StatusInternalServerError).SendString("Error lol")
}
return c.SendString(strconv.Itoa(price) + "$")
}
func satsperdollar_handler(c *fiber.Ctx) error {
var price int = get_price()
if price == 0 {
return c.Status(fiber.StatusInternalServerError).SendString("Error lol")
}
var satsperdollar int = 100000000 / price
return c.SendString(strconv.Itoa(satsperdollar))
}
func blockstohalving_handler(c *fiber.Ctx) error {
var blockheight int = get_blockheight()
if blockheight == 0 {
return c.Status(fiber.StatusInternalServerError).SendString("Error lol")
}
var blockstohalving int = 840000 - blockheight
return c.SendString(strconv.Itoa(blockstohalving))
}
func main() {
app := fiber.New()
app.Static("/", "./static")
app.Get("/api/v1/blockheight", blockheight_handler)
app.Get("/api/v1/price", price_handler)
app.Get("/api/v1/satsperdollar", satsperdollar_handler)
app.Get("/api/v1/blockstohalving", blockstohalving_handler)
app.Listen(":3001")
}