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

Dong/pagination for all intermediary accounts #4752

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
47 changes: 37 additions & 10 deletions x/superfluid/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}

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)
Comment on lines +36 to +39
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also check the returned values as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tested the returned values including ValAddr it is the same address
image

}
func (suite *KeeperTestSuite) TestTotalDelegationByValidatorForAsset() {
suite.SetupTest()
ctx := suite.Ctx
Expand Down