module Plutus.ChainIndex.Compatibility where

import Cardano.Api (Block (..), BlockHeader (..), BlockInMode (..), BlockNo (..), CardanoMode, ChainPoint (..),
                    ChainTip (..), Hash, SlotNo (..))
import Cardano.Api.Shelley (Hash (HeaderHash))
import Data.ByteString.Short (fromShort, toShort)
import Ledger (BlockId (..), Slot (..))
import Plutus.ChainIndex.Tx (ChainIndexTx (..))
import Plutus.ChainIndex.Types (BlockNumber (..), Point (..), Tip (..))
import Plutus.Contract.CardanoAPI qualified as C

fromCardanoTip :: ChainTip -> Tip
fromCardanoTip :: ChainTip -> Tip
fromCardanoTip (ChainTip SlotNo
slotNo Hash BlockHeader
hash BlockNo
blockNo) =
    Tip :: Slot -> BlockId -> BlockNumber -> Tip
Tip { tipSlot :: Slot
tipSlot = SlotNo -> Slot
fromCardanoSlot SlotNo
slotNo
        , tipBlockId :: BlockId
tipBlockId = Hash BlockHeader -> BlockId
fromCardanoBlockId Hash BlockHeader
hash
        , tipBlockNo :: BlockNumber
tipBlockNo = BlockNo -> BlockNumber
fromCardanoBlockNo BlockNo
blockNo
        }
fromCardanoTip ChainTip
ChainTipAtGenesis = Tip
TipAtGenesis

fromCardanoPoint :: ChainPoint -> Point
fromCardanoPoint :: ChainPoint -> Point
fromCardanoPoint ChainPoint
ChainPointAtGenesis = Point
PointAtGenesis
fromCardanoPoint (ChainPoint SlotNo
slot Hash BlockHeader
hash) =
    Point :: Slot -> BlockId -> Point
Point { pointSlot :: Slot
pointSlot = SlotNo -> Slot
fromCardanoSlot SlotNo
slot
          , pointBlockId :: BlockId
pointBlockId = Hash BlockHeader -> BlockId
fromCardanoBlockId Hash BlockHeader
hash
          }

toCardanoPoint :: Point -> ChainPoint
toCardanoPoint :: Point -> ChainPoint
toCardanoPoint Point
PointAtGenesis = ChainPoint
ChainPointAtGenesis
toCardanoPoint (Point Slot
slot BlockId
blockId) =
    SlotNo -> Hash BlockHeader -> ChainPoint
ChainPoint (Slot -> SlotNo
forall a b. (Integral a, Num b) => a -> b
fromIntegral Slot
slot) (Hash BlockHeader -> ChainPoint) -> Hash BlockHeader -> ChainPoint
forall a b. (a -> b) -> a -> b
$ BlockId -> Hash BlockHeader
toCardanoBlockId BlockId
blockId

tipFromCardanoBlock
  :: BlockInMode CardanoMode
  -> Tip
tipFromCardanoBlock :: BlockInMode CardanoMode -> Tip
tipFromCardanoBlock (BlockInMode (Block BlockHeader
header [Tx era]
_) EraInMode era CardanoMode
_) =
    BlockHeader -> Tip
tipFromCardanoBlockHeader BlockHeader
header

tipFromCardanoBlockHeader
  :: BlockHeader
  -> Tip
tipFromCardanoBlockHeader :: BlockHeader -> Tip
tipFromCardanoBlockHeader (BlockHeader SlotNo
slot Hash BlockHeader
hash BlockNo
block) =
    ChainTip -> Tip
fromCardanoTip (ChainTip -> Tip) -> ChainTip -> Tip
forall a b. (a -> b) -> a -> b
$ SlotNo -> Hash BlockHeader -> BlockNo -> ChainTip
ChainTip SlotNo
slot Hash BlockHeader
hash BlockNo
block

fromCardanoSlot :: SlotNo -> Slot
fromCardanoSlot :: SlotNo -> Slot
fromCardanoSlot (SlotNo Word64
slotNo) = Integer -> Slot
Slot (Integer -> Slot) -> Integer -> Slot
forall a b. (a -> b) -> a -> b
$ Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
slotNo

fromCardanoBlockId :: Hash BlockHeader -> BlockId
fromCardanoBlockId :: Hash BlockHeader -> BlockId
fromCardanoBlockId (HeaderHash hash) =
    ByteString -> BlockId
BlockId (ByteString -> BlockId) -> ByteString -> BlockId
forall a b. (a -> b) -> a -> b
$ ShortByteString -> ByteString
fromShort ShortByteString
hash

toCardanoBlockId :: BlockId -> Hash BlockHeader
toCardanoBlockId :: BlockId -> Hash BlockHeader
toCardanoBlockId (BlockId ByteString
bs) =
    ShortByteString -> Hash BlockHeader
HeaderHash (ShortByteString -> Hash BlockHeader)
-> ShortByteString -> Hash BlockHeader
forall a b. (a -> b) -> a -> b
$ ByteString -> ShortByteString
toShort ByteString
bs

fromCardanoBlockHeader :: BlockHeader -> Tip
fromCardanoBlockHeader :: BlockHeader -> Tip
fromCardanoBlockHeader (BlockHeader SlotNo
slotNo Hash BlockHeader
hash BlockNo
blockNo) =
    Tip :: Slot -> BlockId -> BlockNumber -> Tip
Tip { tipSlot :: Slot
tipSlot = SlotNo -> Slot
fromCardanoSlot SlotNo
slotNo
        , tipBlockId :: BlockId
tipBlockId = Hash BlockHeader -> BlockId
fromCardanoBlockId Hash BlockHeader
hash
        , tipBlockNo :: BlockNumber
tipBlockNo = BlockNo -> BlockNumber
fromCardanoBlockNo BlockNo
blockNo
        }

fromCardanoBlockNo :: BlockNo -> BlockNumber
fromCardanoBlockNo :: BlockNo -> BlockNumber
fromCardanoBlockNo (BlockNo Word64
blockNo) = Word64 -> BlockNumber
BlockNumber Word64
blockNo

fromCardanoBlock
    :: BlockInMode CardanoMode
    -> [ChainIndexTx]
fromCardanoBlock :: BlockInMode CardanoMode -> [ChainIndexTx]
fromCardanoBlock = BlockInMode CardanoMode -> [ChainIndexTx]
C.fromCardanoBlock