forked from hjoelh/Eth-Price-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (110 loc) · 3.01 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
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
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"sync"
"time"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
)
const endpoint = "https://api.etherscan.io/api?module=gastracker&action=gasoracle&apikey="
const shards = 1
type Response struct {
Status string `json:"status"`
Message string `json:"message"`
Result struct {
LastBlock string `json:"LastBlock"`
SafeGasPrice string `json:"SafeGasPrice"`
ProposeGasPrice string `json:"ProposeGasPrice"`
FastGasPrice string `json:"FastGasPrice"`
SuggestBaseFee string `json:"suggestBaseFee"`
GasUsedRatio string `json:"gasUsedRatio"`
} `json:"result"`
}
func getEnvOrDie(key string) string {
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error loading env: %v", err)
}
value, ok := os.LookupEnv(key)
if !ok {
log.Fatalf("Could not find %v in .env", key)
}
return value
}
func getPrices(api_key string) (string, error) {
res, err := http.Get(endpoint + api_key)
if res != nil {
defer res.Body.Close()
}
if err != nil {
return "", fmt.Errorf("failed to fetch: %v", err)
}
jsonPayload, err := decodeJson[Response](res.Body)
if err != nil {
return "", fmt.Errorf("failed to decode json: %v", err)
}
slow_amount, err := strconv.ParseFloat(jsonPayload.Result.SafeGasPrice, 64)
if err != nil {
return "", fmt.Errorf("invalid slow amount format: %v", err)
}
mid_amount, err := strconv.ParseFloat(jsonPayload.Result.ProposeGasPrice, 64)
if err != nil {
return "", fmt.Errorf("invalid mid amount format: %v", err)
}
fast_amount, err := strconv.ParseFloat(jsonPayload.Result.FastGasPrice, 64)
if err != nil {
return "", fmt.Errorf("invalid fast amount format: %v", err)
}
slow_price := fmt.Sprintf("%.0f", slow_amount)
mid_price := fmt.Sprintf("%.0f", mid_amount)
fast_price := fmt.Sprintf("%.0f", fast_amount)
return "🚀 " + fast_price + " | 🐦 " + mid_price + " | 🐌 " + slow_price, err
}
func decodeJson[T any](r io.Reader) (T, error) {
var v T
err := json.NewDecoder(r).Decode(&v)
return v, err
}
func worker(id int, token string, api_key string) {
discord, err := discordgo.New("Bot " + token)
if err != nil {
log.Fatalf("Error creating Discord session: %v", err)
}
discord.ShardCount = shards
discord.ShardID = id
err = discord.Open()
if err != nil {
log.Fatalf("Error opening Discord ws: %v", err)
}
defer discord.Close()
for {
res, err := getPrices(api_key)
if err != nil {
log.Printf("Error getting gas price for shard %d: %v \n", id, err)
} else {
fmt.Printf("WorkerId %v got %v \n", id, res)
err = discord.UpdateWatchStatus(0, res)
if err != nil {
log.Printf("Error updating Discord status for shard %d: %v \n", id, err)
}
}
time.Sleep(30 * time.Second)
}
}
func main() {
fmt.Println("Hello world! 👋🌍")
token := getEnvOrDie("TOKEN")
api_key := getEnvOrDie("API_KEY")
wg := sync.WaitGroup{}
for shardId := 0; shardId < shards; shardId++ {
wg.Add(1)
go worker(shardId, token, api_key)
}
wg.Wait()
}