Skip to content

Commit

Permalink
feat: fix denom formatting (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Sep 20, 2023
1 parent c8de773 commit 8dbd5d5
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ linters:
- ifshort
- gci
- musttag
- structcheck
- varcheck
- deadcode
- depguard
12 changes: 6 additions & 6 deletions pkg/config/toml_config/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ type Chain struct {
Filters []string `toml:"filters"`
MintscanPrefix string `toml:"mintscan-prefix"`
PingPrefix string `toml:"ping-prefix"`
PingBaseUrl string `toml:"ping-base-url" default:"https://ping.pub"`
PingBaseUrl string `default:"https://ping.pub" toml:"ping-base-url"`
Explorer *Explorer `toml:"explorer"`
LogUnknownMessages null.Bool `toml:"log-unknown-messages" default:"false"`
LogUnparsedMessages null.Bool `toml:"log-unparsed-messages" default:"true"`
LogFailedTransactions null.Bool `toml:"log-failed-transactions" default:"true"`
LogNodeErrors null.Bool `toml:"log-node-errors" default:"true"`
FilterInternalMessages null.Bool `toml:"filter-internal-messages" default:"true"`
LogUnknownMessages null.Bool `default:"false" toml:"log-unknown-messages"`
LogUnparsedMessages null.Bool `default:"true" toml:"log-unparsed-messages"`
LogFailedTransactions null.Bool `default:"true" toml:"log-failed-transactions"`
LogNodeErrors null.Bool `default:"true" toml:"log-node-errors"`
FilterInternalMessages null.Bool `default:"true" toml:"filter-internal-messages"`
Denoms DenomInfos `toml:"denoms"`
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/config/toml_config/denom.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

type DenomInfo struct {
Denom string `toml:"denom"`
DisplayDenom string `toml:"display-denom" default:""`
DenomCoefficient int64 `toml:"denom-coefficient" default:"1000000"`
DisplayDenom string `default:"" toml:"display-denom"`
DenomCoefficient int64 `default:"1000000" toml:"denom-coefficient"`
CoingeckoCurrency string `toml:"coingecko-currency"`
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/config/toml_config/toml_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ type TelegramConfig struct {
}

type LogConfig struct {
LogLevel string `toml:"level" default:"info"`
JSONOutput null.Bool `toml:"json" default:"false"`
LogLevel string `default:"info" toml:"level"`
JSONOutput null.Bool `default:"false" toml:"json"`
}

func (c *TomlConfig) Validate() error {
Expand Down
14 changes: 8 additions & 6 deletions pkg/reporters/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"main/pkg/types/amount"
"time"

"github.com/dustin/go-humanize"
"gopkg.in/telebot.v3/middleware"

"main/pkg/alias_manager"
"main/pkg/config"
configTypes "main/pkg/config/types"
Expand All @@ -18,7 +21,6 @@ import (

"github.com/rs/zerolog"
tele "gopkg.in/telebot.v3"
"gopkg.in/telebot.v3/middleware"
)

type TelegramReporter struct {
Expand Down Expand Up @@ -232,17 +234,17 @@ func (reporter *TelegramReporter) SerializeLink(link configTypes.Link) template.
func (reporter *TelegramReporter) SerializeAmount(amount amount.Amount) template.HTML {
if amount.PriceUSD == nil {
return template.HTML(fmt.Sprintf(
"%.6f%s",
amount.Value,
"%s %s",
humanize.BigCommaf(amount.Value),
amount.Denom,
))
}

return template.HTML(fmt.Sprintf(
"%.6f%s ($%.2f)",
amount.Value,
"%s %s ($%s)",
humanize.BigCommaf(amount.Value),
amount.Denom,
amount.PriceUSD,
utils.StripTrailingDigits(humanize.BigCommaf(amount.PriceUSD), 3),
))
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,17 @@ func SplitStringIntoChunks(msg string, maxLineLength int) []string {
outMessages = append(outMessages, sb.String())
return outMessages
}

func StripTrailingDigits(s string, digits int) string {
if i := strings.Index(s, "."); i >= 0 {
if digits <= 0 {
return s[:i]
}
i++
if i+digits >= len(s) {
return s
}
return s[:i+digits]
}
return s
}

0 comments on commit 8dbd5d5

Please sign in to comment.