Return TransactionUnbalanced error for empty swap inputs/outputs #1381

Merged
crodas merged 1 commit from fix/error-message-empty-proofs into main 2025-12-03 09:49:42 +00:00
crodas commented 2025-12-03 01:08:17 +00:00 (Migrated from github.com)

Description

Fixes #1365 and probably #1356

When a swap request has empty inputs or empty outputs, the mint now correctly returns error code 11002 (TransactionUnbalanced) instead of 11010 (UnitMismatch).

The issue occurred because when inputs or outputs are empty, the unit verification returns None, and comparing Some(unit) != None would trigger a UnitMismatch error before the balance check could detect the actual problem.

Changes:

  • Add early validation in process_swap_request to catch empty inputs and return TransactionUnbalanced with proper amounts (0 input vs actual output amount)
  • Update verify_transaction_balanced to return TransactionUnbalanced when either input or output unit is None, instead of attempting unit comparison
  • Add test case documenting both scenarios:
    • Swap with inputs but empty outputs (attempting to destroy tokens)
    • Swap with empty inputs but outputs (attempting to create tokens)

This makes error handling more intuitive for API consumers, as the error code now correctly indicates the transaction is unbalanced (trying to create or destroy value) rather than suggesting a unit mismatch issue.


Notes to the reviewers


Suggested CHANGELOG Updates

CHANGED

ADDED

REMOVED

FIXED


Checklist

### Description Fixes #1365 and probably #1356 When a swap request has empty inputs or empty outputs, the mint now correctly returns error code 11002 (TransactionUnbalanced) instead of 11010 (UnitMismatch). The issue occurred because when inputs or outputs are empty, the unit verification returns `None`, and comparing `Some(unit) != None` would trigger a UnitMismatch error before the balance check could detect the actual problem. Changes: - Add early validation in process_swap_request to catch empty inputs and return TransactionUnbalanced with proper amounts (0 input vs actual output amount) - Update verify_transaction_balanced to return TransactionUnbalanced when either input or output unit is None, instead of attempting unit comparison - Add test case documenting both scenarios: - Swap with inputs but empty outputs (attempting to destroy tokens) - Swap with empty inputs but outputs (attempting to create tokens) This makes error handling more intuitive for API consumers, as the error code now correctly indicates the transaction is unbalanced (trying to create or destroy value) rather than suggesting a unit mismatch issue. ----- ### Notes to the reviewers <!-- In this section you can include notes directed to the reviewers, like explaining why some parts of the PR were done in a specific way --> ----- ### Suggested [CHANGELOG](https://github.com/cashubtc/cdk/blob/main/CHANGELOG.md) Updates <!-- Please do not edit the actual changelog but note what you changed here. --> #### CHANGED #### ADDED #### REMOVED #### FIXED ---- ### Checklist * [ ] I followed the [code style guidelines](https://github.com/cashubtc/cdk/blob/main/CODE_STYLE.md) * [ ] I ran `just final-check` before committing
thesimplekid (Migrated from github.com) reviewed 2025-12-03 01:08:17 +00:00
cdk-bot commented 2025-12-03 09:49:55 +00:00 (Migrated from github.com)

Successfully created backport PR for v0.14.x:

Successfully created backport PR for `v0.14.x`: - #1382
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cashubtc/cdk!1381
No description provided.