Skip to content

Commit

Permalink
Dong/pagination for all intermediary accounts (#4752)
Browse files Browse the repository at this point in the history
* Add pagination support for AllIntermediaryAccounts query in Superfluid Module

* lint

* merge main
  • Loading branch information
DongLieu authored May 9, 2023
1 parent 86f9c1e commit a186836
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
47 changes: 37 additions & 10 deletions x/superfluid/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/gogo/protobuf/proto"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

appparams "github.com/osmosis-labs/osmosis/v15/app/params"

"github.com/cosmos/cosmos-sdk/store/prefix"
"github.com/cosmos/cosmos-sdk/types/query"

lockuptypes "github.com/osmosis-labs/osmosis/v15/x/lockup/types"
"github.com/osmosis-labs/osmosis/v15/x/superfluid/types"
)
Expand Down Expand Up @@ -90,22 +94,45 @@ func (q Querier) AssetMultiplier(goCtx context.Context, req *types.AssetMultipli
}

// AllIntermediaryAccounts returns all superfluid intermediary accounts.
func (q Querier) AllIntermediaryAccounts(goCtx context.Context, _ *types.AllIntermediaryAccountsRequest) (*types.AllIntermediaryAccountsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
accounts := q.Keeper.GetAllIntermediaryAccounts(ctx)
func (q Querier) AllIntermediaryAccounts(goCtx context.Context, req *types.AllIntermediaryAccountsRequest) (*types.AllIntermediaryAccountsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
sdkCtx := sdk.UnwrapSDKContext(goCtx)
store := sdkCtx.KVStore(q.Keeper.storeKey)
accStore := prefix.NewStore(store, types.KeyPrefixIntermediaryAccount)
iterator := sdk.KVStorePrefixIterator(accStore, nil)
defer iterator.Close()

accInfos := []types.SuperfluidIntermediaryAccountInfo{}

for _, acc := range accounts {
accInfos = append(accInfos, types.SuperfluidIntermediaryAccountInfo{
Denom: acc.Denom,
ValAddr: acc.ValAddr,
GaugeId: acc.GaugeId,
Address: acc.GetAccAddress().String(),
pageRes, err := query.FilteredPaginate(accStore, req.Pagination,
func(key, value []byte, accumulate bool) (bool, error) {
account := types.SuperfluidIntermediaryAccount{}
err := proto.Unmarshal(iterator.Value(), &account)
if err != nil {
return false, err
}
iterator.Next()

accountInfo := types.SuperfluidIntermediaryAccountInfo{
Denom: account.Denom,
ValAddr: account.ValAddr,
GaugeId: account.GaugeId,
Address: account.GetAccAddress().String(),
}
if accumulate {
accInfos = append(accInfos, accountInfo)
}
return true, nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &types.AllIntermediaryAccountsResponse{
Accounts: accInfos,
Accounts: accInfos,
Pagination: pageRes,
}, nil
}

Expand Down
22 changes: 22 additions & 0 deletions x/superfluid/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@ func (suite *KeeperTestSuite) TestGRPCParams() {
suite.Require().True(res.Params.MinimumRiskFactor.Equal(types.DefaultParams().MinimumRiskFactor))
}

func (suite *KeeperTestSuite) TestAllIntermediaryAccounts() {
suite.SetupTest()
// set account 1
valAddr1 := sdk.ValAddress([]byte("test1-AllIntermediaryAccounts"))
acc1 := types.NewSuperfluidIntermediaryAccount("test1", valAddr1.String(), 0)
suite.App.SuperfluidKeeper.SetIntermediaryAccount(suite.Ctx, acc1)

// set account 2
valAddr2 := sdk.ValAddress([]byte("test2-AllIntermediaryAccounts"))
acc2 := types.NewSuperfluidIntermediaryAccount("test2", valAddr2.String(), 0)
suite.App.SuperfluidKeeper.SetIntermediaryAccount(suite.Ctx, acc2)

// set account 3
valAddr3 := sdk.ValAddress([]byte("test3-AllIntermediaryAccounts"))
acc3 := types.NewSuperfluidIntermediaryAccount("test3", valAddr3.String(), 0)
suite.App.SuperfluidKeeper.SetIntermediaryAccount(suite.Ctx, acc3)

res, err := suite.querier.AllIntermediaryAccounts(sdk.WrapSDKContext(suite.Ctx), &types.AllIntermediaryAccountsRequest{})
suite.Require().NoError(err)
suite.Require().Equal(3, len(res.Accounts))
suite.Require().Equal(uint64(3), res.Pagination.Total)
}
func (suite *KeeperTestSuite) TestTotalDelegationByValidatorForAsset() {
suite.SetupTest()
ctx := suite.Ctx
Expand Down

0 comments on commit a186836

Please sign in to comment.