Getting Started¶
Installation¶
Add cardano-coin-selection to your build-depends in your .cabal file:
The package is available from the Cardano Haskell Package repository (CHaP).
Add CHaP to your cabal.project:
Module imports¶
The library is designed around qualified imports:
import qualified Cardano.CoinSelection as CS
import qualified Cardano.CoinSelection.Balance as Balance
import qualified Cardano.CoinSelection.Collateral as Collateral
import qualified Cardano.CoinSelection.UTxOIndex as UTxOIndex
import qualified Cardano.CoinSelection.UTxOSelection as UTxOSelection
Defining a selection context¶
Before performing a selection, you need to define a SelectionContext that
specifies your address and UTxO identifier types:
data MyContext
instance SelectionContext MyContext where
type Address MyContext = MyAddress
type UTxO MyContext = MyTxIn
The only requirements are that both associated types have Ord and Show
instances.
Performing a selection¶
The main entry point is Cardano.CoinSelection.performSelection:
import Cardano.CoinSelection
( Selection
, SelectionConstraints (..)
, SelectionError
, SelectionParams (..)
, performSelection
)
result <- runExceptT $ performSelection constraints params
-- result :: Either (SelectionError MyContext) (Selection MyContext)
The performSelection function:
- Selects inputs from the UTxO set to cover user-specified outputs
- Selects inputs to cover collateral (if required)
- Produces change outputs to return excess value
- Balances the selection to pay for the transaction fee
See the Tutorial for a complete worked example.