diff --git a/x/superfluid/keeper/grpc_query.go b/x/superfluid/keeper/grpc_query.go index 9dc927f5b12..0cb528d490b 100644 --- a/x/superfluid/keeper/grpc_query.go +++ b/x/superfluid/keeper/grpc_query.go @@ -8,11 +8,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" ) @@ -86,22 +90,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 } diff --git a/x/superfluid/keeper/grpc_query_test.go b/x/superfluid/keeper/grpc_query_test.go index 8ed9a25e9a5..5ad4ea01a9c 100644 --- a/x/superfluid/keeper/grpc_query_test.go +++ b/x/superfluid/keeper/grpc_query_test.go @@ -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