Skip to content

Commit

Permalink
Query osmo equivilent is staked via superfluid (backport #1632) (#1676)
Browse files Browse the repository at this point in the history
* Query osmo equivilent is staked via superfluid (#1632)

* fix proto

* get osmo equivilent is staked via superfluid

* format

* fix go_fmt

* Revert "fix go_fmt"

This reverts commit 5407105.

* augment SuperfluidDelegationsByDelegator query

* remove fmt

* format

* fix spell

* fix spell, changelog

* query

(cherry picked from commit 19fedeb)

# Conflicts:
#	x/superfluid/keeper/grpc_query.go

* resolved

* fix lint

Co-authored-by: Hieu Vu <[email protected]>
  • Loading branch information
mergify[bot] and hieuvubk authored Jun 7, 2022
1 parent 9bb27a0 commit ddd75fe
Show file tree
Hide file tree
Showing 8 changed files with 301 additions and 154 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Cosmwasm bindings have been added, to make swapping and creating these new token
* Cosmwasm builds on M1 macs
* [#1435](https://github.com/osmosis-labs/osmosis/pull/1435) `x/tokenfactory` create denom fee for spam resistance
* [#1253](https://github.com/osmosis-labs/osmosis/pull/1253) Add a message to increase the duration of a bonded lock.
* [#1632](https://github.com/osmosis-labs/osmosis/pull/1632) augment SuperfluidDelegationsByDelegator query, return osmo equivilent is staked via superfluid

## [v8.0.0 - Emergency proposals upgrade](https://github.com/osmosis-labs/osmosis/releases/tag/v8.0.0)

Expand Down
2 changes: 2 additions & 0 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -3247,6 +3247,7 @@ and OSMO tokens for superfluid staking
| `delegator_address` | [string](#string) | | |
| `validator_address` | [string](#string) | | |
| `delegation_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | |
| `equivalent_staked_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | |



Expand Down Expand Up @@ -3691,6 +3692,7 @@ assets
| ----- | ---- | ----- | ----------- |
| `superfluid_delegation_records` | [SuperfluidDelegationRecord](#osmosis.superfluid.SuperfluidDelegationRecord) | repeated | |
| `total_delegated_coins` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | repeated | |
| `total_equivalent_staked_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | |



Expand Down
4 changes: 4 additions & 0 deletions proto/osmosis/superfluid/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ message SuperfluidDelegationsByDelegatorResponse {
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];
cosmos.base.v1beta1.Coin total_equivalent_staked_amount = 3 [
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coin"
];
}

message SuperfluidUndelegationsByDelegatorRequest {
Expand Down
2 changes: 2 additions & 0 deletions proto/osmosis/superfluid/superfluid.proto
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ message SuperfluidDelegationRecord {
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coin"
];
cosmos.base.v1beta1.Coin equivalent_staked_amount = 4
[ (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coin" ];
}

message LockIdIntermediaryAccountConnection {
Expand Down
16 changes: 13 additions & 3 deletions x/superfluid/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

appparams "github.com/osmosis-labs/osmosis/v9/app/params"
lockuptypes "github.com/osmosis-labs/osmosis/v9/x/lockup/types"
"github.com/osmosis-labs/osmosis/v9/x/superfluid/types"
)
Expand Down Expand Up @@ -195,6 +196,7 @@ func (q Querier) SuperfluidDelegationsByDelegator(goCtx context.Context, req *ty
res := types.SuperfluidDelegationsByDelegatorResponse{
SuperfluidDelegationRecords: []types.SuperfluidDelegationRecord{},
TotalDelegatedCoins: sdk.NewCoins(),
TotalEquivalentStakedAmount: sdk.NewCoin(appparams.BaseCoinUnit, sdk.ZeroInt()),
}

syntheticLocks := q.Keeper.lk.GetAllSyntheticLockupsByAddr(ctx, delAddr)
Expand All @@ -213,17 +215,25 @@ func (q Querier) SuperfluidDelegationsByDelegator(goCtx context.Context, req *ty
baseDenom := periodLock.Coins.GetDenomByIndex(0)
lockedCoins := sdk.NewCoin(baseDenom, periodLock.GetCoins().AmountOf(baseDenom))
valAddr, err := ValidatorAddressFromSyntheticDenom(syntheticLock.SynthDenom)

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

if err != nil {
return nil, err
}
res.SuperfluidDelegationRecords = append(res.SuperfluidDelegationRecords,
types.SuperfluidDelegationRecord{
DelegatorAddress: req.DelegatorAddress,
ValidatorAddress: valAddr,
DelegationAmount: lockedCoins,
DelegatorAddress: req.DelegatorAddress,
ValidatorAddress: valAddr,
DelegationAmount: lockedCoins,
EquivalentStakedAmount: &coin,
},
)
res.TotalDelegatedCoins = res.TotalDelegatedCoins.Add(lockedCoins)
res.TotalEquivalentStakedAmount = res.TotalEquivalentStakedAmount.Add(coin)
}

return &res, nil
Expand Down
10 changes: 10 additions & 0 deletions x/superfluid/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,22 @@ func (suite *KeeperTestSuite) TestGRPCQuerySuperfluidDelegations() {
res, err := suite.queryClient.SuperfluidDelegationsByDelegator(sdk.WrapSDKContext(suite.Ctx), &types.SuperfluidDelegationsByDelegatorRequest{
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.TotalDelegatedCoins.IsEqual(sdk.NewCoins(
sdk.NewInt64Coin(denoms[0], 1000000),
sdk.NewInt64Coin(denoms[1], 1000000),
)))
suite.Require().True(res.SuperfluidDelegationRecords[0].EquivalentStakedAmount.IsEqual(sdk.NewCoin("uosmo", expectAmount0.RoundInt())))
suite.Require().True(res.SuperfluidDelegationRecords[1].EquivalentStakedAmount.IsEqual(sdk.NewCoin("uosmo", expectAmount1.RoundInt())))
}

// for each validator denom pair, make sure they have 1 delegations
Expand Down
Loading

0 comments on commit ddd75fe

Please sign in to comment.