module Cooked.MockChain.GenerateTx
  ( txSignersAndBodyToCardanoTx,
    txSkelToCardanoTx,
  )
where

import Cardano.Api.Shelley qualified as Cardano
import Cooked.MockChain.BlockChain
import Cooked.MockChain.GenerateTx.Body
import Cooked.Skeleton
import Cooked.Wallet
import Data.Set (Set)
import Ledger.Address qualified as Ledger
import Ledger.Tx qualified as Ledger
import Ledger.Tx.CardanoAPI qualified as Ledger
import PlutusLedgerApi.V3 qualified as Api

-- | Generates a Cardano transaction and signs it
txSignersAndBodyToCardanoTx :: [Wallet] -> Cardano.TxBody Cardano.ConwayEra -> Cardano.Tx Cardano.ConwayEra
txSignersAndBodyToCardanoTx :: [Wallet] -> TxBody ConwayEra -> Tx ConwayEra
txSignersAndBodyToCardanoTx [Wallet]
signers TxBody ConwayEra
txBody =
  CardanoTx -> Tx ConwayEra
Ledger.getEmulatorEraTx (CardanoTx -> Tx ConwayEra) -> CardanoTx -> Tx ConwayEra
forall a b. (a -> b) -> a -> b
$
    (CardanoTx -> ShelleyWitnessSigningKey -> CardanoTx)
-> CardanoTx -> [ShelleyWitnessSigningKey] -> CardanoTx
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl
      ((ShelleyWitnessSigningKey -> CardanoTx -> CardanoTx)
-> CardanoTx -> ShelleyWitnessSigningKey -> CardanoTx
forall a b c. (a -> b -> c) -> b -> a -> c
flip ShelleyWitnessSigningKey -> CardanoTx -> CardanoTx
Ledger.addCardanoTxWitness)
      (Tx ConwayEra -> CardanoTx
Ledger.CardanoEmulatorEraTx (Tx ConwayEra -> CardanoTx) -> Tx ConwayEra -> CardanoTx
forall a b. (a -> b) -> a -> b
$ TxBody ConwayEra
txBody TxBody ConwayEra -> [KeyWitness ConwayEra] -> Tx ConwayEra
forall era. TxBody era -> [KeyWitness era] -> Tx era
`Cardano.Tx` [])
      (PaymentPrivateKey -> ShelleyWitnessSigningKey
forall a. ToWitness a => a -> ShelleyWitnessSigningKey
Ledger.toWitness (PaymentPrivateKey -> ShelleyWitnessSigningKey)
-> (Wallet -> PaymentPrivateKey)
-> Wallet
-> ShelleyWitnessSigningKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XPrv -> PaymentPrivateKey
Ledger.PaymentPrivateKey (XPrv -> PaymentPrivateKey)
-> (Wallet -> XPrv) -> Wallet -> PaymentPrivateKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Wallet -> XPrv
walletSK (Wallet -> ShelleyWitnessSigningKey)
-> [Wallet] -> [ShelleyWitnessSigningKey]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Wallet]
signers)

-- | Generates a full Cardano transaction for a skeleton, fees and collaterals
txSkelToCardanoTx :: (MonadBlockChainBalancing m) => TxSkel -> Integer -> Maybe (Set Api.TxOutRef, Wallet) -> m (Cardano.Tx Cardano.ConwayEra)
txSkelToCardanoTx :: forall (m :: * -> *).
MonadBlockChainBalancing m =>
TxSkel
-> Integer -> Maybe (Set TxOutRef, Wallet) -> m (Tx ConwayEra)
txSkelToCardanoTx TxSkel
txSkel Integer
fee Maybe (Set TxOutRef, Wallet)
mCollaterals =
  [Wallet] -> TxBody ConwayEra -> Tx ConwayEra
txSignersAndBodyToCardanoTx (TxSkel -> [Wallet]
txSkelSigners TxSkel
txSkel) (TxBody ConwayEra -> Tx ConwayEra)
-> m (TxBody ConwayEra) -> m (Tx ConwayEra)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TxSkel
-> Integer -> Maybe (Set TxOutRef, Wallet) -> m (TxBody ConwayEra)
forall (m :: * -> *).
MonadBlockChainBalancing m =>
TxSkel
-> Integer -> Maybe (Set TxOutRef, Wallet) -> m (TxBody ConwayEra)
txSkelToTxBody TxSkel
txSkel Integer
fee Maybe (Set TxOutRef, Wallet)
mCollaterals