This repository has been archived by the owner on Aug 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
143 lines (112 loc) · 3.99 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
136
137
138
139
140
141
142
143
package main
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/PACZone/pactus-status/client"
"github.com/PACZone/pactus-status/config"
"github.com/PACZone/pactus-status/utils"
"github.com/go-telegram/bot"
"github.com/pactus-project/pactus/util"
pactus "github.com/pactus-project/pactus/www/grpc/gen/go"
)
type StatusChecker struct {
ctx context.Context
cfg config.Config
cmgr *client.Mgr
tgbot *bot.Bot
}
func main() {
cfg, err := config.LoadConfig()
if err != nil {
log.Panic(err)
}
ctx := context.Background()
log.Println("starting")
cmgr := client.NewClientMgr(ctx)
for _, rn := range cfg.RPCNodes {
c, e := client.NewClient(rn)
if e != nil {
log.Printf("error: %v adding client %s\n", e, rn)
continue
}
cmgr.AddClient(*c)
log.Printf("client added %s\n", rn)
}
b, err := bot.New(cfg.BotToken, bot.WithAllowedUpdates(bot.AllowedUpdates{}))
if err != nil {
panic(err)
}
sc := StatusChecker{
ctx: ctx,
cfg: cfg,
cmgr: cmgr,
tgbot: b,
}
go sc.postUpdates()
b.Start(ctx)
}
func (sc *StatusChecker) postUpdates() {
for {
log.Println("posting new update!")
status, lbt, lbh, td := networkHealth(sc.cmgr)
bi, err := sc.cmgr.GetBlockchainInfo()
if err != nil {
panic(err)
}
log.Println("got network health and Blockcahin info successfully")
cs, err := sc.cmgr.GetCirculatingSupply()
if err != nil {
panic(err)
}
log.Println("got circ supply successfully")
price := utils.GetPACPrice(sc.cfg.PriceAPI)
log.Println("got price successfully")
msg := makeMessage(bi, cs, td, status, lbt, price, lbh)
_, err = sc.tgbot.EditMessageText(sc.ctx, utils.MakeMessageParams(msg, 37))
if err != nil {
log.Printf("can't post updates: %v\n", err)
}
log.Println("updated posted successfully")
time.Sleep(7 * time.Second)
}
}
func makeMessage(b *pactus.GetBlockchainInfoResponse, c, timeDiff int64, status, lastBlkTime string, price float64, lastBlkH uint32) string {
var s strings.Builder
mcap := float64(util.ChangeToCoin(c)) * price
fdv := float64(util.ChangeToCoin(c+b.TotalPower)) * price
tvl := float64(util.ChangeToCoin(b.TotalPower)) * price
s.WriteString("🟢 Pactus Network Status Update\n\n")
s.WriteString(fmt.Sprintf("⛓️ %s Last Block Height\n\n", utils.FormatNumber(int64(lastBlkH))))
s.WriteString(fmt.Sprintf("👤 %v Accounts\n\n", utils.FormatNumber(int64(b.TotalAccounts))))
s.WriteString(fmt.Sprintf("🕵️ %v Validators\n\n", utils.FormatNumber(int64(b.TotalValidators))))
s.WriteString(fmt.Sprintf("🦾 %v PAC Staked\n\n", utils.FormatNumber(int64(util.ChangeToCoin(b.TotalPower)))))
s.WriteString(fmt.Sprintf("🦾 %v PAC Committee Power\n\n", utils.FormatNumber(int64(util.ChangeToCoin(b.CommitteePower)))))
s.WriteString(fmt.Sprintf("🔄 %v PAC Circulating Supply\n\n", utils.FormatNumber(int64(util.ChangeToCoin(c)))))
s.WriteString(fmt.Sprintf("🪙 %v PAC Total Supply\n\n", utils.FormatNumber(int64(util.ChangeToCoin(c+b.TotalPower)))))
s.WriteString(fmt.Sprintf("📊 %v$ Market Cap\n\n", utils.FormatNumber(int64(mcap))))
s.WriteString(fmt.Sprintf("💹 %v$ Fully Diluted Value (FDV)\n\n", utils.FormatNumber(int64(fdv))))
s.WriteString(fmt.Sprintf("🔒 %v$ Total Value Locked (TVL)\n\n", utils.FormatNumber(int64(tvl))))
s.WriteString(fmt.Sprintf("📈 Exbitron Price %v$ \n\n", price))
s.WriteString(fmt.Sprintf("Network is %s\n%s is The LastBlock time and there is %v seconds passed from last block", status, lastBlkTime, timeDiff))
return s.String()
}
func networkHealth(cmgr *client.Mgr) (string, string, uint32, int64) {
lastBlockTime, lastBlockHeight := cmgr.GetLastBlockTime()
lastBlockTimeFormatted := time.Unix(int64(lastBlockTime), 0).Format("02/01/2006, 15:04:05")
currentTime := time.Now()
timeDiff := (currentTime.Unix() - int64(lastBlockTime))
healthStatus := true
if timeDiff > 15 {
healthStatus = false
}
var status string
if healthStatus {
status = "Healthy✅"
} else {
status = "UnHealthy❌"
}
return status, lastBlockTimeFormatted, lastBlockHeight, timeDiff
}