chore:move write function logic into private function#111
Conversation
WalkthroughThe 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
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
sequenceDiagram
participant Owner
participant Contract
Owner->>Contract: upgrade(new_class_hash)
Contract->>Contract: (Checks owner, performs upgrade)
Contract-->>Owner: (Upgrade complete)
Possibly related PRs
Suggested reviewers
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms (1)
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed 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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (6)
src/campaign_donation.cairo (6)
161-163: Remove unnecessary cloning of value typesCairo passes value types like
felt252andu256by 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 namingCloning 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 parametersSince 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 cloningCloning the
campaign_idvalue 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 parameterSince 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 formattingThe 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
📒 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 correctThe upgrade function properly checks ownership before allowing upgrades, following the standard pattern for upgradeable contracts.
closes #113
Summary by CodeRabbit
New Features
Refactor