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.
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:
ada-only first, with >= 5 ada, prioritized by lowest ada amount
then outputs with as few native tokens as possible, because each token increases the Tx size, thus increases the Tx fees. Also >= 5 ada, prioritized by lowest ada amount
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).
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: