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

[NonPoolSuperfluid] Add risk factors to individual denoms. #8250

Merged
merged 49 commits into from
May 22, 2024

Conversation

nicolaslara
Copy link
Contributor

@nicolaslara nicolaslara commented May 10, 2024

What is the purpose of the change

Add independent risk factors to different superfluid denoms

Testing and Verifying

Added necessary tests

Documentation and Release Note

  • Does this pull request introduce a new feature or user-facing behavior changes?
  • Changelog entry added to Unreleased section of CHANGELOG.md?

Where is the change documented?

  • Specification (x/{module}/README.md)
  • Osmosis documentation site
  • Code comments?
  • N/A

@nicolaslara nicolaslara added V:state/breaking State machine breaking PR A:no-changelog labels May 10, 2024
@nicolaslara
Copy link
Contributor Author

should I rename RiskFactor to DiscountFactor?

@nicolaslara nicolaslara changed the base branch from main to nicolas/superfluid-btc May 16, 2024 11:01
@nicolaslara nicolaslara marked this pull request as ready for review May 17, 2024 14:01
Copy link
Member

@czarcas7ic czarcas7ic left a comment

Choose a reason for hiding this comment

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

Nice work!

string sender = 1 [ (gogoproto.moretags) = "yaml:\"sender\"" ];
string denom = 2 [ (gogoproto.moretags) = "yaml:\"denom\"" ];
string risk_factor = 3 [ (gogoproto.moretags) = "yaml:\"risk_factor\"" ];
Copy link
Member

Choose a reason for hiding this comment

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

I think this needs a sdk.Dec tag right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

message DenomRiskFactor {
// superfluid asset denom, can be LP token or native token
string denom = 1;
string risk_factor = 2 [
Copy link
Member

Choose a reason for hiding this comment

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

Can a brief explanation of what the risk factor is be added here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

@@ -151,7 +151,7 @@ func (k Keeper) UpdateOsmoEquivalentMultipliers(ctx sdk.Context, asset types.Sup
return errors.New("osmo should not be a superfluid asset. It can be staked natively")
}
// get the twap price of the native asset in osmo
startTime := k.ek.GetEpochInfo(ctx, k.GetEpochIdentifier(ctx)).StartTime
startTime := k.ek.GetEpochInfo(ctx, k.GetEpochIdentifier(ctx)).StartTime // TODO: do 5 mins instead of 1 epoch
Copy link
Member

Choose a reason for hiding this comment

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

What does this TODO mean? You want the twap over 5 minutes rather than the full epoch? Is there any reason we cant just do that here in this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, that's what it means. We can do it here, but I wanted to make it a param so I thought it'd be nice to have its own PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed

Comment on lines 618 to 623
{"happy path", govAddr, "something", "0.1", ""},
{"happy path with zero", govAddr, "something", "0", ""},
{"bad gov addr", "osmo1herasn5ewvv9acpujdmqxz698y849aq9ucsccl", "something", "0", "only the governance module is allowed to execute this message"},
{"bad gov addr 2", "something else", "something", "0", "invalid sender address (decoding bech32 failed"},
{"bad denom", govAddr, "", "0", "denom cannot be empty"},
{"bad risk factor", govAddr, "something", "NaN", "invalid risk factor (failed to set decimal string"},
Copy link
Member

Choose a reason for hiding this comment

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

nit: Since this is a new test, can this be refactored to show the struct name next to each entry? It makes it 100x easier to parse rather than matching each field up to its respective struct field.

Also, can the expectedError be of type error, so we can use error types instead of hard coding the text of the error?

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 like using strings for the error because that way I can do partial matching (Contains). Other than that, yeah, we can refactor

x/superfluid/keeper/msg_server_test.go Outdated Show resolved Hide resolved
x/superfluid/keeper/superfluid_asset.go Outdated Show resolved Hide resolved
Comment on lines +55 to +60
tests := []riskFactorTest{
{"test risk factor not set ", "random", "", "btc", 100, 50},
{"test risk factor set high", "btc", "0.8", "btc", 100, 20},
{"test risk factor set low", "btc", "0.1", "btc", 100, 90},
{"test risk set diff query", "gamm/pool/1", "0.1", "btc", 100, 50}, // default
{"test risk set same query", "gamm/pool/1", "0.1", "gamm/pool/1", 100, 90}, // set
Copy link
Member

Choose a reason for hiding this comment

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

Same requests with these, would be nice to have struct names in front of each input to make understanding each test case easier.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

doesn't your editor display the types inline? Mine looks like this:
Cursor_and_Notification_Center

Base automatically changed from nicolas/superfluid-btc to main May 21, 2024 13:30
Copy link
Contributor

coderabbitai bot commented May 21, 2024

Walkthrough

The recent updates to the Osmosis superfluid module bring enhancements for managing risk factors tied to specific denominations. Notable changes include new message types and RPC methods for setting and unsetting denomination risk factors, updated risk-adjusted value computations, and related test scenarios. Additionally, the time duration for adjusting Osmo equivalent multipliers has shifted from 1 epoch to 5 minutes.

Changes

File Summary
proto/.../superfluid.proto
proto/.../tx.proto
Introduce DenomRiskFactor message and RPC methods for setting/unsetting risk factors.
x/superfluid/keeper/epoch.go Adjust startTime calculation to 5 minutes in UpdateOsmoEquivalentMultipliers.
x/superfluid/keeper/msg_server.go
x/superfluid/keeper/msg_server_test.go
Add functions and tests for managing denomination risk factors.
x/superfluid/keeper/superfluid_asset.go
x/superfluid/keeper/superfluid_asset_store.go
Implement functions to handle denomination-specific risk factors.
x/superfluid/types/msgs.go Include message types for setting/unsetting denomination risk factors.
x/superfluid/keeper/epoch_test.go
x/superfluid/keeper/integration_test.go
Modify time-related functionalities and tests for enhanced precision.

🐇

In the world of superfluid streams,
Risk factors dance in data dreams.
With new methods to set and clear,
Denom values now crystal clear.
Five-minute tweaks to time's embrace,
Ensuring precision in every case.
Hopping through code, the rabbit's cheer,
For Osmosis grows, year by year.

🌟


Recent Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 722bfb5 and f3a3eb4.
Files selected for processing (1)
  • x/superfluid/keeper/gov/gov_test.go (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • x/superfluid/keeper/gov/gov_test.go

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

Outside diff range and nitpick comments (2)
x/superfluid/keeper/superfluid_asset_test.go (1)

55-60: Consider adding struct field names in the test cases for clarity.

This will improve readability and maintainability, especially for new contributors or when revisiting this code after some time.

proto/osmosis/superfluid/superfluid.proto (1)

108-108: Add a description for the risk_factor field.

It would be beneficial to include a brief comment explaining what the risk_factor represents and how it is used within the system. This will enhance the understandability of the code for developers and maintainers.

Comment on lines 602 to 648
func (s *KeeperTestSuite) TestSetDenomRiskFactors() {
s.SetupTest()

msgServer := keeper.NewMsgServerImpl(s.App.SuperfluidKeeper)
c := sdk.WrapSDKContext(s.Ctx)

govAddr := s.App.AccountKeeper.GetModuleAccount(s.Ctx, govtypes.ModuleName).GetAddress().String()

// Let's turn this into a table test
setDenomRiskFactorTests := []struct {
name string
sender string
denom string
riskFactor string
expectedError string
}{
{"happy path", govAddr, "something", "0.1", ""},
{"happy path with zero", govAddr, "something", "0", ""},
{"bad gov addr", "osmo1herasn5ewvv9acpujdmqxz698y849aq9ucsccl", "something", "0", "only the governance module is allowed to execute this message"},
{"bad gov addr 2", "something else", "something", "0", "invalid sender address (decoding bech32 failed"},
{"bad denom", govAddr, "", "0", "denom cannot be empty"},
{"bad risk factor", govAddr, "something", "NaN", "invalid risk factor (failed to set decimal string"},
}

for _, test := range setDenomRiskFactorTests {
s.Run(test.name, func() {
s.SetupTest()

msg := &types.MsgSetDenomRiskFactor{
Sender: test.sender,
Denom: test.denom,
RiskFactor: test.riskFactor,
}
err := msg.ValidateBasic()
if err == nil {
_, err = msgServer.SetDenomRiskFactor(c, msg)
}

if test.expectedError != "" {
s.Require().Error(err)
s.Require().ErrorContains(err, test.expectedError)
} else {
s.Require().NoError(err)
}
})
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider refactoring the test cases into a separate function for clarity and reusability.

This will improve the readability and maintainability of the test code. Here's a suggested refactor:

func getSetDenomRiskFactorTests(govAddr string) []struct {
    name          string
    sender        string
    denom         string
    riskFactor    string
    expectedError string
} {
    return []struct {
        name          string
        sender        string
        denom         string
        riskFactor    string
        expectedError string
    }{
        {"happy path", govAddr, "something", "0.1", ""},
        {"happy path with zero", govAddr, "something", "0", ""},
        {"bad gov addr", "osmo1herasn5ewvv9acpujdmqxz698y849aq9ucsccl", "something", "0", "only the governance module is allowed to execute this message"},
        {"bad gov addr 2", "something else", "something", "0", "invalid sender address (decoding bech32 failed"},
        {"bad denom", govAddr, "", "0", "denom cannot be empty"},
        {"bad risk factor", govAddr, "something", "NaN", "invalid risk factor (failed to set decimal string"},
    }
}

@@ -151,7 +151,7 @@ func (k Keeper) UpdateOsmoEquivalentMultipliers(ctx sdk.Context, asset types.Sup
return errors.New("osmo should not be a superfluid asset. It can be staked natively")
}
// get the twap price of the native asset in osmo
startTime := k.ek.GetEpochInfo(ctx, k.GetEpochIdentifier(ctx)).StartTime
startTime := k.ek.GetEpochInfo(ctx, k.GetEpochIdentifier(ctx)).StartTime // TODO: do 5 mins instead of 1 epoch
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider resolving the TODO about making the start time configurable or create a tracking issue.

Would you like me to help by creating a GitHub issue to track this enhancement?

Comment on lines 62 to 115
func (k Keeper) SetDenomRiskFactor(ctx sdk.Context, denom string, riskFactor osmomath.Dec) error {
store := ctx.KVStore(k.storeKey)
prefixStore := prefix.NewStore(store, types.KeyPrefixDenomRiskFactor)
riskFactorRecord := types.DenomRiskFactor{
RiskFactor: riskFactor,
}
bz, err := proto.Marshal(&riskFactorRecord)
if err != nil {
return err
}
prefixStore.Set([]byte(denom), bz)
return nil
}

func (k Keeper) DeleteDenomRiskFactor(ctx sdk.Context, denom string) {
store := ctx.KVStore(k.storeKey)
prefixStore := prefix.NewStore(store, types.KeyPrefixDenomRiskFactor)
prefixStore.Delete([]byte(denom))
}

func (k Keeper) GetDenomRiskFactor(ctx sdk.Context, denom string) (osmomath.Dec, bool) {
store := ctx.KVStore(k.storeKey)
prefixStore := prefix.NewStore(store, types.KeyPrefixDenomRiskFactor)
bz := prefixStore.Get([]byte(denom))
if bz == nil {
return osmomath.ZeroDec(), false
}
denomRiskFactor := types.DenomRiskFactor{}
err := proto.Unmarshal(bz, &denomRiskFactor)
if err != nil {
return osmomath.ZeroDec(), false
}
return denomRiskFactor.RiskFactor, true
}

func (k Keeper) GetAllDenomRiskFactors(ctx sdk.Context) []types.DenomRiskFactor {
store := ctx.KVStore(k.storeKey)
prefixStore := prefix.NewStore(store, types.KeyPrefixDenomRiskFactor)
iterator := prefixStore.Iterator(nil, nil)
defer iterator.Close()

denomRiskFactors := []types.DenomRiskFactor{}
for ; iterator.Valid(); iterator.Next() {
denomRiskFactor := types.DenomRiskFactor{}

err := proto.Unmarshal(iterator.Value(), &denomRiskFactor)
if err != nil {
panic(err)
}

denomRiskFactors = append(denomRiskFactors, denomRiskFactor)
}
return denomRiskFactors
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider refactoring these functions into a store.go file.

Moving SetDenomRiskFactor, DeleteDenomRiskFactor, and GetDenomRiskFactor to a store.go file could improve the organization of the codebase. This aligns with common practices where store-related functions are kept separate from business logic, enhancing maintainability.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

x/superfluid/keeper/superfluid_asset_store.go Show resolved Hide resolved
@nicolaslara nicolaslara merged commit c8a99b7 into main May 22, 2024
1 check passed
@nicolaslara nicolaslara deleted the nicolas/superfluid-btc-independent-risk-factor branch May 22, 2024 09:01
@coderabbitai coderabbitai bot mentioned this pull request Jun 5, 2024
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants