add ToData/FromData instances to Discrete

This commit is contained in:
Emily Martins 2022-03-26 13:01:56 +01:00
parent dee3de0a73
commit 9c3d7c4271
2 changed files with 25 additions and 2 deletions

View file

@ -29,7 +29,9 @@ import Prelude
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
import Plutus.V1.Ledger.Api (BuiltinData (..), Data (..))
import Plutus.V1.Ledger.Value (AssetClass (AssetClass)) import Plutus.V1.Ledger.Value (AssetClass (AssetClass))
import PlutusTx.IsData.Class (FromData (..), ToData (..))
import Plutarch.Api.V1 (PValue) import Plutarch.Api.V1 (PValue)
import Plutarch.Builtin () import Plutarch.Builtin ()
@ -55,11 +57,32 @@ data ADATag
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- | A tagged AssetClass. Use to resolve a reference inside of a PDiscrete -- | A tagged AssetClass. Use to resolve a reference inside of a PDiscrete
data AssetClassRef (tag :: Type) = AssetClassRef {getAssetClass :: AssetClass} newtype AssetClassRef (tag :: Type) = AssetClassRef {getAssetClass :: AssetClass}
-- | Resolves ada tags.
adaRef :: AssetClassRef ADATag adaRef :: AssetClassRef ADATag
adaRef = AssetClassRef (AssetClass ("", "")) adaRef = AssetClassRef (AssetClass ("", ""))
{- | Represents a single asset in a 'Value' related to a particular 'AssetClass'
through 'AssetClassRef'.
-}
newtype Discrete (tag :: Type)
= Discrete Integer
deriving stock (Show, Eq)
{- We have to manually write these instances because the `tag` will confuse
`makeIsDataIndexed`.
-}
instance forall tag. FromData (Discrete tag) where
fromBuiltinData (BuiltinData (I x)) = Just (Discrete x)
fromBuiltinData _ = Nothing
instance forall tag. ToData (Discrete tag) where
toBuiltinData (Discrete x) = BuiltinData (I x)
{- | Represents a single asset in a 'PValue' related to a particular 'AssetClass'
through 'AssetClassRef'.
-}
newtype PDiscrete (tag :: Type) (s :: S) newtype PDiscrete (tag :: Type) (s :: S)
= PDiscrete (Term s PInteger) = PDiscrete (Term s PInteger)
deriving (PlutusType, PIsData, PEq, POrd) via (DerivePNewtype (PDiscrete tag) PInteger) deriving (PlutusType, PIsData, PEq, POrd) via (DerivePNewtype (PDiscrete tag) PInteger)

View file

@ -79,7 +79,7 @@ import Agora.Utils (
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- | Parameters for creating Stake scripts. -- | Parameters for creating Stake scripts.
data Stake = Stake newtype Stake = Stake
{ gtClassRef :: AssetClassRef GTTag { gtClassRef :: AssetClassRef GTTag
-- ^ Resolve governance token -- ^ Resolve governance token
} }