package local
import (
"fmt"
"github.com/cosmos/cosmos-sdk/types/query"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/forbole/juno/v5/node/local"
"github.com/cybercongress/cyberindex/v2/modules/bank/source"
"github.com/forbole/callisto/v4/types"
)
var (
_ source.Source = &Source{}
)
type Source struct {
*local.Source
q banktypes.QueryServer
}
func NewSource(source *local.Source, bk banktypes.QueryServer) *Source {
return &Source{
Source: source,
q: bk,
}
}
func (s Source) GetBalances(addresses []string, height int64) ([]types.AccountBalance, error) {
ctx, err := s.LoadHeight(height)
if err != nil {
return nil, fmt.Errorf("error while loading height: %s", err)
}
var balances []types.AccountBalance
for _, address := range addresses {
res, err := s.q.AllBalances(sdk.WrapSDKContext(ctx), &banktypes.QueryAllBalancesRequest{Address: address})
if err != nil {
return nil, err
}
balances = append(balances, types.NewAccountBalance(address, res.Balances, height))
}
return balances, nil
}
func (s Source) GetSupply(height int64) (sdk.Coins, error) {
ctx, err := s.LoadHeight(height)
if err != nil {
return nil, fmt.Errorf("error while loading height: %s", err)
}
var coins []sdk.Coin
var nextKey []byte
var stop = false
for !stop {
res, err := s.q.TotalSupply(
sdk.WrapSDKContext(ctx),
&banktypes.QueryTotalSupplyRequest{
Pagination: &query.PageRequest{
Key: nextKey,
Limit: 100, },
})
if err != nil {
return nil, fmt.Errorf("error while getting total supply: %s", err)
}
nextKey = res.Pagination.NextKey
stop = len(res.Pagination.NextKey) == 0
coins = append(coins, res.Supply...)
}
return coins, nil
}
func (s Source) GetAccountBalance(address string, height int64) ([]sdk.Coin, error) {
ctx, err := s.LoadHeight(height)
if err != nil {
return nil, fmt.Errorf("error while loading height: %s", err)
}
balRes, err := s.q.AllBalances(sdk.WrapSDKContext(ctx), &banktypes.QueryAllBalancesRequest{Address: address})
if err != nil {
return nil, fmt.Errorf("error while getting all balances: %s", err)
}
return balRes.Balances, nil
}