/
Tx.hs
145 lines (124 loc) · 4.91 KB
/
Tx.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
-- editorconfig-checker-disable-file
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# OPTIONS_GHC -fno-specialise #-}
{-# OPTIONS_GHC -fno-omit-interface-pragmas #-}
module PlutusLedgerApi.V2.Tx
(
-- * Transactions
TxId (..)
, ScriptTag (..)
, RedeemerPtr (..)
, Redeemers
-- * Transaction outputs
, TxOut(..)
, TxOutRef(..)
, OutputDatum (..)
, isPubKeyOut
, isPayToScriptOut
, outAddress
, outValue
, txOutPubKey
, outDatum
, outReferenceScript
, pubKeyHashTxOut
-- * Transaction inputs
, TxInType(..)
, TxIn(..)
, inRef
, inType
, inScripts
, pubKeyTxIn
, scriptTxIn
, pubKeyTxIns
, scriptTxIns
) where
import Control.DeepSeq (NFData)
import Control.Lens
import Data.Maybe (isJust)
import GHC.Generics (Generic)
import Prettyprinter
import PlutusTx qualified
import PlutusTx.Bool qualified as PlutusTx
import PlutusTx.Eq qualified as PlutusTx
import PlutusLedgerApi.V1.Address
import PlutusLedgerApi.V1.Crypto
import PlutusLedgerApi.V1.Scripts
import PlutusLedgerApi.V1.Tx hiding (TxOut (..), isPayToScriptOut, isPubKeyOut, outAddress, outValue, pubKeyHashTxOut,
txOutDatum, txOutPubKey)
import PlutusLedgerApi.V1.Value
-- | The datum attached to an output: either nothing; a datum hash; or the datum itself (an "inline datum").
data OutputDatum = NoOutputDatum | OutputDatumHash DatumHash | OutputDatum Datum
deriving stock (Show, Eq, Generic)
deriving anyclass (NFData)
instance PlutusTx.Eq OutputDatum where
{-# INLINABLE (==) #-}
NoOutputDatum == NoOutputDatum = True
(OutputDatumHash dh) == (OutputDatumHash dh') = dh PlutusTx.== dh'
(OutputDatum d) == (OutputDatum d') = d PlutusTx.== d'
_ == _ = False
instance Pretty OutputDatum where
pretty NoOutputDatum = "no datum"
pretty (OutputDatumHash dh) = "datum hash: " <+> pretty dh
pretty (OutputDatum d) = "inline datum : " <+> pretty d
-- | A transaction output, consisting of a target address, a value,
-- optionally a datum/datum hash, and optionally a reference script.
data TxOut = TxOut {
txOutAddress :: Address,
txOutValue :: Value,
txOutDatum :: OutputDatum,
txOutReferenceScript :: Maybe ScriptHash
}
deriving stock (Show, Eq, Generic)
deriving anyclass (NFData)
instance Pretty TxOut where
pretty TxOut{txOutAddress, txOutValue, txOutDatum, txOutReferenceScript} =
hang 2 $ vsep ["-" <+> pretty txOutValue <+> "addressed to", pretty txOutAddress, "with datum", pretty txOutDatum, "with referenceScript", pretty txOutReferenceScript]
instance PlutusTx.Eq TxOut where
{-# INLINABLE (==) #-}
(TxOut txOutAddress txOutValue txOutDatum txOutRefScript) == (TxOut txOutAddress' txOutValue' txOutDatum' txOutRefScript') =
txOutAddress PlutusTx.== txOutAddress'
PlutusTx.&& txOutValue PlutusTx.== txOutValue'
PlutusTx.&& txOutDatum PlutusTx.== txOutDatum'
PlutusTx.&& txOutRefScript PlutusTx.== txOutRefScript'
-- | The public key attached to a 'TxOut', if there is one.
txOutPubKey :: TxOut -> Maybe PubKeyHash
txOutPubKey TxOut{txOutAddress} = toPubKeyHash txOutAddress
-- | The validator hash attached to a 'TxOut', if there is one.
txOutValidatorHash :: TxOut -> Maybe ValidatorHash
txOutValidatorHash TxOut{txOutAddress} = toValidatorHash txOutAddress
-- | The address of a transaction output.
outAddress :: Lens' TxOut Address
outAddress = lens txOutAddress s where
s tx a = tx { txOutAddress = a }
-- | The datum attached to a 'TxOut'.
outDatum :: Lens' TxOut OutputDatum
outDatum = lens txOutDatum s where
s tx v = tx { txOutDatum = v }
-- | The value of a transaction output.
-- | TODO: Compute address again
outValue :: Lens' TxOut Value
outValue = lens txOutValue s where
s tx v = tx { txOutValue = v }
-- | The reference script attached to a 'TxOut'.
outReferenceScript :: Lens' TxOut (Maybe ScriptHash)
outReferenceScript = lens txOutReferenceScript s where
s tx v = tx { txOutReferenceScript = v }
-- | Whether the output is a pay-to-pubkey output.
isPubKeyOut :: TxOut -> Bool
isPubKeyOut = isJust . txOutPubKey
-- | Whether the output is a pay-to-script output.
isPayToScriptOut :: TxOut -> Bool
isPayToScriptOut = isJust . txOutValidatorHash
-- | Create a transaction output locked by a public key.
pubKeyHashTxOut :: Value -> PubKeyHash -> TxOut
pubKeyHashTxOut v pkh = TxOut (pubKeyHashAddress pkh) v NoOutputDatum Nothing
PlutusTx.makeIsDataIndexed ''OutputDatum [('NoOutputDatum,0), ('OutputDatumHash,1), ('OutputDatum,2)]
PlutusTx.makeLift ''OutputDatum
PlutusTx.makeIsDataIndexed ''TxOut [('TxOut,0)]
PlutusTx.makeLift ''TxOut