Skip to content
This repository was archived by the owner on May 27, 2025. It is now read-only.

fix: handle edge case when change amount is below minimum capacity#313

Open
fghdotio wants to merge 1 commit into
utxostack:developfrom
fghdotio:feat/offline-utxo
Open

fix: handle edge case when change amount is below minimum capacity#313
fghdotio wants to merge 1 commit into
utxostack:developfrom
fghdotio:feat/offline-utxo

Conversation

@fghdotio
Copy link
Copy Markdown
Contributor

When actualInputsCapacity > sumOutputsCapacity but the resulting change amount is below MIN_CAPACITY, we also need to collect additional cells to ensure the change output meets the minimum capacity requirement. This prevents creation of invalid cells with insufficient capacity.

const txFee = MAX_FEE;
if (actualInputsCapacity <= sumOutputsCapacity) {
let changeCapacity = actualInputsCapacity - sumOutputsCapacity;
while (changeCapacity < MIN_CAPACITY) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why do you use a while loop? How do you return from the loop?

Copy link
Copy Markdown
Collaborator

@duanyytop duanyytop Feb 26, 2025

Choose a reason for hiding this comment

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

I think if the changeCapacity < MIN_CAPACITY is true, throwing an error is enough for issuers.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Initially I mistook txFee for fee rate - I thought the loop ensures sufficient capacity to cover increasing transaction fees as the transaction size grows with each new input from collectInputs. As actualInputsCapacity accumulates in the loop, changeCapacity gradually increases until it either exceeds MIN_CAPACITY for normal exit, or throws CapacityNotEnoughError when available cells are insufficient.

Upon closer inspection after reading your comments, I realize a while loop is unnecessary since txFee is set to a sufficiently large MAX_FEE constant. A simple if statement would be adequate here.

Despite its name, appendIssuerCellToBtcBatchTransferToSign serves as a general fee supplement method in offline mode. Perhaps we should collect additional issuer's cells as inputs to cover tx fee whenever possible, instead of throwing an error. Please let me know your thoughts.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

  1. The MAX_FEE is 0.2CKB, and it is enough for almost transactions.
  2. If the issuer's balance is not enough for collecting, the loop does not work.
  3. I think you could provide a capacity estimate function for the issuer before collecting cells.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point! I'll try to implement it later.

Comment thread packages/ckb/src/rgbpp/btc-transfer.ts
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants