Cardano.Value

Handling Cardano values.

type alias Value =
{ lovelace : Natural
, assets : MultiAsset Natural
}

A multi-asset output Value. Contains tokens indexed by policy id and asset name.

This type maintains some invariants by construction. In particular, a Value will never contain a zero quantity of a particular token.

TODO: make sure the previous statement stays true by construction. That would require an opaque type and some property tests.

zero : Value

Empty [Value] with 0 ada and no token.

onlyLovelace : Natural -> Value

Create a [Value] just containing Ada lovelaces.

onlyToken : Bytes PolicyId -> Bytes AssetName -> Natural -> Value

Create a [Value] just from some token amount.

add : Value -> Value -> Value

Add the values of two UTxOs together.

addTokens : MultiAsset Natural -> Value -> Value

Add some tokens to another [Value].

subtract : Value -> Value -> Value

subtract the second value from the first one: (v1 - v2).

It’s a saturating difference, so if the second value is bigger than the first, the difference is clamped to 0.

The resulting [Value] is not normalized by default. So the result may contain assets with 0 amounts. To remove all 0 amount assets, call [normalize] on the substraction result.

atLeast : Value -> Value -> Bool

Check that some value contains at least some minimum value.

onlyLovelace Natural.two
  |> atLeast (onlyLovelace Natural.one)
  --> True
sum : List Value -> Value

Sum the values of all tokens.

normalize : Value -> Value

Remove 0 amounts in non-ada assets.

compare : (Value -> Natural) -> Value -> Value -> Order

Compare by amount of a given token.

encode : Value -> Encoder

CBOR encoder for [Value].

fromCbor : Decoder Value

CBOR decoder for [Value].

toMultilineString : Value -> List String

Helper function to display a Value.