Skip to content

Commit

Permalink
Add query for superfluid undelegations per delegator (osmosis-labs#975)
Browse files Browse the repository at this point in the history
* Add query for superfluid undelegations per denom

* Add superfluid undelegation query

* Add cli

* Fix cli

* Remove denom specifier in url

* fix typo :(

* Fix lint

* Re-generate protofiles due to FUD

Co-authored-by: ValarDragon <[email protected]>
  • Loading branch information
mattverse and ValarDragon authored Feb 24, 2022
1 parent 794926a commit a319664
Show file tree
Hide file tree
Showing 5 changed files with 881 additions and 157 deletions.
27 changes: 24 additions & 3 deletions proto/osmosis/superfluid/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ service Query {

// Returns superfluid asset type
rpc AssetType(AssetTypeRequest) returns (AssetTypeResponse) {
option (google.api.http).get =
"/osmosis/superfluid/v1beta1/asset_type";
option (google.api.http).get = "/osmosis/superfluid/v1beta1/asset_type";
}
// Returns all superfluid asset types
rpc AllAssets(AllAssetsRequest) returns (AllAssetsResponse) {
Expand All @@ -32,7 +31,7 @@ service Query {
rpc AssetMultiplier(AssetMultiplierRequest)
returns (AssetMultiplierResponse) {
option (google.api.http).get =
"/osmosis/superfluid/v1beta1/asset_multiplier/{denom}";
"/osmosis/superfluid/v1beta1/asset_multiplier";
}
// Returns all superfluid intermediary account
rpc AllIntermediaryAccounts(AllIntermediaryAccountsRequest)
Expand Down Expand Up @@ -70,6 +69,14 @@ service Query {
"superfluid_delegations/{delegator_address}";
}

rpc SuperfluidUndelegationsByDelegator(
SuperfluidUndelegationsByDelegatorRequest)
returns (SuperfluidUndelegationsByDelegatorResponse) {
option (google.api.http).get =
"/osmosis/superfluid/v1beta1/"
"superfluid_undelegations_by_delegator/{delegator_address}";
}

// Returns all the superfluid positions of a specific denom delegated to one
// validator
rpc SuperfluidDelegationsByValidatorDenom(
Expand Down Expand Up @@ -181,6 +188,20 @@ message SuperfluidDelegationsByDelegatorResponse {
];
}

message SuperfluidUndelegationsByDelegatorRequest {
string delegator_address = 1;
string denom = 2;
}

message SuperfluidUndelegationsByDelegatorResponse {
repeated SuperfluidDelegationRecord superfluid_delegation_records = 1
[ (gogoproto.nullable) = false ];
repeated cosmos.base.v1beta1.Coin total_undelegated_coins = 2 [
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];
}

message SuperfluidDelegationsByValidatorDenomRequest {
string validator_address = 1;
string denom = 2;
Expand Down
62 changes: 62 additions & 0 deletions x/superfluid/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
GetCmdAllIntermediaryAccounts(),
GetCmdConnectedIntermediaryAccount(),
GetCmdSuperfluidDelegationAmount(),
GetCmdSuperfluidDelegationsByDelegator(),
GetCmdSuperfluidUndelegationsByDelegator(),
GetCmdTotalSuperfluidDelegations(),
)

Expand Down Expand Up @@ -229,6 +231,66 @@ func GetCmdSuperfluidDelegationAmount() *cobra.Command {
return cmd
}

// GetCmdSuperfluidDelegationsByDelegator returns the coins superfluid delegated for the specified delegator
func GetCmdSuperfluidDelegationsByDelegator() *cobra.Command {
cmd := &cobra.Command{
Use: "superfluid-delegation-by-delegator [delegator_address]",
Short: "Query coins superfluid delegated for the specified delegator",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.SuperfluidDelegationsByDelegator(cmd.Context(), &types.SuperfluidDelegationsByDelegatorRequest{
DelegatorAddress: args[0],
})

if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

// GetCmdSuperfluidUndelegationsByDelegator returns the coins superfluid undelegated for the specified delegator
func GetCmdSuperfluidUndelegationsByDelegator() *cobra.Command {
cmd := &cobra.Command{
Use: "superfluid-undelegation-by-delegator [delegator_address]",
Short: "Query coins superfluid undelegated for the specified delegator",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.SuperfluidUndelegationsByDelegator(cmd.Context(), &types.SuperfluidUndelegationsByDelegatorRequest{
DelegatorAddress: args[0],
})

if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

// GetCmdTotalSuperfluidDelegations returns total amount of base denom delegated via superfluid staking
func GetCmdTotalSuperfluidDelegations() *cobra.Command {
cmd := &cobra.Command{
Expand Down
44 changes: 44 additions & 0 deletions x/superfluid/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,50 @@ func (k Keeper) SuperfluidDelegationsByDelegator(goCtx context.Context, req *typ

}

// SuperfluidUndelegationsByDelegator returns total amount undelegating by delegator
func (k Keeper) SuperfluidUndelegationsByDelegator(goCtx context.Context, req *types.SuperfluidUndelegationsByDelegatorRequest) (*types.SuperfluidUndelegationsByDelegatorResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

delAddr, err := sdk.AccAddressFromBech32(req.DelegatorAddress)
if err != nil {
return nil, err
}

res := types.SuperfluidUndelegationsByDelegatorResponse{
SuperfluidDelegationRecords: []types.SuperfluidDelegationRecord{},
TotalUndelegatedCoins: sdk.NewCoins(),
}

syntheticLocks := k.lk.GetAllSyntheticLockupsByAddr(ctx, delAddr)

for _, syntheticLock := range syntheticLocks {
if strings.Contains(syntheticLock.SynthDenom, "superbonding") {
continue
}

periodLock, err := k.lk.GetLockByID(ctx, syntheticLock.UnderlyingLockId)
if err != nil {
return nil, err
}

baseDenom := periodLock.Coins.GetDenomByIndex(0)
lockedCoin := sdk.NewCoin(baseDenom, periodLock.GetCoins().AmountOf(baseDenom))
valAddr, err := ValidatorAddressFromSyntheticDenom(syntheticLock.SynthDenom)
if err != nil {
return nil, err
}
res.SuperfluidDelegationRecords = append(res.SuperfluidDelegationRecords,
types.SuperfluidDelegationRecord{
DelegatorAddress: req.DelegatorAddress,
ValidatorAddress: valAddr,
DelegationAmount: lockedCoin,
},
)
res.TotalUndelegatedCoins = res.TotalUndelegatedCoins.Add(lockedCoin)
}
return &res, nil
}

// SuperfluidDelegationsByValidatorDenom returns all the superfluid positions
// of a specific denom delegated to one validator
func (k Keeper) SuperfluidDelegationsByValidatorDenom(goCtx context.Context, req *types.SuperfluidDelegationsByValidatorDenomRequest) (*types.SuperfluidDelegationsByValidatorDenomResponse, error) {
Expand Down
Loading

0 comments on commit a319664

Please sign in to comment.