module Cooked.MockChain.GenerateTx.Mint (toMintValue) where
import Cardano.Api qualified as Cardano
import Control.Monad
import Cooked.MockChain.BlockChain
import Cooked.MockChain.GenerateTx.Common
import Cooked.MockChain.GenerateTx.Witness
import Cooked.Skeleton
import Data.Map qualified as Map
import Data.Map.NonEmpty qualified as NEMap
import Data.Map.Strict qualified as SMap
import GHC.Exts (fromList)
import Ledger.Tx.CardanoAPI qualified as Ledger
import Plutus.Script.Utils.Scripts qualified as Script
import PlutusLedgerApi.V3 qualified as Api
import PlutusTx.Builtins.Internal qualified as PlutusTx
import Test.QuickCheck.Modifiers (NonZero (NonZero))
toMintValue :: (MonadBlockChainBalancing m) => TxSkelMints -> m (Cardano.TxMintValue Cardano.BuildTx Cardano.ConwayEra)
toMintValue :: forall (m :: * -> *).
MonadBlockChainBalancing m =>
TxSkelMints -> m (TxMintValue BuildTx ConwayEra)
toMintValue TxSkelMints
mints | TxSkelMints -> Bool
forall a. Map (Versioned MintingPolicy) a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null TxSkelMints
mints = TxMintValue BuildTx ConwayEra -> m (TxMintValue BuildTx ConwayEra)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return TxMintValue BuildTx ConwayEra
forall build era. TxMintValue build era
Cardano.TxMintNone
toMintValue TxSkelMints
mints = ([(PolicyId,
(PolicyAssets,
BuildTxWith BuildTx (ScriptWitness WitCtxMint ConwayEra)))]
-> TxMintValue BuildTx ConwayEra)
-> m [(PolicyId,
(PolicyAssets,
BuildTxWith BuildTx (ScriptWitness WitCtxMint ConwayEra)))]
-> m (TxMintValue BuildTx ConwayEra)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (MaryEraOnwards ConwayEra
-> Map
PolicyId
(PolicyAssets,
BuildTxWith BuildTx (ScriptWitness WitCtxMint ConwayEra))
-> TxMintValue BuildTx ConwayEra
forall era build.
MaryEraOnwards era
-> Map
PolicyId
(PolicyAssets, BuildTxWith build (ScriptWitness WitCtxMint era))
-> TxMintValue build era
Cardano.TxMintValue MaryEraOnwards ConwayEra
Cardano.MaryEraOnwardsConway (Map
PolicyId
(PolicyAssets,
BuildTxWith BuildTx (ScriptWitness WitCtxMint ConwayEra))
-> TxMintValue BuildTx ConwayEra)
-> ([(PolicyId,
(PolicyAssets,
BuildTxWith BuildTx (ScriptWitness WitCtxMint ConwayEra)))]
-> Map
PolicyId
(PolicyAssets,
BuildTxWith BuildTx (ScriptWitness WitCtxMint ConwayEra)))
-> [(PolicyId,
(PolicyAssets,
BuildTxWith BuildTx (ScriptWitness WitCtxMint ConwayEra)))]
-> TxMintValue BuildTx ConwayEra
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(PolicyId,
(PolicyAssets,
BuildTxWith BuildTx (ScriptWitness WitCtxMint ConwayEra)))]
-> Map
PolicyId
(PolicyAssets,
BuildTxWith BuildTx (ScriptWitness WitCtxMint ConwayEra))
forall k a. Ord k => [(k, a)] -> Map k a
SMap.fromList) (m [(PolicyId,
(PolicyAssets,
BuildTxWith BuildTx (ScriptWitness WitCtxMint ConwayEra)))]
-> m (TxMintValue BuildTx ConwayEra))
-> m [(PolicyId,
(PolicyAssets,
BuildTxWith BuildTx (ScriptWitness WitCtxMint ConwayEra)))]
-> m (TxMintValue BuildTx ConwayEra)
forall a b. (a -> b) -> a -> b
$
[(Versioned MintingPolicy,
(TxSkelRedeemer, NEMap TokenName (NonZero Integer)))]
-> ((Versioned MintingPolicy,
(TxSkelRedeemer, NEMap TokenName (NonZero Integer)))
-> m (PolicyId,
(PolicyAssets,
BuildTxWith BuildTx (ScriptWitness WitCtxMint ConwayEra))))
-> m [(PolicyId,
(PolicyAssets,
BuildTxWith BuildTx (ScriptWitness WitCtxMint ConwayEra)))]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM (TxSkelMints
-> [(Versioned MintingPolicy,
(TxSkelRedeemer, NEMap TokenName (NonZero Integer)))]
forall k a. Map k a -> [(k, a)]
Map.toList TxSkelMints
mints) (((Versioned MintingPolicy,
(TxSkelRedeemer, NEMap TokenName (NonZero Integer)))
-> m (PolicyId,
(PolicyAssets,
BuildTxWith BuildTx (ScriptWitness WitCtxMint ConwayEra))))
-> m [(PolicyId,
(PolicyAssets,
BuildTxWith BuildTx (ScriptWitness WitCtxMint ConwayEra)))])
-> ((Versioned MintingPolicy,
(TxSkelRedeemer, NEMap TokenName (NonZero Integer)))
-> m (PolicyId,
(PolicyAssets,
BuildTxWith BuildTx (ScriptWitness WitCtxMint ConwayEra))))
-> m [(PolicyId,
(PolicyAssets,
BuildTxWith BuildTx (ScriptWitness WitCtxMint ConwayEra)))]
forall a b. (a -> b) -> a -> b
$ \(Versioned MintingPolicy
policy, (TxSkelRedeemer
red, Map TokenName (NonZero Integer) -> [(TokenName, NonZero Integer)]
forall k a. Map k a -> [(k, a)]
Map.toList (Map TokenName (NonZero Integer) -> [(TokenName, NonZero Integer)])
-> (NEMap TokenName (NonZero Integer)
-> Map TokenName (NonZero Integer))
-> NEMap TokenName (NonZero Integer)
-> [(TokenName, NonZero Integer)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NEMap TokenName (NonZero Integer)
-> Map TokenName (NonZero Integer)
forall k a. NEMap k a -> Map k a
NEMap.toMap -> [(TokenName, NonZero Integer)]
assets)) -> do
PolicyId
policyId <-
String -> Either ToCardanoError PolicyId -> m PolicyId
forall (m :: * -> *) a.
MonadError MockChainError m =>
String -> Either ToCardanoError a -> m a
throwOnToCardanoError
String
"toMintValue: Unable to translate minting policy hash"
(MintingPolicyHash -> Either ToCardanoError PolicyId
Ledger.toCardanoPolicyId (MintingPolicyHash -> Either ToCardanoError PolicyId)
-> MintingPolicyHash -> Either ToCardanoError PolicyId
forall a b. (a -> b) -> a -> b
$ Versioned MintingPolicy -> MintingPolicyHash
forall a. ToMintingPolicyHash a => a -> MintingPolicyHash
Script.toMintingPolicyHash Versioned MintingPolicy
policy)
BuildTxWith BuildTx (ScriptWitness WitCtxMint ConwayEra)
mintWitness <- ScriptWitness WitCtxMint ConwayEra
-> BuildTxWith BuildTx (ScriptWitness WitCtxMint ConwayEra)
forall a. a -> BuildTxWith BuildTx a
Cardano.BuildTxWith (ScriptWitness WitCtxMint ConwayEra
-> BuildTxWith BuildTx (ScriptWitness WitCtxMint ConwayEra))
-> m (ScriptWitness WitCtxMint ConwayEra)
-> m (BuildTxWith BuildTx (ScriptWitness WitCtxMint ConwayEra))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Versioned MintingPolicy
-> TxSkelRedeemer
-> ScriptDatum WitCtxMint
-> m (ScriptWitness WitCtxMint ConwayEra)
forall (m :: * -> *) a b.
(MonadBlockChainBalancing m, ToVersioned Script a) =>
a
-> TxSkelRedeemer -> ScriptDatum b -> m (ScriptWitness b ConwayEra)
toScriptWitness Versioned MintingPolicy
policy TxSkelRedeemer
red ScriptDatum WitCtxMint
Cardano.NoScriptDatumForMint
(PolicyId,
(PolicyAssets,
BuildTxWith BuildTx (ScriptWitness WitCtxMint ConwayEra)))
-> m (PolicyId,
(PolicyAssets,
BuildTxWith BuildTx (ScriptWitness WitCtxMint ConwayEra)))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
( PolicyId
policyId,
( [Item PolicyAssets] -> PolicyAssets
forall l. IsList l => [Item l] -> l
fromList
[ (ByteString -> AssetName
Cardano.AssetName ByteString
name, Integer -> Quantity
Cardano.Quantity Integer
quantity)
| (Api.TokenName (PlutusTx.BuiltinByteString ByteString
name), NonZero Integer
quantity) <- [(TokenName, NonZero Integer)]
assets
],
BuildTxWith BuildTx (ScriptWitness WitCtxMint ConwayEra)
mintWitness
)
)