Skip to content

Commit

Permalink
adds listassets for particular network
Browse files Browse the repository at this point in the history
  • Loading branch information
dixitaniket committed Aug 22, 2023
1 parent 18dc5bc commit f2296de
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
17 changes: 17 additions & 0 deletions monitor/ethservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,20 @@ func (ec *EthService) getPrices(network, assetName string) (float64, float64, er

return pool.truePrice, pool.expectedPrice, nil
}

func (ec *EthService) getAllTokenNames(network string) (names []string, ids []string, err error) {
service, found := ec.services[network]
if !found {
err = fmt.Errorf("network %s not found", network)
return
}

service.mut.Lock()
defer service.mut.Unlock()
for assetName, id := range service.assetIdMap {
names = append(names, assetName)
ids = append(ids, id)
}

return
}
14 changes: 13 additions & 1 deletion monitor/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,27 @@ func handleSlashCommand(es *EthService, command *slack.SlashCommand) error {
return fmt.Errorf("no network")
}

network, tokenName := commands[0], commands[1]
switch command.Command {
case "/netstatus":
if len(commands) < 2 {
return fmt.Errorf("no token name")
}

network, tokenName := commands[0], commands[1]
check, truth, err := es.getPrices(network, tokenName)
if err != nil {
return err
}

slackChan <- checkPriceAttachment(check, truth, tokenName, network)

case "/listassets":
names, ids, err := es.getAllTokenNames(commands[0])
if err != nil {
return err
}

slackChan <- listAttachment(commands[0], names, ids)
}
return nil
}
25 changes: 25 additions & 0 deletions monitor/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,31 @@ func checkPriceAttachment(check, truth float64, assetName, network string) slack
return attachment
}

func listAttachment(network string, assets, ids []string) slack.Attachment {
attachment := slack.Attachment{
Pretext: fmt.Sprintf("*Network*: %s", network),
Color: "good",
Fields: []slack.AttachmentField{
{
Title: "Network",
Value: fmt.Sprintf("```%s```", network),
Short: false,
},
},
Footer: "Monitor Bot",
Ts: json.Number(strconv.FormatInt(time.Now().Unix(), 10)),
}

for i, asset := range assets {
attachment.Fields = append(attachment.Fields, slack.AttachmentField{
Value: fmt.Sprintf("```%s - %s```", asset, ids[i]),
Short: false,
})
}

return attachment
}

func postErr(err error) slack.Attachment {
attachment := slack.Attachment{
Pretext: "An error has occurred:",
Expand Down

0 comments on commit f2296de

Please sign in to comment.