cometa-0.1.0.0: Idiomatic Haskell bindings to cardano-c
Safe HaskellSafe-Inferred
LanguageHaskell2010

Cometa.Value

Description

Immutable signed asset bundles with arbitrary-precision quantities. Duplicate assets add, zero quantities disappear, and missing assets have quantity zero. Signed deltas compose without overflow or clamping.

import qualified Cometa.AssetId as AssetId
import qualified Cometa.Value as Value

remaining :: Value.Value -> Value.Value -> Integer
remaining available required =
  Value.quantity AssetId.Ada (Value.difference available required)
Synopsis

Documentation

data Value Source #

A finite bundle of signed asset quantities. Ada quantities count lovelace; native quantities count units of the corresponding asset. Equality compares quantities, ignoring absent zero entries. Show is diagnostic output. Signed values need not be valid transaction outputs or mint quantities; those uses impose additional constraints.

Instances

Instances details
Monoid Value Source #

The empty bundle is the identity for addition.

Instance details

Defined in Cometa.Value

Methods

mempty :: Value #

mappend :: Value -> Value -> Value #

mconcat :: [Value] -> Value #

Semigroup Value Source #

Pointwise addition with zero results removed. Associative and commutative.

Instance details

Defined in Cometa.Value

Methods

(<>) :: Value -> Value -> Value #

sconcat :: NonEmpty Value -> Value #

stimes :: Integral b => b -> Value -> Value #

Show Value Source # 
Instance details

Defined in Cometa.Value

Methods

showsPrec :: Int -> Value -> ShowS #

show :: Value -> String #

showList :: [Value] -> ShowS #

Eq Value Source # 
Instance details

Defined in Cometa.Value

Methods

(==) :: Value -> Value -> Bool #

(/=) :: Value -> Value -> Bool #

Construction

singleton :: AssetId -> Integer -> Value Source #

One asset quantity. Zero produces mempty; negative quantities are retained.

lovelace :: Integer -> Value Source #

An Ada quantity measured in lovelace. Zero produces mempty; negative amounts represent signed Ada deltas.

fromList :: [(AssetId, Integer)] -> Value Source #

Assemble entries by adding duplicate assets and removing zero totals. Input order does not affect the resulting value.

Inspection

toList :: Value -> [(AssetId, Integer)] Source #

Nonzero entries in ascending AssetId order: Ada, then policy/name bytes lexicographically. This is not insertion order or CBOR map ordering.

quantity :: AssetId -> Value -> Integer Source #

Quantity of the requested asset, or zero when absent.

isZero :: Value -> Bool Source #

Whether all quantities are zero, equivalently whether the bundle is empty.

isNonNegative :: Value -> Bool Source #

Whether every quantity is nonnegative. True for the empty bundle. This inspection does not check quantity limits, minimum Ada or other output requirements.

assetCount :: Value -> Int Source #

Number of distinct nonzero assets, including Ada when its quantity is nonzero. Negative amounts count as present, just as positive amounts do.

intersection :: Value -> Value -> Value Source #

Left-biased intersection: retain the left quantities for assets with nonzero quantities in both bundles, regardless of sign. This includes signed Ada; it does not clamp amounts or take a minimum. Results use normal asset ordering. Useful for restricting a requested bundle to available asset kinds.

Arithmetic

negate :: Value -> Value Source #

Negate every quantity exactly. The sum of a value and its negation is zero.

difference :: Value -> Value -> Value Source #

Left value minus right value, retaining negative results.

scale :: Integer -> Value -> Value Source #

Multiply every quantity by an arbitrary signed scalar. Scaling by zero yields mempty; no quantity limit is imposed.

positivePart :: Value -> Value Source #

Retain strictly positive quantities, including Ada. The sum of positivePart and negativePart recovers the original signed value.

negativePart :: Value -> Value Source #

Retain strictly negative quantities, including Ada, without negating them.

Serialization

encodeCbor :: Value -> Either Error ByteString Source #

Encode a standalone native value as an unsigned Ada integer, or [Ada, {policy: {name: signed quantity}}] when native assets are present. Ada must be in 0 through 2^63-1; native quantities must fit signed 64 bits. Invalid quantities return InvalidValueQuantity before encoding. These are serialization bounds, not transaction output validation: negative native quantities are retained. Maps use canonical byte-length/lexicographic order. This is fresh semantic encoding; zero entries have already been removed.

decodeCbor :: ByteString -> Either Error Value Source #

Decode exactly one complete standalone value. Definite or indefinite arrays and maps and nonminimal integers are accepted. Duplicate policies or names, invalid identifiers, out-of-range quantities, wrong schemas and trailing bytes return InvalidValueCbor. Zero entries and empty policy maps normalize away; original bytes are not retained. Ada is limited to 0 through 2^63-1.

encodeMintCbor :: Value -> Either Error ByteString Source #

Encode a mint/burn bundle as {policy: {name: signed quantity}} CBOR. Native quantities must fit signed 64 bits, otherwise InvalidValueQuantity. Any nonzero Ada returns InvalidValueCbor: Ada cannot be minted. Empty bundles encode an empty map. This does not validate policies or witnesses against a transaction; identifiers are already validated by their types.

decodeMintCbor :: ByteString -> Either Error Value Source #

Decode exactly one complete standalone mint/burn map. Policy and name lengths, signed 64-bit quantities and duplicate keys are checked. Invalid encodings or trailing bytes return InvalidValueCbor. Definite/indefinite maps are accepted; zero entries and empty policy maps normalize away.