Skip to content

chore:move write function logic into private function#111

Merged
mubarak23 merged 2 commits into
mainfrom
refactor_code
Jun 22, 2025
Merged

chore:move write function logic into private function#111
mubarak23 merged 2 commits into
mainfrom
refactor_code

Conversation

@mubarak23

@mubarak23 mubarak23 commented Jun 21, 2025

Copy link
Copy Markdown
Contributor

closes #113

Summary by CodeRabbit

  • New Features

    • Added support for contract upgrades, allowing the contract owner to upgrade to a new version.
  • Refactor

    • Improved the structure of campaign creation, donation, and withdrawal processes for better modularity and maintainability. Public methods now delegate to internal logic, with no changes to user-facing behavior.

@mubarak23
mubarak23 requested a review from Utilitycoder June 21, 2025 11:48
@mubarak23 mubarak23 self-assigned this Jun 21, 2025
@coderabbitai

coderabbitai Bot commented Jun 21, 2025

Copy link
Copy Markdown

Walkthrough

The contract logic for campaign creation, donation, and withdrawal has been refactored by extracting the core implementations into new internal helper functions. Public ABI methods now delegate to these helpers. An upgradeability implementation has also been added, enforcing owner-only upgrades. The core functional behavior and validations remain unchanged.

Changes

File(s) Change Summary
src/campaign_donation.cairo Refactored public methods (create_campaign, donate_to_campaign, withdraw_from_campaign) to delegate to new internal helpers (_create_campaign, _donate_to_campaign, _withdraw_from_campaign). Added UpgradeableImpl for contract upgrades.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Contract
    participant InternalLogic

    User->>Contract: create_campaign(params)
    Contract->>InternalLogic: _create_campaign(params)
    InternalLogic-->>Contract: campaign_id
    Contract-->>User: campaign_id

    User->>Contract: donate_to_campaign(campaign_id, amount)
    Contract->>InternalLogic: _donate_to_campaign(campaign_id, amount)
    InternalLogic-->>Contract: donation_id
    Contract-->>User: donation_id

    User->>Contract: withdraw_from_campaign(campaign_id)
    Contract->>InternalLogic: _withdraw_from_campaign(campaign_id)
    InternalLogic-->>Contract: withdrawn_amount
    Contract-->>User: withdrawn_amount
Loading
sequenceDiagram
    participant Owner
    participant Contract

    Owner->>Contract: upgrade(new_class_hash)
    Contract->>Contract: (Checks owner, performs upgrade)
    Contract-->>Owner: (Upgrade complete)
Loading

Possibly related PRs

Suggested reviewers

  • Utilitycoder

Poem

In the warren of code, we hop and refactor,
Splitting big tasks, making logic more proper.
Now helpers abound for each rabbit’s delight,
Campaigns, donations, and upgrades done right!
With modular paws and a twitch of the nose,
The contract grows tidy—onward it goes! 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6c8c1e7 and 0ee12bd.

📒 Files selected for processing (1)
  • src/campaign_donation.cairo (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/campaign_donation.cairo
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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>, please review it.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (6)
src/campaign_donation.cairo (6)

161-163: Remove unnecessary cloning of value types

Cairo passes value types like felt252 and u256 by value, not by reference, so cloning them is unnecessary and adds overhead. Also, the variable naming is inconsistent.

-let ref_campaign = campaign_ref.clone();
-let campaign_target_amount = target_amount.clone();
-let campaign_id = self._create_campaign(ref_campaign, campaign_target_amount);
+let campaign_id = self._create_campaign(campaign_ref, target_amount);

181-185: Remove unnecessary cloning and improve variable naming

Cloning value types is unnecessary in Cairo. Additionally, prefixing variables with underscores is not idiomatic in Cairo/Rust unless they're meant to be unused.

-let _campaign_id = campaign_id.clone();
-let _amount = amount.clone();
 let donor = get_caller_address();
 let timestamp = get_block_timestamp();
-let donation_id = self._donate_to_campaign(_campaign_id, _amount);
+let donation_id = self._donate_to_campaign(campaign_id, amount);

190-190: Update event emission to use original parameters

Since the cloned variables are unnecessary, update the event to use the original parameters.

-Donation { donor, campaign_id: _campaign_id, amount: _amount, timestamp },
+Donation { donor, campaign_id, amount, timestamp },

200-201: Remove unnecessary cloning

Cloning the campaign_id value type is unnecessary.

-let _campaign_id = campaign_id.clone();

-let withdrawn_amount = self._withdraw_from_campaign(_campaign_id);
+let withdrawn_amount = self._withdraw_from_campaign(campaign_id);

210-212: Update event emission to use original parameter

Since the cloned variable is unnecessary, update the event to use the original parameter.

 owner: caller,
-campaign_id: _campaign_id,
+campaign_id,
 amount: withdrawn_amount,

473-473: Use consistent error message formatting

The error message should use the predefined constant for consistency with other error messages in the contract.

-assert!(amount <= campaign.target_amount, "More than Target");
+assert!(amount <= campaign.target_amount, MORE_THAN_TARGET);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d6944f7 and 6c8c1e7.

📒 Files selected for processing (1)
  • src/campaign_donation.cairo (4 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: tests
🔇 Additional comments (1)
src/campaign_donation.cairo (1)

583-589: Upgrade implementation looks correct

The upgrade function properly checks ownership before allowing upgrades, following the standard pattern for upgradeable contracts.

Comment thread src/campaign_donation.cairo Outdated
@mubarak23
mubarak23 merged commit 34454fa into main Jun 22, 2025
3 checks passed
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.

Add Contract Upgrade Support and Refactor Core Functionality

1 participant