Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3217: Make `UTxOIndex` agnostic to the distinction between ada and non-ada assets. r=jonathanknowles a=jonathanknowles ## Issue Number ADP-1419 ## Summary This PR makes the `UTxOIndex` type **_agnostic_** to the distinction between **_ada_** and **_non-ada_** assets. ## Motivation We want to evolve the coin selection algorithm so that: - the concept of "value" (provided by inputs and consumed by outputs) is **_generalized_**. - all asset types are treated equally, with no special treatment for **_ada_** versus **_non-ada_** assets. Currently, there are a few obstacles that prevent such a generalization: 1. The `UTxOIndex` type indexes ada quantities differently from other assets. 2. The `computeMinimumCost` function is ada-specific. 3. The `computeMinimumAdaQuantity` function is ada-specific. This PR tackles the **_first_** of these obstacles. ## Details We make the following change to `SelectionFilter`: ```patch - data SelectionFilter - = WithAdaOnly - | WithAssetOnly AssetId - | WithAsset AssetId - | Any + data SelectionFilter asset + = SelectSingleton asset + | SelectPairWith asset + | SelectAnyWith asset + | SelectAny ``` We also (temporarily) introduce a type called `Asset`, which can represent both ada and non-ada assets: ```hs data Asset = AssetLovelace | Asset AssetId ``` This allows us to generalize the way we perform random selection for ada and non-ada assets in the `Balance` module: ```patch selectAdaQuantity = - selectMatchingQuantity (WithAdaOnly :| [Any]) + selectMatchingQuantity + [ SelectSingleton AssetLovelace + , SelectPairWith AssetLovelace + , SelectAnyWith AssetLovelace + ] selectNonAdaAssetQuantity asset = - selectMatchingQuantity [WithAssetOnly asset, WithAsset asset] + selectMatchingQuantity + [ SelectSingleton (Asset asset) + , SelectPairWith (Asset asset) + , SelectAnyWith (Asset asset) + ] ``` In the above functions, selection filters are processed in the following order of priority: 1. `SelectSingleton` matches UTxOs that contain **_just_** the given asset and **_no other_** assets 2. `SelectPairWith` matches UTxOs that contain the given asset and **_one other_** asset. 2. `SelectAnyWith` matches UTxOs that contain the given asset and **_any number_** of other assets. Because both selection functions now share **_exactly the same priority order_**, we can simplify these definitions even further by extracting out the shared priority order and parameterizing over the type of asset. Co-authored-by: Jonathan Knowles <[email protected]>
- Loading branch information