Skip to content

Commit 87f1346

Browse files
committed
add NatLedger for property based testing
1 parent 24db59c commit 87f1346

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

bot-plutus-interface.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ test-suite bot-plutus-interface-test
186186
Spec.BotPlutusInterface.TxStatusChange
187187
Spec.BotPlutusInterface.UtxoParser
188188
Spec.MockContract
189+
Spec.NatLedger
189190

190191
build-depends:
191192
, aeson ^>=1.5.0.0

test/Spec/BotPlutusInterface/CoinSelection.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,5 @@ greedyApprox = do
9696
case eresult of
9797
Left e -> assertFailure (Text.unpack e)
9898
Right result -> result @?= expectedResults
99+
100+

test/Spec/NatLedger.hs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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

Comments
 (0)