Skip to content

Commit

Permalink
txOutRefToTxSkelOut
Browse files Browse the repository at this point in the history
  • Loading branch information
mmontin committed Feb 14, 2025
1 parent 6a35e02 commit 91364d9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
be used on a redeemer to manually attach a reference input (which does not
necessarily have to contain the right reference script).
- Capability to test the result of a mockchain run based on the log entries.
- `txOutRefToTxSkelOut` helper to query the mock chain for recreating a
`TxSkelOut` from a `TxOutRef`. This is very useful when using Tweaks that need
to pay back an existing output with a slight modification.

### Removed

Expand Down
4 changes: 4 additions & 0 deletions src/Cooked/Conversion/ToCredential.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import PlutusLedgerApi.V3 qualified as Api
class ToCredential a where
toCredential :: a -> Api.Credential

instance (ToCredential a, ToCredential b) => ToCredential (Either a b) where
toCredential (Left l) = toCredential l
toCredential (Right r) = toCredential r

instance ToCredential Api.Credential where
toCredential = id

Expand Down
30 changes: 29 additions & 1 deletion src/Cooked/MockChain/BlockChain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ module Cooked.MockChain.BlockChain
validateTxSkel_,
txSkelProposalsDeposit,
govActionDeposit,
txOutRefToTxSkelOut,
)
where

Expand Down Expand Up @@ -79,7 +80,7 @@ import Ledger.Index qualified as Ledger
import Ledger.Slot qualified as Ledger
import Ledger.Tx qualified as Ledger
import Ledger.Tx.CardanoAPI qualified as Ledger
import ListT
import ListT hiding (null)
import Optics.Core
import Plutus.Script.Utils.Scripts qualified as Script
import PlutusLedgerApi.V3 qualified as Api
Expand Down Expand Up @@ -381,6 +382,33 @@ txSkelInputDataAsHashes skel = do
(Map.elems -> inputTxOuts) <- txSkelInputUtxos skel
catMaybes <$> mapM outputToDatumHashM inputTxOuts

-- | This creates a payment from an existing TxOut. This is not trivial because
-- we need to reconstruct a bunch of information using data withing the blockchain
txOutRefToTxSkelOut :: (MonadBlockChainBalancing m) => Api.TxOutRef -> m TxSkelOut
txOutRefToTxSkelOut oRef = do
Just txOut@(Api.TxOut (Api.Address cred _) _ dat refS) <- txOutByRef oRef
target <- case cred of
Api.PubKeyCredential pkh -> return $ Left pkh
Api.ScriptCredential (Api.ScriptHash sh) -> do
Just val <- validatorFromHash (Script.ValidatorHash sh)
return $ Right val
datum <- case dat of
Api.NoOutputDatum -> return TxSkelOutNoDatum
Api.OutputDatumHash hash -> do
Just (Api.Datum dat') <- datumFromHash hash
return $ TxSkelOutDatum dat' -- TODO: investigate between this and TxSkelOutDatum
Api.OutputDatum (Api.Datum dat') -> return $ TxSkelOutInlineDatum dat'
refScript <- case refS of
Nothing -> return Nothing
Just (Api.ScriptHash sh) -> validatorFromHash (Script.ValidatorHash sh)
return $
Pays $
(fromAbstractOutput txOut)
{ concreteOutputOwner = target,
concreteOutputDatum = datum,
concreteOutputReferenceScript = refScript
}

-- ** Slot and Time Management

-- $slotandtime
Expand Down
3 changes: 3 additions & 0 deletions src/Cooked/Skeleton.hs
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,9 @@ instance IsTxSkelOutAllowedOwner Api.PubKeyHash where
instance IsTxSkelOutAllowedOwner (Script.TypedValidator a) where
toPKHOrValidator = Right . Script.tvValidator

instance IsTxSkelOutAllowedOwner (Either Api.PubKeyHash (Script.Versioned Script.Validator)) where
toPKHOrValidator = id

-- | Transaction outputs. The 'Pays' constructor is really general, and you'll
-- probably want to use one of the smart constructors like 'paysScript' or
-- 'paysPK' in most cases.
Expand Down

0 comments on commit 91364d9

Please sign in to comment.