Skip to content

Commit

Permalink
feat: fix balance printing in roller run (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsitrin authored Jul 5, 2023
1 parent 9e27d18 commit 969670b
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 32 deletions.
4 changes: 2 additions & 2 deletions cmd/register/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/relayer/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand All @@ -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,
})
Expand Down
2 changes: 1 addition & 1 deletion cmd/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)

Expand Down
2 changes: 1 addition & 1 deletion cmd/sequencer/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
54 changes: 34 additions & 20 deletions cmd/utils/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions cmd/utils/fetch_accounts_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions data_layer/celestia/celestia.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion data_layer/damock/damock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion utils/service_manager/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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..."
Expand Down

0 comments on commit 969670b

Please sign in to comment.