Skip to content

Commit

Permalink
fix: Display balance on roller run in the bigger token denom (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
ItayLevyOfficial authored Jul 10, 2023
1 parent f033699 commit 04d6ffa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
4 changes: 2 additions & 2 deletions cmd/run/services_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func RenderUI(rollappConfig config.RollappConfig, manager *servicemanager.Servic
servicesInfoTable := NewServicesInfoTable(rollappConfig, termWidth)

manager.InitServicesData(rollappConfig)
updateUITable(manager.GetUIData(), servicesStatusTable)
updateUITable(manager.GetUIData(), servicesStatusTable, rollappConfig)
ui.Render(p, servicesStatusTable, servicesInfoTable)

//TODO: the renderer should be a struct that holds the config and the tables
Expand All @@ -51,7 +51,7 @@ func eventLoop(events <-chan ui.Event, ticker <-chan time.Time, manager *service
case <-ticker:
manager.Logger.Println("Fetching service data...")
manager.FetchServicesData(config.rollappConfig)
updateUITable(manager.GetUIData(), config.table)
updateUITable(manager.GetUIData(), config.table, config.rollappConfig)
ui.Render(config.table)
}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/run/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ func NewServiceStatusTable(termWidth int) *widgets.Table {
return table
}

func updateUITable(serviceData []servicemanager.UIData, table *widgets.Table) {
func updateUITable(serviceData []servicemanager.UIData, table *widgets.Table, cfg config.RollappConfig) {
table.Rows = [][]string{{"Name", "Balance", "Status"}}
sort.Slice(serviceData, func(i, j int) bool {
return serviceData[i].Name < serviceData[j].Name
})
for _, service := range serviceData {
balances := []string{}
for _, account := range service.Accounts {
balances = append(balances, account.Balance.String())
balances = append(balances, account.Balance.BiggerDenomStr(cfg))
}

table.Rows = append(table.Rows, []string{service.Name, strings.Join(balances, ","), service.Status})
Expand Down
35 changes: 32 additions & 3 deletions cmd/utils/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"bytes"
"encoding/json"
"errors"
"math/big"
"os/exec"

"fmt"
"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/config"
"math/big"
"os/exec"
"strings"
)

type ChainQueryConfig struct {
Expand Down Expand Up @@ -68,6 +69,34 @@ func (b *Balance) String() string {
return b.Amount.String() + b.Denom
}

func formatBalance(balance *big.Int, decimals uint) string {
divisor := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(decimals)), nil)
quotient := new(big.Int).Div(balance, divisor)
remainder := new(big.Int).Mod(balance, divisor)
remainderStr := fmt.Sprintf("%0*s", decimals, remainder.String())
const decimalPrecision = 6
if len(remainderStr) > decimalPrecision {
remainderStr = remainderStr[:decimalPrecision]
}
remainderStr = strings.TrimRight(remainderStr, "0")
if remainderStr != "" {
return fmt.Sprintf("%s.%s", quotient.String(), remainderStr)
}
return quotient.String()
}

func (b *Balance) BiggerDenomStr(cfg config.RollappConfig) string {
biggerDenom := b.Denom[1:]
decimalsMap := map[string]uint{
consts.Denoms.Hub: 6,
consts.Denoms.Celestia: 6,
cfg.Denom: cfg.Decimals,
}
decimals, _ := decimalsMap[b.Denom]
formattedBalance := formatBalance(b.Amount, decimals)
return formattedBalance + biggerDenom
}

type BalanceResp struct {
Denom string `json:"denom"`
Amount string `json:"amount"`
Expand Down

0 comments on commit 04d6ffa

Please sign in to comment.