package remote
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/forbole/juno/v5/node/remote"
bankkeeper "github.com/forbole/callisto/v4/modules/bank/source"
"github.com/forbole/callisto/v4/types"
)
var (
_ bankkeeper.Source = &Source{}
)
type Source struct {
*remote.Source
bankClient banktypes.QueryClient
}
func NewSource(source *remote.Source, bankClient banktypes.QueryClient) *Source {
return &Source{
Source: source,
bankClient: bankClient,
}
}
func (s Source) GetBalances(addresses []string, height int64) ([]types.AccountBalance, error) {
ctx := remote.GetHeightRequestContext(s.Ctx, height)
var balances []types.AccountBalance
for _, address := range addresses {
balRes, err := s.bankClient.AllBalances(ctx, &banktypes.QueryAllBalancesRequest{Address: address})
if err != nil {
return nil, fmt.Errorf("error while getting all balances: %s", err)
}
balances = append(balances, types.NewAccountBalance(
address,
balRes.Balances,
height,
))
}
return balances, nil
}
func (s Source) GetSupply(height int64) (sdk.Coins, error) {
ctx := remote.GetHeightRequestContext(s.Ctx, height)
var coins []sdk.Coin
var nextKey []byte
var stop = false
for !stop {
res, err := s.bankClient.TotalSupply(
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
}