|
| 1 | +{-# LANGUAGE NamedFieldPuns #-} |
| 2 | + |
| 3 | +module Spec.NatLedger |
| 4 | + ( NatValue(PValue) |
| 5 | + , NatTxOut(NatTxOut, natTxOutAddress, natTxOutValue, natTxOutDatumHash) |
| 6 | + , NatTxOutRef(NatTxOutRef, natTxOutRefId, natTxRefIdx) |
| 7 | + , toLedgerTxOut |
| 8 | + , toLedgerTxOutRef |
| 9 | + , toLedgerValue |
| 10 | + ) |
| 11 | + where |
| 12 | + |
| 13 | +import BotPlutusInterface.Types () |
| 14 | + |
| 15 | +import Data.Bifunctor (second) |
| 16 | +import GHC.Natural (Natural) |
| 17 | +import Ledger.Tx (TxOut (..), TxOutRef (..), Address) |
| 18 | +import Ledger.Value (Value(Value), CurrencySymbol, TokenName) |
| 19 | +import PlutusTx.AssocMap qualified as AssocMap |
| 20 | +import Plutus.V1.Ledger.Api (TxId, DatumHash) |
| 21 | +import Test.QuickCheck (Arbitrary(arbitrary)) |
| 22 | +import Prelude |
| 23 | + |
| 24 | +newtype NatValue = PValue { getNatValue :: AssocMap.Map CurrencySymbol |
| 25 | + (AssocMap.Map TokenName Natural) |
| 26 | + } deriving newtype (Arbitrary, Show) |
| 27 | + |
| 28 | +toLedgerValue :: NatValue -> Value |
| 29 | +toLedgerValue = Value |
| 30 | + . AssocMap.fromList |
| 31 | + . map (second $ AssocMap.fromList |
| 32 | + . map (second toInteger) |
| 33 | + . AssocMap.toList) |
| 34 | + . AssocMap.toList . getNatValue |
| 35 | + |
| 36 | +data NatTxOutRef = NatTxOutRef { natTxOutRefId :: TxId |
| 37 | + , natTxRefIdx :: Natural |
| 38 | + } deriving stock (Show) |
| 39 | + |
| 40 | +instance Arbitrary NatTxOutRef where |
| 41 | + arbitrary = do |
| 42 | + txId <- arbitrary |
| 43 | + refIdx <- arbitrary |
| 44 | + return (NatTxOutRef txId refIdx) |
| 45 | + |
| 46 | +toLedgerTxOutRef :: NatTxOutRef -> TxOutRef |
| 47 | +toLedgerTxOutRef NatTxOutRef {natTxRefIdx, natTxOutRefId} = TxOutRef natTxOutRefId (toInteger natTxRefIdx) |
| 48 | + |
| 49 | +data NatTxOut = NatTxOut { natTxOutAddress :: Address |
| 50 | + , natTxOutValue :: NatValue |
| 51 | + , natTxOutDatumHash :: Maybe DatumHash |
| 52 | + } deriving stock (Show) |
| 53 | + |
| 54 | +instance Arbitrary NatTxOut where |
| 55 | + arbitrary = do |
| 56 | + addr <- arbitrary |
| 57 | + value <- arbitrary |
| 58 | + datumhash <- arbitrary |
| 59 | + return (NatTxOut addr value datumhash) |
| 60 | + |
| 61 | +toLedgerTxOut :: NatTxOut -> TxOut |
| 62 | +toLedgerTxOut NatTxOut{natTxOutAddress, natTxOutValue, natTxOutDatumHash} |
| 63 | + = TxOut natTxOutAddress (toLedgerValue natTxOutValue) natTxOutDatumHash |
0 commit comments