| Copyright | © 2018-2024 Intersect MBO |
|---|---|
| License | Apache-2.0 |
| Safe Haskell | Safe-Inferred |
| Language | Haskell2010 |
Cardano.CoinSelection
Description
Provides general functions and types relating to coin selection.
The CoinSelection type represents a coin selection, the basis for a
transaction in a UTxO-based blockchain.
The CoinSelectionAlgorithm type provides a common interface to
algorithms that generate coin selections.
For a guide to algorithms provided by this library, see the Cardano.CoinSelection.Algorithm module.
To adjust coin selections in order to pay for transaction fees, see the Cardano.CoinSelection.Fee module.
Synopsis
- data CoinSelection i o = CoinSelection {}
- sumInputs :: CoinSelection i o -> Coin
- sumOutputs :: CoinSelection i o -> Coin
- sumChange :: CoinSelection i o -> Coin
- newtype CoinSelectionAlgorithm i o m = CoinSelectionAlgorithm {
- selectCoins :: CoinSelectionParameters i o -> ExceptT CoinSelectionError m (CoinSelectionResult i o)
- data CoinSelectionParameters i o = CoinSelectionParameters {}
- data CoinSelectionResult i o = CoinSelectionResult {
- coinSelection :: CoinSelection i o
- inputsRemaining :: CoinMap i
- newtype CoinSelectionLimit = CoinSelectionLimit {
- calculateLimit :: Word16 -> Word16
- data Coin
- coinFromNatural :: Natural -> Coin
- coinToNatural :: Coin -> Natural
- newtype CoinMap a = CoinMap {}
- data CoinMapEntry a = CoinMapEntry {
- entryKey :: a
- entryValue :: Coin
- coinMapFromList :: Ord a => [CoinMapEntry a] -> CoinMap a
- coinMapToList :: CoinMap a -> [CoinMapEntry a]
- coinMapValue :: CoinMap a -> Coin
- data CoinSelectionError
- data InputValueInsufficientError = InputValueInsufficientError {}
- data InputCountInsufficientError = InputCountInsufficientError {}
- newtype InputLimitExceededError = InputLimitExceededError {}
- data InputsExhaustedError = InputsExhaustedError
Coin Selections
data CoinSelection i o Source #
A coin selection is the basis for a transaction.
It consists of a selection of inputs, outputs, and change.
The inputs and outputs fields are both maps of unique keys to associated
Coin values, where:
- Each key-value pair in the
inputsmap corresponds to an unspent output from a previous transaction (also known as a UTxO). The key is a unique reference to that output, and the value is the amount of unspent value associated with it. - Each key-value pair in the
outputsmap corresponds to a payment. The key is a unique reference to a payment recipient, and the value is the amount of money to pay to that recipient.
The change field is a set of coins to be returned to the originator of the
transaction.
The CoinSelectionAlgorithm type provides a common interface for generating
coin selections.
Since: 1.0.0
Constructors
| CoinSelection | |
Instances
sumInputs :: CoinSelection i o -> Coin Source #
Calculate the total sum of all inputs for the given CoinSelection.
Since: 1.0.0
sumOutputs :: CoinSelection i o -> Coin Source #
Calculate the total sum of all outputs for the given CoinSelection.
Since: 1.0.0
sumChange :: CoinSelection i o -> Coin Source #
Calculate the total sum of all change for the given CoinSelection.
Since: 1.0.0
Coin Selection Algorithms
newtype CoinSelectionAlgorithm i o m Source #
Provides a common interface for coin selection algorithms.
The function selectCoins, when applied to the given
CoinSelectionParameters object (with available inputs and requested
outputs), will generate a CoinSelectionResult (with remaining inputs
and a coin selection).
For implementations provided by this library, see Cardano.CoinSelection.Algorithm.
Since: 1.0.0
Constructors
| CoinSelectionAlgorithm | |
Fields
| |
data CoinSelectionParameters i o Source #
The complete set of parameters required for a CoinSelectionAlgorithm.
The inputsAvailable and outputsRequested fields are both maps of unique
keys to associated Coin values, where:
- Each key-value pair in the
inputsAvailablemap corresponds to an unspent output from a previous transaction that is available for selection as an input by the coin selection algorithm. The key is a unique reference to that output, and the value is the amount of unspent value associated with it. - Each key-value pair in the
outputsRequestedmap corresponds to a payment whose value is to be paid for by the coin selection algorithm. The key is a unique reference to a payment recipient, and the value is the amount of money to pay to that recipient.
A coin selection algorithm will select a subset of inputs from
inputsAvailable in order to pay for all the outputs in
outputsRequested, where:
- Inputs selected by the algorithm are included in the
inputsset of the generatedCoinSelection. - Inputs not selected by the algorithm are included in the
inputsRemainingset of theCoinSelectionResult.
The number of inputs that can selected is limited by limit.
The total value of inputsAvailable must be greater than or equal to
the total value of outputsRequested, as given by the coinMapValue
function.
Since: 1.0.0
Constructors
| CoinSelectionParameters | |
Fields
| |
Instances
| Generic (CoinSelectionParameters i o) Source # | |
Defined in Cardano.CoinSelection Methods from :: CoinSelectionParameters i o -> Rep (CoinSelectionParameters i o) x Source # to :: Rep (CoinSelectionParameters i o) x -> CoinSelectionParameters i o Source # | |
| type Rep (CoinSelectionParameters i o) Source # | |
Defined in Cardano.CoinSelection type Rep (CoinSelectionParameters i o) = D1 ('MetaData "CoinSelectionParameters" "Cardano.CoinSelection" "cardano-coin-selection-1.0.1-inplace" 'False) (C1 ('MetaCons "CoinSelectionParameters" 'PrefixI 'True) (S1 ('MetaSel ('Just "inputsAvailable") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (CoinMap i)) :*: (S1 ('MetaSel ('Just "outputsRequested") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (CoinMap o)) :*: S1 ('MetaSel ('Just "limit") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 CoinSelectionLimit)))) | |
data CoinSelectionResult i o Source #
Constructors
| CoinSelectionResult | |
Fields
| |
Instances
| (Show i, Show o) => Show (CoinSelectionResult i o) Source # | |
Defined in Cardano.CoinSelection | |
| (Eq i, Eq o) => Eq (CoinSelectionResult i o) Source # | |
Defined in Cardano.CoinSelection Methods (==) :: CoinSelectionResult i o -> CoinSelectionResult i o -> Bool Source # (/=) :: CoinSelectionResult i o -> CoinSelectionResult i o -> Bool Source # | |
newtype CoinSelectionLimit Source #
Defines an inclusive upper bound on the number of inputs that
a CoinSelectionAlgorithm is allowed to select.
Since: 1.0.0
Constructors
| CoinSelectionLimit | |
Fields
| |
Instances
| Generic CoinSelectionLimit Source # | |
Defined in Cardano.CoinSelection Methods from :: CoinSelectionLimit -> Rep CoinSelectionLimit x Source # to :: Rep CoinSelectionLimit x -> CoinSelectionLimit Source # | |
| type Rep CoinSelectionLimit Source # | |
Defined in Cardano.CoinSelection type Rep CoinSelectionLimit = D1 ('MetaData "CoinSelectionLimit" "Cardano.CoinSelection" "cardano-coin-selection-1.0.1-inplace" 'True) (C1 ('MetaCons "CoinSelectionLimit" 'PrefixI 'True) (S1 ('MetaSel ('Just "calculateLimit") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Word16 -> Word16)))) | |
Coins
Represents a non-negative integral amount of currency.
Use coinFromNatural to create a coin from a natural number.
Use coinToNatural to convert a coin into a natural number.
Since: 1.0.0
coinFromNatural :: Natural -> Coin Source #
Creates a coin from a natural number.
Since: 1.0.0
coinToNatural :: Coin -> Natural Source #
Converts the given coin into a natural number.
Since: 1.0.0
Coin Maps
A mapping from unique keys to associated Coin values.
A CoinMap can be used to represent:
- a UTxO set, where each key within the map refers to an unspent output from a previous transaction.
- a set of
inputsto aCoinSelection, where each input is an entry selected from a UTxO set by aCoinSelectionAlgorithm. - a set of
outputsfor aCoinSelection, where each key within the map refers to the address of a payment recipient.
A CoinMap can be constructed with the coinMapFromList function.
The total value of a CoinMap is given by the coinMapValue function.
Since: 1.0.0
Instances
data CoinMapEntry a Source #
An entry for a CoinMap.
Since: 1.0.0
Constructors
| CoinMapEntry | |
Fields
| |
Instances
coinMapFromList :: Ord a => [CoinMapEntry a] -> CoinMap a Source #
coinMapToList :: CoinMap a -> [CoinMapEntry a] Source #
coinMapValue :: CoinMap a -> Coin Source #
Calculates the total coin value associated with a CoinMap.
Since: 1.0.0
Coin Selection Errors
data CoinSelectionError Source #
Represents the set of possible failures that can occur when attempting
to produce a CoinSelection with a CoinSelectionAlgorithm.
See selectCoins.
Since: 1.0.0
Constructors
| InputValueInsufficient InputValueInsufficientError | |
| InputCountInsufficient InputCountInsufficientError | |
| InputLimitExceeded InputLimitExceededError | |
| InputsExhausted InputsExhaustedError |
Instances
| Show CoinSelectionError Source # | |
Defined in Cardano.CoinSelection | |
| Eq CoinSelectionError Source # | |
Defined in Cardano.CoinSelection Methods (==) :: CoinSelectionError -> CoinSelectionError -> Bool Source # (/=) :: CoinSelectionError -> CoinSelectionError -> Bool Source # | |
data InputValueInsufficientError Source #
Indicates that the total value of inputsAvailable is less than the total
value of outputsRequested, making it impossible to cover all payments,
regardless of which algorithm is chosen.
Since: 1.0.0
Constructors
| InputValueInsufficientError | |
Fields
| |
Instances
| Show InputValueInsufficientError Source # | |
Defined in Cardano.CoinSelection | |
| Eq InputValueInsufficientError Source # | |
Defined in Cardano.CoinSelection Methods (==) :: InputValueInsufficientError -> InputValueInsufficientError -> Bool Source # (/=) :: InputValueInsufficientError -> InputValueInsufficientError -> Bool Source # | |
data InputCountInsufficientError Source #
Indicates that the total count of entries in inputsAvailable is fewer
than required by the algorithm. The number required depends on the
particular algorithm implementation.
Since: 1.0.0
Constructors
| InputCountInsufficientError | |
Fields
| |
Instances
| Show InputCountInsufficientError Source # | |
Defined in Cardano.CoinSelection | |
| Eq InputCountInsufficientError Source # | |
Defined in Cardano.CoinSelection Methods (==) :: InputCountInsufficientError -> InputCountInsufficientError -> Bool Source # (/=) :: InputCountInsufficientError -> InputCountInsufficientError -> Bool Source # | |
newtype InputLimitExceededError Source #
Indicates that the coin selection algorithm is unable to cover the total
value of outputsRequested without exceeding the maximum number of inputs
defined by limit.
See calculateLimit.
Since: 1.0.0
Constructors
| InputLimitExceededError | |
Fields | |
Instances
| Show InputLimitExceededError Source # | |
Defined in Cardano.CoinSelection | |
| Eq InputLimitExceededError Source # | |
Defined in Cardano.CoinSelection Methods (==) :: InputLimitExceededError -> InputLimitExceededError -> Bool Source # (/=) :: InputLimitExceededError -> InputLimitExceededError -> Bool Source # | |
data InputsExhaustedError Source #
Indicates that all available entries in inputsAvailable were depleted
before all the payments in outputsRequested could be paid for.
This condition can occur even if the total value of inputsAvailable is
greater than or equal to the total value of outputsRequested, due to
differences in the way that algorithms select inputs.
Since: 1.0.0
Constructors
| InputsExhaustedError |
Instances
| Show InputsExhaustedError Source # | |
Defined in Cardano.CoinSelection | |
| Eq InputsExhaustedError Source # | |
Defined in Cardano.CoinSelection Methods (==) :: InputsExhaustedError -> InputsExhaustedError -> Bool Source # (/=) :: InputsExhaustedError -> InputsExhaustedError -> Bool Source # | |