<p>You are speaking about what we call <em>Coin Selection Algorithms</em> and they are described in <a href="https://cips.cardano.org/cips/cip2/" rel="nofollow noreferrer">CIP2</a>.</p>
<p>There are several implementations floating around on Github, such as  <a href="https://github.com/Berry-Pool/nami-wallet/blob/main/src/lib/coinSelection.js" rel="nofollow noreferrer">BerryPool implementation of the Random-Improve coin selection algorithm</a> or <a href="https://github.com/Emurgo/yoroi-frontend/blob/develop/packages/yoroi-extension/app/api/ada/transactions/shelley/transactions.js" rel="nofollow noreferrer">what Yoroi does</a>.</p>

CC BY-SA 4.0
2021-11-13T03:25:00.760Z
Yes i have seen Berrys implementation, but it semed a little bit too complex, and the CIP2 apears to be in haskell right? I did one script that only querys one by one until it gets the minimun and then the extra is sended back to the owner, i dont know if this is enought for my dapp or if should I use Berry implementation.
Alicia Basilio
CC BY-SA 4.0
2021-11-13T19:50:09.000Z
No, the CIP describes the algorithm, these are language agnostic.
Marek Mahut - StakeNuts
CC BY-SA 4.0
2022-01-15T13:43:15.053Z
+1 for the links to the coin selection CIP and the implementations!
LACE pool
CC BY-SA 4.0
2022-01-15T13:52:51.533Z
The cardano-wallet's implementation in Haskell for coin selection can be found here: https://github.com/input-output-hk/cardano-wallet/blob/fe7632e44bfb6be9171069e3bf70b6a4e43bf6ff/lib/core/src/Cardano/Wallet/Primitive/CoinSelection.hs
LACE pool
CC BY-SA 4.0
2022-11-02T23:01:05.570Z
@MarekMahut-StakeNuts CIP20 states "Problems relating to network fees, and how to adjust coin selections to pay for such fees, are outside the scope of this article." Any ideas how to solve that?
wutzebaer
CC BY-SA 4.0
2021-11-12T17:44:43.290Z
2021-11-12T17:44:43.290Z
Marek Mahut - StakeNuts
<p>Is there any prebuild method in Cardano Serialization Lib that allows to select some UTXOs that cover the balance needed for a transaction?</p>
<p>Or should i make a script that querys all the utxo in that addres and selects the first ones that covers this given amount?</p>

<utxo><cardano-serialization-lib>
How to select UTXOs needed for covering a given ada amount using the Cardano Serialization Lib?
# How to select UTXOs needed for covering a given ada amount using the Cardano Serialization Lib?

(Asked by: username_0 on 2021-11-11)

Is there any prebuild method in Cardano Serialization Lib that allows to select some UTXOs that cover the balance needed for a transaction?

Or should i make a script that querys all the utxo in that addres and selects the first ones that covers this given amount?

**Tags:** utxo, cardano-serialization-lib

---

## 1 answers

### Answer by username_1 on 2021-11-12. Score: 3 | Accepted answer

You are speaking about what we call *Coin Selection Algorithms* and they are described in [CIP2](https://cips.cardano.org/cips/cip2/).

There are several implementations floating around on Github, such as [BerryPool implementation of the Random-Improve coin selection algorithm](https://github.com/Berry-Pool/nami-wallet/blob/main/src/lib/coinSelection.js) or [what Yoroi does](https://github.com/Emurgo/yoroi-frontend/blob/develop/packages/yoroi-extension/app/api/ada/transactions/shelley/transactions.js).

> **Comments:**
> **username_0**: Yes i have seen Berrys implementation, but it semed a little bit too complex, and the CIP2 apears to be in haskell right? I did one script that only querys one by one until it gets the minimun and then the extra is sended back to the owner, i dont know if this is enought for my dapp or if should I use Berry implementation.  
> **username_1**: No, the CIP describes the algorithm, these are language agnostic.  
> **username_2**: +1 for the links to the coin selection CIP and the implementations!  
> **username_2**: The cardano-wallet's implementation in Haskell for coin selection can be found here: https://github.com/input-output-hk/cardano-wallet/blob/fe7632e44bfb6be9171069e3bf70b6a4e43bf6ff/lib/core/src/Cardano/Wallet/Primitive/CoinSelection.hs  
> **username_3**: @MarekMahut-StakeNuts CIP20 states "Problems relating to network fees, and how to adjust coin selections to pay for such fees, are outside the scope of this article." Any ideas how to solve that?  

---

