From 04d6fface8f840da43aaf678ecd8716d1620bbbb Mon Sep 17 00:00:00 2001 From: Itay Date: Mon, 10 Jul 2023 16:08:01 +0300 Subject: [PATCH] fix: Display balance on roller run in the bigger token denom (#231) --- cmd/run/services_status.go | 4 ++-- cmd/run/ui.go | 4 ++-- cmd/utils/balance.go | 35 ++++++++++++++++++++++++++++++++--- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/cmd/run/services_status.go b/cmd/run/services_status.go index 430db26f..9a7949ee 100644 --- a/cmd/run/services_status.go +++ b/cmd/run/services_status.go @@ -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 @@ -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) } } diff --git a/cmd/run/ui.go b/cmd/run/ui.go index 4817a172..1ecdc72f 100644 --- a/cmd/run/ui.go +++ b/cmd/run/ui.go @@ -34,7 +34,7 @@ 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 @@ -42,7 +42,7 @@ func updateUITable(serviceData []servicemanager.UIData, table *widgets.Table) { 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}) diff --git a/cmd/utils/balance.go b/cmd/utils/balance.go index 2b48c543..4f6bc938 100644 --- a/cmd/utils/balance.go +++ b/cmd/utils/balance.go @@ -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 { @@ -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"`