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

fix: Implement retaining rewards for transfers #7785

Merged
merged 9 commits into from
Mar 21, 2024

Conversation

AlpinYukseloglu
Copy link
Contributor

@AlpinYukseloglu AlpinYukseloglu commented Mar 20, 2024

Closes: #7776

What is the purpose of the change

This PR removes reward claiming logic that previously executing prior to position transfers. Since the transfers are done through manual reassignment of ownership over position IDs, handling forfeited incentives become tricky/hacky since the positions do not actually move (see issue for further discussion).

Spread reward claiming is also removed for consistency. This also makes the transfer function run more efficiently, as these claims were not necessary for the correct functioning of the logic.

Testing and Verifying

Tests in position_test.go are updated to ensure that claiming of old and new rewards (both incentives and spread rewards) function properly (i.e. go to the new owner and not the old).

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

Summary by CodeRabbit

  • Bug Fixes
    • Updated event trigger counts in tests to accurately reflect no additional events being triggered during position transfers.
  • New Features
    • Removed the ability to claim rewards during position transfers for enhanced system integrity and fairness.

Copy link
Contributor

coderabbitai bot commented Mar 20, 2024

Walkthrough

This update primarily focuses on refining the handling of incentives during position transfers. It eliminates the collection of rewards at the time of transferring positions, ensuring that incentives remain with the positions and are not prematurely distributed. This change aligns with the goal of managing incentives more effectively and avoiding past issues related to their distribution.

Changes

Files Change Summary
CHANGELOG.md Removed reward claiming during position transfers in v23.0.8.
.../msg_server_test.go Updated expectedMessageEvents to 0 in TestTransferPositions.
.../concentrated-liquidity/position.go Removed incentive collection logic before position deletion.

Assessment against linked issues

Objective Addressed Explanation
Properly handle forfeited incentives during position transfers (#7776)
Retain incentives in the transferred position instead of redepositing them directly (#7776)
Ensure that incentives are not collected before the transfer by removing specific code (#7776)
Avoid sending incentives to the community pool as it is considered inelegant (#7776)
Ensure that incentive forfeiting for position transfers is handled correctly (#7776)

Poem

In the realm of code, where logic reigns,
A rabbit hopped, fixing chains.
No more rewards in premature flight,
Positions transfer with incentives right.

🌟 Under the moon, so bright and keen,
This coderabbit, on the green,
Celebrates a task, so deftly done,
In the world of code, another win spun.

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-tests 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 tests 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 tests.
    • @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.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

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.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 452f273 and 994b773.
Files selected for processing (3)
  • x/concentrated-liquidity/msg_server_test.go (3 hunks)
  • x/concentrated-liquidity/position.go (1 hunks)
  • x/concentrated-liquidity/position_test.go (1 hunks)
Additional comments: 27
x/concentrated-liquidity/msg_server_test.go (1)
  • 572-593: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [561-608]

The update to expectedMessageEvents to always be 0 in TestTransferPositions_Events aligns with the PR's objectives to simplify the transfer process by not triggering additional events related to claiming rewards before transfers. It's crucial to ensure that this change does not inadvertently suppress any essential events that should be emitted during the transfer process. Please confirm that all necessary events, aside from those related to reward claims, are still being emitted as expected.

x/concentrated-liquidity/position.go (22)
  • 704-709: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [14-14]

The package and import statements appear to be correctly organized and relevant to the functionality provided in this file. It's good practice to ensure that only necessary packages are imported to keep the code clean and maintainable.

  • 704-709: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [16-16]

The constant MinNumPositions is defined but not directly referenced in the provided code changes. Ensure that this constant is used appropriately elsewhere in the codebase or consider removing it if it's no longer needed.

  • 704-709: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [18-18]

The variable emptyOptions is defined for use with accum.Options{}. It's a good practice to define such placeholders for default or empty configurations. Ensure that it's used appropriately where default options are needed.

  • 704-709: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [20-20]

The function getOrInitPosition is well-documented and handles the retrieval or initialization of a position's liquidity. It correctly returns zero liquidity for non-existent positions, which is a safe default behavior.

  • 704-709: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [32-32]

The function initOrUpdatePosition is crucial for managing position liquidity. It's important to ensure that the logic correctly handles both initialization and updates, especially in terms of liquidity adjustments and error handling. The warning about mutating the pool is appropriately placed, reminding developers to refetch the pool after calling this method.

  • 704-709: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [74-74]

The function hasPosition provides a straightforward way to check for the existence of a position by its ID. Using store.Has(positionIdKey) is an efficient approach to perform this check.

  • 704-709: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [76-76]

The function HasAnyPositionForPool is designed to check if any position exists for a given pool. The use of osmoutils.HasAnyAtPrefix along with a custom parsing function is a clever way to achieve this without needing to iterate over all positions manually.

  • 704-709: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [88-88]

The function GetAllPositionIdsForPoolId is well-structured and correctly parses position IDs from the store keys. The error handling and sorting of position IDs ensure that the function's output is reliable and consistent.

  • 704-709: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [120-120]

The function GetPositionLiquidity and GetPosition are essential for retrieving position details. They correctly handle errors and return the expected results. It's important to ensure that these functions are used consistently throughout the codebase to fetch position information.

  • 704-709: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [142-142]

The function GetUserPositions and GetUserPositionsSerialized provide mechanisms to retrieve user positions, with the latter supporting pagination. The separation of these concerns and the use of pagination demonstrate good API design principles.

  • 704-709: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [164-164]

The function SetPosition is critical for creating or updating position information. The detailed comments and structured approach to setting various mappings in the store are commendable. Ensure that error handling is robust, especially for operations that modify the store.

  • 704-709: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [226-226]

The function deletePosition handles the deletion of position information comprehensively, including removing all related mappings. The error handling for missing mappings before deletion is a good practice to prevent inconsistencies.

  • 704-709: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [274-274]

The functions CreateFullRangePosition, CreateFullRangePositionLocked, and CreateFullRangePositionUnlocking provide various ways to create full-range positions, including handling locked positions. These functions are well-documented and appear to handle the creation process correctly. Pay special attention to error handling and the consistency of state changes.

  • 704-709: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [332-332]

The function mintSharesAndLock is a key part of handling locked positions. It correctly mints shares and locks them for a specified duration. Ensure that the minting and locking processes are secure and that any potential errors are handled appropriately.

  • 704-709: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [370-370]

The function CalculateUnderlyingAssetsFromPosition accurately calculates the underlying assets for a given position. This function is crucial for determining the value of positions and should be used consistently where such calculations are needed.

  • 704-709: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [388-388]

The function getNextPositionIdAndIncrement provides a simple and effective way to manage position IDs. Ensuring atomicity in incrementing and retrieving the next position ID is crucial to prevent race conditions.

  • 704-709: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [396-396]

The functions GetLockIdFromPositionId and GetPositionIdToLockId are essential for managing the mappings between positions and locks. The clear separation of concerns and error handling in these functions are good practices.

  • 704-709: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [414-414]

The function setPositionIdToLock and RemovePositionIdForLockId correctly manage the bidirectional mappings between positions and locks. This is crucial for maintaining the integrity of the system's state.

  • 704-709: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [432-432]

The functions PositionHasActiveUnderlyingLock and positionHasActiveUnderlyingLockAndUpdate are important for determining the status of underlying locks associated with positions. The distinction between non-mutative and mutative versions of the check is a good design choice.

  • 704-709: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [460-460]

The function GetFullRangeLiquidityInPool and updateFullRangeLiquidityInPool manage the liquidity information for full-range positions within a pool. Ensuring accurate tracking of this liquidity is essential for the system's operation.

  • 704-709: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [484-484]

The transferPositions function is central to the PR's objectives, handling the transfer of positions between users. It's important to ensure that this function correctly implements the new logic for handling rewards during transfers, as described in the PR objectives. The removal of the requirement to claim rewards before transferring positions should be reflected here.

Verification successful

The analysis of the transferPositions function's implementation confirms that there is no explicit logic requiring rewards to be claimed before transferring positions. This aligns with the PR objectives of removing such a requirement, suggesting that the function correctly implements the new logic for handling rewards during transfers.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that the logic for handling rewards during transfers has been correctly implemented.
# Specifically, check that rewards are no longer required to be claimed before transferring positions.
ast-grep --lang go --pattern $'func (k Keeper) transferPositions($_, $_, $_, $_) error {
  $$$
  // Check for logic related to claiming rewards before transfer
  $$$
}'

Length of output: 169



Script:

#!/bin/bash
# Search for the `transferPositions` function in the Go codebase to verify its existence and signature.
ast-grep --lang go --pattern $'func (k Keeper) transferPositions($$$) $$$'

Length of output: 4616

* 704-709: > 📝 **NOTE** > This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [536-536]

The function UnderlyingPositionsValue calculates the value of underlying assets for a set of positions. This function is useful for assessing the total value of positions and should be accurate and efficient in its calculations.

x/concentrated-liquidity/position_test.go (4)
  • 2447-2466: The assertions made after transferring positions and claiming rewards seem to validate the expected behavior correctly. However, it's crucial to ensure that the rewards are indeed transferred to the new owner and not accessible by the old owner post-transfer. Additionally, verifying the balance of the old owner before and after claiming rewards by the new owner is a good practice to confirm that no unintended transfers occur.
  • 2470-2470: The logic for funding incentives and spread rewards appears to be correctly implemented, with careful consideration of the expected rewards based on uptime growth and the funding of both incentives and forfeited amounts to the pool. It's important to ensure that the calculations for expected rewards align with the system's rules and that the funded amounts are accurately reflected in the pool's state.
  • 2447-2466: The TestNegativeTickRange_SpreadFactor test case effectively covers the edge case of negative tick range accumulators and validates the correct handling of spread factor rewards. It's crucial to ensure that the system can handle these scenarios without errors and that rewards are accurately calculated and distributed, even in edge cases. The use of helper functions to estimate swap amounts and fund rewards aids in setting up the test scenarios accurately.
  • 2447-2466: The TestMultipleRanges test case provides a comprehensive examination of handling multiple position ranges, including adjacent, non-adjacent, and overlapping ranges. It's important to verify that the system correctly handles these scenarios, especially in terms of liquidity calculations, rewards distribution, and position management. The test cases should ensure that positions within and outside the current tick range are accurately processed and that rewards are correctly allocated based on position ownership and range.

x/concentrated-liquidity/position_test.go Show resolved Hide resolved
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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 994b773 and 4a13cfc.
Files selected for processing (1)
  • CHANGELOG.md (1 hunks)
Additional comments: 1
CHANGELOG.md (1)
  • 69-69: The addition to the changelog correctly documents the removal of reward claiming during position transfers as part of the pull request fix: Implement retaining rewards for transfers #7785. This change aligns with the PR's objective to simplify the transfer process and address the challenges associated with the manual reassignment of ownership over position IDs.

postTransferNewOwnerFunds := s.App.BankKeeper.GetAllBalances(s.Ctx, newOwner)
s.Require().Equal(preTransferNewOwnerFunds, postTransferNewOwnerFunds)

// Test that claiming incentives/spread rewards with the new owner returns nothing
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems like this diff is rendering weirdly. The changes here were:

  1. Remove previous assertion that claiming rewards post transfer yields nothing
  2. Claim rewards post transfer and ensure they go to new owner

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.

ACK!

x/concentrated-liquidity/position.go Show resolved Hide resolved
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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 4a13cfc and 8b0977f.
Files selected for processing (2)
  • x/concentrated-liquidity/position.go (2 hunks)
  • x/concentrated-liquidity/position_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • x/concentrated-liquidity/position.go
  • x/concentrated-liquidity/position_test.go

Copy link
Collaborator

@PaddyMc PaddyMc left a comment

Choose a reason for hiding this comment

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

🙇

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 8b0977f and 0a73347.
Files selected for processing (4)
  • CHANGELOG.md (1 hunks)
  • x/concentrated-liquidity/msg_server_test.go (3 hunks)
  • x/concentrated-liquidity/position.go (2 hunks)
  • x/concentrated-liquidity/position_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (4)
  • CHANGELOG.md
  • x/concentrated-liquidity/msg_server_test.go
  • x/concentrated-liquidity/position.go
  • x/concentrated-liquidity/position_test.go

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 0a73347 and 1855916.
Files selected for processing (1)
  • x/concentrated-liquidity/position_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • x/concentrated-liquidity/position_test.go

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 1855916 and e191dea.
Files selected for processing (1)
  • x/concentrated-liquidity/position.go (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • x/concentrated-liquidity/position.go

@AlpinYukseloglu AlpinYukseloglu merged commit d28ed22 into main Mar 21, 2024
1 check passed
@AlpinYukseloglu AlpinYukseloglu deleted the alpo/retain-rewards-transfer branch March 21, 2024 16:08
@czarcas7ic czarcas7ic added the A:backport/v24.x backport patches to v24.x branch label Mar 22, 2024
mergify bot pushed a commit that referenced this pull request Mar 22, 2024
* implement retaining rewards for transfers

* add changelog

* clean up tests

* driveby test cleanup

* Revert "driveby test cleanup"

This reverts commit b1952b9.

* update transfer method spec

* fix conflicts

* lint

(cherry picked from commit d28ed22)

# Conflicts:
#	CHANGELOG.md
#	x/concentrated-liquidity/msg_server_test.go
#	x/concentrated-liquidity/position.go
PaddyMc added a commit that referenced this pull request Mar 22, 2024
* implement retaining rewards for transfers

* add changelog

* clean up tests

* driveby test cleanup

* Revert "driveby test cleanup"

This reverts commit b1952b9.

* update transfer method spec

* fix conflicts

* lint

(cherry picked from commit d28ed22)

# Conflicts:
#	CHANGELOG.md
#	x/concentrated-liquidity/msg_server_test.go
#	x/concentrated-liquidity/position.go

Co-authored-by: Alpo <[email protected]>
Co-authored-by: PaddyMc <[email protected]>
@github-actions github-actions bot mentioned this pull request Apr 1, 2024
@github-actions github-actions bot mentioned this pull request May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A:backport/v24.x backport patches to v24.x branch C:x/concentrated-liquidity V:state/breaking State machine breaking PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fix: Properly handle forfeited incentives during position transfers
4 participants