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:
Run in the browser¶
The hosted browser demo is available at https://cardano-foundation.github.io/cardano-coin-selection/demo/.
The demo runs the stdin-driven coin-select.wasm executable. The input is a
line-oriented text document: use utxo <id> <lovelace> for available UTxOs and
output <address> <lovelace> for target outputs. Blank lines are ignored.
To run the same executable locally, save the text as input.txt and pass it to
wasmtime on stdin:
In the browser bundle, web/src/bootstrap.js imports coin-select.wasm and
uses @bjorn3/browser_wasi_shim to provide WASI stdin, stdout, and stderr
around the same executable.
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.