-- | Block queries

module Blockfrost.Client.Cardano.Blocks
  ( getLatestBlock
  , getLatestBlockTxs
  , getLatestBlockTxs'
  , getBlock
  , getBlockSlot
  , getBlockEpochSlot
  , getNextBlocks
  , getNextBlocks'
  , getPreviousBlocks
  , getPreviousBlocks'
  , getBlockTxs
  , getBlockTxs'
  , getBlockAffectedAddresses'
  , getBlockAffectedAddresses
  ) where

import Blockfrost.API
import Blockfrost.Client.Types
import Blockfrost.Types

blocksClient :: MonadBlockfrost m => Project -> BlocksAPI (AsClientT m)
blocksClient :: Project -> BlocksAPI (AsClientT m)
blocksClient = (((m Block :<|> (Paged -> SortOrder -> m [TxHash]))
  :<|> ((Either Integer BlockHash -> m Block)
        :<|> (Slot -> m Block)))
 :<|> (((Epoch -> Slot -> m Block)
        :<|> (Either Integer BlockHash -> Paged -> m [Block]))
       :<|> ((Either Integer BlockHash -> Paged -> m [Block])
             :<|> ((Either Integer BlockHash
                    -> Paged -> SortOrder -> m [TxHash])
                   :<|> (Either Integer BlockHash
                         -> Paged -> m [(Address, [TxHash])])))))
-> BlocksAPI (AsClientT m)
forall (routes :: * -> *) mode.
GenericServant routes mode =>
ToServant routes mode -> routes mode
fromServant ((((m Block :<|> (Paged -> SortOrder -> m [TxHash]))
   :<|> ((Either Integer BlockHash -> m Block)
         :<|> (Slot -> m Block)))
  :<|> (((Epoch -> Slot -> m Block)
         :<|> (Either Integer BlockHash -> Paged -> m [Block]))
        :<|> ((Either Integer BlockHash -> Paged -> m [Block])
              :<|> ((Either Integer BlockHash
                     -> Paged -> SortOrder -> m [TxHash])
                    :<|> (Either Integer BlockHash
                          -> Paged -> m [(Address, [TxHash])])))))
 -> BlocksAPI (AsClientT m))
-> (Project
    -> ((m Block :<|> (Paged -> SortOrder -> m [TxHash]))
        :<|> ((Either Integer BlockHash -> m Block)
              :<|> (Slot -> m Block)))
       :<|> (((Epoch -> Slot -> m Block)
              :<|> (Either Integer BlockHash -> Paged -> m [Block]))
             :<|> ((Either Integer BlockHash -> Paged -> m [Block])
                   :<|> ((Either Integer BlockHash
                          -> Paged -> SortOrder -> m [TxHash])
                         :<|> (Either Integer BlockHash
                               -> Paged -> m [(Address, [TxHash])])))))
-> Project
-> BlocksAPI (AsClientT m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CardanoAPI (AsClientT m)
-> ((m Block :<|> (Paged -> SortOrder -> m [TxHash]))
    :<|> ((Either Integer BlockHash -> m Block)
          :<|> (Slot -> m Block)))
   :<|> (((Epoch -> Slot -> m Block)
          :<|> (Either Integer BlockHash -> Paged -> m [Block]))
         :<|> ((Either Integer BlockHash -> Paged -> m [Block])
               :<|> ((Either Integer BlockHash
                      -> Paged -> SortOrder -> m [TxHash])
                     :<|> (Either Integer BlockHash
                           -> Paged -> m [(Address, [TxHash])]))))
forall route.
CardanoAPI route
-> route
   :- ("blocks"
       :> (Tag "Cardano \187 Blocks" :> ToServantApi BlocksAPI))
_blocks (CardanoAPI (AsClientT m)
 -> ((m Block :<|> (Paged -> SortOrder -> m [TxHash]))
     :<|> ((Either Integer BlockHash -> m Block)
           :<|> (Slot -> m Block)))
    :<|> (((Epoch -> Slot -> m Block)
           :<|> (Either Integer BlockHash -> Paged -> m [Block]))
          :<|> ((Either Integer BlockHash -> Paged -> m [Block])
                :<|> ((Either Integer BlockHash
                       -> Paged -> SortOrder -> m [TxHash])
                      :<|> (Either Integer BlockHash
                            -> Paged -> m [(Address, [TxHash])])))))
-> (Project -> CardanoAPI (AsClientT m))
-> Project
-> ((m Block :<|> (Paged -> SortOrder -> m [TxHash]))
    :<|> ((Either Integer BlockHash -> m Block)
          :<|> (Slot -> m Block)))
   :<|> (((Epoch -> Slot -> m Block)
          :<|> (Either Integer BlockHash -> Paged -> m [Block]))
         :<|> ((Either Integer BlockHash -> Paged -> m [Block])
               :<|> ((Either Integer BlockHash
                      -> Paged -> SortOrder -> m [TxHash])
                     :<|> (Either Integer BlockHash
                           -> Paged -> m [(Address, [TxHash])]))))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Project -> CardanoAPI (AsClientT m)
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> CardanoAPI (AsClientT m)
cardanoClient

getLatestBlock_ :: MonadBlockfrost m => Project -> m Block
getLatestBlock_ :: Project -> m Block
getLatestBlock_ = BlocksAPI (AsClientT m) -> m Block
forall route.
BlocksAPI route
-> route
   :- (Summary "Latest block"
       :> (Description
             "Return the latest block available to the backends, also known as the tip of the blockchain."
           :> ("latest" :> Get '[JSON] Block)))
_latest (BlocksAPI (AsClientT m) -> m Block)
-> (Project -> BlocksAPI (AsClientT m)) -> Project -> m Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Project -> BlocksAPI (AsClientT m)
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> BlocksAPI (AsClientT m)
blocksClient

-- | Return the latest block available to the backends, also known as the tip of the blockchain.
getLatestBlock :: MonadBlockfrost m => m Block
getLatestBlock :: m Block
getLatestBlock = (Project -> m Block) -> m Block
forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go Project -> m Block
forall (m :: * -> *). MonadBlockfrost m => Project -> m Block
getLatestBlock_

getLatestBlockTxs_ :: MonadBlockfrost m => Project -> Paged -> SortOrder -> m [TxHash]
getLatestBlockTxs_ :: Project -> Paged -> SortOrder -> m [TxHash]
getLatestBlockTxs_ = BlocksAPI (AsClientT m) -> Paged -> SortOrder -> m [TxHash]
forall route.
BlocksAPI route
-> route
   :- (Summary "Latest block transactions"
       :> (Description "Return the transactions within the latest block."
           :> ("latest"
               :> ("txs" :> (Pagination :> (Sorting :> Get '[JSON] [TxHash]))))))
_latestTxs (BlocksAPI (AsClientT m) -> Paged -> SortOrder -> m [TxHash])
-> (Project -> BlocksAPI (AsClientT m))
-> Project
-> Paged
-> SortOrder
-> m [TxHash]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Project -> BlocksAPI (AsClientT m)
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> BlocksAPI (AsClientT m)
blocksClient

-- | Return the transactions within the latest block.
-- Allows custom paging and ordering using @Paged@ and @SortOrder@.
getLatestBlockTxs' :: MonadBlockfrost m => Paged -> SortOrder -> m [TxHash]
getLatestBlockTxs' :: Paged -> SortOrder -> m [TxHash]
getLatestBlockTxs' Paged
pg SortOrder
s = (Project -> m [TxHash]) -> m [TxHash]
forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go (\Project
p -> Project -> Paged -> SortOrder -> m [TxHash]
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> Paged -> SortOrder -> m [TxHash]
getLatestBlockTxs_ Project
p Paged
pg SortOrder
s)

getLatestBlockTxs :: MonadBlockfrost m => m [TxHash]
getLatestBlockTxs :: m [TxHash]
getLatestBlockTxs = Paged -> SortOrder -> m [TxHash]
forall (m :: * -> *).
MonadBlockfrost m =>
Paged -> SortOrder -> m [TxHash]
getLatestBlockTxs' Paged
forall a. Default a => a
def SortOrder
forall a. Default a => a
def

getBlock_ :: MonadBlockfrost m => Project -> Either Integer BlockHash -> m Block
getBlock_ :: Project -> Either Integer BlockHash -> m Block
getBlock_ = BlocksAPI (AsClientT m) -> Either Integer BlockHash -> m Block
forall route.
BlocksAPI route
-> route
   :- (Summary "Latest block transactions"
       :> (Description "Return the transactions within the latest block."
           :> (Capture "hash_or_number" (Either Integer BlockHash)
               :> Get '[JSON] Block)))
_block (BlocksAPI (AsClientT m) -> Either Integer BlockHash -> m Block)
-> (Project -> BlocksAPI (AsClientT m))
-> Project
-> Either Integer BlockHash
-> m Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Project -> BlocksAPI (AsClientT m)
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> BlocksAPI (AsClientT m)
blocksClient

-- | Return the content of a requested block.
getBlock :: MonadBlockfrost m => Either Integer BlockHash -> m Block
getBlock :: Either Integer BlockHash -> m Block
getBlock Either Integer BlockHash
a = (Project -> m Block) -> m Block
forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go (Project -> Either Integer BlockHash -> m Block
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> Either Integer BlockHash -> m Block
`getBlock_` Either Integer BlockHash
a)

getBlockSlot_ :: MonadBlockfrost m => Project -> Slot -> m Block
getBlockSlot_ :: Project -> Slot -> m Block
getBlockSlot_ = BlocksAPI (AsClientT m) -> Slot -> m Block
forall route.
BlocksAPI route
-> route
   :- (Summary "Specific block in a slot"
       :> (Description
             "Return the content of a requested block for a specific slot."
           :> ("slot" :> (Capture "slot_number" Slot :> Get '[JSON] Block))))
__blockSlot (BlocksAPI (AsClientT m) -> Slot -> m Block)
-> (Project -> BlocksAPI (AsClientT m))
-> Project
-> Slot
-> m Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Project -> BlocksAPI (AsClientT m)
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> BlocksAPI (AsClientT m)
blocksClient

-- | Return the content of a requested block for a specific slot.
getBlockSlot :: MonadBlockfrost m => Slot -> m Block
getBlockSlot :: Slot -> m Block
getBlockSlot Slot
i = (Project -> m Block) -> m Block
forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go (Project -> Slot -> m Block
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> Slot -> m Block
`getBlockSlot_` Slot
i)

getBlockEpochSlot_ :: MonadBlockfrost m => Project -> Epoch -> Slot -> m Block
getBlockEpochSlot_ :: Project -> Epoch -> Slot -> m Block
getBlockEpochSlot_ = BlocksAPI (AsClientT m) -> Epoch -> Slot -> m Block
forall route.
BlocksAPI route
-> route
   :- (Summary "Specific block in a slot in an epoch"
       :> (Description
             "Return the content of a requested block for a specific slot in an epoch."
           :> ("epoch"
               :> (Capture "epoch_number" Epoch
                   :> ("slot"
                       :> (Capture "slot_number" Slot :> Get '[JSON] Block))))))
__blockEpochSlot (BlocksAPI (AsClientT m) -> Epoch -> Slot -> m Block)
-> (Project -> BlocksAPI (AsClientT m))
-> Project
-> Epoch
-> Slot
-> m Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Project -> BlocksAPI (AsClientT m)
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> BlocksAPI (AsClientT m)
blocksClient

-- | Return the content of a requested block for a specific slot in an epoch.
getBlockEpochSlot :: MonadBlockfrost m => Epoch -> Slot -> m Block
getBlockEpochSlot :: Epoch -> Slot -> m Block
getBlockEpochSlot Epoch
ep Slot
sl = (Project -> m Block) -> m Block
forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go (\Project
p -> Project -> Epoch -> Slot -> m Block
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> Epoch -> Slot -> m Block
getBlockEpochSlot_ Project
p Epoch
ep Slot
sl)

getNextBlocks_ :: MonadBlockfrost m => Project -> Either Integer BlockHash -> Paged -> m [Block]
getNextBlocks_ :: Project -> Either Integer BlockHash -> Paged -> m [Block]
getNextBlocks_ = BlocksAPI (AsClientT m)
-> Either Integer BlockHash -> Paged -> m [Block]
forall route.
BlocksAPI route
-> route
   :- (Summary "Listing of next blocks"
       :> (Description
             "Return the list of blocks following a specific block."
           :> (Capture "hash_or_number" (Either Integer BlockHash)
               :> ("next" :> (Pagination :> Get '[JSON] [Block])))))
_blockNext (BlocksAPI (AsClientT m)
 -> Either Integer BlockHash -> Paged -> m [Block])
-> (Project -> BlocksAPI (AsClientT m))
-> Project
-> Either Integer BlockHash
-> Paged
-> m [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Project -> BlocksAPI (AsClientT m)
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> BlocksAPI (AsClientT m)
blocksClient

-- | Return the list of blocks following a specific block.
-- Allows custom paging using @Paged@.
getNextBlocks' :: MonadBlockfrost m => Either Integer BlockHash -> Paged -> m [Block]
getNextBlocks' :: Either Integer BlockHash -> Paged -> m [Block]
getNextBlocks' Either Integer BlockHash
a Paged
pg = (Project -> m [Block]) -> m [Block]
forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go (\Project
p -> Project -> Either Integer BlockHash -> Paged -> m [Block]
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> Either Integer BlockHash -> Paged -> m [Block]
getNextBlocks_ Project
p Either Integer BlockHash
a Paged
pg)

-- | Return the list of blocks following a specific block.
getNextBlocks :: MonadBlockfrost m => Either Integer BlockHash -> m [Block]
getNextBlocks :: Either Integer BlockHash -> m [Block]
getNextBlocks Either Integer BlockHash
a = Either Integer BlockHash -> Paged -> m [Block]
forall (m :: * -> *).
MonadBlockfrost m =>
Either Integer BlockHash -> Paged -> m [Block]
getNextBlocks' Either Integer BlockHash
a Paged
forall a. Default a => a
def

getPreviousBlocks_ :: MonadBlockfrost m => Project -> Either Integer BlockHash -> Paged -> m [Block]
getPreviousBlocks_ :: Project -> Either Integer BlockHash -> Paged -> m [Block]
getPreviousBlocks_ = BlocksAPI (AsClientT m)
-> Either Integer BlockHash -> Paged -> m [Block]
forall route.
BlocksAPI route
-> route
   :- (Summary "Listing of preious blocks"
       :> (Description
             "Return the list of blocks preceeding a specific block."
           :> (Capture "hash_or_number" (Either Integer BlockHash)
               :> ("previous" :> (Pagination :> Get '[JSON] [Block])))))
_blockPrevious (BlocksAPI (AsClientT m)
 -> Either Integer BlockHash -> Paged -> m [Block])
-> (Project -> BlocksAPI (AsClientT m))
-> Project
-> Either Integer BlockHash
-> Paged
-> m [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Project -> BlocksAPI (AsClientT m)
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> BlocksAPI (AsClientT m)
blocksClient

-- | Return the list of blocks preceding a specific block.
-- Allows custom paging using @Paged@.
getPreviousBlocks' :: MonadBlockfrost m => Either Integer BlockHash -> Paged -> m [Block]
getPreviousBlocks' :: Either Integer BlockHash -> Paged -> m [Block]
getPreviousBlocks' Either Integer BlockHash
a Paged
pg = (Project -> m [Block]) -> m [Block]
forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go (\Project
p -> Project -> Either Integer BlockHash -> Paged -> m [Block]
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> Either Integer BlockHash -> Paged -> m [Block]
getPreviousBlocks_ Project
p Either Integer BlockHash
a Paged
pg)

-- | Return the list of blocks preceding a specific block.
getPreviousBlocks :: MonadBlockfrost m => Either Integer BlockHash -> m [Block]
getPreviousBlocks :: Either Integer BlockHash -> m [Block]
getPreviousBlocks Either Integer BlockHash
a = Either Integer BlockHash -> Paged -> m [Block]
forall (m :: * -> *).
MonadBlockfrost m =>
Either Integer BlockHash -> Paged -> m [Block]
getPreviousBlocks' Either Integer BlockHash
a Paged
forall a. Default a => a
def

getBlockTxs_ :: MonadBlockfrost m => Project -> Either Integer BlockHash -> Paged -> SortOrder -> m [TxHash]
getBlockTxs_ :: Project
-> Either Integer BlockHash -> Paged -> SortOrder -> m [TxHash]
getBlockTxs_ = BlocksAPI (AsClientT m)
-> Either Integer BlockHash -> Paged -> SortOrder -> m [TxHash]
forall route.
BlocksAPI route
-> route
   :- (Summary "Block transactions"
       :> (Description "Return the transactions within the block."
           :> (Capture "hash_or_number" (Either Integer BlockHash)
               :> ("txs" :> (Pagination :> (Sorting :> Get '[JSON] [TxHash]))))))
_blockTxs (BlocksAPI (AsClientT m)
 -> Either Integer BlockHash -> Paged -> SortOrder -> m [TxHash])
-> (Project -> BlocksAPI (AsClientT m))
-> Project
-> Either Integer BlockHash
-> Paged
-> SortOrder
-> m [TxHash]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Project -> BlocksAPI (AsClientT m)
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> BlocksAPI (AsClientT m)
blocksClient

-- | Return the transactions within the block.
-- Allows custom paging and ordering using @Paged@ and @SortOrder@.
getBlockTxs' :: MonadBlockfrost m => Either Integer BlockHash -> Paged -> SortOrder -> m [TxHash]
getBlockTxs' :: Either Integer BlockHash -> Paged -> SortOrder -> m [TxHash]
getBlockTxs' Either Integer BlockHash
a Paged
pg SortOrder
s = (Project -> m [TxHash]) -> m [TxHash]
forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go (\Project
p -> Project
-> Either Integer BlockHash -> Paged -> SortOrder -> m [TxHash]
forall (m :: * -> *).
MonadBlockfrost m =>
Project
-> Either Integer BlockHash -> Paged -> SortOrder -> m [TxHash]
getBlockTxs_ Project
p Either Integer BlockHash
a Paged
pg SortOrder
s)

-- | Return the transactions within the block.
getBlockTxs :: MonadBlockfrost m => Either Integer BlockHash -> m [TxHash]
getBlockTxs :: Either Integer BlockHash -> m [TxHash]
getBlockTxs Either Integer BlockHash
a = Either Integer BlockHash -> Paged -> SortOrder -> m [TxHash]
forall (m :: * -> *).
MonadBlockfrost m =>
Either Integer BlockHash -> Paged -> SortOrder -> m [TxHash]
getBlockTxs' Either Integer BlockHash
a Paged
forall a. Default a => a
def SortOrder
forall a. Default a => a
def

getBlockAffectedAddresses_ :: MonadBlockfrost m => Project -> Either Integer BlockHash -> Paged -> m [(Address, [TxHash])]
getBlockAffectedAddresses_ :: Project
-> Either Integer BlockHash -> Paged -> m [(Address, [TxHash])]
getBlockAffectedAddresses_ = BlocksAPI (AsClientT m)
-> Either Integer BlockHash -> Paged -> m [(Address, [TxHash])]
forall route.
BlocksAPI route
-> route
   :- (Summary "Addresses affected in a specific block"
       :> (Description
             "Return list of addresses affected in the specified block with additional information, sorted by the bech32 address, ascending."
           :> (Capture "hash_or_number" (Either Integer BlockHash)
               :> ("addresses"
                   :> (Pagination :> Get '[JSON] [(Address, [TxHash])])))))
_blockAffectedAddresses (BlocksAPI (AsClientT m)
 -> Either Integer BlockHash -> Paged -> m [(Address, [TxHash])])
-> (Project -> BlocksAPI (AsClientT m))
-> Project
-> Either Integer BlockHash
-> Paged
-> m [(Address, [TxHash])]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Project -> BlocksAPI (AsClientT m)
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> BlocksAPI (AsClientT m)
blocksClient

-- | Return list of addresses affected in the specified block with additional information, sorted by the bech32 address, ascending.
-- Allows custom paging using @Paged@.
getBlockAffectedAddresses' :: MonadBlockfrost m => Either Integer BlockHash -> Paged -> m [(Address, [TxHash])]
getBlockAffectedAddresses' :: Either Integer BlockHash -> Paged -> m [(Address, [TxHash])]
getBlockAffectedAddresses' Either Integer BlockHash
a Paged
pg = (Project -> m [(Address, [TxHash])]) -> m [(Address, [TxHash])]
forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go (\Project
p -> Project
-> Either Integer BlockHash -> Paged -> m [(Address, [TxHash])]
forall (m :: * -> *).
MonadBlockfrost m =>
Project
-> Either Integer BlockHash -> Paged -> m [(Address, [TxHash])]
getBlockAffectedAddresses_ Project
p Either Integer BlockHash
a Paged
pg)

-- | Return list of addresses affected in the specified block with additional information, sorted by the bech32 address, ascending.
getBlockAffectedAddresses :: MonadBlockfrost m => Either Integer BlockHash -> m [(Address, [TxHash])]
getBlockAffectedAddresses :: Either Integer BlockHash -> m [(Address, [TxHash])]
getBlockAffectedAddresses Either Integer BlockHash
a = Either Integer BlockHash -> Paged -> m [(Address, [TxHash])]
forall (m :: * -> *).
MonadBlockfrost m =>
Either Integer BlockHash -> Paged -> m [(Address, [TxHash])]
getBlockAffectedAddresses' Either Integer BlockHash
a Paged
forall a. Default a => a
def