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

refactor(airdrop-token-vesting): remove unnecessary fields #129

Merged

Conversation

k-yang
Copy link
Member

@k-yang k-yang commented Mar 6, 2024

  • remove VestingSchedule::LinearVesting
  • remove cliff_amount and vesting_amount from VestingSchedule
  • allow zero cliff_amount

Summary by CodeRabbit

  • New Features
    • Updated the vesting schedule functionality to support direct input of vesting and cliff amounts.
  • Bug Fixes
    • Removed unnecessary variants and methods to streamline vesting operations.
  • Refactor
    • Simplified validation methods for user rewards and vesting schedules.
    • Adjusted test cases to align with the updated vesting schedule structure.
  • Chores
    • Cleaned up imports and removed unused code paths for better code efficiency.

Copy link
Contributor

coderabbitai bot commented Mar 6, 2024

Walkthrough

The recent updates focus on optimizing the airdrop token vesting contract, enhancing its functionality and efficiency. Modifications include changes to function parameters, simplification of validation methods, and adjustments in handling vesting schedules. The removal of unnecessary variants and methods streamlines the code, while the addition of direct parameter handling and new methods improve the contract's clarity and performance.

Changes

Files Summary
.../contract.rs - Changed reward_users to take vesting_schedule by value.
- register_vesting_account now uses direct parameters for vesting_amount and cliff_amount.
- Updated validation and attribute handling.
.../errors.rs - Removed ZeroAmount from CliffError.
.../msg.rs - Removed unused imports.
- Changed RewardUserRequest and VestingSchedule structures.
- Simplified validation methods.
.../state.rs - Added cliff_amount to VestingAccount.
- Introduced vested_amount method.
.../testing.rs - Updated create_msg and RewardUserRequest for cliff_amount handling.
- Adjusted VestingSchedule usage in tests.

"In the realm of code, where logic does thrive,
Changes were made, to make contracts alive.
🐇 With every hop and a clever tweak,
A vesting schedule, no longer bleak.
So here's to the devs, with minds so bright,
Crafting the code, into the night."
🌟🌙

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

codecov bot commented Mar 6, 2024

Codecov Report

Attention: Patch coverage is 94.65649% with 7 lines in your changes are missing coverage. Please review.

Project coverage is 87.37%. Comparing base (902b788) to head (15fb12c).
Report is 8 commits behind head on main.

Additional details and impacted files
Files Coverage Δ
contracts/airdrop-token-vesting/src/contract.rs 98.12% <100.00%> (ø)
contracts/airdrop-token-vesting/src/errors.rs 100.00% <ø> (ø)
contracts/airdrop-token-vesting/src/msg.rs 80.00% <100.00%> (ø)
contracts/airdrop-token-vesting/src/testing.rs 98.25% <100.00%> (ø)
contracts/airdrop-token-vesting/src/state.rs 80.00% <72.00%> (ø)

... and 7 files with indirect coverage changes

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 6595c82 and 15fb12c.
Files selected for processing (5)
  • contracts/airdrop-token-vesting/src/contract.rs (10 hunks)
  • contracts/airdrop-token-vesting/src/errors.rs (1 hunks)
  • contracts/airdrop-token-vesting/src/msg.rs (7 hunks)
  • contracts/airdrop-token-vesting/src/state.rs (2 hunks)
  • contracts/airdrop-token-vesting/src/testing.rs (16 hunks)
Files skipped from review due to trivial changes (1)
  • contracts/airdrop-token-vesting/src/errors.rs
Additional comments: 28
contracts/airdrop-token-vesting/src/state.rs (1)
  • 37-37: The addition of the cliff_amount field to the VestingAccount struct aligns with the PR objectives to adjust the vesting schedule logic. This field is crucial for calculating the vested amount when a cliff is involved.
contracts/airdrop-token-vesting/src/msg.rs (4)
  • 60-60: Changing the cliff_amount field in RewardUserRequest from an optional to a fixed type simplifies the data structure and aligns with the PR objectives of simplifying function parameters. This change ensures that every reward request must explicitly include a cliff amount, which could improve clarity in reward scheduling.
  • 64-75: The updated validate method in RewardUserRequest now checks for a zero vesting amount and ensures that the cliff amount does not exceed the vesting amount. These validations are crucial for maintaining the integrity of reward requests and preventing logical errors in vesting calculations. The simplification of this method, by removing checks for optional fields, enhances readability and maintainability.
  • 115-117: The adjustments to the VestingSchedule enum, specifically the removal of the LinearVesting variant and the simplification of the LinearVestingWithCliff fields, streamline the vesting schedule representation. This change supports the PR's objective of simplifying the contract's structure and makes the vesting logic more straightforward.
  • 159-159: The simplified validate method in VestingSchedule now focuses on ensuring the validity of time ranges for the LinearVestingWithCliff variant. This method is essential for preventing logical errors in vesting schedules by enforcing that the start time is before the end time and that the cliff time is a valid point within the vesting period. The removal of redundant checks and the focus on time validation contribute to the method's clarity and effectiveness.
contracts/airdrop-token-vesting/src/contract.rs (5)
  • 142-142: The change to have reward_users take vesting_schedule by value instead of by mutable reference simplifies parameter handling and aligns with the PR's objective of simplifying function parameters. This approach also ensures that the vesting schedule is explicitly provided for each reward operation, enhancing clarity.
  • 163-163: The call to vesting_schedule.validate(env.block.time) within reward_users is crucial for ensuring that the provided vesting schedule is valid at the time of rewarding users. This validation step helps prevent logical errors related to time constraints in vesting schedules.
  • 168-168: Validating each RewardUserRequest within the loop in reward_users ensures that all reward requests meet the necessary criteria before proceeding with vesting account registration. This validation step is essential for maintaining the integrity of reward operations.
  • 207-214: > 📝 NOTE

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

The register_vesting_account function now directly takes vesting_amount and cliff_amount as parameters, which simplifies the function signature and aligns with the PR's objective of simplifying function parameters. This change also ensures that the necessary information is explicitly provided when registering a vesting account, enhancing clarity and reducing the risk of errors.

  • 272-272: Calculating the vested_amount within deregister_vesting_account to determine the amount that can be claimed or needs to be transferred is a critical operation. This calculation ensures that the contract accurately handles the distribution of vested tokens upon account deregistration, aligning with the contract's logic for handling vested amounts.
contracts/airdrop-token-vesting/src/testing.rs (18)
  • 249-249: The change from Option<u128> to u128 for the cliff_amount parameter in the create_msg function simplifies the handling of this value, aligning with the PR objectives to streamline the contract's code structure.
  • 256-256: Updating the cliff_amount field in the RewardUserRequest struct to always be of type Uint128::new(cliff_amount) is consistent with the simplification of the cliff_amount handling. This change ensures that the cliff_amount is always explicitly defined, enhancing the contract's readability and maintainability.
  • 267-267: The test case for unauthorized sender correctly simulates the scenario where an unauthorized address attempts to execute the RewardUsers message. This is crucial for ensuring the contract's security by validating that only authorized users can perform certain actions.
  • 277-277: Testing for a zero vesting amount is important for ensuring that the contract correctly handles edge cases and prevents invalid operations. This test case aligns with the objective of streamlining the validation process for user rewards and vesting schedules.
  • 287-287: The test case for cliff time being less than block time is essential for validating the contract's logic regarding time constraints in vesting schedules. This ensures that the vesting schedule logic is robust and less prone to errors, in line with the PR objectives.
  • 300-300: Testing for the scenario where the end time is less than the start time is crucial for ensuring the integrity of the vesting schedule. This test case helps prevent logical errors in the vesting schedule definition, contributing to the contract's overall robustness.
  • 314-314: The test case for the cliff amount being greater than the vesting amount is important for validating the contract's handling of invalid input values. This ensures that the contract's logic for managing vesting amounts and schedules is consistent and error-resistant.
  • 331-331: Testing for the scenario where the deposit amount is higher than unallocated funds is crucial for ensuring the contract's financial integrity. This test case aligns with the objective of enhancing the contract's validation process for user rewards and vesting schedules.
  • 342-342: The test case for a valid vesting amount is essential for verifying the contract's functionality under normal operating conditions. This ensures that the contract behaves as expected when processing valid vesting schedules and amounts.
  • 379-384: Updating test cases to reflect the changes in the function calls and assertions, especially with the cliff_amount parameter handling, is crucial for ensuring that the contract's functionality remains consistent and predictable. This aligns with the PR objectives to update test cases according to the structural changes made to the contract.
  • 441-441: Similar to the previous create_msg function modification, changing the cliff_amount parameter from an Option<u128> to a u128 type in this context simplifies the handling and improves the clarity of the contract's logic.
  • 448-448: Consistently updating the cliff_amount field in the RewardUserRequest struct across different test cases ensures that the contract's handling of this value is uniform, enhancing the code's readability and maintainability.
  • 460-460: The inclusion of a test case for a valid vesting amount in a different context (withdrawal scenario) is important for comprehensive testing of the contract's functionality. This ensures that the contract behaves as expected across various operations.
  • 563-563: Testing for a zero vesting amount in the context of registering a vesting account with a native token is crucial for ensuring that the contract correctly handles edge cases and prevents invalid operations.
  • 585-585: The test case for an excessive vesting amount is important for validating the contract's financial integrity and its ability to handle scenarios where the requested vesting amount exceeds available funds.
  • 608-619: Testing for excessive vesting amounts across multiple rewards is crucial for ensuring that the contract can correctly handle scenarios involving multiple transactions. This test case contributes to the robustness of the contract's financial validation logic.
  • 636-641: The inclusion of a test case for a valid vesting amount in the context of registering a vesting account with a native token is essential for verifying the contract's functionality under normal operating conditions.
  • 723-727: The test case for claiming native tokens with a valid amount is crucial for ensuring that the contract correctly handles the claim process, aligning with the PR objectives to streamline the validation process for user rewards and vesting schedules.

Comment on lines +43 to +71
pub fn vested_amount(&self, block_time: u64) -> StdResult<Uint128> {
match self.vesting_schedule {
VestingSchedule::LinearVestingWithCliff {
start_time: _start_time,
end_time,
cliff_time,
} => {
if block_time < cliff_time.u64() {
return Ok(Uint128::zero());
}

if block_time == cliff_time.u64() {
return Ok(self.cliff_amount);
}

if block_time >= end_time.u64() {
return Ok(self.vesting_amount);
}

let remaining_token =
self.vesting_amount.checked_sub(self.cliff_amount)?;
let vested_token = remaining_token
.checked_mul(Uint128::from(block_time - cliff_time.u64()))?
.checked_div(Uint128::from(end_time - cliff_time))?;

Ok(vested_token + self.cliff_amount)
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The vested_amount method implementation seems logically sound and aligns with the PR objectives of simplifying the vesting schedule logic. However, there are a few points to consider for improvement:

  • The method assumes a single vesting schedule type (LinearVestingWithCliff). If the contract plans to support multiple vesting types in the future, this method might need to be adjusted or extended.
  • The calculation logic within the if blocks is clear, but adding brief comments explaining each case (e.g., before the cliff, at the cliff, after the cliff but before the end, and after the end) could enhance readability and maintainability.
  • Error handling for arithmetic operations is done using ?, which is appropriate. However, consider adding explicit error messages for potential failures in these operations for easier debugging.

Overall, the method effectively calculates the vested amount based on the provided block time and the vesting schedule parameters.

Consider adding comments to explain the logic within each conditional block for better readability.

@matthiasmatt matthiasmatt merged commit 1049e84 into main Mar 6, 2024
5 checks passed
@matthiasmatt matthiasmatt deleted the refactor/airdrop-token-vesting/remove-redundant-fields branch March 6, 2024 08:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants