Skip to content

Commit

Permalink
Apply suggestions from #419
Browse files Browse the repository at this point in the history
  • Loading branch information
ngua committed Jun 10, 2022
1 parent 65d7109 commit 9de755a
Show file tree
Hide file tree
Showing 16 changed files with 166 additions and 158 deletions.
21 changes: 11 additions & 10 deletions src/Plutus/Types/Address.purs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Aeson
, encodeAeson'
, (.:)
)
import Contract.Prelude (Either(Left))
import Data.Either (Either(Left))
import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe(Just, Nothing))
import Data.Newtype (class Newtype, wrap, unwrap)
Expand All @@ -39,7 +39,11 @@ import Plutus.Types.DataSchema
import Serialization.Address (NetworkId)
import ToData (class ToData, genericToData)
import TypeLevel.Nat (Z)
import Types.PubKeyHash (PaymentPubKeyHash(..), StakePubKeyHash, PubKeyHash)
import Types.PubKeyHash
( PaymentPubKeyHash(PaymentPubKeyHash)
, StakePubKeyHash
, PubKeyHash
)
import Types.Scripts (ValidatorHash)

--------------------------------------------------------------------------------
Expand Down Expand Up @@ -94,14 +98,11 @@ instance FromData Address where
fromData = genericFromData

instance DecodeAeson Address where
decodeAeson aes = caseAesonObject
(Left $ TypeMismatch "Expected object")
( \obj -> do
addressCredential <- obj .: "addressCredential"
addressStakingCredential <- obj .: "addressStakingCredential"
pure $ Address { addressCredential, addressStakingCredential }
)
aes
decodeAeson = caseAesonObject (Left $ TypeMismatch "Expected object") $
\obj -> do
addressCredential <- obj .: "addressCredential"
addressStakingCredential <- obj .: "addressStakingCredential"
pure $ Address { addressCredential, addressStakingCredential }

instance EncodeAeson Address where
encodeAeson' (Address addr) = encodeAeson' addr
Expand Down
52 changes: 30 additions & 22 deletions src/Plutus/Types/Credential.purs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import Plutus.Types.DataSchema
)
import ToData (class ToData, genericToData)
import TypeLevel.Nat (S, Z)
import Aeson.Decode as D
import Aeson.Encode as E
import Aeson.Decode as Decode
import Aeson.Encode as Encode
import Data.Map as Map
import Serialization.Address (CertificateIndex, Slot, TransactionIndex)
import Types.PubKeyHash (PubKeyHash)
Expand Down Expand Up @@ -64,18 +64,22 @@ instance

-- NOTE: mlabs-haskell/purescript-bridge generated and applied here
instance EncodeAeson Credential where
encodeAeson' x = encodeAeson' $
( defer \_ -> case _ of
PubKeyCredential a -> E.encodeTagged "PubKeyCredential" a E.value
ScriptCredential a -> E.encodeTagged "ScriptCredential" a E.value
) x
encodeAeson' = encodeAeson' <<<
( defer $ const $ case _ of
PubKeyCredential a -> Encode.encodeTagged "PubKeyCredential" a
Encode.value
ScriptCredential a -> Encode.encodeTagged "ScriptCredential" a
Encode.value
)

instance DecodeAeson Credential where
decodeAeson = defer \_ -> D.decode
$ D.sumType "Credential"
decodeAeson = defer $ const $ Decode.decode
$ Decode.sumType "Credential"
$ Map.fromFoldable
[ "PubKeyCredential" /\ D.content (PubKeyCredential <$> D.value)
, "ScriptCredential" /\ D.content (ScriptCredential <$> D.value)
[ "PubKeyCredential" /\ Decode.content
(PubKeyCredential <$> Decode.value)
, "ScriptCredential" /\ Decode.content
(ScriptCredential <$> Decode.value)
]

instance ToData Credential where
Expand Down Expand Up @@ -128,21 +132,25 @@ instance FromData StakingCredential where

-- NOTE: mlabs-haskell/purescript-bridge generated and applied here
instance EncodeAeson StakingCredential where
encodeAeson' x = encodeAeson' $
( defer \_ -> case _ of
StakingHash a -> E.encodeTagged "StakingHash" a E.value
StakingPtr ptr -> E.encodeTagged "StakingPtr"
encodeAeson' = encodeAeson' <<< defer
( const $ case _ of
StakingHash a -> Encode.encodeTagged "StakingHash" a Encode.value
StakingPtr ptr -> Encode.encodeTagged "StakingPtr"
(ptr.slot /\ ptr.txIx /\ ptr.certIx)
(E.tuple (E.value >/\< E.value >/\< E.value))
) x
(Encode.tuple (Encode.value >/\< Encode.value >/\< Encode.value))
)

instance DecodeAeson StakingCredential where
decodeAeson = defer \_ -> D.decode
$ D.sumType "StakingCredential"
decodeAeson = defer $ const $ Decode.decode
$ Decode.sumType "StakingCredential"
$ Map.fromFoldable
[ "StakingHash" /\ D.content (StakingHash <$> D.value)
, "StakingPtr" /\ D.content
(D.tuple $ toStakingPtr </$\> D.value </*\> D.value </*\> D.value)
[ "StakingHash" /\ Decode.content (StakingHash <$> Decode.value)
, "StakingPtr" /\ Decode.content
( Decode.tuple $ toStakingPtr </$\> Decode.value </*\> Decode.value
</*\> Decode.value
)
]
where
toStakingPtr
:: Slot -> TransactionIndex -> CertificateIndex -> StakingCredential
toStakingPtr slot txIx certIx = StakingPtr { slot, txIx, certIx }
2 changes: 1 addition & 1 deletion src/Plutus/Types/Value.purs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ import Aeson
, getField
, JsonDecodeError(TypeMismatch)
)
import Contract.Prelude (Either(..))
import Control.Apply (lift3)
import Data.Array (concatMap, filter)
import Data.BigInt (BigInt)
import Data.Either (Either(Left))
import Data.Foldable (all)
import Data.Generic.Rep (class Generic)
import Data.Lattice (class JoinSemilattice, class MeetSemilattice)
Expand Down
3 changes: 1 addition & 2 deletions src/ReindexRedeemers.purs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Data.Array (elemIndex)
import Data.BigInt (fromInt)
import Data.Either (Either(Right), note)
import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe(Just, Nothing))
import Data.Maybe (Maybe(Just))
import Data.Show.Generic (genericShow)
import Data.Traversable (traverse)
import Data.Tuple (fst)
Expand Down Expand Up @@ -65,5 +65,4 @@ reindexSpentScriptRedeemers' inputs redeemersTxIns = runExceptT do
index <- note (CannotGetTxOutRefIndexForRedeemer red)
(fromInt <$> elemIndex txOutRef ipts)
Right $ T.Redeemer red' { index = index } /\ Just txOutRef
red@(T.Redeemer { tag: Spend }) /\ Nothing -> Right $ red /\ Nothing
mintRed /\ txOutRef -> Right $ mintRed /\ txOutRef
12 changes: 4 additions & 8 deletions src/Serialization/Hash.purs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import Aeson
, caseAesonString
, encodeAeson'
)
import Contract.Prelude (Either(..))
import Data.Either (Either(Left), note)
import Data.Either (Either(Left, Right), note)
import Data.Function (on)
import Data.Maybe (Maybe(Nothing, Just), maybe)
import Data.Newtype (unwrap, wrap)
Expand Down Expand Up @@ -144,12 +143,9 @@ instance FromMetadata ScriptHash where

-- Corresponds to Plutus' `Plutus.V1.Ledger.Api.Script` Aeson instances
instance DecodeAeson ScriptHash where
decodeAeson aes = do
let
mayHash = caseAesonString Nothing
(Just <=< scriptHashFromBytes <=< hexToRawBytes)
aes
maybe (Left $ TypeMismatch "Expected hex-encoded script hash") Right mayHash
decodeAeson = do
maybe (Left $ TypeMismatch "Expected hex-encoded script hash") Right <<<
caseAesonString Nothing (Just <=< scriptHashFromBytes <=< hexToRawBytes)

instance EncodeAeson ScriptHash where
encodeAeson' sh = encodeAeson' $ scriptHashToBytes sh
Expand Down
11 changes: 6 additions & 5 deletions src/Types/Datum.purs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import FromData (class FromData)
import ToData (class ToData, toData)
import Types.PlutusData (PlutusData)
import Types.Transaction (DataHash(DataHash)) as X
import Aeson.Decode as D
import Aeson.Encode as E
import Aeson.Decode as Decode
import Aeson.Encode as Encode

-- | Define data types mirroring Plutus `Datum`, like `Datum` itself and
-- | `Redeemer` where the latter is not to be confused with the CSL-stype
Expand All @@ -34,13 +34,14 @@ derive newtype instance Ord Datum
derive newtype instance ToData Datum

instance EncodeAeson Datum where
encodeAeson' x = encodeAeson' $ (defer \_ -> E.encode $ unwrap >$< E.value) x
encodeAeson' = encodeAeson' <<<
defer (const $ Encode.encode $ unwrap >$< Encode.value)

instance DecodeAeson Datum where
decodeAeson = defer \_ -> D.decode $ (Datum <$> D.value)
decodeAeson = defer $ const $ Decode.decode $ Datum <$> Decode.value

instance Show Datum where
show = genericShow

unitDatum :: Datum
unitDatum = Datum (toData unit)
unitDatum = Datum $ toData unit
82 changes: 44 additions & 38 deletions src/Types/Interval.purs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ import Aeson
, isNull
)
import Aeson.Decode ((</$\>), (</*\>))
import Aeson.Decode as D
import Aeson.Decode as Decode
import Aeson.Encode ((>$<), (>/\<))
import Aeson.Encode as E
import Aeson.Encode as Encode
import Control.Lazy (defer)
import Control.Monad.Error.Class (throwError)
import Control.Monad.Except.Trans (ExceptT(ExceptT), runExceptT)
Expand Down Expand Up @@ -132,6 +132,7 @@ import TypeLevel.Nat (S, Z)
type Closure = Boolean

-- | A set extended with a positive and negative infinity.
data Extended :: Type -> Type
data Extended a = NegInf | Finite a | PosInf

instance
Expand Down Expand Up @@ -273,7 +274,7 @@ instance EncodeAeson a => EncodeAeson (Interval a) where

instance DecodeAeson a => DecodeAeson (Interval a) where
decodeAeson a = do
(HaskInterval i) <- decodeAeson a
HaskInterval i <- decodeAeson a
pure $ Interval { from: i.ivFrom, to: i.ivTo }

--------------------------------------------------------------------------------
Expand Down Expand Up @@ -1026,57 +1027,62 @@ derive instance Functor HaskInterval
derive instance Newtype (HaskInterval a) _

instance (EncodeAeson a) => EncodeAeson (HaskInterval a) where
encodeAeson' x = encodeAeson' $
( defer \_ -> E.encode $ unwrap >$<
( E.record
{ ivFrom: E.value :: _ (LowerBound a)
, ivTo: E.value :: _ (UpperBound a)
}
)
) x
encodeAeson' = encodeAeson' <<<
defer
( const $ Encode.encode $ unwrap >$<
( Encode.record
{ ivFrom: Encode.value :: _ (LowerBound a)
, ivTo: Encode.value :: _ (UpperBound a)
}
)
)

instance (DecodeAeson a) => DecodeAeson (HaskInterval a) where
decodeAeson = defer \_ -> D.decode $
( HaskInterval <$> D.record "Interval"
{ ivFrom: D.value :: _ (LowerBound a)
, ivTo: D.value :: _ (UpperBound a)
}
)
decodeAeson = defer $ const $ Decode.decode $
HaskInterval <$> Decode.record "Interval"
{ ivFrom: Decode.value :: _ (LowerBound a)
, ivTo: Decode.value :: _ (UpperBound a)
}

instance (EncodeAeson a) => EncodeAeson (LowerBound a) where
encodeAeson' x = encodeAeson' $
( defer \_ -> E.encode $ (case _ of LowerBound a b -> (a /\ b)) >$<
(E.tuple (E.value >/\< E.value))
) x
encodeAeson' = encodeAeson' <<<
defer
( const $ Encode.encode $ (case _ of LowerBound a b -> (a /\ b)) >$<
(Encode.tuple (Encode.value >/\< Encode.value))
)

instance (DecodeAeson a) => DecodeAeson (LowerBound a) where
decodeAeson = defer \_ -> D.decode $
(D.tuple $ LowerBound </$\> D.value </*\> D.value)
decodeAeson = defer $ const $ Decode.decode
$ Decode.tuple
$ LowerBound </$\> Decode.value </*\> Decode.value

instance (EncodeAeson a) => EncodeAeson (UpperBound a) where
encodeAeson' x = encodeAeson' $
( defer \_ -> E.encode $ (case _ of UpperBound a b -> (a /\ b)) >$<
(E.tuple (E.value >/\< E.value))
) x
encodeAeson' = encodeAeson' <<<
defer
( const $ Encode.encode $ (case _ of UpperBound a b -> (a /\ b)) >$<
(Encode.tuple (Encode.value >/\< Encode.value))
)

instance (DecodeAeson a) => DecodeAeson (UpperBound a) where
decodeAeson = defer \_ -> D.decode $
(D.tuple $ UpperBound </$\> D.value </*\> D.value)
decodeAeson = defer $ const $ Decode.decode
$ Decode.tuple
$ UpperBound </$\> Decode.value </*\> Decode.value

instance (EncodeAeson a) => EncodeAeson (Extended a) where
encodeAeson' x = encodeAeson' $
( defer \_ -> case _ of
NegInf -> encodeAeson { tag: "NegInf" }
Finite a -> E.encodeTagged "Finite" a E.value
PosInf -> encodeAeson { tag: "PosInf" }
) x
encodeAeson' = encodeAeson' <<<
defer
( const $ case _ of
NegInf -> encodeAeson { tag: "NegInf" }
Finite a -> Encode.encodeTagged "Finite" a Encode.value
PosInf -> encodeAeson { tag: "PosInf" }
)

instance (DecodeAeson a) => DecodeAeson (Extended a) where
decodeAeson = defer \_ -> D.decode
$ D.sumType "Extended"
decodeAeson = defer $ const $ Decode.decode
$ Decode.sumType "Extended"
$ Map.fromFoldable
[ "NegInf" /\ pure NegInf
, "Finite" /\ D.content (Finite <$> D.value)
, "Finite" /\ Decode.content (Finite <$> Decode.value)
, "PosInf" /\ pure PosInf
]

Expand Down
24 changes: 12 additions & 12 deletions src/Types/PubKeyHash.purs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import Aeson
, encodeAeson'
, getField
)
import Aeson.Decode as D
import Aeson.Encode as E
import Aeson.Decode as Decode
import Aeson.Encode as Encode
import Data.Either (Either(Left))
import Data.Generic.Rep (class Generic)
import Data.Newtype (class Newtype, unwrap, wrap)
Expand Down Expand Up @@ -65,14 +65,16 @@ instance Show PubKeyHash where

-- NOTE: mlabs-haskell/purescript-bridge generated and applied here
instance EncodeAeson PubKeyHash where
encodeAeson' x = encodeAeson' $ E.encode
(E.record { getPubKeyHash: E.value :: _ (Ed25519KeyHash) })
encodeAeson' x = encodeAeson' $ Encode.encode
(Encode.record { getPubKeyHash: Encode.value :: _ (Ed25519KeyHash) })
{ getPubKeyHash: unwrap x }

instance DecodeAeson PubKeyHash where
decodeAeson x = wrap <<< get (Proxy :: Proxy "getPubKeyHash") <$> D.decode
(D.record "getPubKeyHash " { getPubKeyHash: D.value :: _ (Ed25519KeyHash) })
x
decodeAeson = map (wrap <<< get (Proxy :: Proxy "getPubKeyHash")) <<<
Decode.decode
( Decode.record "getPubKeyHash "
{ getPubKeyHash: Decode.value :: _ (Ed25519KeyHash) }
)

ed25519EnterpriseAddress
:: forall (n :: Type)
Expand Down Expand Up @@ -126,11 +128,9 @@ instance Show PaymentPubKeyHash where
-- This is needed for `ApplyArgs`. Plutus has an `unPaymentPubKeyHash` field so
-- don't newtype derive.
instance DecodeAeson PaymentPubKeyHash where
decodeAeson = caseAesonObject
(Left $ TypeMismatch "Expected object")
( flip getField "unPaymentPubKeyHash" >=>
decodeAeson >>> map PaymentPubKeyHash
)
decodeAeson = caseAesonObject (Left $ TypeMismatch "Expected object")
$ flip getField "unPaymentPubKeyHash" >=> decodeAeson >>> map
PaymentPubKeyHash

newtype StakePubKeyHash = StakePubKeyHash PubKeyHash

Expand Down
8 changes: 4 additions & 4 deletions src/Types/Scripts.purs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ instance Show PlutusScript where
show = genericShow

decodeAesonHelper
(a Type) (b :: Type)
:: (a :: Type) (b :: Type)
. DecodeAeson a
=> String
(a -> b)
Aeson
Either JsonDecodeError b
-> (a -> b)
-> Aeson
-> Either JsonDecodeError b
decodeAesonHelper constrName constr = caseAesonObject
(Left $ TypeMismatch "Expected object")
(flip getField constrName >=> decodeAeson >>> map constr)
Expand Down
Loading

0 comments on commit 9de755a

Please sign in to comment.