11{-# LANGUAGE BangPatterns #-}
22{-# LANGUAGE DataKinds #-}
33{-# LANGUAGE FlexibleContexts #-}
4+ {-# LANGUAGE GADTs #-}
45{-# LANGUAGE LambdaCase #-}
56{-# LANGUAGE OverloadedStrings #-}
67{-# LANGUAGE ScopedTypeVariables #-}
@@ -15,11 +16,14 @@ import Cardano.Ledger.Compactible (Compactible (..))
1516import qualified Cardano.Ledger.Core as LC
1617import Cardano.Ledger.Crypto (StandardCrypto )
1718import qualified Cardano.Ledger.Shelley.API as L
19+ import qualified Cardano.Ledger.Shelley.API as Shelley
1820import qualified Cardano.Ledger.Shelley.Rewards as L
1921import qualified Cardano.Ledger.Shelley.RewardUpdate as L
2022import qualified Cardano.Ledger.UMap as UM
2123import qualified Ouroboros.Consensus.Shelley.Ledger as Shelley
2224
25+ import qualified Cardano.Api.Ledger as L
26+ import qualified Cardano.Ledger.Coin as L
2327import qualified Codec.Binary.Bech32 as Bech32
2428import Control.Monad.Trans.Except (runExceptT )
2529import Control.Monad.Trans.Fail.String
@@ -254,7 +258,7 @@ main = do
254258 _
255259 (BlockInMode
256260 (Block (BlockHeader slotNo _blockHeaderHash (BlockNo _blockNoI)) transactions)
257- _era )
261+ eim )
258262 state -> do
259263 let getGoSnapshot = L. unStake . L. ssStake . L. ssStakeGo . L. esSnapshots . L. nesEs
260264 getBalances = UM. rewardMap
@@ -286,8 +290,8 @@ main = do
286290 (" conway" , L. nesEL ls, Just (L. nesRu ls, getGoSnapshot ls, getBalances ls, getPV ls))
287291
288292 let txBodyComponents = map ( (\ (TxBody txbc) -> txbc) . getTxBody ) transactions
289-
290- mapM_ (delegationEvents targetCredAsAPI epochNo slotNo) txBodyComponents
293+ let sbe'm = shelleyBasedEraFromEraInMode eim
294+ mapM_ (delegationEvents sbe'm targetCredAsAPI epochNo slotNo) txBodyComponents
291295 mapM_ (withdrawalEvents targetCredAsAPI epochNo slotNo) txBodyComponents
292296
293297 lastcheck <- displayCheckpoint slotNo (lastCheckpoint state) (checkpoint args)
@@ -315,7 +319,6 @@ main = do
315319
316320 return ()
317321 where
318-
319322 -- CheckPoints --
320323 displayCheckpoint :: SlotNo -> SlotNo -> CheckPoint -> IO SlotNo
321324 displayCheckpoint _ lastcheck CheckPointOff = return lastcheck
@@ -337,31 +340,64 @@ main = do
337340 else return pvLast
338341
339342 -- Delegation Events --
340- delegationEvents :: StakeCredential -> EpochNo -> SlotNo -> TxBodyContent ViewTx era -> IO ()
341- delegationEvents t epochNo slotNo txbc = case txCertificates txbc of
342- TxCertificatesNone -> return ()
343- TxCertificates _ cs _ -> mapM_ msg $ mapMaybe (targetedCert t epochNo slotNo) cs
344-
345- targetedCert :: StakeCredential -> EpochNo -> SlotNo -> Certificate era -> Maybe (Event c )
346- targetedCert t epochNo slotNo = \ case
347- StakeAddressRegistrationCertificate cred ->
348- if t == cred then Just (StakeRegistrationEvent epochNo slotNo) else Nothing
349- StakeAddressDeregistrationCertificate cred ->
350- if t == cred then Just (StakeDeRegistrationEvent epochNo slotNo) else Nothing
351- StakeAddressPoolDelegationCertificate cred pool ->
352- if t == cred then Just (DelegationEvent slotNo pool) else Nothing
353- StakePoolRegistrationCertificate pool ->
354- inPoolCert t slotNo pool
355- StakePoolRetirementCertificate _ _ -> Nothing
356- GenesisKeyDelegationCertificate {} -> Nothing
357- MIRCertificate pot (StakeAddressesMIR mir) ->
358- inMir t epochNo slotNo mir pot
359- MIRCertificate _ (SendToReservesMIR _) -> Nothing
360- MIRCertificate _ (SendToTreasuryMIR _) -> Nothing
361-
362- -- TODO CIP-1694 These are also delegation events. Should there be new events for these?
363- CommitteeDelegationCertificate _ _ -> Nothing
364- CommitteeHotKeyDeregistrationCertificate _ -> Nothing
343+ delegationEvents :: Maybe (ShelleyBasedEra era ) -> StakeCredential -> EpochNo -> SlotNo -> TxBodyContent ViewTx era -> IO ()
344+ delegationEvents sbe'm t epochNo slotNo txbc = do
345+ case (txCertificates txbc, sbe'm) of
346+ (TxCertificates _ cs _, Just sbe) -> mapM_ msg $ mapMaybe (targetedCert sbe t epochNo slotNo) cs
347+ (_, _) -> return ()
348+
349+ targetedCert :: ShelleyBasedEra era -> StakeCredential -> EpochNo -> SlotNo -> Certificate era -> Maybe (Event c )
350+ targetedCert sbe t epochNo slotNo = shelleyBasedEraConstraints sbe $ \ case
351+ ShelleyRelatedCertificate _ c ->
352+ case c of
353+ L. ShelleyTxCertDelegCert (L. ShelleyRegCert cred) ->
354+ if t == fromShelleyStakeCredential cred then Just (StakeRegistrationEvent epochNo slotNo) else Nothing
355+ L. ShelleyTxCertDelegCert (L. ShelleyUnRegCert cred) ->
356+ if t == fromShelleyStakeCredential cred then Just (StakeDeRegistrationEvent epochNo slotNo) else Nothing
357+ L. ShelleyTxCertDelegCert (L. ShelleyDelegCert cred poolId) ->
358+ if t == fromShelleyStakeCredential cred then Just (DelegationEvent slotNo (StakePoolKeyHash poolId)) else Nothing
359+ L. ShelleyTxCertPool (L. RetirePool _poolId _retirementEpoch) ->
360+ Nothing
361+ L. ShelleyTxCertPool (L. RegPool poolParams) ->
362+ inPoolCert t slotNo (fromShelleyPoolParams poolParams)
363+ L. ShelleyTxCertGenesisDeleg (L. GenesisDelegCert _genesisKeyHash _delegateKeyHash _vrfKeyHash) ->
364+ Nothing
365+ L. ShelleyTxCertMir (L. MIRCert pot (L. StakeAddressesMIR mir)) -> do
366+ let addrs = flip map (Map. assocs mir) $ \ (cred, L. DeltaCoin coin) -> (fromShelleyStakeCredential cred, Lovelace coin)
367+ inMir t epochNo slotNo addrs pot
368+ L. ShelleyTxCertMir (L. MIRCert _pot (L. SendToOppositePotMIR _coin)) -> do
369+ Nothing -- TODO: unsure if Nothing
370+
371+ -- TODO: Any events for ConwayCertificates?
372+ ConwayCertificate w cert ->
373+ conwayEraOnwardsConstraints w $
374+ case cert of
375+ L. RegDRepTxCert _credential _coin ->
376+ Nothing
377+ L. UnRegDRepTxCert _credential _coin ->
378+ Nothing
379+ L. AuthCommitteeHotKeyTxCert (Shelley. KeyHash _coldKey) (Shelley. KeyHash _hotKey) ->
380+ Nothing
381+ L. ResignCommitteeColdTxCert (Shelley. KeyHash _coldKey) ->
382+ Nothing
383+ L. RegTxCert _stakeCredential ->
384+ Nothing
385+ L. UnRegTxCert _stakeCredential ->
386+ Nothing
387+ L. RegDepositTxCert _stakeCredential _deposit ->
388+ Nothing
389+ L. UnRegDepositTxCert _stakeCredential _refund ->
390+ Nothing
391+ L. DelegTxCert _stakeCredential _delegatee ->
392+ Nothing
393+ L. RegDepositDelegTxCert _stakeCredential _delegatee _deposit ->
394+ Nothing
395+ L. RegPoolTxCert _poolParams ->
396+ Nothing
397+ L. RetirePoolTxCert (Shelley. KeyHash _kh) _epoch ->
398+ Nothing
399+ L. DelegStakeTxCert _stakeCredential (Shelley. KeyHash _kh) ->
400+ Nothing
365401
366402 stakeCredentialFromStakeAddress (StakeAddress _ cred) = fromShelleyStakeCredential cred
367403
@@ -412,3 +448,16 @@ main = do
412448 -- Reward Calculation End Event --
413449 rewardEndEvent epochLast epochCurrent slot rs t =
414450 epochEvent epochLast epochCurrent slot rs t RewardEndEvent
451+
452+ shelleyBasedEraFromEraInMode :: EraInMode era mode -> Maybe (ShelleyBasedEra era )
453+ shelleyBasedEraFromEraInMode = \ case
454+ ByronEraInByronMode -> Nothing
455+ ByronEraInCardanoMode -> Nothing
456+ ShelleyEraInCardanoMode -> Just ShelleyBasedEraShelley
457+ ShelleyEraInShelleyMode -> Just ShelleyBasedEraShelley
458+ AllegraEraInCardanoMode -> Just ShelleyBasedEraAllegra
459+ MaryEraInCardanoMode -> Just ShelleyBasedEraMary
460+ AlonzoEraInCardanoMode -> Just ShelleyBasedEraAlonzo
461+ BabbageEraInCardanoMode -> Just ShelleyBasedEraBabbage
462+ ConwayEraInCardanoMode -> Just ShelleyBasedEraConway
463+
0 commit comments