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

module Cardano.Ledger.CanonicalState.Namespace.EntitiesAccounts.V0 (
  EntitiesAccountsIn (..),
  EntitiesAccountsOut (..),
  CanonicalAccountState (..),
) where

import Cardano.Ledger.CanonicalState.BasicTypes (CanonicalCoin, decodeNamespacedField)
import Cardano.Ledger.CanonicalState.LedgerCBOR (LedgerCBOR (LedgerCBOR))
import Cardano.Ledger.CanonicalState.Namespace (Era, NamespaceEra)
import Cardano.Ledger.Core (KeyHash, KeyRole (StakePool), Staking)
import Cardano.Ledger.Credential (Credential)
import Cardano.Ledger.State (DRep)
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.Maybe.Strict (StrictMaybe)
import Data.MemPack (MemPack (packM, unpackM))
import Data.Proxy (Proxy (..))
import Data.Text (Text)
import GHC.Generics (Generic)

instance (Era era, NamespaceEra "entities/accounts/v0" ~ era) => KnownNamespace "entities/accounts/v0" where
  type NamespaceKey "entities/accounts/v0" = EntitiesAccountsIn
  type NamespaceEntry "entities/accounts/v0" = EntitiesAccountsOut

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

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

newtype EntitiesAccountsIn = EntitiesAccountsIn (Credential Staking)
  deriving (EntitiesAccountsIn -> EntitiesAccountsIn -> Bool
(EntitiesAccountsIn -> EntitiesAccountsIn -> Bool)
-> (EntitiesAccountsIn -> EntitiesAccountsIn -> Bool)
-> Eq EntitiesAccountsIn
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: EntitiesAccountsIn -> EntitiesAccountsIn -> Bool
== :: EntitiesAccountsIn -> EntitiesAccountsIn -> Bool
$c/= :: EntitiesAccountsIn -> EntitiesAccountsIn -> Bool
/= :: EntitiesAccountsIn -> EntitiesAccountsIn -> Bool
Eq, Eq EntitiesAccountsIn
Eq EntitiesAccountsIn =>
(EntitiesAccountsIn -> EntitiesAccountsIn -> Ordering)
-> (EntitiesAccountsIn -> EntitiesAccountsIn -> Bool)
-> (EntitiesAccountsIn -> EntitiesAccountsIn -> Bool)
-> (EntitiesAccountsIn -> EntitiesAccountsIn -> Bool)
-> (EntitiesAccountsIn -> EntitiesAccountsIn -> Bool)
-> (EntitiesAccountsIn -> EntitiesAccountsIn -> EntitiesAccountsIn)
-> (EntitiesAccountsIn -> EntitiesAccountsIn -> EntitiesAccountsIn)
-> Ord EntitiesAccountsIn
EntitiesAccountsIn -> EntitiesAccountsIn -> Bool
EntitiesAccountsIn -> EntitiesAccountsIn -> Ordering
EntitiesAccountsIn -> EntitiesAccountsIn -> EntitiesAccountsIn
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 :: EntitiesAccountsIn -> EntitiesAccountsIn -> Ordering
compare :: EntitiesAccountsIn -> EntitiesAccountsIn -> Ordering
$c< :: EntitiesAccountsIn -> EntitiesAccountsIn -> Bool
< :: EntitiesAccountsIn -> EntitiesAccountsIn -> Bool
$c<= :: EntitiesAccountsIn -> EntitiesAccountsIn -> Bool
<= :: EntitiesAccountsIn -> EntitiesAccountsIn -> Bool
$c> :: EntitiesAccountsIn -> EntitiesAccountsIn -> Bool
> :: EntitiesAccountsIn -> EntitiesAccountsIn -> Bool
$c>= :: EntitiesAccountsIn -> EntitiesAccountsIn -> Bool
>= :: EntitiesAccountsIn -> EntitiesAccountsIn -> Bool
$cmax :: EntitiesAccountsIn -> EntitiesAccountsIn -> EntitiesAccountsIn
max :: EntitiesAccountsIn -> EntitiesAccountsIn -> EntitiesAccountsIn
$cmin :: EntitiesAccountsIn -> EntitiesAccountsIn -> EntitiesAccountsIn
min :: EntitiesAccountsIn -> EntitiesAccountsIn -> EntitiesAccountsIn
Ord, Int -> EntitiesAccountsIn -> ShowS
[EntitiesAccountsIn] -> ShowS
EntitiesAccountsIn -> String
(Int -> EntitiesAccountsIn -> ShowS)
-> (EntitiesAccountsIn -> String)
-> ([EntitiesAccountsIn] -> ShowS)
-> Show EntitiesAccountsIn
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> EntitiesAccountsIn -> ShowS
showsPrec :: Int -> EntitiesAccountsIn -> ShowS
$cshow :: EntitiesAccountsIn -> String
show :: EntitiesAccountsIn -> String
$cshowList :: [EntitiesAccountsIn] -> ShowS
showList :: [EntitiesAccountsIn] -> ShowS
Show)

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

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

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

deriving newtype instance
  ToCanonicalCBOR "entities/accounts/v0" CanonicalAccountState =>
  ToCanonicalCBOR "entities/accounts/v0" EntitiesAccountsOut

deriving instance
  FromCanonicalCBOR "entities/accounts/v0" CanonicalAccountState =>
  FromCanonicalCBOR "entities/accounts/v0" EntitiesAccountsOut

data CanonicalAccountState = CanonicalAccountState
  { CanonicalAccountState -> CanonicalCoin
casBalance :: !CanonicalCoin
  , CanonicalAccountState -> CanonicalCoin
casDeposit :: !CanonicalCoin
  , CanonicalAccountState -> StrictMaybe DRep
casDRepDelegation :: !(StrictMaybe DRep)
  , CanonicalAccountState -> StrictMaybe (KeyHash StakePool)
casStakePoolDelegation :: !(StrictMaybe (KeyHash StakePool))
  }
  deriving (CanonicalAccountState -> CanonicalAccountState -> Bool
(CanonicalAccountState -> CanonicalAccountState -> Bool)
-> (CanonicalAccountState -> CanonicalAccountState -> Bool)
-> Eq CanonicalAccountState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CanonicalAccountState -> CanonicalAccountState -> Bool
== :: CanonicalAccountState -> CanonicalAccountState -> Bool
$c/= :: CanonicalAccountState -> CanonicalAccountState -> Bool
/= :: CanonicalAccountState -> CanonicalAccountState -> Bool
Eq, Int -> CanonicalAccountState -> ShowS
[CanonicalAccountState] -> ShowS
CanonicalAccountState -> String
(Int -> CanonicalAccountState -> ShowS)
-> (CanonicalAccountState -> String)
-> ([CanonicalAccountState] -> ShowS)
-> Show CanonicalAccountState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CanonicalAccountState -> ShowS
showsPrec :: Int -> CanonicalAccountState -> ShowS
$cshow :: CanonicalAccountState -> String
show :: CanonicalAccountState -> String
$cshowList :: [CanonicalAccountState] -> ShowS
showList :: [CanonicalAccountState] -> ShowS
Show, (forall x. CanonicalAccountState -> Rep CanonicalAccountState x)
-> (forall x. Rep CanonicalAccountState x -> CanonicalAccountState)
-> Generic CanonicalAccountState
forall x. Rep CanonicalAccountState x -> CanonicalAccountState
forall x. CanonicalAccountState -> Rep CanonicalAccountState x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. CanonicalAccountState -> Rep CanonicalAccountState x
from :: forall x. CanonicalAccountState -> Rep CanonicalAccountState x
$cto :: forall x. Rep CanonicalAccountState x -> CanonicalAccountState
to :: forall x. Rep CanonicalAccountState x -> CanonicalAccountState
Generic)

instance
  (Era era, NamespaceEra "entities/accounts/v0" ~ era) =>
  ToCanonicalCBOR "entities/accounts/v0" CanonicalAccountState
  where
  toCanonicalCBOR :: forall (proxy :: Symbol -> *).
proxy "entities/accounts/v0"
-> CanonicalAccountState -> CanonicalEncoding
toCanonicalCBOR proxy "entities/accounts/v0"
v CanonicalAccountState {StrictMaybe (KeyHash StakePool)
StrictMaybe DRep
CanonicalCoin
casBalance :: CanonicalAccountState -> CanonicalCoin
casDeposit :: CanonicalAccountState -> CanonicalCoin
casDRepDelegation :: CanonicalAccountState -> StrictMaybe DRep
casStakePoolDelegation :: CanonicalAccountState -> StrictMaybe (KeyHash StakePool)
casBalance :: CanonicalCoin
casDeposit :: CanonicalCoin
casDRepDelegation :: StrictMaybe DRep
casStakePoolDelegation :: StrictMaybe (KeyHash StakePool)
..} =
    [SomeEncodablePair "entities/accounts/v0"] -> CanonicalEncoding
forall (v :: Symbol) (t :: * -> *).
Traversable t =>
t (SomeEncodablePair v) -> CanonicalEncoding
encodeAsMap
      [ proxy "entities/accounts/v0"
-> Text
-> CanonicalCoin
-> SomeEncodablePair "entities/accounts/v0"
forall (v :: Symbol) k val (proxy :: Symbol -> *).
(ToCanonicalCBOR v k, ToCanonicalCBOR v val) =>
proxy v -> k -> val -> SomeEncodablePair v
mkEncodablePair proxy "entities/accounts/v0"
v (Text
"balance" :: Text) CanonicalCoin
casBalance
      , proxy "entities/accounts/v0"
-> Text
-> CanonicalCoin
-> SomeEncodablePair "entities/accounts/v0"
forall (v :: Symbol) k val (proxy :: Symbol -> *).
(ToCanonicalCBOR v k, ToCanonicalCBOR v val) =>
proxy v -> k -> val -> SomeEncodablePair v
mkEncodablePair proxy "entities/accounts/v0"
v (Text
"deposit" :: Text) CanonicalCoin
casDeposit
      , proxy "entities/accounts/v0"
-> Text
-> StrictMaybe DRep
-> SomeEncodablePair "entities/accounts/v0"
forall (v :: Symbol) k val (proxy :: Symbol -> *).
(ToCanonicalCBOR v k, ToCanonicalCBOR v val) =>
proxy v -> k -> val -> SomeEncodablePair v
mkEncodablePair proxy "entities/accounts/v0"
v (Text
"drep_delegation" :: Text) StrictMaybe DRep
casDRepDelegation
      , proxy "entities/accounts/v0"
-> Text
-> StrictMaybe (KeyHash StakePool)
-> SomeEncodablePair "entities/accounts/v0"
forall (v :: Symbol) k val (proxy :: Symbol -> *).
(ToCanonicalCBOR v k, ToCanonicalCBOR v val) =>
proxy v -> k -> val -> SomeEncodablePair v
mkEncodablePair proxy "entities/accounts/v0"
v (Text
"stake_pool_delegation" :: Text) StrictMaybe (KeyHash StakePool)
casStakePoolDelegation
      ]

instance (Era era, NamespaceEra "entities/accounts/v0" ~ era) => FromCanonicalCBOR v CanonicalAccountState where
  fromCanonicalCBOR :: forall s. CanonicalDecoder s (Versioned v CanonicalAccountState)
fromCanonicalCBOR = do
    Int -> CanonicalDecoder s ()
forall s. Int -> CanonicalDecoder s ()
decodeMapLenCanonicalOf Int
4
    Versioned casBalance <- forall (v :: Symbol) s a.
FromCanonicalCBOR v a =>
Text -> CanonicalDecoder s (Versioned v a)
decodeNamespacedField @"entities/accounts/v0" (Text
"balance" :: Text)
    Versioned casDeposit <- decodeNamespacedField @"entities/accounts/v0" ("deposit" :: Text)
    Versioned casDRepDelegation <-
      decodeNamespacedField @"entities/accounts/v0" ("drep_delegation" :: Text)
    Versioned casStakePoolDelegation <-
      decodeNamespacedField @"entities/accounts/v0" ("stake_pool_delegation" :: Text)
    pure $ Versioned $ CanonicalAccountState {..}

deriving via
  LedgerCBOR v DRep
  instance
    (Era era, NamespaceEra v ~ era) => FromCanonicalCBOR v DRep

deriving via
  LedgerCBOR v DRep
  instance
    (Era era, NamespaceEra v ~ era) => ToCanonicalCBOR v DRep