Skip to content

Commit 52821c1

Browse files
committed
feat: more peer-info command details
1 parent 66389f9 commit 52821c1

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

client/client.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ func (c *Client) IsValidator(address string) (bool, error) {
9999
return false, nil
100100
}
101101

102+
func (c *Client) GetValidatorInfo(address string) (*pactus.GetValidatorResponse, error) {
103+
validator, err := c.blockchainClient.GetValidator(context.Background(),
104+
&pactus.GetValidatorRequest{Address: address})
105+
if err != nil {
106+
return nil, err
107+
}
108+
109+
return validator, nil
110+
}
111+
102112
func (c *Client) TransactionData(hash string) (*pactus.TransactionInfo, error) {
103113
data, err := c.transactionClient.GetTransaction(context.Background(),
104114
&pactus.GetTransactionRequest{

discord/discord.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ func (b *Bot) messageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
151151
return
152152
}
153153

154+
parts := strings.Split(strings.Split(peerInfo.Address, "/")[2], "/")
155+
ip := parts[0]
156+
geoData := getGeoIP(ip)
157+
154158
notSyncedMsg := "this peer is not synced with network, gRPC is disabled or doesn't have public IP address."
155159
syncedMsg := "**this peer is synced with network**"
156160

@@ -169,12 +173,27 @@ func (b *Bot) messageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
169173
isSynced = syncedMsg
170174
}
171175

176+
val, err := c.GetValidatorInfo(trimmedAddress)
177+
if err != nil {
178+
msg := p.Sprintf("An error occurred %v\n", err)
179+
_, _ = s.ChannelMessageSendReply(m.ChannelID, msg, m.Reference())
180+
return
181+
}
182+
172183
msg := p.Sprintf("Peer info\n")
173184
msg += p.Sprintf("Peer ID: %v\n", peerID)
174185
msg += p.Sprintf("IP address: %v\n", peerInfo.Address)
175186
msg += p.Sprintf("Agent: %v\n", peerInfo.Agent)
176187
msg += p.Sprintf("Moniker: %v\n", peerInfo.Moniker)
177188
msg += p.Sprintf("IsSynced: %v\n", isSynced)
189+
msg += p.Sprintf("Country: %v\n", geoData.CountryName)
190+
msg += p.Sprintf("City: %v\n", geoData.City)
191+
msg += p.Sprintf("Region Name: %v\n", geoData.RegionName)
192+
msg += p.Sprintf("--------------------Validator Info----------------\n")
193+
msg += p.Sprintf("Number: %v", val.Validator.Number)
194+
msg += p.Sprintf("Last bonding height: %v", val.Validator.LastBondingHeight)
195+
msg += p.Sprintf("Last sortition height: %v", val.Validator.LastSortitionHeight)
196+
msg += p.Sprintf("Stake amount: %v", val.Validator.Stake)
178197
_, _ = s.ChannelMessageSendReply(m.ChannelID, msg, m.Reference())
179198
return
180199
}

discord/ip.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package discord
2+
3+
import (
4+
"encoding/json"
5+
"io"
6+
"net/http"
7+
)
8+
9+
type GeoIP struct {
10+
// The right side is the name of the JSON variable
11+
Ip string `json:"ip"`
12+
CountryCode string `json:"country_code"`
13+
CountryName string `json:"country_name"`
14+
RegionCode string `json:"region_code"`
15+
RegionName string `json:"region_name"`
16+
City string `json:"city"`
17+
Zipcode string `json:"zipcode"`
18+
Lat float32 `json:"latitude"`
19+
Lon float32 `json:"longitude"`
20+
MetroCode int `json:"metro_code"`
21+
AreaCode int `json:"area_code"`
22+
}
23+
24+
func getGeoIP(ip string) *GeoIP {
25+
geo := &GeoIP{}
26+
response, err := http.Get("https://freegeoip.net/json/" + ip)
27+
if err != nil {
28+
return geo
29+
}
30+
defer response.Body.Close()
31+
32+
// response.Body() is a reader type. We have
33+
// to use ioutil.ReadAll() to read the data
34+
// in to a byte slice(string)
35+
body, err := io.ReadAll(response.Body)
36+
if err != nil {
37+
return geo
38+
}
39+
40+
// Unmarshal the JSON byte slice to a GeoIP struct
41+
err = json.Unmarshal(body, &geo)
42+
if err != nil {
43+
return geo
44+
}
45+
46+
return geo
47+
}

0 commit comments

Comments
 (0)