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: Splitter Adjustments #457

Merged
merged 3 commits into from
May 20, 2024
Merged

Conversation

joemonem
Copy link
Contributor

@joemonem joemonem commented May 15, 2024

Motivation

There were cases where zero send messages could be sent, contract owner couldn't update lock at any time.

Implementation

Use .mul_floor to multiply Uint128 with Decimal
Removed check that prevented the contract owner to update lock at any time
Set non-zero and non-empty checks to prevent zero send messages.

Testing

No new tests were created.

Notes

The above changes were requested through this comment

Summary by CodeRabbit

  • Bug Fixes

    • Improved accuracy of payment distribution based on recipient percentage.
    • Ensured messages are only generated and processed when necessary.
  • Refactor

    • Enhanced code efficiency by optimizing conditional checks and message handling logic.

@joemonem joemonem requested a review from crnbarr93 May 15, 2024 15:58
Copy link
Contributor

@crnbarr93 crnbarr93 left a comment

Choose a reason for hiding this comment

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

Looks good! One small thing I noticed while reviewing:

let distro_msg = pkt.to_sub_msg(kernel_address, Some(amp_funds), 1)?;

This packet might actually end up being empty. In which case we should probably skip attaching it.

This and changelog entry and should be good to merge!

Copy link
Contributor

coderabbitai bot commented May 15, 2024

Walkthrough

The recent updates to the andromeda-splitter contract refine fund distribution logic in execute_send and simplify operations in execute_update_lock. Funds are now distributed accurately based on recipient percentages, ensuring payments only when needed. The removal of expiration checks in execute_update_lock streamlines the process.

Changes

File Change Summary
contracts/finance/andromeda-splitter/src/contract.rs - Enhanced execute_send by calculating amount_owed based on recipient_percent.
- Added conditional checks for proper fund distribution.
- Simplified execute_update_lock by removing the expiration check.

🐇
The splitter's code refined with care,
Ensuring funds are always fair.
No more checks for locks expired,
Simplicity is now admired.
With logic clear and pathways bright,
Our contract runs both day and 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 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: 1

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 11e545d and ce65640.
Files selected for processing (1)
  • contracts/finance/andromeda-splitter/src/contract.rs (3 hunks)

Comment on lines +171 to +179
let amount_owed = coin.amount.mul_floor(recipient_percent);
if !amount_owed.is_zero() {
let mut recip_coin: Coin = coin.clone();
recip_coin.amount = amount_owed;
remainder_funds[i].amount =
remainder_funds[i].amount.checked_sub(recip_coin.amount)?;
vec_coin.push(recip_coin.clone());
amp_funds.push(recip_coin);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Optimize nested loops to improve performance.

Consider using a HashMap to store and update remainder_funds to avoid nested loops and improve performance.

let mut remainder_funds_map: HashMap<String, Uint128> = info.funds.iter().map(|coin| (coin.denom.clone(), coin.amount)).collect();

for recipient_addr in &splitter.recipients {
    let recipient_percent = recipient_addr.percent;
    let mut vec_coin: Vec<Coin> = Vec::new();
    for coin in info.funds.iter() {
        let amount_owed = coin.amount.mul_floor(recipient_percent);
        if !amount_owed.is_zero() {
            let mut recip_coin = coin.clone();
            recip_coin.amount = amount_owed;
            if let Some(remainder) = remainder_funds_map.get_mut(&coin.denom) {
                *remainder = remainder.checked_sub(amount_owed)?;
            }
            vec_coin.push(recip_coin.clone());
            amp_funds.push(recip_coin);
        }
    }
    if !vec_coin.is_empty() {
        let amp_msg = recipient_addr.recipient.generate_amp_msg(&deps.as_ref(), Some(vec_coin))?;
        pkt = pkt.add_message(amp_msg);
    }
}

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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between ce65640 and bc27e57.
Files selected for processing (1)
  • CHANGELOG.md (1 hunks)
Additional Context Used
LanguageTool (7)
CHANGELOG.md (7)

Near line 12: Unpaired symbol: ‘]’ seems to be missing
Context: ...## Added - Added Validator Staking ADO [(#330)](https://github.com/andromedaprot...


Near line 13: Unpaired symbol: ‘]’ seems to be missing
Context: ...eda-core/pull/330) - Added Asset enum [(#415)](https://github.com/andromedaprot...


Near line 14: Unpaired symbol: ‘]’ seems to be missing
Context: ...dded ADOBaseVersion query to all ADOs [(#416)](https://github.com/andromedaprot...


Near line 15: Unpaired symbol: ‘]’ seems to be missing
Context: ... ability to remove/replace reward token [(#418)](https://github.com/andromedaprot...


Near line 16: Unpaired symbol: ‘]’ seems to be missing
Context: ...meda-core/pull/418) - Added Expiry Enum [(#419)](https://github.com/andromedaprot...


Near line 20: Unpaired symbol: ‘]’ seems to be missing
Context: ... Merkle Root: stage expiration now uses Milliseconds[(#417)](https://github.com/andromedaprot...


Near line 24: Unpaired symbol: ‘]’ seems to be missing
Context: ...d messages, owner updates lock any time [(#457)](https://github.com/andromedaprot...

Comment on lines +24 to +25
- Splitter: avoid zero send messages, owner updates lock any time [(#457)](https://github.com/andromedaprotocol/andromeda-core/pull/457)

Copy link
Contributor

Choose a reason for hiding this comment

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

Correct the grammar and punctuation in the change description.

- Splitter: avoid zero send messages, owner updates lock any time [(#457)](https://github.com/andromedaprotocol/andromeda-core/pull/457)
+ Splitter: avoid zero send messages; owner can update lock at any time [(#457)](https://github.com/andromedaprotocol/andromeda-core/pull/457)

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
- Splitter: avoid zero send messages, owner updates lock any time [(#457)](https://github.com/andromedaprotocol/andromeda-core/pull/457)
Splitter: avoid zero send messages; owner can update lock at any time [(#457)](https://github.com/andromedaprotocol/andromeda-core/pull/457)

@joemonem
Copy link
Contributor Author

Looks good! One small thing I noticed while reviewing:

let distro_msg = pkt.to_sub_msg(kernel_address, Some(amp_funds), 1)?;

This packet might actually end up being empty. In which case we should probably skip attaching it.

I assume that's also applicable to the conditional splitter?

@crnbarr93
Copy link
Contributor

Looks good! One small thing I noticed while reviewing:

let distro_msg = pkt.to_sub_msg(kernel_address, Some(amp_funds), 1)?;

This packet might actually end up being empty. In which case we should probably skip attaching it.

I assume that's also applicable to the conditional splitter?

Yea I'd assume so

@joemonem joemonem merged commit 951d7c4 into development May 20, 2024
9 checks passed
@joemonem joemonem deleted the joe/splitter-adjustment branch May 20, 2024 12:20
@coderabbitai coderabbitai bot mentioned this pull request Dec 4, 2024
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.

2 participants