mpizenberg / elm-cardano

Elm offchain package for Cardano
https://elm-doc-preview.netlify.app/Cardano?repo=mpizenberg%2Felm-cardano&version=elm-doc-preview
BSD 3-Clause "New" or "Revised" License
11 stars 3 forks source link

Improve collateral selection to accept UTxOs with native tokens #58

Open mpizenberg opened 2 hours ago

mpizenberg commented 2 hours ago

Currently, collateral selection is done using the same strategy that the generic coin selection algorithm, but limited to UTxOs containing only Ada. For newcomers, this can become an issue because they might start with a single UTxO, use it in DeFi, and end up with another single UTxOs also containing native tokens.

To solve this UX problem, CIP-40 was introduced: https://cips.cardano.org/cip/CIP-40. It enables usage of UTxOs also containing native tokens, as long as we specify a collateral change, where the difference between collateral in, and collateral out is Ada only.

Currently, the code handling the collateral only partially support CIP-40. It already computes the collateral change, but is restricted to Ada-only input UTxOs.

https://github.com/mpizenberg/elm-cardano/blob/e23413ab78d70ce4168670b0202352f2bead219b/src/Cardano.elm#L1366

We need to change it, not use the default coin selection algorithm (largest first). Instead, it should start by sorting UTxOs by preference for collateral:

keyan-m commented 1 hour ago

One other restriction to keep in mind is the number of UTxOs; currently at most 3 UTxOs can be used for collateral (max_collateral_inputs under protocol parameters).