Safe Haskell | None |
---|---|
Language | Haskell2010 |
Provides the
TokenBundle
type, which combines a
Coin
(lovelace) value
with a map of named token quantities, scoped by token policy.
This module is meant to be imported qualified. For example:
>>>
import qualified Cardano.Wallet.Primitive.Types.TokenBundle as TB
Synopsis
- data TokenBundle = TokenBundle { }
-
data
AssetId
=
AssetId
{
- tokenPolicyId :: ! TokenPolicyId
- tokenName :: ! TokenName
- empty :: TokenBundle
- fromFlatList :: Coin -> [( AssetId , TokenQuantity )] -> TokenBundle
- fromNestedList :: Coin -> [( TokenPolicyId , NonEmpty ( TokenName , TokenQuantity ))] -> TokenBundle
- fromNestedMap :: ( Coin , Map TokenPolicyId ( NonEmptyMap TokenName TokenQuantity )) -> TokenBundle
- fromTokenMap :: TokenMap -> TokenBundle
- toFlatList :: TokenBundle -> ( Coin , [( AssetId , TokenQuantity )])
- fromCoin :: Coin -> TokenBundle
- toCoin :: TokenBundle -> Maybe Coin
- isCoin :: TokenBundle -> Bool
- getCoin :: TokenBundle -> Coin
- setCoin :: TokenBundle -> Coin -> TokenBundle
- add :: TokenBundle -> TokenBundle -> TokenBundle
- subtract :: TokenBundle -> TokenBundle -> Maybe TokenBundle
- difference :: TokenBundle -> TokenBundle -> TokenBundle
- getQuantity :: TokenBundle -> AssetId -> TokenQuantity
- hasQuantity :: TokenBundle -> AssetId -> Bool
- setQuantity :: TokenBundle -> AssetId -> TokenQuantity -> TokenBundle
- equipartitionAssets :: TokenBundle -> NonEmpty a -> NonEmpty TokenBundle
- equipartitionQuantitiesWithUpperBound :: TokenBundle -> TokenQuantity -> NonEmpty TokenBundle
-
newtype
Lexicographic
a =
Lexicographic
{
- unLexicographic :: a
-
newtype
Flat
a =
Flat
{
- getFlat :: a
-
newtype
Nested
a =
Nested
{
- getNested :: a
- getAssets :: TokenBundle -> Set AssetId
- mapAssetIds :: ( AssetId -> AssetId ) -> TokenBundle -> TokenBundle
- unsafeSubtract :: TokenBundle -> TokenBundle -> TokenBundle
Types
data TokenBundle Source #
Combines a
Coin
(lovelace) value with a map of named token quantities,
grouped by token policy.
Instances
A combination of a token policy identifier and a token name that can be used as a compound identifier.
AssetId | |
|
Instances
Eq AssetId Source # | |
Ord AssetId Source # | |
Defined in Cardano.Wallet.Primitive.Types.TokenMap |
|
Read AssetId Source # | |
Show AssetId Source # | |
Generic AssetId Source # | |
NFData AssetId Source # | |
Defined in Cardano.Wallet.Primitive.Types.TokenMap |
|
type Rep AssetId Source # | |
Defined in Cardano.Wallet.Primitive.Types.TokenMap
type
Rep
AssetId
=
D1
('
MetaData
"AssetId" "Cardano.Wallet.Primitive.Types.TokenMap" "cardano-wallet-core-2022.7.1-AGKhlyz9liLKN3QqZD1gj" '
False
) (
C1
('
MetaCons
"AssetId" '
PrefixI
'
True
) (
S1
('
MetaSel
('
Just
"tokenPolicyId") '
NoSourceUnpackedness
'
SourceStrict
'
DecidedStrict
) (
Rec0
TokenPolicyId
)
:*:
S1
('
MetaSel
('
Just
"tokenName") '
NoSourceUnpackedness
'
SourceStrict
'
DecidedStrict
) (
Rec0
TokenName
)))
|
Construction
empty :: TokenBundle Source #
The empty token bundle.
fromFlatList :: Coin -> [( AssetId , TokenQuantity )] -> TokenBundle Source #
Creates a token bundle from a coin and a flat list of token quantities.
If a token name appears more than once in the list under the same policy, its associated quantities will be added together in the resultant bundle.
fromNestedList :: Coin -> [( TokenPolicyId , NonEmpty ( TokenName , TokenQuantity ))] -> TokenBundle Source #
Creates a token bundle from a coin and a nested list of token quantities.
If a token name appears more than once in the list under the same policy, its associated quantities will be added together in the resultant bundle.
fromNestedMap :: ( Coin , Map TokenPolicyId ( NonEmptyMap TokenName TokenQuantity )) -> TokenBundle Source #
fromTokenMap :: TokenMap -> TokenBundle Source #
Deconstruction
toFlatList :: TokenBundle -> ( Coin , [( AssetId , TokenQuantity )]) Source #
Converts a token bundle to a coin and a flat list of token quantities.
Coins
toCoin :: TokenBundle -> Maybe Coin Source #
Coerces a token bundle to an ada
Coin
value.
Returns a coin if (and only if) the token bundle has no other tokens.
isCoin :: TokenBundle -> Bool Source #
getCoin :: TokenBundle -> Coin Source #
setCoin :: TokenBundle -> Coin -> TokenBundle Source #
Sets the current ada
Coin
value for a token bundle.
Arithmetic
add :: TokenBundle -> TokenBundle -> TokenBundle Source #
Adds one token bundle to another.
subtract :: TokenBundle -> TokenBundle -> Maybe TokenBundle Source #
difference :: TokenBundle -> TokenBundle -> TokenBundle Source #
Analogous to
Set.difference
, return the difference between two token
maps.
The following property holds:
prop> x
leq
(x
difference
y)
add
y
Note that there's a
leq
rather than equality, which we'd expect if this was
subtraction of integers. I.e.
>>>
(0 - 1) + 1
0
whereas
>>>
let oneToken = fromFlatList coin [(aid, TokenQuantity 1)]
>>>
(mempty `difference` oneToken) `add` oneToken
oneToken
Quantities
getQuantity :: TokenBundle -> AssetId -> TokenQuantity Source #
Gets the quantity associated with a given asset.
If the given bundle does not have an entry for the specified asset, this function returns a value of zero.
hasQuantity :: TokenBundle -> AssetId -> Bool Source #
Returns true if and only if the given bundle has a non-zero quantity for the given asset.
setQuantity :: TokenBundle -> AssetId -> TokenQuantity -> TokenBundle Source #
Sets the quantity associated with a given asset.
If the given quantity is zero, the resultant bundle will not have an entry for the given asset.
Partitioning
:: TokenBundle |
The token bundle to be partitioned. |
-> NonEmpty a |
Represents the number of portions in which to partition the bundle. |
-> NonEmpty TokenBundle |
The partitioned bundles. |
Partitions a token bundle into
n
smaller bundles, where the asset sets
of the resultant bundles are disjoint.
In the resultant bundles, the smallest asset set size and largest asset set size will differ by no more than 1.
The ada
Coin
quantity is equipartitioned across the resulting bundles.
The quantities of each non-ada asset are unchanged.
equipartitionQuantitiesWithUpperBound Source #
:: TokenBundle | |
-> TokenQuantity |
Maximum allowable token quantity. |
-> NonEmpty TokenBundle |
The partitioned bundles. |
Partitions a token bundle into
n
smaller bundles, where the quantity of
each token is equipartitioned across the resultant bundles, with the goal
that no token quantity in any of the resultant bundles exceeds the given
upper bound.
The value
n
is computed automatically, and is the minimum value required
to achieve the goal that no token quantity in any of the resulting bundles
exceeds the maximum allowable token quantity.
Ordering
newtype Lexicographic a Source #
Defines a lexicographic ordering.
Instances
Serialization
When used with the
Buildable
or
ToJSON
instances, provides a flat
serialization style, where token quantities are paired with their asset
identifiers.
Instances
Eq a => Eq ( Flat a) Source # | |
Ord a => Ord ( Flat a) Source # | |
Defined in Cardano.Wallet.Primitive.Types.TokenMap |
|
Show a => Show ( Flat a) Source # | |
Generic ( Flat a) Source # | |
ToJSON ( Flat TokenMap ) Source # | |
FromJSON ( Flat TokenMap ) Source # | |
Buildable ( Flat TokenMap ) Source # | |
Buildable ( Flat TokenBundle ) Source # | |
Defined in Cardano.Wallet.Primitive.Types.TokenBundle |
|
type Rep ( Flat a) Source # | |
Defined in Cardano.Wallet.Primitive.Types.TokenMap |
When used with the
Buildable
or
ToJSON
instances, provides a nested
serialization style, where token quantities are grouped by policy
identifier.
Instances
Eq a => Eq ( Nested a) Source # | |
Ord a => Ord ( Nested a) Source # | |
Defined in Cardano.Wallet.Primitive.Types.TokenMap |
|
Show a => Show ( Nested a) Source # | |
Generic ( Nested a) Source # | |
ToJSON ( Nested TokenMap ) Source # | |
FromJSON ( Nested TokenMap ) Source # | |
Buildable ( Nested TokenMap ) Source # | |
Buildable ( Nested TokenBundle ) Source # | |
Defined in Cardano.Wallet.Primitive.Types.TokenBundle |
|
type Rep ( Nested a) Source # | |
Defined in Cardano.Wallet.Primitive.Types.TokenMap
type
Rep
(
Nested
a) =
D1
('
MetaData
"Nested" "Cardano.Wallet.Primitive.Types.TokenMap" "cardano-wallet-core-2022.7.1-AGKhlyz9liLKN3QqZD1gj" '
True
) (
C1
('
MetaCons
"Nested" '
PrefixI
'
True
) (
S1
('
MetaSel
('
Just
"getNested") '
NoSourceUnpackedness
'
NoSourceStrictness
'
DecidedLazy
) (
Rec0
a)))
|
Queries
Transformations
mapAssetIds :: ( AssetId -> AssetId ) -> TokenBundle -> TokenBundle Source #
Unsafe operations
unsafeSubtract :: TokenBundle -> TokenBundle -> TokenBundle Source #
Subtracts the second token bundle from the first.
Pre-condition: the second bundle is less than or equal to the first bundle
when compared with the
leq
function.
Throws a run-time exception if the pre-condition is violated.