Question about Box Selection in Transaction Construction Using ergpy

Question about Box Selection in Transaction Construction Using ergpy

Context:

While attempting to construct a transaction using ergpy (the Python wrapper for the appkit), I encountered an unusual situation related to the selection of input boxes. In my case, I have two available input boxes:

  1. InputBox 1: 90ea90ca0cd4af9465f94d90bf667aca21ef5984c4e9196b83e862fe028301c8 with a balance of 796,500,000 nanoERGs.
  2. InputBox 2: 9ec3c2608afab30dd3a7140ed0e508908d99fe3b86eef68c99539106dd3b2654 with a balance of 794,500,000 nanoERGs.

When constructing the transaction, I need an output of 795,500,000 nanoERGs and a fee of 1,000,000 nanoERGs.

Issue:

Despite the fact that the first box (InputBox 1) has more ERGs and should be sufficient to cover the output and the fee, the method buildUnsignedTransaction() ends up selecting the second box (InputBox 2), which has fewer ERGs. This results in an error, as the selected box does not have enough balance to cover both the output and the fee.

The specific error encountered is:

reason: NotEnoughErgsError(not enough boxes to meet ERG needs 796499900 (found only 796500000),796500000)

This error is thrown at the following line of code in the ergo-appkit repository:

Additional Observation:

If the transaction is built using the box with the smaller balance (InputBox 2), the transaction executes correctly. In this case, the output is the same balance as the selected box minus the fee. You can see the relevant code in the following link for testing:

Questions:

  1. Why is the box selection algorithm in ergpy choosing the box with the smaller amount of ERGs (InputBox 2) instead of the one with more (InputBox 1)?
  2. Is there a mechanism in transaction construction that prioritizes spending from boxes with smaller amounts of ERGs before those with larger amounts?
  3. If box selection is handled automatically, what is the purpose of specifying input boxes when constructing the transaction if the buildUnsignedTransaction() function might ignore the optimal box?
  4. How can I ensure that the appropriate box (the one with more ERGs) is selected to avoid errors in the transaction?
  5. Is it known if the version of appkit used in ergpy is the same as the version in the ergo-appkit repository? A discrepancy in versions might be causing this unexpected behavior.
1 Like

A few things.

  1. Noticed you committed “avoid notEnoughErgs error” around the time you posted this, did you find the solution?

  2. I copied and ran this locally and it got past the buildUnsignedTransaction call so I just want to highlight this edge case is somewhat difficult to reproduce (assuming you didnt commit the fix already, just noticed that).

  3. The mnemonic hardcoded in the file is convenient (assuming its testnet only lol) but is really bad practice so please avoid that in the future.

  4. Ergpy could overall use some work to make it more intuitive and less prone to throwing exceptions all over the place

2 Likes

Thank you for the response.

As a temporary solution, I select as input the box with the lowest balance. This way, the error doesn’t occur. I understand that it might not reproduce in all cases. I’ve been reviewing the AppKit code, and I still don’t see why the error would occur when currentBalance == targetBalance.

P.S.: The mnemonic key comes from the Ergpy repository, but thanks for the advice.

P.S-2: If I have some free time, I might work on improving a few things in Ergpy, as I plan to use it.

1 Like