From 969670b23aeca4692bdae3a04cd2ab55568c86de Mon Sep 17 00:00:00 2001 From: Michael Tsitrin <114929630+mtsitrin@users.noreply.github.com> Date: Wed, 5 Jul 2023 14:40:55 +0300 Subject: [PATCH] feat: fix balance printing in roller run (#193) --- cmd/register/register.go | 4 +-- cmd/relayer/start/start.go | 8 ++--- cmd/run/run.go | 2 +- cmd/sequencer/start/start.go | 2 +- cmd/utils/balance.go | 54 ++++++++++++++++++++------------ cmd/utils/fetch_accounts_data.go | 1 + data_layer/celestia/celestia.go | 4 +-- data_layer/damock/damock.go | 5 ++- utils/service_manager/service.go | 2 +- 9 files changed, 50 insertions(+), 32 deletions(-) diff --git a/cmd/register/register.go b/cmd/register/register.go index 20daad00..117aa072 100644 --- a/cmd/register/register.go +++ b/cmd/register/register.go @@ -5,11 +5,11 @@ import ( "encoding/json" "errors" "fmt" - "github.com/dymensionxyz/roller/cmd/consts" "math/big" "strings" initconfig "github.com/dymensionxyz/roller/cmd/config/init" + "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" "github.com/dymensionxyz/roller/config" "github.com/spf13/cobra" @@ -41,7 +41,7 @@ func register(cmd *cobra.Command, args []string) error { if err != nil { return err } - notFundedAddrs, err := utils.GetSequencerInsufficientAddrs(rollappConfig, *registerUdymPrice) + notFundedAddrs, err := utils.GetSequencerInsufficientAddrs(rollappConfig, registerUdymPrice) if err != nil { return err } diff --git a/cmd/relayer/start/start.go b/cmd/relayer/start/start.go index 00332cf6..454ddbf4 100644 --- a/cmd/relayer/start/start.go +++ b/cmd/relayer/start/start.go @@ -77,11 +77,11 @@ func GetRlyHubInsufficientBalances(config config.RollappConfig) ([]utils.NotFund return nil, err } insufficientBalances := make([]utils.NotFundedAddressData, 0) - if HubRlyBalance.Cmp(oneDayRelayPriceHub) < 0 { + if HubRlyBalance.Amount.Cmp(oneDayRelayPriceHub) < 0 { insufficientBalances = append(insufficientBalances, utils.NotFundedAddressData{ KeyName: consts.KeysIds.HubRelayer, Address: HubRlyAddr, - CurrentBalance: HubRlyBalance, + CurrentBalance: HubRlyBalance.Amount, RequiredBalance: oneDayRelayPriceHub, Denom: consts.Denoms.Hub, }) @@ -98,11 +98,11 @@ func GetRelayerInsufficientBalances(config config.RollappConfig) ([]utils.NotFun if err != nil { return insufficientBalances, err } - if rolRlyData.Balance.Cmp(oneDayRelayPriceRollapp) < 0 { + if rolRlyData.Balance.Amount.Cmp(oneDayRelayPriceRollapp) < 0 { insufficientBalances = append(insufficientBalances, utils.NotFundedAddressData{ KeyName: consts.KeysIds.RollappRelayer, Address: rolRlyData.Address, - CurrentBalance: rolRlyData.Balance, + CurrentBalance: rolRlyData.Balance.Amount, RequiredBalance: oneDayRelayPriceRollapp, Denom: config.Denom, }) diff --git a/cmd/run/run.go b/cmd/run/run.go index bd577431..3ad5d26b 100644 --- a/cmd/run/run.go +++ b/cmd/run/run.go @@ -104,7 +104,7 @@ func verifyBalances(rollappConfig config.RollappConfig) { utils.PrettifyErrorIfExists(err) sequencerInsufficientBalances, err := utils.GetSequencerInsufficientAddrs( - rollappConfig, *sequnecer_start.OneDaySequencePrice) + rollappConfig, sequnecer_start.OneDaySequencePrice) utils.PrettifyErrorIfExists(err) insufficientBalances = append(insufficientBalances, sequencerInsufficientBalances...) diff --git a/cmd/sequencer/start/start.go b/cmd/sequencer/start/start.go index 7a308717..60541b17 100644 --- a/cmd/sequencer/start/start.go +++ b/cmd/sequencer/start/start.go @@ -36,7 +36,7 @@ func StartCmd() *cobra.Command { RollappBinary = rollappConfig.RollappBinary RollappDirPath = filepath.Join(rollappConfig.Home, consts.ConfigDirName.Rollapp) - sequencerInsufficientAddrs, err := utils.GetSequencerInsufficientAddrs(rollappConfig, *OneDaySequencePrice) + sequencerInsufficientAddrs, err := utils.GetSequencerInsufficientAddrs(rollappConfig, OneDaySequencePrice) utils.PrettifyErrorIfExists(err) utils.PrintInsufficientBalancesIfAny(sequencerInsufficientAddrs) LightNodeEndpoint := cmd.Flag(FlagNames.DAEndpoint).Value.String() diff --git a/cmd/utils/balance.go b/cmd/utils/balance.go index 49277776..c64dcfd4 100644 --- a/cmd/utils/balance.go +++ b/cmd/utils/balance.go @@ -17,49 +17,63 @@ type ChainQueryConfig struct { Binary string } -func QueryBalance(chainConfig ChainQueryConfig, address string) (*big.Int, error) { +func QueryBalance(chainConfig ChainQueryConfig, address string) (Balance, error) { cmd := exec.Command(chainConfig.Binary, "query", "bank", "balances", address, "--node", chainConfig.RPC, "--output", "json") out, err := ExecBashCommand(cmd) if err != nil { - return nil, err + return Balance{}, err } return ParseBalanceFromResponse(out, chainConfig.Denom) } -func ParseBalanceFromResponse(out bytes.Buffer, denom string) (*big.Int, error) { +func ParseBalanceFromResponse(out bytes.Buffer, denom string) (Balance, error) { var balanceResp BalanceResponse err := json.Unmarshal(out.Bytes(), &balanceResp) if err != nil { - return nil, err + return Balance{}, err } - for _, balance := range balanceResp.Balances { - if balance.Denom == denom { - amount := new(big.Int) - _, ok := amount.SetString(balance.Amount, 10) - if !ok { - return nil, errors.New("unable to convert balance amount to big.Int") - } - return amount, nil + + balance := Balance{ + Denom: denom, + Amount: big.NewInt(0), + } + for _, resbalance := range balanceResp.Balances { + if resbalance.Denom != denom { + continue + } + amount := new(big.Int) + _, ok := amount.SetString(resbalance.Amount, 10) + if !ok { + return Balance{}, errors.New("unable to convert balance amount to big.Int") } + balance.Amount = amount } - return big.NewInt(0), nil + return balance, nil } type Balance struct { + Denom string `json:"denom"` + Amount *big.Int `json:"amount"` +} + +func (b *Balance) String() string { + return b.Amount.String() + b.Denom +} + +type resp struct { Denom string `json:"denom"` Amount string `json:"amount"` } - type BalanceResponse struct { - Balances []Balance `json:"balances"` + Balances []resp `json:"balances"` } type AccountData struct { Address string - Balance *big.Int + Balance Balance } -func GetSequencerInsufficientAddrs(cfg config.RollappConfig, requiredBalance big.Int) ([]NotFundedAddressData, error) { +func GetSequencerInsufficientAddrs(cfg config.RollappConfig, requiredBalance *big.Int) ([]NotFundedAddressData, error) { sequencerData, err := GetSequencerData(cfg) if err != nil { return nil, err @@ -68,13 +82,13 @@ func GetSequencerInsufficientAddrs(cfg config.RollappConfig, requiredBalance big return nil, err } for _, seq := range sequencerData { - if seq.Balance.Cmp(&requiredBalance) < 0 { + if seq.Balance.Amount.Cmp(requiredBalance) < 0 { return []NotFundedAddressData{ { Address: seq.Address, Denom: consts.Denoms.Hub, - CurrentBalance: seq.Balance, - RequiredBalance: &requiredBalance, + CurrentBalance: seq.Balance.Amount, + RequiredBalance: requiredBalance, KeyName: consts.KeysIds.HubSequencer, }, }, nil diff --git a/cmd/utils/fetch_accounts_data.go b/cmd/utils/fetch_accounts_data.go index 17c5bb9f..3c93ffc9 100644 --- a/cmd/utils/fetch_accounts_data.go +++ b/cmd/utils/fetch_accounts_data.go @@ -68,6 +68,7 @@ func GetSequencerData(cfg config.RollappConfig) ([]AccountData, error) { if err != nil { return nil, err } + sequencerBalance, err := QueryBalance(ChainQueryConfig{ Binary: consts.Executables.Dymension, Denom: consts.Denoms.Hub, diff --git a/data_layer/celestia/celestia.go b/data_layer/celestia/celestia.go index 8098280f..e2671f73 100644 --- a/data_layer/celestia/celestia.go +++ b/data_layer/celestia/celestia.go @@ -89,10 +89,10 @@ func (c *Celestia) CheckDABalance() ([]utils.NotFundedAddressData, error) { return nil, err } var insufficientBalances []utils.NotFundedAddressData - if accData.Balance.Cmp(lcMinBalance) < 0 { + if accData.Balance.Amount.Cmp(lcMinBalance) < 0 { insufficientBalances = append(insufficientBalances, utils.NotFundedAddressData{ Address: accData.Address, - CurrentBalance: accData.Balance, + CurrentBalance: accData.Balance.Amount, RequiredBalance: lcMinBalance, KeyName: consts.KeysIds.DALightNode, Denom: consts.Denoms.Celestia, diff --git a/data_layer/damock/damock.go b/data_layer/damock/damock.go index 16737e85..199d8b98 100644 --- a/data_layer/damock/damock.go +++ b/data_layer/damock/damock.go @@ -36,7 +36,10 @@ func (d *DAMock) GetDAAccData(c config.RollappConfig) ([]utils.AccountData, erro return []utils.AccountData{ { Address: "mockDA", - Balance: big.NewInt(999999999999999), + Balance: utils.Balance{ + Denom: "mock", + Amount: big.NewInt(999999999999999), + }, }, }, nil } diff --git a/utils/service_manager/service.go b/utils/service_manager/service.go index b02dc8ae..f95af150 100644 --- a/utils/service_manager/service.go +++ b/utils/service_manager/service.go @@ -57,7 +57,7 @@ func (s *ServiceConfig) FetchServicesData(cfg config.RollappConfig) { //FIXME: the status function should be part of the service for _, account := range accountData { - service.UIData.Status = activeIfSufficientBalance(account.Balance, big.NewInt(1)) + service.UIData.Status = activeIfSufficientBalance(account.Balance.Amount, big.NewInt(1)) } if k == "Relayer" { service.UIData.Status = "Starting..."