@@ -314,6 +314,7 @@ module Cardano.Api.Internal.Tx.Body
314314 , indexTxCertificates
315315 , TxUpdateProposal (.. )
316316 , TxMintValue (.. )
317+ , mkTxMintValue
317318 , txMintValueToValue
318319 , indexTxMintValue
319320 , TxVotingProcedures (.. )
@@ -487,6 +488,7 @@ import Data.Map.Ordered.Strict (OMap)
487488import Data.Map.Strict (Map )
488489import Data.Map.Strict qualified as Map
489490import Data.Maybe
491+ import Data.MonoTraversable (omap )
490492import Data.Monoid
491493import Data.OSet.Strict (OSet )
492494import Data.OSet.Strict qualified as OSet
@@ -1105,6 +1107,9 @@ instance Applicative (BuildTxWith BuildTx) where
11051107 pure = BuildTxWith
11061108 (BuildTxWith f) <*> (BuildTxWith a) = BuildTxWith (f a)
11071109
1110+ instance Semigroup (BuildTxWith ViewTx a ) where
1111+ ViewTx <> ViewTx = ViewTx
1112+
11081113buildTxWithToMaybe :: BuildTxWith build a -> Maybe a
11091114buildTxWithToMaybe ViewTx = Nothing
11101115buildTxWithToMaybe (BuildTxWith a) = Just a
@@ -1536,25 +1541,46 @@ data TxMintValue build era where
15361541 :: MaryEraOnwards era
15371542 -> Map
15381543 PolicyId
1539- [ ( AssetName
1540- , Quantity
1541- , BuildTxWith build (ScriptWitness WitCtxMint era )
1542- )
1543- ]
1544+ ( PolicyAssets
1545+ , BuildTxWith build (ScriptWitness WitCtxMint era )
1546+ )
15441547 -> TxMintValue build era
15451548
15461549deriving instance Eq (TxMintValue build era )
15471550
15481551deriving instance Show (TxMintValue build era )
15491552
1553+ instance Semigroup (TxMintValue build era ) where
1554+ TxMintNone <> b = b
1555+ a <> TxMintNone = a
1556+ TxMintValue w a <> TxMintValue _ b = TxMintValue w $ Map. unionWith mergeElements a b
1557+ where
1558+ mergeElements (a1, w1) (a2, _w2) = (a1 <> a2, w1)
1559+
1560+ instance Monoid (TxMintValue build era ) where
1561+ mempty = TxMintNone
1562+ mappend = (<>)
1563+
1564+ -- | A helper function for building 'TxMintValue' with present witnesses. Only the first witness
1565+ -- in the argument will be used for each policy id.
1566+ mkTxMintValue
1567+ :: MaryEraOnwards era
1568+ -> [(PolicyId , PolicyAssets , BuildTxWith build (ScriptWitness WitCtxMint era ))]
1569+ -> TxMintValue build era
1570+ mkTxMintValue _ [] = TxMintNone
1571+ mkTxMintValue w vs =
1572+ mconcat $
1573+ [ TxMintValue w (fromList [(policyId, (assets, bWit))])
1574+ | (policyId, assets, bWit) <- vs
1575+ ]
1576+
15501577-- | Convert 'TxMintValue' to a more handy 'Value'.
15511578txMintValueToValue :: TxMintValue build era -> Value
15521579txMintValueToValue TxMintNone = mempty
15531580txMintValueToValue (TxMintValue _ policiesWithAssets) =
1554- fromList
1555- [ (AssetId policyId' assetName', quantity)
1556- | (policyId', assets) <- toList policiesWithAssets
1557- , (assetName', quantity, _) <- assets
1581+ mconcat
1582+ [ policyAssetsToValue policyId assets
1583+ | (policyId, (assets, _witness)) <- toList policiesWithAssets
15581584 ]
15591585
15601586-- | Index the assets with witnesses in the order of policy ids.
@@ -1563,16 +1589,14 @@ indexTxMintValue
15631589 :: TxMintValue build era
15641590 -> [ ( ScriptWitnessIndex
15651591 , PolicyId
1566- , AssetName
1567- , Quantity
1592+ , PolicyAssets
15681593 , BuildTxWith build (ScriptWitness WitCtxMint era )
15691594 )
15701595 ]
15711596indexTxMintValue TxMintNone = []
15721597indexTxMintValue (TxMintValue _ policiesWithAssets) =
1573- [ (ScriptWitnessIndexMint ix, policyId', assetName', quantity, witness)
1574- | (ix, (policyId', assets)) <- zip [0 .. ] $ toList policiesWithAssets
1575- , (assetName', quantity, witness) <- assets
1598+ [ (ScriptWitnessIndexMint ix, policyId, assets, witness)
1599+ | (ix, (policyId, (assets, witness))) <- zip [0 .. ] $ toList policiesWithAssets
15761600 ]
15771601
15781602-- ----------------------------------------------------------------------------
@@ -1967,23 +1991,23 @@ modTxMintValue f tx = tx{txMintValue = f (txMintValue tx)}
19671991
19681992addTxMintValue
19691993 :: IsMaryBasedEra era
1970- => Map PolicyId [( AssetName , Quantity , BuildTxWith build (ScriptWitness WitCtxMint era ))]
1994+ => Map PolicyId ( PolicyAssets , BuildTxWith build (ScriptWitness WitCtxMint era ))
19711995 -> TxBodyContent build era
19721996 -> TxBodyContent build era
19731997addTxMintValue assets =
19741998 modTxMintValue
19751999 ( \ case
19762000 TxMintNone -> TxMintValue maryBasedEra assets
1977- TxMintValue era t -> TxMintValue era (t <> assets)
2001+ TxMintValue era t -> TxMintValue era $ Map. unionWith ( \ (v1, w1) (v2, _w2) -> (v1 <> v2, w1)) assets t -- w1 == w2
19782002 )
19792003
19802004-- | Adds the negation of the provided assets and quantities to the txMintValue field of the `TxBodyContent`.
19812005subtractTxMintValue
19822006 :: IsMaryBasedEra era
1983- => Map PolicyId [( AssetName , Quantity , BuildTxWith build (ScriptWitness WitCtxMint era ))]
2007+ => Map PolicyId ( PolicyAssets , BuildTxWith build (ScriptWitness WitCtxMint era ))
19842008 -> TxBodyContent build era
19852009 -> TxBodyContent build era
1986- subtractTxMintValue assets = addTxMintValue ( fmap ( fmap ( \ (x, y, z) -> (x, negate y, z))) assets)
2010+ subtractTxMintValue assets = addTxMintValue $ first (omap negate ) <$> assets
19872011
19882012setTxScriptValidity :: TxScriptValidity era -> TxBodyContent build era -> TxBodyContent build era
19892013setTxScriptValidity v txBodyContent = txBodyContent{txScriptValidity = v}
@@ -2774,18 +2798,11 @@ fromLedgerTxMintValue
27742798 -> TxMintValue ViewTx era
27752799fromLedgerTxMintValue sbe body = forEraInEon (toCardanoEra sbe) TxMintNone $ \ w ->
27762800 maryEraOnwardsConstraints w $ do
2777- let mint = MaryValue ( Ledger. Coin 0 ) ( body ^. L. mintTxBodyL)
2778- if L. isZero mint
2801+ let multiAsset = body ^. L. mintTxBodyL
2802+ if L. isZero $ MaryValue ( Ledger. Coin 0 ) multiAsset
27792803 then TxMintNone
2780- else do
2781- let assetMap = toList $ fromMaryValue mint
2782- TxMintValue w $
2783- Map. fromListWith
2784- (<>)
2785- [ (policyId', [(assetName', quantity, ViewTx )])
2786- | -- only non-ada can be here
2787- (AssetId policyId' assetName', quantity) <- toList assetMap
2788- ]
2804+ else
2805+ TxMintValue w $ (,ViewTx ) <$> multiAssetToPolicyAssets multiAsset
27892806
27902807makeByronTransactionBody
27912808 :: ()
@@ -3823,7 +3840,7 @@ collectTxBodyScriptWitnesses
38233840 scriptWitnessesMinting txMintValue' =
38243841 List. nub
38253842 [ (ix, AnyScriptWitness witness)
3826- | (ix, _, _, _, BuildTxWith witness) <- indexTxMintValue txMintValue'
3843+ | (ix, _, _, BuildTxWith witness) <- indexTxMintValue txMintValue'
38273844 ]
38283845
38293846 scriptWitnessesVoting
0 commit comments