Skip to content

Commit

Permalink
fixed panic when no balance found
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsitrin committed Jul 4, 2023
1 parent 1906634 commit 1246f17
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
22 changes: 12 additions & 10 deletions cmd/utils/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,25 @@ type ChainQueryConfig struct {
Binary string
}

func QueryBalance(chainConfig ChainQueryConfig, address string) (*Balance, 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) (*Balance, 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
}

balance := Balance{
Denom: denom,
Amount: big.NewInt(0),
}
for _, resbalance := range balanceResp.Balances {
if resbalance.Denom != denom {
Expand All @@ -39,14 +44,11 @@ func ParseBalanceFromResponse(out bytes.Buffer, denom string) (*Balance, error)
amount := new(big.Int)
_, ok := amount.SetString(resbalance.Amount, 10)
if !ok {
return nil, errors.New("unable to convert balance amount to big.Int")
return Balance{}, errors.New("unable to convert balance amount to big.Int")
}
return &Balance{
Denom: denom,
Amount: amount,
}, nil
balance.Amount = amount
}
return nil, nil
return balance, nil
}

type Balance struct {
Expand Down
7 changes: 4 additions & 3 deletions cmd/utils/fetch_accounts_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func GetRolRlyAccData(cfg config.RollappConfig) (*AccountData, error) {
}
return &AccountData{
Address: RollappRlyAddr,
Balance: *RollappRlyBalance,
Balance: RollappRlyBalance,
}, nil
}

Expand All @@ -56,7 +56,7 @@ func GetHubRlyAccData(cfg config.RollappConfig) (*AccountData, error) {
}
return &AccountData{
Address: HubRlyAddr,
Balance: *HubRlyBalance,
Balance: HubRlyBalance,
}, nil
}

Expand All @@ -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 All @@ -79,7 +80,7 @@ func GetSequencerData(cfg config.RollappConfig) ([]AccountData, error) {
return []AccountData{
{
Address: sequencerAddress,
Balance: *sequencerBalance,
Balance: sequencerBalance,
},
}, nil
}
2 changes: 1 addition & 1 deletion data_layer/celestia/celestia.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (c *Celestia) getDAAccData(config.RollappConfig) (*utils.AccountData, error
}
return &utils.AccountData{
Address: celAddress,
Balance: *balance,
Balance: balance,
}, nil
}

Expand Down

0 comments on commit 1246f17

Please sign in to comment.