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

Add grpc option IncludePoolData to QueryUserDeposits #390

Merged
merged 9 commits into from
Feb 16, 2024
Merged
83,374 changes: 19,498 additions & 63,876 deletions docs/static/openapi.yml

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions proto/neutron/dex/deposit_record.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package neutron.dex;

import "gogoproto/gogo.proto";
import "neutron/dex/pair_id.proto";
import "neutron/dex/pool.proto";

option go_package = "github.com/neutron-org/neutron/v2/x/dex/types";

Expand All @@ -18,4 +19,11 @@ message DepositRecord {
int64 lower_tick_index = 4;
int64 upper_tick_index = 5;
uint64 fee = 6;
string total_shares = 7 [
(gogoproto.moretags) = "yaml:\"total_shares\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
Copy link
Contributor

Choose a reason for hiding this comment

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

github.com/cosmos/cosmos-sdk/types.Int is deprecated and it is an alias to cosmossdk.io/math.Int. But it's not necessary to fix it right now. We want to move sometime, but in standalone task

(gogoproto.nullable) = true,
(gogoproto.jsontag) = "total_shares"
];
Pool pool = 8 [(gogoproto.nullable) = true];
}
1 change: 1 addition & 0 deletions proto/neutron/dex/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ message QueryAllLimitOrderTrancheResponse {
message QueryAllUserDepositsRequest {
string address = 1;
cosmos.base.query.v1beta1.PageRequest pagination = 2;
bool include_pool_data = 3;
}

message QueryAllUserDepositsResponse {
Expand Down
1 change: 0 additions & 1 deletion wasmbinding/message_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,6 @@ func (m *CustomMessenger) resubmitFailure(ctx sdk.Context, contractAddr sdk.AccA
}

err = m.ContractmanagerKeeper.ResubmitFailure(ctx, contractAddr, failure)

if err != nil {
ctx.Logger().Error("failed to resubmitFailure",
"from_address", contractAddr.String(),
Expand Down
5 changes: 2 additions & 3 deletions x/contractmanager/types/failure.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions x/contractmanager/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions x/contractmanager/types/params.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions x/contractmanager/types/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions x/contractmanager/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions x/contractmanager/types/v1/failure.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions x/cron/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions x/cron/types/params.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions x/cron/types/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions x/cron/types/schedule.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions x/cron/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion x/dex/client/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ package cli
import flag "github.com/spf13/pflag"

const (
FlagMaxAmountOut = "max-amount-out"
FlagMaxAmountOut = "max-amount-out"
FlagIncludePoolData = "include-pool-data"
)

func FlagSetMaxAmountOut() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.String(FlagMaxAmountOut, "", "Max amount to be returned from trade")
return fs
}

func FlagSetIncludePoolData() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.Bool(FlagIncludePoolData, false, "Include pool data with response")
return fs
}
13 changes: 10 additions & 3 deletions x/dex/client/cli/query_user_deposits.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

func CmdListUserDeposits() *cobra.Command {
cmd := &cobra.Command{
Use: "list-user-deposits [address]",
Use: "list-user-deposits [address] ?(--include-pool-data)",
Short: "list all users deposits",
Example: "list-user-deposits alice",
Example: "list-user-deposits alice --include-pool-data",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
reqAddress := args[0]
Expand All @@ -24,8 +24,14 @@ func CmdListUserDeposits() *cobra.Command {

queryClient := types.NewQueryClient(clientCtx)

includePoolData, err := cmd.Flags().GetBool(FlagIncludePoolData)
if err != nil {
return err
}

params := &types.QueryAllUserDepositsRequest{
Address: reqAddress,
Address: reqAddress,
IncludePoolData: includePoolData,
}

res, err := queryClient.UserDepositsAll(cmd.Context(), params)
Expand All @@ -39,6 +45,7 @@ func CmdListUserDeposits() *cobra.Command {

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, cmd.Use)
cmd.Flags().AddFlagSet(FlagSetIncludePoolData())

return cmd
}
17 changes: 17 additions & 0 deletions x/dex/keeper/grpc_query_user_deposits.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func (k Keeper) UserDepositsAll(
Fee: poolMetadata.Fee,
}

if req.IncludePoolData {
k.addPoolData(ctx, depositRecord)
}

depositArr = append(depositArr, depositRecord)
}

Expand All @@ -70,3 +74,16 @@ func (k Keeper) UserDepositsAll(
Pagination: pageRes,
}, nil
}

func (k Keeper) addPoolData(ctx sdk.Context, record *types.DepositRecord) *types.DepositRecord {
pool, found := k.GetPool(ctx, record.PairId, record.CenterTickIndex, record.Fee)
if !found {
panic("Pool does not exist")
}

record.Pool = pool
supply := k.bankKeeper.GetSupply(ctx, pool.GetPoolDenom())
record.TotalShares = &supply.Amount

return record
}
46 changes: 42 additions & 4 deletions x/dex/keeper/grpc_query_user_deposits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ import (
"github.com/neutron-org/neutron/v2/x/dex/types"
)

func simulateDeposit(ctx sdk.Context, app *neutronapp.App, addr sdk.AccAddress, deposit *types.DepositRecord) {
// NOTE: For simplicyt sake, we are not actually doing a deposit, we are just initializing
func simulateDeposit(ctx sdk.Context, app *neutronapp.App, addr sdk.AccAddress, deposit *types.DepositRecord) *types.Pool {
// NOTE: For simplicity sake, we are not actually doing a deposit, we are just initializing
// the pool and adding the poolDenom to the users account
pool, err := app.DexKeeper.InitPool(ctx, deposit.PairId, deposit.CenterTickIndex, deposit.Fee)
if err != nil {
panic("Cannot init pool")
}
coin := sdk.NewCoin(pool.GetPoolDenom(), deposit.SharesOwned)
keepertest.FundAccount(app.BankKeeper, ctx, addr, sdk.Coins{coin})

return pool
}

func TestUserDepositsAllQueryPaginated(t *testing.T) {
Expand Down Expand Up @@ -76,9 +78,11 @@ func TestUserDepositsAllQueryPaginated(t *testing.T) {
Fee: 1,
},
}

poolsFromDeposits := make([]*types.Pool, 0)
for _, d := range msgs {
simulateDeposit(ctx, app.(*neutronapp.App), addr, d)
pool := simulateDeposit(ctx, app.(*neutronapp.App), addr, d)
poolsFromDeposits = append(poolsFromDeposits, pool)

}

// Add random noise to make sure only pool denoms are picked up
Expand Down Expand Up @@ -138,6 +142,40 @@ func TestUserDepositsAllQueryPaginated(t *testing.T) {
resp.Deposits,
)
})
t.Run("WithPoolData", func(t *testing.T) {
req := request(nil, 0, 2, false)
req.IncludePoolData = true
resp, err := keeper.UserDepositsAll(wctx, req)
require.NoError(t, err)
require.Equal(t, len(resp.Deposits), 2)
expectedShares := math.NewInt(10)
expectedRespWithPoolData := []*types.DepositRecord{
{
PairId: defaultPairID,
SharesOwned: math.NewInt(10),
CenterTickIndex: 2,
LowerTickIndex: 1,
UpperTickIndex: 3,
Fee: 1,
Pool: poolsFromDeposits[0],
TotalShares: &expectedShares,
},
{
PairId: defaultPairID,
SharesOwned: math.NewInt(10),
CenterTickIndex: 3,
LowerTickIndex: 2,
UpperTickIndex: 4,
Fee: 1,
Pool: poolsFromDeposits[1],
TotalShares: &expectedShares,
},
}
require.ElementsMatch(t,
expectedRespWithPoolData,
resp.Deposits,
)
})
t.Run("InvalidRequest", func(t *testing.T) {
_, err := keeper.UserDepositsAll(wctx, nil)
require.ErrorIs(t, err, status.Error(codes.InvalidArgument, "invalid request"))
Expand Down
Loading
Loading