improve ergonomics and add documentation

This commit is contained in:
Emily Martins 2022-03-23 22:03:11 +01:00
parent 78d104c213
commit 210a6195b6
3 changed files with 82 additions and 22 deletions

View file

@ -10,10 +10,6 @@ import Test.Tasty (TestTree, testGroup)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
import Plutarch.Builtin (pforgetData)
--------------------------------------------------------------------------------
import Agora.Stake (StakeDatum (StakeDatum), StakeRedeemer (DepositWithdraw), stakePolicy, stakeValidator) import Agora.Stake (StakeDatum (StakeDatum), StakeRedeemer (DepositWithdraw), stakePolicy, stakeValidator)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -31,35 +27,35 @@ tests =
[ policySucceedsWith [ policySucceedsWith
"stakeCreation" "stakeCreation"
(stakePolicy Stake.stake) (stakePolicy Stake.stake)
(pforgetData (pconstantData ())) ()
Stake.stakeCreation Stake.stakeCreation
, policyFailsWith , policyFailsWith
"stakeCreationWrongDatum" "stakeCreationWrongDatum"
(stakePolicy Stake.stake) (stakePolicy Stake.stake)
(pforgetData (pconstantData ())) ()
Stake.stakeCreationWrongDatum Stake.stakeCreationWrongDatum
, policyFailsWith , policyFailsWith
"stakeCreationUnsigned" "stakeCreationUnsigned"
(stakePolicy Stake.stake) (stakePolicy Stake.stake)
(pforgetData (pconstantData ())) ()
Stake.stakeCreationUnsigned Stake.stakeCreationUnsigned
, validatorSucceedsWith , validatorSucceedsWith
"stakeDepositWithdraw deposit" "stakeDepositWithdraw deposit"
(stakeValidator Stake.stake) (stakeValidator Stake.stake)
(pforgetData (pconstantData . toDatum $ StakeDatum 100_000 signer)) (toDatum $ StakeDatum 100_000 signer)
(pforgetData (pconstantData . toDatum $ DepositWithdraw 100_000)) (toDatum $ DepositWithdraw 100_000)
(Stake.stakeDepositWithdraw $ DepositWithdrawExample {startAmount = 100_000, delta = 100_000}) (Stake.stakeDepositWithdraw $ DepositWithdrawExample {startAmount = 100_000, delta = 100_000})
, validatorSucceedsWith , validatorSucceedsWith
"stakeDepositWithdraw withdraw" "stakeDepositWithdraw withdraw"
(stakeValidator Stake.stake) (stakeValidator Stake.stake)
(pforgetData (pconstantData . toDatum $ StakeDatum 100_000 signer)) (toDatum $ StakeDatum 100_000 signer)
(pforgetData (pconstantData . toDatum $ DepositWithdraw (negate 100_000))) (toDatum $ DepositWithdraw (negate 100_000))
(Stake.stakeDepositWithdraw $ DepositWithdrawExample {startAmount = 100_000, delta = negate 100_000}) (Stake.stakeDepositWithdraw $ DepositWithdrawExample {startAmount = 100_000, delta = negate 100_000})
, validatorFailsWith , validatorFailsWith
"stakeDepositWithdraw negative GT" "stakeDepositWithdraw negative GT"
(stakeValidator Stake.stake) (stakeValidator Stake.stake)
(pforgetData (pconstantData . toDatum $ StakeDatum 100_000 signer)) (toDatum $ StakeDatum 100_000 signer)
(pforgetData (pconstantData . toDatum $ DepositWithdraw (negate 1_000_000))) (toDatum $ DepositWithdraw (negate 1_000_000))
(Stake.stakeDepositWithdraw $ DepositWithdrawExample {startAmount = 100_000, delta = negate 1_000_000}) (Stake.stakeDepositWithdraw $ DepositWithdrawExample {startAmount = 100_000, delta = negate 1_000_000})
] ]
] ]

View file

@ -32,30 +32,93 @@ import Test.Tasty.HUnit (assertFailure, testCase)
import Plutarch import Plutarch
import Plutarch.Api.V1 (PMintingPolicy, PValidator) import Plutarch.Api.V1 (PMintingPolicy, PValidator)
import Plutarch.Builtin (pforgetData)
import Plutarch.Crypto (pblake2b_256) import Plutarch.Crypto (pblake2b_256)
import Plutarch.Evaluate (evalScript) import Plutarch.Evaluate (evalScript)
import Plutarch.Lift (PUnsafeLiftDecl (PLifted))
import Plutarch.Prelude () import Plutarch.Prelude ()
import Plutus.V1.Ledger.Contexts (ScriptContext)
import Plutus.V1.Ledger.Scripts (Datum (Datum), DatumHash (DatumHash), Script) import Plutus.V1.Ledger.Scripts (Datum (Datum), DatumHash (DatumHash), Script)
import PlutusTx.Builtins qualified as PlutusTx import PlutusTx.Builtins qualified as PlutusTx
import PlutusTx.IsData qualified as PlutusTx import PlutusTx.IsData qualified as PlutusTx
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
policySucceedsWith :: String -> ClosedTerm PMintingPolicy -> ClosedTerm PData -> _ -> TestTree policySucceedsWith ::
( PLift redeemer
, PlutusTx.ToData (PLifted redeemer)
) =>
String ->
ClosedTerm PMintingPolicy ->
PLifted redeemer ->
ScriptContext ->
TestTree
policySucceedsWith tag policy redeemer scriptContext = policySucceedsWith tag policy redeemer scriptContext =
scriptSucceeds tag $ compile (policy # redeemer # pconstant scriptContext) scriptSucceeds tag $
compile
( policy
# pforgetData (pconstantData redeemer)
# pconstant scriptContext
)
policyFailsWith :: String -> ClosedTerm PMintingPolicy -> ClosedTerm PData -> _ -> TestTree policyFailsWith ::
( PLift redeemer
, PlutusTx.ToData (PLifted redeemer)
) =>
String ->
ClosedTerm PMintingPolicy ->
PLifted redeemer ->
ScriptContext ->
TestTree
policyFailsWith tag policy redeemer scriptContext = policyFailsWith tag policy redeemer scriptContext =
scriptFails tag $ compile (policy # redeemer # pconstant scriptContext) scriptFails tag $
compile
( policy
# pforgetData (pconstantData redeemer)
# pconstant scriptContext
)
validatorSucceedsWith :: String -> ClosedTerm PValidator -> ClosedTerm PData -> ClosedTerm PData -> _ -> TestTree validatorSucceedsWith ::
( PLift datum
, PlutusTx.ToData (PLifted datum)
, PLift redeemer
, PlutusTx.ToData (PLifted redeemer)
) =>
String ->
ClosedTerm PValidator ->
PLifted datum ->
PLifted redeemer ->
ScriptContext ->
TestTree
validatorSucceedsWith tag policy datum redeemer scriptContext = validatorSucceedsWith tag policy datum redeemer scriptContext =
scriptSucceeds tag $ compile (policy # datum # redeemer # pconstant scriptContext) scriptSucceeds tag $
compile
( policy
# pforgetData (pconstantData datum)
# pforgetData (pconstantData redeemer)
# pconstant scriptContext
)
validatorFailsWith :: String -> ClosedTerm PValidator -> ClosedTerm PData -> ClosedTerm PData -> _ -> TestTree validatorFailsWith ::
( PLift datum
, PlutusTx.ToData (PLifted datum)
, PLift redeemer
, PlutusTx.ToData (PLifted redeemer)
) =>
String ->
ClosedTerm PValidator ->
PLifted datum ->
PLifted redeemer ->
ScriptContext ->
TestTree
validatorFailsWith tag policy datum redeemer scriptContext = validatorFailsWith tag policy datum redeemer scriptContext =
scriptFails tag $ compile (policy # datum # redeemer # pconstant scriptContext) scriptFails tag $
compile
( policy
# pforgetData (pconstantData datum)
# pforgetData (pconstantData redeemer)
# pconstant scriptContext
)
scriptSucceeds :: String -> Script -> TestTree scriptSucceeds :: String -> Script -> TestTree
scriptSucceeds name script = testCase name $ do scriptSucceeds name script = testCase name $ do

View file

@ -75,6 +75,7 @@ pgeqDiscrete = phoistAcyclic $
PDiscrete y' <- pmatch y PDiscrete y' <- pmatch y
y' #<= x' y' #<= x'
-- | Conjure zero discrete unit for any moneyclass
pzeroDiscrete :: forall (mc :: MoneyClass) (s :: S). Term s (PDiscrete mc) pzeroDiscrete :: forall (mc :: MoneyClass) (s :: S). Term s (PDiscrete mc)
pzeroDiscrete = phoistAcyclic $ pcon (PDiscrete 0) pzeroDiscrete = phoistAcyclic $ pcon (PDiscrete 0)
@ -113,7 +114,7 @@ pvalueDiscrete = phoistAcyclic $
# f # f
{- | Get a `PValue` from a `PDiscrete`. {- | Get a `PValue` from a `PDiscrete`.
__NOTE__: `pdiscreteValue` after `pvalueDiscrete` is loses information __NOTE__: `pdiscreteValue` after `pvalueDiscrete` loses information
-} -}
pdiscreteValue :: pdiscreteValue ::
forall (moneyClass :: MoneyClass) (ac :: Symbol) (n :: Symbol) (scale :: Nat) s. forall (moneyClass :: MoneyClass) (ac :: Symbol) (n :: Symbol) (scale :: Nat) s.