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: return funds to original sources when voiding holds #46

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gfyrag
Copy link
Contributor

@gfyrag gfyrag commented Feb 13, 2025

No description provided.

Copy link

coderabbitai bot commented Feb 13, 2025

Walkthrough

This pull request updates how transaction data is structured across several components and tests. The changes primarily transition the Vars maps from using nested maps with separate keys to using a simple string format (e.g. "USD 100"). Several test cases and functions have been modified accordingly—including those for holds confirmation, voiding holds, wallet credits/debits, and transaction listing. In addition, error handling has been enhanced in the manager’s VoidHold method, and the cancel hold script now iterates over postings instead of processing a single destination.

Changes

File(s) Change Summary
pkg/api/handler_holds_confirm_test.go, pkg/api/handler_wallets_credit_test.go, pkg/api/handler_wallets_debit_test.go Updated the Vars map in tests: converted from map[string]interface{} with nested structures to map[string]string with a single string for amount (e.g. "USD 100") while leaving other fields intact.
pkg/api/handler_holds_void_test.go Modified TestHoldsVoid: added a new WithListTransactions function and updated WithCreateTransaction to include detailed postings; removed the "dest" key in the variable map.
pkg/api/handler_transactions_list_test.go Changed the mapping function’s return type from shared.V2ExpandedTransaction to shared.V2Transaction, ensuring consistency in the transaction data structure.
pkg/manager.go Adjusted transaction variable handling by updating the Vars maps in the Debit, Credit, and VoidHold methods; added error checking in VoidHold for transaction count; updated the internal handling in ListTransactions to leverage shared.V2Transaction.
pkg/numscript/cancel-hold.num Removed the single destination variable and restructured the destination parameter: now uses a range loop over .Postings, applies a max operation per posting, and includes a remaining kept statement for residual assets.
pkg/scripts.go Updated BuildCancelHoldScript: changed its signature to accept a variadic parameter (postings ...shared.V2Posting), added the new import for shared, and included the Postings key in the template mapping.
pkg/transaction.go Modified the Transaction struct to embed shared.V2Transaction instead of shared.V2ExpandedTransaction and updated the JSON marshaling accordingly.
test/e2e/debit_test.go Introduced a new test context for wallet debits with secondary balances, including creating a secondary balance, crediting, debiting with a hold, and verifying the balance post-void.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Manager
    participant Ledger
    participant ScriptBuilder

    Client->>Manager: Request to void hold
    Manager->>Ledger: List transactions for hold
    Ledger-->>Manager: Return transaction list
    alt Exactly one transaction found
        Manager->>ScriptBuilder: BuildCancelHoldScript(postings)
        ScriptBuilder-->>Manager: Return cancel hold script
        Manager->>Ledger: Submit void hold transaction
        Ledger-->>Manager: Confirmation
        Manager-->>Client: Success response
    else Transaction count != 1
        Manager-->>Client: Error ("expected 1 transaction")
    end
Loading

Suggested reviewers

  • paul-nicolas

Poem

I'm a little rabbit, hopping with glee,
Changes in the code make a sweeter tea.
Vars now sing as a single string,
Void holds check, and the scripts take wing.
Quiet code crunching, carrots in delight,
Hop along, for our code is just right!
🥕🐇 Happy coding under the moonlight!

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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>, please review it.
    • 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 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 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.
    • @coderabbitai help me debug CodeRabbit configuration file.

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. (Beta)
  • @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.

Copy link

@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: 0

🧹 Nitpick comments (1)
pkg/scripts.go (1)

54-59: Add validation for postings parameter.

Consider adding validation to ensure postings is not empty to prevent potential runtime errors.

 func BuildCancelHoldScript(asset string, postings ...shared.V2Posting) string {
+	if len(postings) == 0 {
+		panic("postings cannot be empty")
+	}
 	return renderTemplate(CancelHoldScript, map[string]any{
 		"Asset":        asset,
 		"Postings": postings,
 	})
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 4a3972a and f800c70.

⛔ Files ignored due to path filters (2)
  • go.mod is excluded by !**/*.mod
  • go.sum is excluded by !**/*.sum, !**/*.sum
📒 Files selected for processing (10)
  • pkg/api/handler_holds_confirm_test.go (3 hunks)
  • pkg/api/handler_holds_void_test.go (1 hunks)
  • pkg/api/handler_transactions_list_test.go (2 hunks)
  • pkg/api/handler_wallets_credit_test.go (5 hunks)
  • pkg/api/handler_wallets_debit_test.go (9 hunks)
  • pkg/manager.go (7 hunks)
  • pkg/numscript/cancel-hold.num (1 hunks)
  • pkg/scripts.go (2 hunks)
  • pkg/transaction.go (1 hunks)
  • test/e2e/debit_test.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Tests
🔇 Additional comments (22)
pkg/numscript/cancel-hold.num (1)

7-12: LGTM! Improved hold cancellation logic.

The new implementation correctly returns funds to their original sources by:

  1. Iterating over postings to handle multiple sources
  2. Using max operation to ensure correct amount is returned
  3. Safely handling any remaining funds
pkg/transaction.go (2)

9-12: LGTM! Simplified transaction structure.

The change to V2Transaction provides a cleaner structure while maintaining all necessary functionality.


14-25: LGTM! MarshalJSON updated correctly.

The MarshalJSON method has been properly updated to use V2Transaction while preserving the ledger field.

pkg/api/handler_holds_void_test.go (2)

48-69: LGTM! Comprehensive test coverage for transaction listing.

The mock implementation correctly provides the original transaction data needed for returning funds to their sources.


70-94: LGTM! Thorough validation of hold cancellation.

The test verifies that funds are correctly returned to both secondary and main balance accounts with proper amounts and assets.

pkg/api/handler_transactions_list_test.go (1)

66-67: LGTM! Clean type simplification.

The change from V2ExpandedTransaction to V2Transaction maintains all necessary fields while simplifying the data structure.

Also applies to: 89-90

pkg/api/handler_wallets_credit_test.go (1)

45-48: LGTM! Consistent simplification of transaction data structure.

The changes streamline the Vars map by using a simpler string format for amounts (e.g., "USD 100") across all test cases, making the code more maintainable and consistent.

Also applies to: 72-75, 95-98, 114-117, 142-145

pkg/api/handler_holds_confirm_test.go (1)

47-51: LGTM! Consistent hold transaction data structure.

The changes maintain the same simplified string format for amounts in hold confirmation tests, ensuring consistency across the codebase.

Also applies to: 99-103, 231-236

test/e2e/debit_test.go (1)

51-126: Great addition of test coverage for secondary balance operations!

The new test cases effectively verify that:

  1. Funds can be debited from a secondary balance using holds
  2. Voiding a hold correctly returns funds to the original secondary balance
  3. Balance amounts are accurately tracked before and after voiding holds

This directly supports the PR objective of fixing fund returns to original sources when voiding holds.

pkg/api/handler_wallets_debit_test.go (8)

58-61: LGTM! The amount format change is consistent.

The simplified string format for the amount variable improves readability and reduces complexity.


77-80: LGTM! The amount format change is consistent.

The simplified string format for the amount variable improves readability and reduces complexity.


97-100: LGTM! The amount format change is consistent.

The simplified string format for the amount variable improves readability and reduces complexity.


116-119: LGTM! The amount format change is consistent.

The simplified string format for the amount variable improves readability and reduces complexity.


152-155: LGTM! The amount format change is consistent.

The simplified string format for the amount variable improves readability and reduces complexity.


174-177: LGTM! The amount format change is consistent.

The simplified string format for the amount variable improves readability and reduces complexity.


193-196: LGTM! The amount format change is consistent.

The simplified string format for the amount variable improves readability and reduces complexity.


233-236: LGTM! The amount format change is consistent.

The simplified string format for the amount variable improves readability and reduces complexity.

pkg/manager.go (5)

166-169: LGTM! The amount format change is consistent.

The simplified string format for the amount variable improves readability and reduces complexity.


207-211: LGTM! The amount format change is consistent.

The simplified string format for the amount variable improves readability and reduces complexity.


284-287: LGTM! The amount format change is consistent.

The simplified string format for the amount variable improves readability and reduces complexity.


405-409: LGTM! Simplified transaction data structure.

The change from shared.V2ExpandedTransaction to shared.V2Transaction aligns with the overall effort to simplify data structures.


232-241:

❓ Verification inconclusive

Improved error handling and fund return logic.

The changes enhance the robustness of the VoidHold method by:

  1. Adding validation to ensure exactly one transaction is retrieved.
  2. Iterating over postings to return funds to their original sources.

These changes align with the PR objective and improve error handling.

Run the following script to verify the impact of these changes:

Also applies to: 255-255


🏁 Script executed:

#!/bin/bash
# Description: Verify the usage of postings in the cancel hold script.

# Test: Search for the BuildCancelHoldScript function to understand how postings are used.
ast-grep --pattern $'func BuildCancelHoldScript($_, $$$) {
  $$$
}'

Length of output: 68


Please verify further the BuildCancelHoldScript usage.

The ast-grep command produced no output, which is inconclusive—it may indicate that the expected function pattern isn’t present or is defined differently. I recommend running an additional script (e.g., using ripgrep) to search for "BuildCancelHoldScript" across the codebase for further confirmation of its usage in the cancel hold logic.

  • Verify that the BuildCancelHoldScript function exists and that its implementation correctly handles postings.
  • Confirm that the changes in error handling and iteration over postings align with the intended functionality in the cancel hold process.

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.

1 participant