-
Notifications
You must be signed in to change notification settings - Fork 229
/
source.go
79 lines (65 loc) · 1.85 KB
/
source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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/v3/node/remote"
bankkeeper "github.com/forbole/bdjuno/v3/modules/bank/source"
"github.com/forbole/bdjuno/v3/types"
)
var (
_ bankkeeper.Source = &Source{}
)
type Source struct {
*remote.Source
bankClient banktypes.QueryClient
}
// NewSource builds a new Source instance
func NewSource(source *remote.Source, bankClient banktypes.QueryClient) *Source {
return &Source{
Source: source,
bankClient: bankClient,
}
}
// GetBalances implements bankkeeper.Source
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
}
// GetSupply implements bankkeeper.Source
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, // Query 100 supplies at time
},
})
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
}