{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Cardano.Ledger.CanonicalState.Namespace.EntitiesDReps.V0 (
  EntitiesDRepsIn (..),
  EntitiesDRepsOut (..),
  CanonicalDRepState (..),
  mkCanonicalDRepState,
  fromCanonicalDRepState,
) where

import Cardano.Ledger.BaseTypes (Anchor, EpochNo, StrictMaybe)
import Cardano.Ledger.CanonicalState.BasicTypes (CanonicalCoin (..), decodeNamespacedField)
import Cardano.Ledger.CanonicalState.Namespace (Era, NamespaceEra)
import Cardano.Ledger.Core (KeyRole (DRepRole), Staking)
import Cardano.Ledger.Credential (Credential)
import Cardano.Ledger.DRep (DRepState (..))
import Cardano.SCLS.CBOR.Canonical.Decoder (FromCanonicalCBOR (..), decodeMapLenCanonicalOf)
import Cardano.SCLS.CBOR.Canonical.Encoder (ToCanonicalCBOR (..), encodeAsMap, mkEncodablePair)
import Cardano.SCLS.Entry.IsKey (IsKey (..))
import Cardano.SCLS.NamespaceCodec (
  CanonicalCBOREntryDecoder (..),
  CanonicalCBOREntryEncoder (..),
  KnownNamespace (..),
  NamespaceKeySize,
  namespaceKeySize,
 )
import Cardano.SCLS.Versioned (Versioned (..))
import Data.MemPack (MemPack (packM, unpackM))
import Data.Proxy (Proxy (..))
import Data.Set (Set)
import Data.Text (Text)
import GHC.Generics (Generic)

instance (Era era, NamespaceEra "entities/dreps/v0" ~ era) => KnownNamespace "entities/dreps/v0" where
  type NamespaceKey "entities/dreps/v0" = EntitiesDRepsIn
  type NamespaceEntry "entities/dreps/v0" = EntitiesDRepsOut

instance
  (Era era, NamespaceEra "entities/dreps/v0" ~ era) =>
  CanonicalCBOREntryEncoder "entities/dreps/v0" EntitiesDRepsOut
  where
  encodeEntry :: EntitiesDRepsOut -> CanonicalEncoding
encodeEntry (EntitiesDRepsOut CanonicalDRepState
n) = Proxy "entities/dreps/v0"
-> CanonicalDRepState -> CanonicalEncoding
forall (v :: Symbol) a (proxy :: Symbol -> *).
ToCanonicalCBOR v a =>
proxy v -> a -> CanonicalEncoding
forall (proxy :: Symbol -> *).
proxy "entities/dreps/v0"
-> CanonicalDRepState -> CanonicalEncoding
toCanonicalCBOR (forall {k} (t :: k). Proxy t
forall (t :: Symbol). Proxy t
Proxy @"entities/dreps/v0") CanonicalDRepState
n

instance
  (Era era, NamespaceEra "entities/dreps/v0" ~ era) =>
  CanonicalCBOREntryDecoder "entities/dreps/v0" EntitiesDRepsOut
  where
  decodeEntry :: forall s.
CanonicalDecoder s (Versioned "entities/dreps/v0" EntitiesDRepsOut)
decodeEntry = (CanonicalDRepState -> EntitiesDRepsOut)
-> Versioned "entities/dreps/v0" CanonicalDRepState
-> Versioned "entities/dreps/v0" EntitiesDRepsOut
forall a b.
(a -> b)
-> Versioned "entities/dreps/v0" a
-> Versioned "entities/dreps/v0" b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap CanonicalDRepState -> EntitiesDRepsOut
EntitiesDRepsOut (Versioned "entities/dreps/v0" CanonicalDRepState
 -> Versioned "entities/dreps/v0" EntitiesDRepsOut)
-> CanonicalDecoder
     s (Versioned "entities/dreps/v0" CanonicalDRepState)
-> CanonicalDecoder
     s (Versioned "entities/dreps/v0" EntitiesDRepsOut)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CanonicalDecoder
  s (Versioned "entities/dreps/v0" CanonicalDRepState)
forall s.
CanonicalDecoder
  s (Versioned "entities/dreps/v0" CanonicalDRepState)
forall (v :: Symbol) a s.
FromCanonicalCBOR v a =>
CanonicalDecoder s (Versioned v a)
fromCanonicalCBOR

newtype EntitiesDRepsIn = EntitiesDRepsIn (Credential DRepRole)
  deriving (EntitiesDRepsIn -> EntitiesDRepsIn -> Bool
(EntitiesDRepsIn -> EntitiesDRepsIn -> Bool)
-> (EntitiesDRepsIn -> EntitiesDRepsIn -> Bool)
-> Eq EntitiesDRepsIn
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: EntitiesDRepsIn -> EntitiesDRepsIn -> Bool
== :: EntitiesDRepsIn -> EntitiesDRepsIn -> Bool
$c/= :: EntitiesDRepsIn -> EntitiesDRepsIn -> Bool
/= :: EntitiesDRepsIn -> EntitiesDRepsIn -> Bool
Eq, Eq EntitiesDRepsIn
Eq EntitiesDRepsIn =>
(EntitiesDRepsIn -> EntitiesDRepsIn -> Ordering)
-> (EntitiesDRepsIn -> EntitiesDRepsIn -> Bool)
-> (EntitiesDRepsIn -> EntitiesDRepsIn -> Bool)
-> (EntitiesDRepsIn -> EntitiesDRepsIn -> Bool)
-> (EntitiesDRepsIn -> EntitiesDRepsIn -> Bool)
-> (EntitiesDRepsIn -> EntitiesDRepsIn -> EntitiesDRepsIn)
-> (EntitiesDRepsIn -> EntitiesDRepsIn -> EntitiesDRepsIn)
-> Ord EntitiesDRepsIn
EntitiesDRepsIn -> EntitiesDRepsIn -> Bool
EntitiesDRepsIn -> EntitiesDRepsIn -> Ordering
EntitiesDRepsIn -> EntitiesDRepsIn -> EntitiesDRepsIn
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: EntitiesDRepsIn -> EntitiesDRepsIn -> Ordering
compare :: EntitiesDRepsIn -> EntitiesDRepsIn -> Ordering
$c< :: EntitiesDRepsIn -> EntitiesDRepsIn -> Bool
< :: EntitiesDRepsIn -> EntitiesDRepsIn -> Bool
$c<= :: EntitiesDRepsIn -> EntitiesDRepsIn -> Bool
<= :: EntitiesDRepsIn -> EntitiesDRepsIn -> Bool
$c> :: EntitiesDRepsIn -> EntitiesDRepsIn -> Bool
> :: EntitiesDRepsIn -> EntitiesDRepsIn -> Bool
$c>= :: EntitiesDRepsIn -> EntitiesDRepsIn -> Bool
>= :: EntitiesDRepsIn -> EntitiesDRepsIn -> Bool
$cmax :: EntitiesDRepsIn -> EntitiesDRepsIn -> EntitiesDRepsIn
max :: EntitiesDRepsIn -> EntitiesDRepsIn -> EntitiesDRepsIn
$cmin :: EntitiesDRepsIn -> EntitiesDRepsIn -> EntitiesDRepsIn
min :: EntitiesDRepsIn -> EntitiesDRepsIn -> EntitiesDRepsIn
Ord, Int -> EntitiesDRepsIn -> ShowS
[EntitiesDRepsIn] -> ShowS
EntitiesDRepsIn -> String
(Int -> EntitiesDRepsIn -> ShowS)
-> (EntitiesDRepsIn -> String)
-> ([EntitiesDRepsIn] -> ShowS)
-> Show EntitiesDRepsIn
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> EntitiesDRepsIn -> ShowS
showsPrec :: Int -> EntitiesDRepsIn -> ShowS
$cshow :: EntitiesDRepsIn -> String
show :: EntitiesDRepsIn -> String
$cshowList :: [EntitiesDRepsIn] -> ShowS
showList :: [EntitiesDRepsIn] -> ShowS
Show)

type instance NamespaceKeySize "entities/dreps/v0" = 29

instance IsKey EntitiesDRepsIn where
  keySize :: Int
keySize = forall (ns :: Symbol). KnownNat (NamespaceKeySize ns) => Int
namespaceKeySize @"entities/dreps/v0"
  packKeyM :: forall b. EntitiesDRepsIn -> Pack b ()
packKeyM (EntitiesDRepsIn Credential DRepRole
drepCredential) =
    Credential DRepRole -> Pack b ()
forall s. Credential DRepRole -> Pack s ()
forall a s. MemPack a => a -> Pack s ()
packM Credential DRepRole
drepCredential
  unpackKeyM :: forall b s. Buffer b => Unpack' s b EntitiesDRepsIn
unpackKeyM =
    Credential DRepRole -> EntitiesDRepsIn
EntitiesDRepsIn (Credential DRepRole -> EntitiesDRepsIn)
-> Unpack s b (Credential DRepRole) -> Unpack s b EntitiesDRepsIn
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Unpack s b (Credential DRepRole)
forall a b s. (MemPack a, Buffer b) => Unpack s b a
forall b s. Buffer b => Unpack s b (Credential DRepRole)
unpackM

newtype EntitiesDRepsOut
  = EntitiesDRepsOut CanonicalDRepState
  deriving (EntitiesDRepsOut -> EntitiesDRepsOut -> Bool
(EntitiesDRepsOut -> EntitiesDRepsOut -> Bool)
-> (EntitiesDRepsOut -> EntitiesDRepsOut -> Bool)
-> Eq EntitiesDRepsOut
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: EntitiesDRepsOut -> EntitiesDRepsOut -> Bool
== :: EntitiesDRepsOut -> EntitiesDRepsOut -> Bool
$c/= :: EntitiesDRepsOut -> EntitiesDRepsOut -> Bool
/= :: EntitiesDRepsOut -> EntitiesDRepsOut -> Bool
Eq, Int -> EntitiesDRepsOut -> ShowS
[EntitiesDRepsOut] -> ShowS
EntitiesDRepsOut -> String
(Int -> EntitiesDRepsOut -> ShowS)
-> (EntitiesDRepsOut -> String)
-> ([EntitiesDRepsOut] -> ShowS)
-> Show EntitiesDRepsOut
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> EntitiesDRepsOut -> ShowS
showsPrec :: Int -> EntitiesDRepsOut -> ShowS
$cshow :: EntitiesDRepsOut -> String
show :: EntitiesDRepsOut -> String
$cshowList :: [EntitiesDRepsOut] -> ShowS
showList :: [EntitiesDRepsOut] -> ShowS
Show, (forall x. EntitiesDRepsOut -> Rep EntitiesDRepsOut x)
-> (forall x. Rep EntitiesDRepsOut x -> EntitiesDRepsOut)
-> Generic EntitiesDRepsOut
forall x. Rep EntitiesDRepsOut x -> EntitiesDRepsOut
forall x. EntitiesDRepsOut -> Rep EntitiesDRepsOut x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. EntitiesDRepsOut -> Rep EntitiesDRepsOut x
from :: forall x. EntitiesDRepsOut -> Rep EntitiesDRepsOut x
$cto :: forall x. Rep EntitiesDRepsOut x -> EntitiesDRepsOut
to :: forall x. Rep EntitiesDRepsOut x -> EntitiesDRepsOut
Generic)

deriving newtype instance
  ToCanonicalCBOR "entities/dreps/v0" CanonicalDRepState =>
  ToCanonicalCBOR "entities/dreps/v0" EntitiesDRepsOut

deriving newtype instance
  FromCanonicalCBOR "entities/dreps/v0" CanonicalDRepState =>
  FromCanonicalCBOR "entities/dreps/v0" EntitiesDRepsOut

data CanonicalDRepState = CanonicalDRepState
  { CanonicalDRepState -> EpochNo
cdsExpiry :: !EpochNo
  , CanonicalDRepState -> StrictMaybe Anchor
cdsAnchor :: !(StrictMaybe Anchor)
  , CanonicalDRepState -> CanonicalCoin
cdsDeposit :: !CanonicalCoin
  , CanonicalDRepState -> Set (Credential Staking)
cdsDelegations :: !(Set (Credential Staking))
  }
  deriving (Int -> CanonicalDRepState -> ShowS
[CanonicalDRepState] -> ShowS
CanonicalDRepState -> String
(Int -> CanonicalDRepState -> ShowS)
-> (CanonicalDRepState -> String)
-> ([CanonicalDRepState] -> ShowS)
-> Show CanonicalDRepState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CanonicalDRepState -> ShowS
showsPrec :: Int -> CanonicalDRepState -> ShowS
$cshow :: CanonicalDRepState -> String
show :: CanonicalDRepState -> String
$cshowList :: [CanonicalDRepState] -> ShowS
showList :: [CanonicalDRepState] -> ShowS
Show, CanonicalDRepState -> CanonicalDRepState -> Bool
(CanonicalDRepState -> CanonicalDRepState -> Bool)
-> (CanonicalDRepState -> CanonicalDRepState -> Bool)
-> Eq CanonicalDRepState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CanonicalDRepState -> CanonicalDRepState -> Bool
== :: CanonicalDRepState -> CanonicalDRepState -> Bool
$c/= :: CanonicalDRepState -> CanonicalDRepState -> Bool
/= :: CanonicalDRepState -> CanonicalDRepState -> Bool
Eq, (forall x. CanonicalDRepState -> Rep CanonicalDRepState x)
-> (forall x. Rep CanonicalDRepState x -> CanonicalDRepState)
-> Generic CanonicalDRepState
forall x. Rep CanonicalDRepState x -> CanonicalDRepState
forall x. CanonicalDRepState -> Rep CanonicalDRepState x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. CanonicalDRepState -> Rep CanonicalDRepState x
from :: forall x. CanonicalDRepState -> Rep CanonicalDRepState x
$cto :: forall x. Rep CanonicalDRepState x -> CanonicalDRepState
to :: forall x. Rep CanonicalDRepState x -> CanonicalDRepState
Generic)

instance (Era era, NamespaceEra v ~ era) => ToCanonicalCBOR v CanonicalDRepState where
  toCanonicalCBOR :: forall (proxy :: Symbol -> *).
proxy v -> CanonicalDRepState -> CanonicalEncoding
toCanonicalCBOR proxy v
v CanonicalDRepState {Set (Credential Staking)
StrictMaybe Anchor
EpochNo
CanonicalCoin
cdsExpiry :: CanonicalDRepState -> EpochNo
cdsAnchor :: CanonicalDRepState -> StrictMaybe Anchor
cdsDeposit :: CanonicalDRepState -> CanonicalCoin
cdsDelegations :: CanonicalDRepState -> Set (Credential Staking)
cdsExpiry :: EpochNo
cdsAnchor :: StrictMaybe Anchor
cdsDeposit :: CanonicalCoin
cdsDelegations :: Set (Credential Staking)
..} =
    [SomeEncodablePair v] -> CanonicalEncoding
forall (v :: Symbol) (t :: * -> *).
Traversable t =>
t (SomeEncodablePair v) -> CanonicalEncoding
encodeAsMap
      [ proxy v -> Text -> StrictMaybe Anchor -> SomeEncodablePair v
forall (v :: Symbol) k val (proxy :: Symbol -> *).
(ToCanonicalCBOR v k, ToCanonicalCBOR v val) =>
proxy v -> k -> val -> SomeEncodablePair v
mkEncodablePair proxy v
v (Text
"anchor" :: Text) StrictMaybe Anchor
cdsAnchor
      , proxy v -> Text -> EpochNo -> SomeEncodablePair v
forall (v :: Symbol) k val (proxy :: Symbol -> *).
(ToCanonicalCBOR v k, ToCanonicalCBOR v val) =>
proxy v -> k -> val -> SomeEncodablePair v
mkEncodablePair proxy v
v (Text
"expiry" :: Text) EpochNo
cdsExpiry
      , proxy v -> Text -> CanonicalCoin -> SomeEncodablePair v
forall (v :: Symbol) k val (proxy :: Symbol -> *).
(ToCanonicalCBOR v k, ToCanonicalCBOR v val) =>
proxy v -> k -> val -> SomeEncodablePair v
mkEncodablePair proxy v
v (Text
"deposit" :: Text) CanonicalCoin
cdsDeposit
      , proxy v -> Text -> Set (Credential Staking) -> SomeEncodablePair v
forall (v :: Symbol) k val (proxy :: Symbol -> *).
(ToCanonicalCBOR v k, ToCanonicalCBOR v val) =>
proxy v -> k -> val -> SomeEncodablePair v
mkEncodablePair proxy v
v (Text
"delegations" :: Text) Set (Credential Staking)
cdsDelegations
      ]

instance (Era era, NamespaceEra v ~ era) => FromCanonicalCBOR v CanonicalDRepState where
  fromCanonicalCBOR :: forall s. CanonicalDecoder s (Versioned v CanonicalDRepState)
fromCanonicalCBOR = do
    Int -> CanonicalDecoder s ()
forall s. Int -> CanonicalDecoder s ()
decodeMapLenCanonicalOf Int
4
    Versioned cdsAnchor <- forall (v :: Symbol) s a.
FromCanonicalCBOR v a =>
Text -> CanonicalDecoder s (Versioned v a)
decodeNamespacedField @v Text
"anchor"
    Versioned cdsExpiry <- decodeNamespacedField @v "expiry"
    Versioned cdsDeposit <- decodeNamespacedField @v "deposit"
    Versioned cdsDelegations <- decodeNamespacedField @v "delegations"
    pure $ Versioned CanonicalDRepState {..}

mkCanonicalDRepState :: DRepState -> CanonicalDRepState
mkCanonicalDRepState :: DRepState -> CanonicalDRepState
mkCanonicalDRepState (DRepState {Set (Credential Staking)
StrictMaybe Anchor
EpochNo
CompactForm Coin
drepExpiry :: EpochNo
drepAnchor :: StrictMaybe Anchor
drepDeposit :: CompactForm Coin
drepDelegs :: Set (Credential Staking)
drepDelegs :: DRepState -> Set (Credential Staking)
drepDeposit :: DRepState -> CompactForm Coin
drepAnchor :: DRepState -> StrictMaybe Anchor
drepExpiry :: DRepState -> EpochNo
..}) =
  CanonicalDRepState
    { cdsExpiry :: EpochNo
cdsExpiry = EpochNo
drepExpiry
    , cdsAnchor :: StrictMaybe Anchor
cdsAnchor = StrictMaybe Anchor
drepAnchor
    , cdsDeposit :: CanonicalCoin
cdsDeposit = CompactForm Coin -> CanonicalCoin
CanonicalCoin CompactForm Coin
drepDeposit
    , cdsDelegations :: Set (Credential Staking)
cdsDelegations = Set (Credential Staking)
drepDelegs
    }

fromCanonicalDRepState :: CanonicalDRepState -> DRepState
fromCanonicalDRepState :: CanonicalDRepState -> DRepState
fromCanonicalDRepState (CanonicalDRepState {Set (Credential Staking)
StrictMaybe Anchor
EpochNo
CanonicalCoin
cdsExpiry :: CanonicalDRepState -> EpochNo
cdsAnchor :: CanonicalDRepState -> StrictMaybe Anchor
cdsDeposit :: CanonicalDRepState -> CanonicalCoin
cdsDelegations :: CanonicalDRepState -> Set (Credential Staking)
cdsExpiry :: EpochNo
cdsAnchor :: StrictMaybe Anchor
cdsDeposit :: CanonicalCoin
cdsDelegations :: Set (Credential Staking)
..}) =
  DRepState
    { drepExpiry :: EpochNo
drepExpiry = EpochNo
cdsExpiry
    , drepAnchor :: StrictMaybe Anchor
drepAnchor = StrictMaybe Anchor
cdsAnchor
    , drepDeposit :: CompactForm Coin
drepDeposit = CanonicalCoin -> CompactForm Coin
unCoin CanonicalCoin
cdsDeposit
    , drepDelegs :: Set (Credential Staking)
drepDelegs = Set (Credential Staking)
cdsDelegations
    }