Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: save all coins from total supply query #410

Merged
merged 2 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#### Hasura
- ([\#395](https://github.com/forbole/bdjuno/pull/395)) Remove time label from Hasura Prometheus monitoring

#### Bank module
- ([\#410](https://github.com/forbole/bdjuno/pull/410)) Change total supply query from only 1 page to all pages

## Version v3.0.1
### Dependencies
Expand Down
27 changes: 22 additions & 5 deletions modules/bank/source/local/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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"
Expand Down Expand Up @@ -50,19 +51,35 @@ func (s Source) GetBalances(addresses []string, height int64) ([]types.AccountBa
return balances, nil
}

// GetSupply implements keeper.Source
// GetSupply implements bankkeeper.Source
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)
}

res, err := s.q.TotalSupply(sdk.WrapSDKContext(ctx), &banktypes.QueryTotalSupplyRequest{})
if err != nil {
return nil, 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, // 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 res.Supply, nil
return coins, nil
}

// GetAccountBalances implements bankkeeper.Source
Expand Down
27 changes: 23 additions & 4 deletions modules/bank/source/remote/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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"

Expand Down Expand Up @@ -51,10 +52,28 @@ func (s Source) GetBalances(addresses []string, height int64) ([]types.AccountBa

// GetSupply implements bankkeeper.Source
func (s Source) GetSupply(height int64) (sdk.Coins, error) {
res, err := s.bankClient.TotalSupply(remote.GetHeightRequestContext(s.Ctx, height), &banktypes.QueryTotalSupplyRequest{})
if err != nil {
return nil, fmt.Errorf("error while getting total supply: %s", err)
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 res.Supply, nil
return coins, nil
}