Skip to content

Commit

Permalink
feat: add /params command
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Nov 9, 2023
1 parent 7cfa3a0 commit 404c92c
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 1 deletion.
135 changes: 135 additions & 0 deletions pkg/data/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"
"main/pkg/tendermint"
"main/pkg/types"
"strconv"
"sync"
"time"

"github.com/rs/zerolog"
)
Expand Down Expand Up @@ -158,3 +160,136 @@ func (m *Manager) GetTallies() (map[string]types.ChainTallyInfos, error) {

return tallyInfos, nil
}

func (m *Manager) GetChainParams(chain *types.Chain) (*types.ChainWithVotingParams, []error) {
var wg sync.WaitGroup
var mutex sync.Mutex

errors := make([]error, 0)
params := &types.ParamsResponse{}

rpc := tendermint.NewRPC(chain, m.Logger)

wg.Add(3)

go func() {
defer wg.Done()

votingParams, err := rpc.GetGovParams("voting")
mutex.Lock()
defer mutex.Unlock()

if err != nil {
errors = append(errors, err)
return
}

params.VotingParams = votingParams.VotingParams
}()

go func() {
defer wg.Done()

depositParams, err := rpc.GetGovParams("deposit")
mutex.Lock()
defer mutex.Unlock()

if err != nil {
errors = append(errors, err)
return
}

params.DepositParams = depositParams.DepositParams
}()

go func() {
defer wg.Done()

tallyingParams, err := rpc.GetGovParams("tallying")
mutex.Lock()
defer mutex.Unlock()

if err != nil {
errors = append(errors, err)
return
}

params.TallyParams = tallyingParams.TallyParams
}()

wg.Wait()

if len(errors) > 0 {
return nil, errors
}

quorum, err := strconv.ParseFloat(params.TallyParams.Quorum, 64)
if err != nil {
return nil, []error{err}
}

threshold, err := strconv.ParseFloat(params.TallyParams.Threshold, 64)
if err != nil {
return nil, []error{err}
}

vetoThreshold, err := strconv.ParseFloat(params.TallyParams.VetoThreshold, 64)
if err != nil {
return nil, []error{err}
}

votingPeriod, err := time.ParseDuration(params.VotingParams.VotingPeriod)
if err != nil {
return nil, []error{err}
}

maxDepositPeriod, err := time.ParseDuration(params.DepositParams.MaxDepositPeriod)
if err != nil {
return nil, []error{err}
}

return &types.ChainWithVotingParams{
Chain: chain,
VotingPeriod: votingPeriod,
MaxDepositPeriod: maxDepositPeriod,
MinDepositAmount: params.DepositParams.MinDepositAmount,
Quorum: quorum,
Threshold: threshold,
VetoThreshold: vetoThreshold,
}, nil
}

func (m *Manager) GetParams() (map[string]types.ChainWithVotingParams, error) {
var wg sync.WaitGroup
var mutex sync.Mutex

params := make(map[string]types.ChainWithVotingParams, 0)
errors := make([]error, 0)

for _, chain := range m.Chains {
wg.Add(1)

go func(chain *types.Chain) {
defer wg.Done()

chainParams, errs := m.GetChainParams(chain)
mutex.Lock()
defer mutex.Unlock()

if len(errs) > 0 {
errors = append(errors, errs...)
return
}

params[chainParams.Chain.Name] = *chainParams
}(chain)
}

wg.Wait()

if len(errors) > 0 {
return nil, fmt.Errorf("got %d errors when fetching chain params", len(errors))
}

return params, nil
}
36 changes: 36 additions & 0 deletions pkg/reporters/telegram/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package telegram

import (
"bytes"
"fmt"

tele "gopkg.in/telebot.v3"
)

func (reporter *Reporter) HandleParams(c tele.Context) error {
reporter.Logger.Info().
Str("sender", c.Sender().Username).
Str("text", c.Text()).
Msg("Got params query")

params, err := reporter.DataManager.GetParams()
if err != nil {
return reporter.BotReply(c, fmt.Sprintf("Error getting chain params: %s", err))
}

template, err := reporter.GetTemplate("params")
if err != nil {
reporter.Logger.Error().
Err(err).
Msg("Error rendering params template")
return reporter.BotReply(c, "Error rendering params template")
}

var buffer bytes.Buffer
if err := template.Execute(&buffer, params); err != nil {
reporter.Logger.Error().Err(err).Msg("Error rendering params template")
return err
}

return reporter.BotReply(c, buffer.String())
}
5 changes: 4 additions & 1 deletion pkg/reporters/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
mutes "main/pkg/mutes"
"main/pkg/report/entry"
"main/pkg/state"
"main/pkg/utils"
"strings"
"time"

Expand Down Expand Up @@ -79,6 +80,7 @@ func (reporter *Reporter) Init() error {
bot.Handle("/proposals_mutes", reporter.HandleListMutes)
bot.Handle("/proposals", reporter.HandleProposals)
bot.Handle("/tally", reporter.HandleTally)
bot.Handle("/params", reporter.HandleParams)

reporter.TelegramBot = bot
go reporter.TelegramBot.Start()
Expand All @@ -101,7 +103,8 @@ func (reporter *Reporter) GetTemplate(tmlpType string) (*template.Template, erro
filename := fmt.Sprintf("%s.html", tmlpType)

t, err := template.New(filename).Funcs(template.FuncMap{
"SerializeLink": reporter.SerializeLink,
"SerializeLink": reporter.SerializeLink,
"FormatDuration": utils.FormatDuration,
}).ParseFS(templates.TemplatesFs, "telegram/"+filename)
if err != nil {
return nil, err
Expand Down
11 changes: 11 additions & 0 deletions pkg/tendermint/tendermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@ func (rpc *RPC) GetStakingPool() (*types.PoolRPCResponse, error) {
return &pool, nil
}

func (rpc *RPC) GetGovParams(paramsType string) (*types.ParamsResponse, error) {
url := fmt.Sprintf("/cosmos/gov/v1beta1/params/%s", paramsType)

var pool types.ParamsResponse
if err := rpc.Get(url, &pool); err != nil {
return nil, err
}

return &pool, nil
}

func (rpc *RPC) Get(url string, target interface{}) error {
nodeErrors := make([]error, len(rpc.URLs))

Expand Down
56 changes: 56 additions & 0 deletions pkg/types/responses.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -149,3 +150,58 @@ type PoolRPCResponse struct {
type Pool struct {
BondedTokens math.LegacyDec `json:"bonded_tokens"`
}

type ParamsResponse struct {
VotingParams VotingParams `json:"voting_params"`
DepositParams DepositParams `json:"deposit_params"`
TallyParams TallyParams `json:"tally_params"`
}

type VotingParams struct {
VotingPeriod string `json:"voting_period"`
}

type DepositParams struct {
MinDepositAmount []Amount `json:"min_deposit"`
MaxDepositPeriod string `json:"max_deposit_period"`
}

type Amount struct {
Denom string `json:"denom"`
Amount string `json:"amount"`
}

type TallyParams struct {
Quorum string `json:"quorum"`
Threshold string `json:"threshold"`
VetoThreshold string `json:"veto_threshold"`
}

type ChainWithVotingParams struct {
Chain *Chain
VotingPeriod time.Duration `json:"voting_period"`
MinDepositAmount []Amount `json:"amount"`
MaxDepositPeriod time.Duration `json:"max_deposit_period"`
Quorum float64 `json:"quorum"`
Threshold float64 `json:"threshold"`
VetoThreshold float64 `json:"veto_threshold"`
}

func (c ChainWithVotingParams) FormatQuorum() string {
return fmt.Sprintf("%.2f%%", c.Quorum*100)
}

func (c ChainWithVotingParams) FormatThreshold() string {
return fmt.Sprintf("%.2f%%", c.Threshold*100)
}

func (c ChainWithVotingParams) FormatVetoThreshold() string {
return fmt.Sprintf("%.2f%%", c.VetoThreshold*100)
}

func (c ChainWithVotingParams) FormatMinDepositAmount() string {
amountsAsStrings := utils.Map(c.MinDepositAmount, func(a Amount) string {
return a.Amount + " " + a.Denom
})
return strings.Join(amountsAsStrings, ",")
}
9 changes: 9 additions & 0 deletions templates/telegram/params.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{- range $chainName, $params := . }}
<strong>Params of chain {{ $params.Chain.GetName }}:</strong>
Voting period: {{ FormatDuration $params.VotingPeriod }}
Max deposit period: {{ FormatDuration $params.MaxDepositPeriod }}
Min deposit amount: {{ $params.FormatMinDepositAmount }}
Quorum: {{ $params.FormatQuorum }}
Threshold: {{ $params.FormatThreshold }}
Veto threshold: {{ $params.FormatVetoThreshold }}
{{ end }}

0 comments on commit 404c92c

Please sign in to comment.