Skip to content

Commit

Permalink
use []types.Account as GetBalances() input
Browse files Browse the repository at this point in the history
  • Loading branch information
huichiaotsou committed Dec 20, 2022
1 parent 7e49b46 commit ce900d6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
8 changes: 4 additions & 4 deletions modules/bank/source/local/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,26 @@ func NewSource(source *local.Source, bk banktypes.QueryServer) *Source {
}

// GetBalances implements bankkeeper.Source
func (s Source) GetBalances(addresses []string, height int64) ([]types.NativeTokenAmount, error) {
func (s Source) GetBalances(accounts []types.Account, height int64) ([]types.NativeTokenAmount, error) {
ctx, err := s.LoadHeight(height)
if err != nil {
return nil, fmt.Errorf("error while loading height: %s", err)
}

var balances []types.NativeTokenAmount
for _, address := range addresses {
for _, a := range accounts {
balRes, err := s.q.Balance(
sdk.WrapSDKContext(ctx),
&banktypes.QueryBalanceRequest{
Address: address,
Address: a.Address,
Denom: pricefeed.GetDenom(),
})
if err != nil {
return nil, fmt.Errorf("error while getting all balances: %s", err)
}

balances = append(balances, types.NewNativeTokenAmount(
address,
a.Address,
balRes.Balance.Amount,
height,
))
Expand Down
8 changes: 4 additions & 4 deletions modules/bank/source/remote/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ func NewSource(source *remote.Source, bankClient banktypes.QueryClient) *Source
}

// GetBalances implements bankkeeper.Source
func (s Source) GetBalances(addresses []string, height int64) ([]types.NativeTokenAmount, error) {
func (s Source) GetBalances(accounts []types.Account, height int64) ([]types.NativeTokenAmount, error) {
ctx := remote.GetHeightRequestContext(s.Ctx, height)

var balances []types.NativeTokenAmount
for _, address := range addresses {
for _, a := range accounts {
balRes, err := s.bankClient.Balance(ctx, &banktypes.QueryBalanceRequest{
Address: address,
Address: a.Address,
Denom: pricefeed.GetDenom(),
})
if err != nil {
return nil, fmt.Errorf("error while getting all balances: %s", err)
}

balances = append(balances, types.NewNativeTokenAmount(
address,
a.Address,
balRes.Balance.Amount,
height,
))
Expand Down
2 changes: 1 addition & 1 deletion modules/bank/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

type Source interface {
GetBalances(addresses []string, height int64) ([]types.NativeTokenAmount, error)
GetBalances(accounts []types.Account, height int64) ([]types.NativeTokenAmount, error)
GetSupply(height int64) (sdk.Coins, error)

// -- For hasura action --
Expand Down
5 changes: 3 additions & 2 deletions modules/bank/utils_update_balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package bank
import (
"fmt"

"github.com/forbole/bdjuno/v3/types"
"github.com/rs/zerolog/log"
)

// UpdateBalances updates the balances of the accounts having the given addresses,
// taking the data at the provided height
func (m *Module) UpdateBalances(addresses []string, height int64) error {
func (m *Module) UpdateBalances(accounts []types.Account, height int64) error {
log.Debug().Str("module", "bank").Int64("height", height).Msg("updating balances")

balances, err := m.keeper.GetBalances(addresses, height)
balances, err := m.keeper.GetBalances(accounts, height)
if err != nil {
return fmt.Errorf("error while getting account balances: %s", err)
}
Expand Down

0 comments on commit ce900d6

Please sign in to comment.