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

Query osmo equivilent is staked via superfluid #1632

Merged
merged 16 commits into from
Jun 6, 2022
34 changes: 34 additions & 0 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@
- [SuperfluidDelegationsByValidatorDenomRequest](#osmosis.superfluid.SuperfluidDelegationsByValidatorDenomRequest)
- [SuperfluidDelegationsByValidatorDenomResponse](#osmosis.superfluid.SuperfluidDelegationsByValidatorDenomResponse)
- [SuperfluidIntermediaryAccountInfo](#osmosis.superfluid.SuperfluidIntermediaryAccountInfo)
- [SuperfluidOSMODelegationsByDelegatorRequest](#osmosis.superfluid.SuperfluidOSMODelegationsByDelegatorRequest)
- [SuperfluidOSMODelegationsByDelegatorResponse](#osmosis.superfluid.SuperfluidOSMODelegationsByDelegatorResponse)
- [SuperfluidUndelegationsByDelegatorRequest](#osmosis.superfluid.SuperfluidUndelegationsByDelegatorRequest)
- [SuperfluidUndelegationsByDelegatorResponse](#osmosis.superfluid.SuperfluidUndelegationsByDelegatorResponse)
- [TotalSuperfluidDelegationsRequest](#osmosis.superfluid.TotalSuperfluidDelegationsRequest)
Expand Down Expand Up @@ -3746,6 +3748,37 @@ assets



<a name="osmosis.superfluid.SuperfluidOSMODelegationsByDelegatorRequest"></a>

### SuperfluidOSMODelegationsByDelegatorRequest



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `delegator_address` | [string](#string) | | |






<a name="osmosis.superfluid.SuperfluidOSMODelegationsByDelegatorResponse"></a>

### SuperfluidOSMODelegationsByDelegatorResponse



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `superfluid_delegation_records` | [SuperfluidDelegationRecord](#osmosis.superfluid.SuperfluidDelegationRecord) | repeated | |
| `total_delegated_osmo` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | |






<a name="osmosis.superfluid.SuperfluidUndelegationsByDelegatorRequest"></a>

### SuperfluidUndelegationsByDelegatorRequest
Expand Down Expand Up @@ -3826,6 +3859,7 @@ Query defines the gRPC querier service.
| `TotalSuperfluidDelegations` | [TotalSuperfluidDelegationsRequest](#osmosis.superfluid.TotalSuperfluidDelegationsRequest) | [TotalSuperfluidDelegationsResponse](#osmosis.superfluid.TotalSuperfluidDelegationsResponse) | Returns the total amount of osmo superfluidly staked response denominated in uosmo | GET|/osmosis/superfluid/v1beta1/all_superfluid_delegations|
| `SuperfluidDelegationAmount` | [SuperfluidDelegationAmountRequest](#osmosis.superfluid.SuperfluidDelegationAmountRequest) | [SuperfluidDelegationAmountResponse](#osmosis.superfluid.SuperfluidDelegationAmountResponse) | Returns the coins superfluid delegated for a delegator, validator, denom triplet | GET|/osmosis/superfluid/v1beta1/superfluid_delegation_amount|
| `SuperfluidDelegationsByDelegator` | [SuperfluidDelegationsByDelegatorRequest](#osmosis.superfluid.SuperfluidDelegationsByDelegatorRequest) | [SuperfluidDelegationsByDelegatorResponse](#osmosis.superfluid.SuperfluidDelegationsByDelegatorResponse) | Returns all the superfluid poistions for a specific delegator | GET|/osmosis/superfluid/v1beta1/superfluid_delegations/{delegator_address}|
| `SuperfluidOSMODelegationsByDelegator` | [SuperfluidOSMODelegationsByDelegatorRequest](#osmosis.superfluid.SuperfluidOSMODelegationsByDelegatorRequest) | [SuperfluidOSMODelegationsByDelegatorResponse](#osmosis.superfluid.SuperfluidOSMODelegationsByDelegatorResponse) | Returns all the superfluid poistions for a specific delegator | GET|/osmosis/superfluid/v1beta1/superfluid_osmo_delegations/{delegator_address}|
| `SuperfluidUndelegationsByDelegator` | [SuperfluidUndelegationsByDelegatorRequest](#osmosis.superfluid.SuperfluidUndelegationsByDelegatorRequest) | [SuperfluidUndelegationsByDelegatorResponse](#osmosis.superfluid.SuperfluidUndelegationsByDelegatorResponse) | | GET|/osmosis/superfluid/v1beta1/superfluid_undelegations_by_delegator/{delegator_address}|
| `SuperfluidDelegationsByValidatorDenom` | [SuperfluidDelegationsByValidatorDenomRequest](#osmosis.superfluid.SuperfluidDelegationsByValidatorDenomRequest) | [SuperfluidDelegationsByValidatorDenomResponse](#osmosis.superfluid.SuperfluidDelegationsByValidatorDenomResponse) | Returns all the superfluid positions of a specific denom delegated to one validator | GET|/osmosis/superfluid/v1beta1/superfluid_delegations_by_validator_denom|
| `EstimateSuperfluidDelegatedAmountByValidatorDenom` | [EstimateSuperfluidDelegatedAmountByValidatorDenomRequest](#osmosis.superfluid.EstimateSuperfluidDelegatedAmountByValidatorDenomRequest) | [EstimateSuperfluidDelegatedAmountByValidatorDenomResponse](#osmosis.superfluid.EstimateSuperfluidDelegatedAmountByValidatorDenomResponse) | Returns the amount of a specific denom delegated to a specific validator This is labeled an estimate, because the way it calculates the amount can lead rounding errors from the true delegated amount | GET|/osmosis/superfluid/v1beta1/estimate_superfluid_delegation_amount_by_validator_denom|
Expand Down
22 changes: 22 additions & 0 deletions proto/osmosis/superfluid/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ service Query {
"superfluid_delegations/{delegator_address}";
}

// Returns all the superfluid poistions for a specific delegator
rpc SuperfluidOSMODelegationsByDelegator(
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a mouthful haha. Any chance we can think of more abbreviated nomenclature.

Copy link
Contributor Author

@hieuvubk hieuvubk Jun 1, 2022

Choose a reason for hiding this comment

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

heee we can think of better names. How do u think about the logic

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm trying to understand what adding OSMO to the query means? I read the original issue and I don't understand why we can't augment the current query (SuperfluidDelegationsByDelegator)?

Copy link
Contributor Author

@hieuvubk hieuvubk Jun 1, 2022

Choose a reason for hiding this comment

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

@mattverse want to create a separate query (as i understant). Refer to #1539 (review)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should change to SuperfluidEquivilentDelegationsByDelegator?

SuperfluidOSMODelegationsByDelegatorRequest)
returns (SuperfluidOSMODelegationsByDelegatorResponse) {
option (google.api.http).get =
"/osmosis/superfluid/v1beta1/"
"superfluid_osmo_delegations/{delegator_address}";
}

rpc SuperfluidUndelegationsByDelegator(
SuperfluidUndelegationsByDelegatorRequest)
returns (SuperfluidUndelegationsByDelegatorResponse) {
Expand Down Expand Up @@ -189,6 +198,19 @@ message SuperfluidDelegationsByDelegatorResponse {
];
}

message SuperfluidOSMODelegationsByDelegatorRequest {
string delegator_address = 1;
}

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

message SuperfluidUndelegationsByDelegatorRequest {
string delegator_address = 1;
string denom = 2;
Expand Down
30 changes: 30 additions & 0 deletions x/superfluid/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func GetQueryCmd() *cobra.Command {
GetCmdSuperfluidDelegationsByDelegator(),
GetCmdSuperfluidUndelegationsByDelegator(),
GetCmdTotalSuperfluidDelegations(),
GetCmdSuperfluidOSMODelegationsByDelegator(),
)

return cmd
Expand Down Expand Up @@ -294,6 +295,35 @@ func GetCmdSuperfluidDelegationsByDelegator() *cobra.Command {
return cmd
}

// GetCmdSuperfluidDelegationsByDelegator returns osmo equivalent is staked via superfluid poistions for a specific delegator.
func GetCmdSuperfluidOSMODelegationsByDelegator() *cobra.Command {
cmd := &cobra.Command{
Use: "superfluid-osmo-delegation-by-delegator [delegator_address]",
Short: "Query osmo equivalent is staked via superfluid 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.SuperfluidOSMODelegationsByDelegator(cmd.Context(), &types.SuperfluidOSMODelegationsByDelegatorRequest{
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{
Expand Down
63 changes: 63 additions & 0 deletions x/superfluid/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

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

lockuptypes "github.com/osmosis-labs/osmosis/v7/x/lockup/types"
"github.com/osmosis-labs/osmosis/v7/x/superfluid/types"
)
Expand Down Expand Up @@ -229,6 +231,67 @@ func (q Querier) SuperfluidDelegationsByDelegator(goCtx context.Context, req *ty
return &res, nil
}

// SuperfluidOSMODelegationsByDelegator returns all osmo equivalent is staked via superfluid poistions for a specific delegator.
func (q Querier) SuperfluidOSMODelegationsByDelegator(goCtx context.Context, req *types.SuperfluidOSMODelegationsByDelegatorRequest) (*types.SuperfluidOSMODelegationsByDelegatorResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if len(req.DelegatorAddress) == 0 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "empty delegator address")
}

ctx := sdk.UnwrapSDKContext(goCtx)

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

res := types.SuperfluidOSMODelegationsByDelegatorResponse{
SuperfluidDelegationRecords: []types.SuperfluidDelegationRecord{},
TotalDelegatedOsmo: sdk.NewCoin(appparams.BaseCoinUnit, sdk.ZeroInt()),
}

syntheticLocks := q.Keeper.lk.GetAllSyntheticLockupsByAddr(ctx, delAddr)

for _, syntheticLock := range syntheticLocks {
// don't include unbonding delegations
if strings.Contains(syntheticLock.SynthDenom, "superunbonding") {
continue
}

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

lockedDenom := periodLock.Coins.GetDenomByIndex(0)
lockedAmount := periodLock.GetCoins().AmountOf(lockedDenom)

// lockedCoins := sdk.NewCoin(baseDenom, periodLock.GetCoins().AmountOf(baseDenom))
valAddr, err := ValidatorAddressFromSyntheticDenom(syntheticLock.SynthDenom)
if err != nil {
return nil, err
}

// Find how many new osmo tokens this delegation is worth at superfluids current risk adjustment
// and twap of the denom.
osmoAmount := q.Keeper.GetSuperfluidOSMOTokens(ctx, lockedDenom, lockedAmount)
coin := sdk.NewCoin(appparams.BaseCoinUnit, osmoAmount)

res.SuperfluidDelegationRecords = append(res.SuperfluidDelegationRecords,
types.SuperfluidDelegationRecord{
DelegatorAddress: req.DelegatorAddress,
ValidatorAddress: valAddr,
DelegationAmount: coin,
},
)
res.TotalDelegatedOsmo = res.TotalDelegatedOsmo.Add(coin)
}

return &res, nil
}

// SuperfluidUndelegationsByDelegator returns total amount undelegating by delegator.
func (q Querier) SuperfluidUndelegationsByDelegator(goCtx context.Context, req *types.SuperfluidUndelegationsByDelegatorRequest) (*types.SuperfluidUndelegationsByDelegatorResponse, error) {
if req == nil {
Expand Down
18 changes: 18 additions & 0 deletions x/superfluid/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ func (suite *KeeperTestSuite) TestGRPCQuerySuperfluidDelegations() {
)))
}

// for each delegator, query all their superfluid osmo delegations and make sure they have 2 delegations
for _, delegator := range delAddrs {
res, err := suite.queryClient.SuperfluidOSMODelegationsByDelegator(sdk.WrapSDKContext(suite.Ctx), &types.SuperfluidOSMODelegationsByDelegatorRequest{
DelegatorAddress: delegator.String(),
})
multiplier0 := suite.querier.Keeper.GetOsmoEquivalentMultiplier(suite.Ctx, denoms[0])
multiplier1 := suite.querier.Keeper.GetOsmoEquivalentMultiplier(suite.Ctx, denoms[1])
minRiskFactor := suite.querier.Keeper.GetParams(suite.Ctx).MinimumRiskFactor

expectAmount0 := multiplier0.Mul(sdk.NewDec(1000000)).Sub(multiplier0.Mul(sdk.NewDec(1000000)).Mul(minRiskFactor))
expectAmount1 := multiplier1.Mul(sdk.NewDec(1000000)).Sub(multiplier1.Mul(sdk.NewDec(1000000)).Mul(minRiskFactor))

suite.Require().NoError(err)
suite.Require().Len(res.SuperfluidDelegationRecords, 2)
suite.Require().True(res.SuperfluidDelegationRecords[0].DelegationAmount.IsEqual(sdk.NewCoin("uosmo", expectAmount0.RoundInt())))
suite.Require().True(res.SuperfluidDelegationRecords[1].DelegationAmount.IsEqual(sdk.NewCoin("uosmo", expectAmount1.RoundInt())))
}

// for each validator denom pair, make sure they have 1 delegations
for _, validator := range valAddrs {
for _, denom := range denoms {
Expand Down
Loading