{-# LANGUAGE TypeFamilies #-}
module Convex.UseCases.Utils (
utxosAt,
) where
import Cardano.Api qualified as C
import Cardano.Api.UTxO qualified as C.UTxO
import Cardano.Ledger.Shelley.API (Credential (ScriptHashObj))
import Convex.Class (MonadMockchain)
import Convex.MockChain (utxoSet)
import Convex.Utxos (toApiUtxo)
utxosAt
:: forall era m
. (MonadMockchain era m, C.IsBabbageBasedEra era)
=> C.ScriptHash -> m [(C.TxIn, C.TxOut C.CtxUTxO era)]
utxosAt :: forall era (m :: * -> *).
(MonadMockchain era m, IsBabbageBasedEra era) =>
ScriptHash -> m [(TxIn, TxOut CtxUTxO era)]
utxosAt ScriptHash
scriptHash = do
UtxoSet CtxUTxO ()
utxos <- m (UtxoSet CtxUTxO ())
forall era (m :: * -> *).
(MonadMockchain era m, IsShelleyBasedEra era) =>
m (UtxoSet CtxUTxO ())
utxoSet
let scriptUtxos :: [(TxIn, TxOut CtxUTxO era)]
scriptUtxos =
[ (TxIn
txIn, TxOut CtxUTxO era
txOut)
| (TxIn
txIn, txOut :: TxOut CtxUTxO era
txOut@(C.TxOut AddressInEra era
addr TxOutValue era
_ TxOutDatum CtxUTxO era
_ ReferenceScript era
_)) <- UTxO era -> [(TxIn, TxOut CtxUTxO era)]
forall era. UTxO era -> [(TxIn, TxOut CtxUTxO era)]
C.UTxO.toList (UtxoSet CtxUTxO () -> UTxO era
forall era a.
IsBabbageBasedEra era =>
UtxoSet CtxUTxO a -> UTxO era
toApiUtxo UtxoSet CtxUTxO ()
utxos)
, AddressInEra era -> Bool
isScriptAddress AddressInEra era
addr
]
[(TxIn, TxOut CtxUTxO era)] -> m [(TxIn, TxOut CtxUTxO era)]
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [(TxIn, TxOut CtxUTxO era)]
scriptUtxos
where
isScriptAddress :: AddressInEra era -> Bool
isScriptAddress (C.AddressInEra AddressTypeInEra addrtype era
_ (C.ShelleyAddress Network
_ (ScriptHashObj ScriptHash
h) StakeReference
_)) =
ScriptHash
h ScriptHash -> ScriptHash -> Bool
forall a. Eq a => a -> a -> Bool
== ScriptHash -> ScriptHash
C.toShelleyScriptHash ScriptHash
scriptHash
isScriptAddress AddressInEra era
_ = Bool
False