|
| 1 | +{-# LANGUAGE DataKinds #-} |
| 2 | +{-# LANGUAGE LambdaCase #-} |
| 3 | +{-# LANGUAGE NumericUnderscores #-} |
| 4 | +{-# LANGUAGE OverloadedStrings #-} |
| 5 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 6 | + |
| 7 | +-- | Script to generate golden test files |
| 8 | +module GenGolden where |
| 9 | + |
| 10 | +import Cardano.Api |
| 11 | +import Cardano.Api.Ledger qualified as L |
| 12 | + |
| 13 | +import Control.Error.Util (hush) |
| 14 | +import Data.Aeson (encode) |
| 15 | +import Data.ByteString (ByteString) |
| 16 | +import Data.ByteString.Lazy qualified as BL |
| 17 | +import Data.ByteString.Lazy.Char8 qualified as BL8 |
| 18 | +import Data.Maybe (fromJust) |
| 19 | +import GHC.Exts (IsList (..)) |
| 20 | +import System.FilePath ((</>)) |
| 21 | + |
| 22 | +goldenPath :: FilePath |
| 23 | +goldenPath = "test/cardano-api-golden/files/TxOut" |
| 24 | + |
| 25 | +main :: IO () |
| 26 | +main = do |
| 27 | + createDirectoryIfMissing True (goldenPath </> "conway") |
| 28 | + createDirectoryIfMissing True (goldenPath </> "dijkstra") |
| 29 | + |
| 30 | + -- Conway TxOut tests |
| 31 | + writeGolden (goldenPath </> "conway" </> "txout-simple.json") txOutConwaySimple |
| 32 | + writeGolden (goldenPath </> "conway" </> "txout-datumhash.json") txOutConwayDatumHash |
| 33 | + writeGolden (goldenPath </> "conway" </> "txout-inlinedatum.json") txOutConwayInlineDatum |
| 34 | + writeGolden (goldenPath </> "conway" </> "txout-referencescript.json") txOutConwayReferenceScript |
| 35 | + writeGolden (goldenPath </> "conway" </> "txout-full.json") txOutConwayFull |
| 36 | + |
| 37 | + -- Dijkstra TxOut tests |
| 38 | + writeGolden (goldenPath </> "dijkstra" </> "txout-simple.json") txOutDijkstraSimple |
| 39 | + writeGolden (goldenPath </> "dijkstra" </> "txout-inlinedatum.json") txOutDijkstraInlineDatum |
| 40 | + |
| 41 | + -- TxOutValue tests |
| 42 | + writeGolden (goldenPath </> "conway" </> "txoutvalue-lovelace.json") txOutValueConwayLovelace |
| 43 | + writeGolden (goldenPath </> "conway" </> "txoutvalue-multiasset.json") txOutValueConwayMultiAsset |
| 44 | + writeGolden (goldenPath </> "dijkstra" </> "txoutvalue-lovelace.json") txOutValueDijkstraLovelace |
| 45 | + |
| 46 | + -- Address tests |
| 47 | + writeGolden (goldenPath </> "conway" </> "address.json") (mkTestAddress ShelleyBasedEraConway :: AddressInEra ConwayEra) |
| 48 | + writeGolden (goldenPath </> "dijkstra" </> "address.json") (mkTestAddress ShelleyBasedEraDijkstra :: AddressInEra DijkstraEra) |
| 49 | + |
| 50 | + -- ReferenceScript tests |
| 51 | + writeGolden (goldenPath </> "conway" </> "referencescript.json") (mkTestReferenceScript ShelleyBasedEraConway) |
| 52 | + writeGolden (goldenPath </> "dijkstra" </> "referencescript.json") (mkTestReferenceScript ShelleyBasedEraDijkstra) |
| 53 | + |
| 54 | + -- UTxO test |
| 55 | + writeGolden (goldenPath </> "conway" </> "utxo.json") utxoConway |
| 56 | + |
| 57 | + putStrLn "Golden files generated successfully!" |
| 58 | + |
| 59 | +writeGolden :: ToJSON a => FilePath -> a -> IO () |
| 60 | +writeGolden path value = do |
| 61 | + BL.writeFile path (encodePretty value) |
| 62 | + putStrLn $ "Wrote: " <> path |
| 63 | + |
| 64 | +-- Test data construction helpers |
| 65 | + |
| 66 | +mkTestAddress :: ShelleyBasedEra era -> AddressInEra era |
| 67 | +mkTestAddress sbe = |
| 68 | + shelleyAddressInEra sbe $ |
| 69 | + makeShelleyAddress |
| 70 | + Mainnet |
| 71 | + (PaymentCredentialByKey testPaymentKeyHash) |
| 72 | + NoStakeAddress |
| 73 | + |
| 74 | +testPaymentKeyHash :: Hash PaymentKey |
| 75 | +testPaymentKeyHash = |
| 76 | + fromJust $ |
| 77 | + hush $ |
| 78 | + deserialiseFromRawBytesHex "1c14ee8e58fbcbd48dc7367c95a63fd1d937ba989820015db16ac7e5" |
| 79 | + |
| 80 | +mkLovelaceValue :: ShelleyBasedEra era -> L.Coin -> TxOutValue era |
| 81 | +mkLovelaceValue = lovelaceToTxOutValue |
| 82 | + |
| 83 | +mkMultiAssetValue :: ShelleyBasedEra era -> TxOutValue era |
| 84 | +mkMultiAssetValue sbe = |
| 85 | + shelleyBasedEraConstraints sbe $ |
| 86 | + TxOutValueShelleyBased sbe $ |
| 87 | + toLedgerValue (maryEraOnwardsToMaryEraOnwards sbe) testMultiAssetValue |
| 88 | + where |
| 89 | + maryEraOnwardsToMaryEraOnwards :: ShelleyBasedEra era -> MaryEraOnwards era |
| 90 | + maryEraOnwardsToMaryEraOnwards = \case |
| 91 | + ShelleyBasedEraConway -> MaryEraOnwardsConway |
| 92 | + ShelleyBasedEraDijkstra -> MaryEraOnwardsDijkstra |
| 93 | + _ -> error "mkMultiAssetValue: unsupported era" |
| 94 | + |
| 95 | +testMultiAssetValue :: Value |
| 96 | +testMultiAssetValue = |
| 97 | + fromList |
| 98 | + [ (AdaAssetId, Quantity 2_000_000) |
| 99 | + , (AssetId testPolicyId testAssetName, Quantity 100) |
| 100 | + ] |
| 101 | + |
| 102 | +testPolicyId :: PolicyId |
| 103 | +testPolicyId = |
| 104 | + fromJust $ |
| 105 | + hush $ |
| 106 | + deserialiseFromRawBytesHex "a0000000000000000000000000000000000000000000000000000000" |
| 107 | + |
| 108 | +testAssetName :: AssetName |
| 109 | +testAssetName = |
| 110 | + fromJust $ |
| 111 | + hush $ |
| 112 | + deserialiseFromRawBytes AsAssetName "TestToken" |
| 113 | + |
| 114 | +testDatumHash :: Hash ScriptData |
| 115 | +testDatumHash = |
| 116 | + fromJust $ |
| 117 | + hush $ |
| 118 | + deserialiseFromRawBytesHex "ffd29f3e52e7cf2eb451a59448fd55f9c64e4c1ad1ab0e500d6ceb6d7ff97e9c" |
| 119 | + |
| 120 | +testScriptData :: HashableScriptData |
| 121 | +testScriptData = |
| 122 | + fromJust $ |
| 123 | + hush $ |
| 124 | + deserialiseFromCBOR AsHashableScriptData testScriptDataCBOR |
| 125 | + where |
| 126 | + testScriptDataCBOR :: ByteString |
| 127 | + testScriptDataCBOR = "\24\42" |
| 128 | + |
| 129 | +testSimpleScript :: Script SimpleScript' |
| 130 | +testSimpleScript = |
| 131 | + SimpleScript $ |
| 132 | + RequireAllOf |
| 133 | + [ RequireSignature testPaymentKeyHash |
| 134 | + ] |
| 135 | + |
| 136 | +mkTestReferenceScript :: ShelleyBasedEra era -> ReferenceScript era |
| 137 | +mkTestReferenceScript = \case |
| 138 | + ShelleyBasedEraConway -> |
| 139 | + ReferenceScript BabbageEraOnwardsConway (ScriptInAnyLang SimpleScriptLanguage testSimpleScript) |
| 140 | + ShelleyBasedEraDijkstra -> |
| 141 | + ReferenceScript BabbageEraOnwardsDijkstra (ScriptInAnyLang SimpleScriptLanguage testSimpleScript) |
| 142 | + _ -> error "mkTestReferenceScript: unsupported era" |
| 143 | + |
| 144 | +-- TxOut test values |
| 145 | + |
| 146 | +txOutConwaySimple :: TxOut CtxTx ConwayEra |
| 147 | +txOutConwaySimple = |
| 148 | + TxOut |
| 149 | + (mkTestAddress ShelleyBasedEraConway) |
| 150 | + (mkLovelaceValue ShelleyBasedEraConway (L.Coin 1_000_000)) |
| 151 | + TxOutDatumNone |
| 152 | + ReferenceScriptNone |
| 153 | + |
| 154 | +txOutConwayDatumHash :: TxOut CtxTx ConwayEra |
| 155 | +txOutConwayDatumHash = |
| 156 | + TxOut |
| 157 | + (mkTestAddress ShelleyBasedEraConway) |
| 158 | + (mkLovelaceValue ShelleyBasedEraConway (L.Coin 2_000_000)) |
| 159 | + (TxOutDatumHash AlonzoEraOnwardsConway testDatumHash) |
| 160 | + ReferenceScriptNone |
| 161 | + |
| 162 | +txOutConwayInlineDatum :: TxOut CtxTx ConwayEra |
| 163 | +txOutConwayInlineDatum = |
| 164 | + TxOut |
| 165 | + (mkTestAddress ShelleyBasedEraConway) |
| 166 | + (mkLovelaceValue ShelleyBasedEraConway (L.Coin 3_000_000)) |
| 167 | + (TxOutDatumInline BabbageEraOnwardsConway testScriptData) |
| 168 | + ReferenceScriptNone |
| 169 | + |
| 170 | +txOutConwayReferenceScript :: TxOut CtxTx ConwayEra |
| 171 | +txOutConwayReferenceScript = |
| 172 | + TxOut |
| 173 | + (mkTestAddress ShelleyBasedEraConway) |
| 174 | + (mkLovelaceValue ShelleyBasedEraConway (L.Coin 4_000_000)) |
| 175 | + TxOutDatumNone |
| 176 | + (mkTestReferenceScript ShelleyBasedEraConway) |
| 177 | + |
| 178 | +txOutConwayFull :: TxOut CtxTx ConwayEra |
| 179 | +txOutConwayFull = |
| 180 | + TxOut |
| 181 | + (mkTestAddress ShelleyBasedEraConway) |
| 182 | + (mkMultiAssetValue ShelleyBasedEraConway) |
| 183 | + (TxOutDatumInline BabbageEraOnwardsConway testScriptData) |
| 184 | + (mkTestReferenceScript ShelleyBasedEraConway) |
| 185 | + |
| 186 | +txOutDijkstraSimple :: TxOut CtxTx DijkstraEra |
| 187 | +txOutDijkstraSimple = |
| 188 | + TxOut |
| 189 | + (mkTestAddress ShelleyBasedEraDijkstra) |
| 190 | + (mkLovelaceValue ShelleyBasedEraDijkstra (L.Coin 1_000_000)) |
| 191 | + TxOutDatumNone |
| 192 | + ReferenceScriptNone |
| 193 | + |
| 194 | +txOutDijkstraInlineDatum :: TxOut CtxTx DijkstraEra |
| 195 | +txOutDijkstraInlineDatum = |
| 196 | + TxOut |
| 197 | + (mkTestAddress ShelleyBasedEraDijkstra) |
| 198 | + (mkLovelaceValue ShelleyBasedEraDijkstra (L.Coin 3_000_000)) |
| 199 | + (TxOutDatumInline BabbageEraOnwardsDijkstra testScriptData) |
| 200 | + (mkTestReferenceScript ShelleyBasedEraDijkstra) |
| 201 | + |
| 202 | +-- TxOutValue test values |
| 203 | + |
| 204 | +txOutValueConwayLovelace :: TxOutValue ConwayEra |
| 205 | +txOutValueConwayLovelace = mkLovelaceValue ShelleyBasedEraConway (L.Coin 5_000_000) |
| 206 | + |
| 207 | +txOutValueConwayMultiAsset :: TxOutValue ConwayEra |
| 208 | +txOutValueConwayMultiAsset = mkMultiAssetValue ShelleyBasedEraConway |
| 209 | + |
| 210 | +txOutValueDijkstraLovelace :: TxOutValue DijkstraEra |
| 211 | +txOutValueDijkstraLovelace = mkLovelaceValue ShelleyBasedEraDijkstra (L.Coin 5_000_000) |
| 212 | + |
| 213 | +-- UTxO test value |
| 214 | + |
| 215 | +utxoConway :: UTxO ConwayEra |
| 216 | +utxoConway = |
| 217 | + UTxO $ |
| 218 | + fromList |
| 219 | + [ (testTxIn, txOut1) |
| 220 | + , (testTxIn2, txOut2) |
| 221 | + ] |
| 222 | + where |
| 223 | + txOut1 :: TxOut CtxUTxO ConwayEra |
| 224 | + txOut1 = |
| 225 | + TxOut |
| 226 | + (mkTestAddress ShelleyBasedEraConway) |
| 227 | + (mkLovelaceValue ShelleyBasedEraConway (L.Coin 1_000_000)) |
| 228 | + TxOutDatumNone |
| 229 | + ReferenceScriptNone |
| 230 | + |
| 231 | + txOut2 :: TxOut CtxUTxO ConwayEra |
| 232 | + txOut2 = |
| 233 | + TxOut |
| 234 | + (mkTestAddress ShelleyBasedEraConway) |
| 235 | + (mkMultiAssetValue ShelleyBasedEraConway) |
| 236 | + (TxOutDatumHash AlonzoEraOnwardsConway testDatumHash) |
| 237 | + ReferenceScriptNone |
| 238 | + |
| 239 | + testTxIn :: TxIn |
| 240 | + testTxIn = |
| 241 | + TxIn testTxId1 (TxIx 0) |
| 242 | + |
| 243 | + testTxIn2 :: TxIn |
| 244 | + testTxIn2 = |
| 245 | + TxIn testTxId2 (TxIx 1) |
| 246 | + |
| 247 | + testTxId1 :: TxId |
| 248 | + testTxId1 = |
| 249 | + fromJust $ |
| 250 | + hush $ |
| 251 | + deserialiseFromRawBytesHex "0000000000000000000000000000000000000000000000000000000000000001" |
| 252 | + |
| 253 | + testTxId2 :: TxId |
| 254 | + testTxId2 = |
| 255 | + fromJust $ |
| 256 | + hush $ |
| 257 | + deserialiseFromRawBytesHex "0000000000000000000000000000000000000000000000000000000000000002" |
0 commit comments