Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PTryFrom tests for V3 types #693

Merged
merged 5 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion plutarch-ledger-api/plutarch-ledger-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ library

other-modules:
Plutarch.LedgerApi.V1.Address
Plutarch.LedgerApi.V1.Contexts
Plutarch.LedgerApi.V1.Credential
Plutarch.LedgerApi.V1.Crypto
Plutarch.LedgerApi.V1.DCert
Expand All @@ -74,6 +75,7 @@ library
Plutarch.LedgerApi.V1.Tx
Plutarch.LedgerApi.V2.Tx
Plutarch.LedgerApi.V3.Contexts
Plutarch.LedgerApi.V3.Tx

build-depends:
, bytestring
Expand All @@ -91,5 +93,8 @@ test-suite tests
import: test-lang
type: exitcode-stdio-1.0
main-is: Main.hs
build-depends: plutarch-orphanage
build-depends:
, plutarch-orphanage
, prettyprinter

hs-source-dirs: test
47 changes: 4 additions & 43 deletions plutarch-ledger-api/src/Plutarch/LedgerApi/V1.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Plutarch.LedgerApi.V1 (
-- * Contexts
PScriptPurpose (..),
Contexts.PScriptPurpose (..),
PScriptContext (..),

-- * Certificates
Expand Down Expand Up @@ -57,6 +57,7 @@ import Plutarch.DataRepr (
import Plutarch.LedgerApi.AssocMap qualified as AssocMap
import Plutarch.LedgerApi.Interval qualified as Interval
import Plutarch.LedgerApi.V1.Address qualified as Address
import Plutarch.LedgerApi.V1.Contexts qualified as Contexts
import Plutarch.LedgerApi.V1.Credential qualified as Credential
import Plutarch.LedgerApi.V1.Crypto qualified as Crypto
import Plutarch.LedgerApi.V1.DCert qualified as DCert
Expand Down Expand Up @@ -162,46 +163,6 @@ deriving via
-- | @since 3.1.1
instance PTryFrom PData (PAsData PTxInInfo)

-- | @since 3.1.1
data PScriptPurpose (s :: S)
= PMinting (Term s (PDataRecord '["_0" ':= Value.PCurrencySymbol]))
| PSpending (Term s (PDataRecord '["_0" ':= Tx.PTxOutRef]))
| PRewarding (Term s (PDataRecord '["_0" ':= Credential.PStakingCredential]))
| PCertifying (Term s (PDataRecord '["_0" ':= DCert.PDCert]))
deriving stock
( -- | @since 3.1.1
Generic
)
deriving anyclass
( -- | @since 3.1.1
PlutusType
, -- | @since 3.1.1
PIsData
, -- | @since 3.1.1
PEq
, -- | @since 3.1.1
PShow
, -- | @since 3.1.1
PTryFrom PData
)

-- | @since 3.1.1
instance DerivePlutusType PScriptPurpose where
type DPTStrat _ = PlutusTypeData

-- | @since 3.1.1
instance PUnsafeLiftDecl PScriptPurpose where
type PLifted PScriptPurpose = Plutus.ScriptPurpose

-- | @since 3.1.1
deriving via
(DerivePConstantViaData Plutus.ScriptPurpose PScriptPurpose)
instance
PConstantDecl Plutus.ScriptPurpose

-- | @since 3.1.1
instance PTryFrom PData (PAsData PScriptPurpose)

-- | @since 3.1.1
newtype PTxInfo (s :: S)
= PTxInfo
Expand All @@ -216,7 +177,7 @@ newtype PTxInfo (s :: S)
, "wdrl" ':= AssocMap.PMap 'AssocMap.Unsorted Credential.PStakingCredential PInteger -- Staking withdrawals
, "validRange" ':= Interval.PInterval Time.PPosixTime
, "signatories" ':= PBuiltinList (PAsData Crypto.PPubKeyHash)
, "redeemers" ':= AssocMap.PMap 'AssocMap.Unsorted PScriptPurpose Scripts.PRedeemer
, "redeemers" ':= AssocMap.PMap 'AssocMap.Unsorted Contexts.PScriptPurpose Scripts.PRedeemer
, "data" ':= AssocMap.PMap 'AssocMap.Unsorted Scripts.PDatumHash Scripts.PDatum
, "id" ':= Tx.PTxId -- hash of the pending transaction
]
Expand Down Expand Up @@ -260,7 +221,7 @@ instance PTryFrom PData (PAsData PTxInfo)

-- | @since 3.1.1
newtype PScriptContext (s :: S)
= PScriptContext (Term s (PDataRecord '["txInfo" ':= PTxInfo, "purpose" ':= PScriptPurpose]))
= PScriptContext (Term s (PDataRecord '["txInfo" ':= PTxInfo, "purpose" ':= Contexts.PScriptPurpose]))
deriving stock
( -- | @since 3.1.1
Generic
Expand Down
59 changes: 59 additions & 0 deletions plutarch-ledger-api/src/Plutarch/LedgerApi/V1/Contexts.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{-# OPTIONS_GHC -Wno-orphans #-}
SeungheonOh marked this conversation as resolved.
Show resolved Hide resolved

module Plutarch.LedgerApi.V1.Contexts (
PScriptPurpose (..),
) where

import Plutarch.DataRepr (
DerivePConstantViaData (DerivePConstantViaData),
)
import Plutarch.LedgerApi.V1.Credential qualified as Credential
import Plutarch.LedgerApi.V1.DCert qualified as DCert
import Plutarch.LedgerApi.V1.Tx qualified as Tx
import Plutarch.LedgerApi.Value qualified as Value
import Plutarch.Lift (
PConstantDecl,
PUnsafeLiftDecl (PLifted),
)
import Plutarch.Prelude
import PlutusLedgerApi.V1 qualified as Plutus

-- | @since 3.1.1
data PScriptPurpose (s :: S)
SeungheonOh marked this conversation as resolved.
Show resolved Hide resolved
= PMinting (Term s (PDataRecord '["_0" ':= Value.PCurrencySymbol]))
| PSpending (Term s (PDataRecord '["_0" ':= Tx.PTxOutRef]))
| PRewarding (Term s (PDataRecord '["_0" ':= Credential.PStakingCredential]))
| PCertifying (Term s (PDataRecord '["_0" ':= DCert.PDCert]))
deriving stock
( -- | @since 3.1.1
Generic
)
deriving anyclass
( -- | @since 3.1.1
PlutusType
, -- | @since 3.1.1
PIsData
, -- | @since 3.1.1
PEq
, -- | @since 3.1.1
PShow
, -- | @since 3.1.1
PTryFrom PData
)

-- | @since 3.1.1
instance DerivePlutusType PScriptPurpose where
type DPTStrat _ = PlutusTypeData

-- | @since 3.1.1
instance PUnsafeLiftDecl PScriptPurpose where
type PLifted PScriptPurpose = Plutus.ScriptPurpose

-- | @since 3.1.1
deriving via
(DerivePConstantViaData Plutus.ScriptPurpose PScriptPurpose)
instance
PConstantDecl Plutus.ScriptPurpose

-- | @since 3.1.1
instance PTryFrom PData (PAsData PScriptPurpose)
9 changes: 4 additions & 5 deletions plutarch-ledger-api/src/Plutarch/LedgerApi/V1/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module Plutarch.LedgerApi.V1.Tx (
PTxOutRef (..),
) where

import Plutarch.Builtin (PDataNewtype (PDataNewtype))
import Plutarch.DataRepr (
DerivePConstantViaData (DerivePConstantViaData),
PDataFields,
Expand All @@ -25,7 +24,7 @@ import PlutusLedgerApi.V1 qualified as Plutus

@since 3.1.0
-}
newtype PTxId (s :: S) = PTxId (Term s (PDataNewtype PByteString))
newtype PTxId (s :: S) = PTxId (Term s (PDataRecord '["_0" ':= PByteString]))
deriving stock
( -- | @since 2.0.0
Generic
Expand All @@ -47,7 +46,7 @@ newtype PTxId (s :: S) = PTxId (Term s (PDataNewtype PByteString))

-- | @since 3.1.0
instance DerivePlutusType PTxId where
type DPTStrat _ = PlutusTypeNewtype
type DPTStrat _ = PlutusTypeData

-- | @since 2.0.0
instance PUnsafeLiftDecl PTxId where
Expand All @@ -69,7 +68,7 @@ instance PTryFrom PData PTxId where
(plengthBS # unwrapped #== 32)
(f ())
(ptraceInfoError "ptryFrom(PTxId): must be 32 bytes long")
pure (punsafeCoerce opq, pcon . PTxId . pcon . PDataNewtype . pdata $ unwrapped)
pure (punsafeCoerce opq, pcon . PTxId $ pdcons # pdata unwrapped # pdnil)

-- | @since 3.1.0
instance PTryFrom PData (PAsData PTxId) where
Expand All @@ -81,7 +80,7 @@ instance PTryFrom PData (PAsData PTxId) where
(plengthBS # unwrapped #== 32)
(f ())
(ptraceInfoError "ptryFrom(PTxId): must be 32 bytes long")
pure (punsafeCoerce opq, pcon . PTxId . pcon . PDataNewtype . pdata $ unwrapped)
pure (punsafeCoerce opq, pcon . PTxId $ pdcons # pdata unwrapped # pdnil)

{- | Reference to a transaction output, with an index referencing which exact
output we mean.
Expand Down
19 changes: 8 additions & 11 deletions plutarch-ledger-api/src/Plutarch/LedgerApi/V2.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Plutarch.LedgerApi.V2 (
-- * Contexts
V1.PScriptPurpose (..),
Contexts.PScriptPurpose (..),
PScriptContext (..),

-- * Certificates
Expand Down Expand Up @@ -51,9 +51,14 @@ module Plutarch.LedgerApi.V2 (
AssocMap.Commutativity (..),
) where

import Plutarch.DataRepr (
DerivePConstantViaData (DerivePConstantViaData),
PDataFields,
)
import Plutarch.LedgerApi.AssocMap qualified as AssocMap
import Plutarch.LedgerApi.Interval qualified as Interval
import Plutarch.LedgerApi.V1.Address qualified as Address
import Plutarch.LedgerApi.V1.Contexts qualified as Contexts
import Plutarch.LedgerApi.V1.Credential qualified as Credential
import Plutarch.LedgerApi.V1.Crypto qualified as Crypto
import Plutarch.LedgerApi.V1.DCert qualified as DCert
Expand All @@ -62,14 +67,6 @@ import Plutarch.LedgerApi.V1.Time qualified as Time
import Plutarch.LedgerApi.V1.Tx qualified as V1Tx
import Plutarch.LedgerApi.V2.Tx qualified as V2Tx
import Plutarch.LedgerApi.Value qualified as Value

-- TODO: Cleaner factoring

import Plutarch.DataRepr (
DerivePConstantViaData (DerivePConstantViaData),
PDataFields,
)
import Plutarch.LedgerApi.V1 qualified as V1
import Plutarch.Lift (
PConstantDecl,
PUnsafeLiftDecl (PLifted),
Expand Down Expand Up @@ -139,7 +136,7 @@ newtype PTxInfo (s :: S)
, "wdrl" ':= AssocMap.PMap 'AssocMap.Unsorted Credential.PStakingCredential PInteger -- Staking withdrawals
, "validRange" ':= Interval.PInterval Time.PPosixTime
, "signatories" ':= PBuiltinList (PAsData Crypto.PPubKeyHash)
, "redeemers" ':= AssocMap.PMap 'AssocMap.Unsorted V1.PScriptPurpose Scripts.PRedeemer
, "redeemers" ':= AssocMap.PMap 'AssocMap.Unsorted Contexts.PScriptPurpose Scripts.PRedeemer
, "data" ':= AssocMap.PMap 'AssocMap.Unsorted Scripts.PDatumHash Scripts.PDatum
, "id" ':= V1Tx.PTxId -- hash of the pending transaction
]
Expand Down Expand Up @@ -183,7 +180,7 @@ instance PTryFrom PData (PAsData PTxInfo)

-- | @since 3.1.1
newtype PScriptContext (s :: S)
= PScriptContext (Term s (PDataRecord '["txInfo" ':= PTxInfo, "purpose" ':= V1.PScriptPurpose]))
= PScriptContext (Term s (PDataRecord '["txInfo" ':= PTxInfo, "purpose" ':= Contexts.PScriptPurpose]))
deriving stock
( -- | @since 3.1.1
Generic
Expand Down
12 changes: 6 additions & 6 deletions plutarch-ledger-api/src/Plutarch/LedgerApi/V3.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ module Plutarch.LedgerApi.V3 (
-- * Tx

-- ** Types
V1Tx.PTxOutRef (..),
V3Tx.PTxOutRef (..),
V2Tx.PTxOut (..),
V1Tx.PTxId (..),
V3Tx.PTxId (..),
Contexts.PTxInInfo (..),
V2Tx.POutputDatum (..),

Expand Down Expand Up @@ -133,9 +133,9 @@ import Plutarch.LedgerApi.V1.Credential qualified as Credential
import Plutarch.LedgerApi.V1.Crypto qualified as Crypto
import Plutarch.LedgerApi.V1.Scripts qualified as Scripts
import Plutarch.LedgerApi.V1.Time qualified as Time
import Plutarch.LedgerApi.V1.Tx qualified as V1Tx
import Plutarch.LedgerApi.V2.Tx qualified as V2Tx
import Plutarch.LedgerApi.V3.Contexts qualified as Contexts
import Plutarch.LedgerApi.V3.Tx qualified as V3Tx
import Plutarch.LedgerApi.Value qualified as Value
import Plutarch.Prelude
import Plutarch.Script (Script (unScript))
Expand Down Expand Up @@ -213,7 +213,7 @@ pgetContinuingOutputs ::
s
( PBuiltinList Contexts.PTxInInfo
:--> PBuiltinList V2Tx.PTxOut
:--> V1Tx.PTxOutRef
:--> V3Tx.PTxOutRef
:--> PBuiltinList V2Tx.PTxOut
)
pgetContinuingOutputs = phoistAcyclic $
Expand Down Expand Up @@ -257,7 +257,7 @@ pfindOwnInput ::
Term
s
( PBuiltinList Contexts.PTxInInfo
:--> V1Tx.PTxOutRef
:--> V3Tx.PTxOutRef
:--> PMaybe Contexts.PTxInInfo
)
pfindOwnInput = phoistAcyclic $
Expand All @@ -266,7 +266,7 @@ pfindOwnInput = phoistAcyclic $
where
matches ::
forall (s' :: S).
Term s' (V1Tx.PTxOutRef :--> Contexts.PTxInInfo :--> PBool)
Term s' (V3Tx.PTxOutRef :--> Contexts.PTxInInfo :--> PBool)
matches = phoistAcyclic $
plam $ \outref txininfo ->
outref #== pfield @"outRef" # txininfo
Expand Down
12 changes: 6 additions & 6 deletions plutarch-ledger-api/src/Plutarch/LedgerApi/V3/Contexts.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import Plutarch.DataRepr (
import Plutarch.LedgerApi.AssocMap qualified as AssocMap
import Plutarch.LedgerApi.Interval qualified as Interval
import Plutarch.LedgerApi.Utils (PMaybeData, PRationalData)
import Plutarch.LedgerApi.V1.Credential (PCredential, PStakingCredential)
import Plutarch.LedgerApi.V1.Credential (PCredential)
import Plutarch.LedgerApi.V1.Crypto (PPubKeyHash)
import Plutarch.LedgerApi.V1.Scripts (
PDatum,
Expand All @@ -54,8 +54,8 @@ import Plutarch.LedgerApi.V1.Scripts (
PScriptHash,
)
import Plutarch.LedgerApi.V1.Time (PPosixTime)
import Plutarch.LedgerApi.V1.Tx (PTxId, PTxOutRef)
import Plutarch.LedgerApi.V2.Tx (PTxOut)
import Plutarch.LedgerApi.V3.Tx (PTxId, PTxOutRef)
import Plutarch.LedgerApi.Value qualified as Value
import Plutarch.Lift (
DerivePConstantViaBuiltin (DerivePConstantViaBuiltin),
Expand Down Expand Up @@ -254,7 +254,7 @@ instance PTryFrom PData (PAsData PDelegatee)

-- | @since 3.1.0
data PTxCert (s :: S)
= PTxCertRegStaking (Term s (PDataRecord '["_0" ':= PCredential, "_1" ':= PMaybeData Value.PCurrencySymbol]))
= PTxCertRegStaking (Term s (PDataRecord '["_0" ':= PCredential, "_1" ':= PMaybeData Value.PLovelace]))
| PTxCertUnRegStaking (Term s (PDataRecord '["_0" ':= PCredential, "_1" ':= PMaybeData Value.PLovelace]))
| PTxCertDelegStaking (Term s (PDataRecord '["_0" ':= PCredential, "_1" ':= PDelegatee]))
| PTxCertRegDeleg (Term s (PDataRecord '["_0" ':= PCredential, "_1" ':= PDelegatee, "_2" ':= Value.PLovelace]))
Expand Down Expand Up @@ -827,10 +827,10 @@ newtype PTxInfo (s :: S)
'[ "inputs" ':= PBuiltinList (PAsData PTxInInfo)
, "referenceInputs" ':= PBuiltinList (PAsData PTxInInfo)
, "outputs" ':= PBuiltinList (PAsData PTxOut)
, "fee" ':= Value.PValue 'AssocMap.Sorted 'Value.Positive
, "mint" ':= Value.PValue 'AssocMap.Sorted 'Value.NonZero -- value minted by transaction
, "fee" ':= Value.PLovelace
, "mint" ':= Value.PValue 'AssocMap.Sorted 'Value.NoGuarantees -- value minted by transaction
, "txCerts" ':= PBuiltinList (PAsData PTxCert)
, "wdrl" ':= AssocMap.PMap 'AssocMap.Unsorted PStakingCredential PInteger -- Staking withdrawals
, "wdrl" ':= AssocMap.PMap 'AssocMap.Unsorted PCredential Value.PLovelace -- Staking withdrawals
, "validRange" ':= Interval.PInterval PPosixTime
, "signatories" ':= PBuiltinList (PAsData PPubKeyHash)
, "redeemers" ':= AssocMap.PMap 'AssocMap.Unsorted PScriptPurpose PRedeemer
Expand Down
Loading