|
| 1 | +{-# OPTIONS_GHC -Wno-orphans #-} |
| 2 | + |
| 3 | +module Spec.RandomLedger ( |
| 4 | + allAssetClasses, |
| 5 | + randomValue, |
| 6 | + randomTxOut, |
| 7 | + randomTxOuts, |
| 8 | + randomTxOutRef, |
| 9 | +) where |
| 10 | + |
| 11 | +import Plutus.PAB.Arbitrary () |
| 12 | + |
| 13 | +import Control.Lens (folded, (%~), (&), (^..)) |
| 14 | +import Control.Monad (replicateM) |
| 15 | +import Data.List.Extra (mconcatMap) |
| 16 | +import Data.Set (Set) |
| 17 | +import Data.Set qualified as Set |
| 18 | +import GHC.Natural (Natural) |
| 19 | +import Ledger.Tx (TxOut (..), TxOutRef (..)) |
| 20 | +import Ledger.Value (AssetClass (AssetClass), Value) |
| 21 | +import Ledger.Value qualified as Value |
| 22 | +import Test.QuickCheck (Arbitrary (arbitrary), Gen, Property, elements, forAll, listOf, property, resize) |
| 23 | +import Prelude |
| 24 | + |
| 25 | +deriving newtype instance Arbitrary AssetClass |
| 26 | + |
| 27 | +allAssetClasses :: Int -> Gen (Set AssetClass) |
| 28 | +allAssetClasses n = Set.fromList <$> resize n (listOf arbitrary) |
| 29 | + |
| 30 | +randomValue :: Int -> Set AssetClass -> Gen Value |
| 31 | +randomValue samplesize assetclasses = |
| 32 | + do |
| 33 | + selectedAc <- |
| 34 | + Set.fromList |
| 35 | + <$> replicateM |
| 36 | + samplesize |
| 37 | + (assetclasses ^.. folded & id %~ elements) |
| 38 | + |
| 39 | + amounts <- replicateM (length selectedAc) (toInteger <$> arbitrary @Natural) |
| 40 | + |
| 41 | + return $ |
| 42 | + mconcatMap (uncurry Value.assetClassValue) $ |
| 43 | + zip (Set.toList selectedAc) amounts |
| 44 | + |
| 45 | +randomTxOut :: Int -> Set AssetClass -> Gen TxOut |
| 46 | +randomTxOut samplesize assetclasses = |
| 47 | + do |
| 48 | + addr <- arbitrary |
| 49 | + value <- randomValue samplesize assetclasses |
| 50 | + datumhash <- arbitrary |
| 51 | + |
| 52 | + return (TxOut addr value datumhash) |
| 53 | + |
| 54 | +randomTxOuts :: Int -> Int -> Set AssetClass -> Gen [TxOut] |
| 55 | +randomTxOuts numTxOuts samplesize = |
| 56 | + replicateM numTxOuts . randomTxOut samplesize |
| 57 | + |
| 58 | +randomTxOutRef :: Gen TxOutRef |
| 59 | +randomTxOutRef = |
| 60 | + do |
| 61 | + txId <- arbitrary |
| 62 | + txIdx <- toInteger <$> arbitrary @Natural |
| 63 | + return (TxOutRef txId txIdx) |
0 commit comments