-- | 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 :: forall (m :: * -> *).
MonadBlockfrost m =>
Project -> BlocksAPI (AsClientT m)
blocksClient = forall (routes :: * -> *) mode.
GenericServant routes mode =>
ToServant routes mode -> routes mode
fromServant forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall route.
CardanoAPI route
-> route
   :- ("blocks"
       :> (Tag "Cardano \187 Blocks" :> ToServantApi BlocksAPI))
_blocks forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
MonadBlockfrost m =>
Project -> CardanoAPI (AsClientT m)
cardanoClient

getLatestBlock_ :: MonadBlockfrost m => Project -> m Block
getLatestBlock_ :: forall (m :: * -> *). MonadBlockfrost m => Project -> m Block
getLatestBlock_ = 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall (m :: * -> *). MonadBlockfrost m => m Block
getLatestBlock = forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go forall (m :: * -> *). MonadBlockfrost m => Project -> m Block
getLatestBlock_

getLatestBlockTxs_ :: MonadBlockfrost m => Project -> Paged -> SortOrder -> m [TxHash]
getLatestBlockTxs_ :: forall (m :: * -> *).
MonadBlockfrost m =>
Project -> Paged -> SortOrder -> m [TxHash]
getLatestBlockTxs_ = 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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' :: forall (m :: * -> *).
MonadBlockfrost m =>
Paged -> SortOrder -> m [TxHash]
getLatestBlockTxs' Paged
pg SortOrder
s = forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go (\Project
p -> forall (m :: * -> *).
MonadBlockfrost m =>
Project -> Paged -> SortOrder -> m [TxHash]
getLatestBlockTxs_ Project
p Paged
pg SortOrder
s)

-- | Return the transactions within the latest block.
--
-- Queries 100 entries. To query all entries use 'Blockfrost.Client.Core.allPages'
-- with principled variant of this function (suffixed with @'@)
-- that accepts 'Paged' argument.
getLatestBlockTxs :: MonadBlockfrost m => m [TxHash]
getLatestBlockTxs :: forall (m :: * -> *). MonadBlockfrost m => m [TxHash]
getLatestBlockTxs = forall (m :: * -> *).
MonadBlockfrost m =>
Paged -> SortOrder -> m [TxHash]
getLatestBlockTxs' forall a. Default a => a
def forall a. Default a => a
def

getBlock_ :: MonadBlockfrost m => Project -> Either Integer BlockHash -> m Block
getBlock_ :: forall (m :: * -> *).
MonadBlockfrost m =>
Project -> Either Integer BlockHash -> m Block
getBlock_ = 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall (m :: * -> *).
MonadBlockfrost m =>
Either Integer BlockHash -> m Block
getBlock Either Integer BlockHash
a = forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go (forall (m :: * -> *).
MonadBlockfrost m =>
Project -> Either Integer BlockHash -> m Block
`getBlock_` Either Integer BlockHash
a)

getBlockSlot_ :: MonadBlockfrost m => Project -> Slot -> m Block
getBlockSlot_ :: forall (m :: * -> *).
MonadBlockfrost m =>
Project -> Slot -> m Block
getBlockSlot_ = 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall (m :: * -> *). MonadBlockfrost m => Slot -> m Block
getBlockSlot Slot
i = forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go (forall (m :: * -> *).
MonadBlockfrost m =>
Project -> Slot -> m Block
`getBlockSlot_` Slot
i)

getBlockEpochSlot_ :: MonadBlockfrost m => Project -> Epoch -> Slot -> m Block
getBlockEpochSlot_ :: forall (m :: * -> *).
MonadBlockfrost m =>
Project -> Epoch -> Slot -> m Block
getBlockEpochSlot_ = 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall (m :: * -> *). MonadBlockfrost m => Epoch -> Slot -> m Block
getBlockEpochSlot Epoch
ep Slot
sl = forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go (\Project
p -> 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_ :: forall (m :: * -> *).
MonadBlockfrost m =>
Project -> Either Integer BlockHash -> Paged -> m [Block]
getNextBlocks_ = 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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' :: forall (m :: * -> *).
MonadBlockfrost m =>
Either Integer BlockHash -> Paged -> m [Block]
getNextBlocks' Either Integer BlockHash
a Paged
pg = forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go (\Project
p -> 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.
--
-- Queries 100 entries. To query all entries use 'Blockfrost.Client.Core.allPages'
-- with principled variant of this function (suffixed with @'@)
-- that accepts 'Paged' argument.
getNextBlocks :: MonadBlockfrost m => Either Integer BlockHash -> m [Block]
getNextBlocks :: forall (m :: * -> *).
MonadBlockfrost m =>
Either Integer BlockHash -> m [Block]
getNextBlocks Either Integer BlockHash
a = forall (m :: * -> *).
MonadBlockfrost m =>
Either Integer BlockHash -> Paged -> m [Block]
getNextBlocks' Either Integer BlockHash
a forall a. Default a => a
def

getPreviousBlocks_ :: MonadBlockfrost m => Project -> Either Integer BlockHash -> Paged -> m [Block]
getPreviousBlocks_ :: forall (m :: * -> *).
MonadBlockfrost m =>
Project -> Either Integer BlockHash -> Paged -> m [Block]
getPreviousBlocks_ = 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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' :: forall (m :: * -> *).
MonadBlockfrost m =>
Either Integer BlockHash -> Paged -> m [Block]
getPreviousBlocks' Either Integer BlockHash
a Paged
pg = forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go (\Project
p -> 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.
--
-- Queries 100 entries. To query all entries use 'Blockfrost.Client.Core.allPages'
-- with principled variant of this function (suffixed with @'@)
-- that accepts 'Paged' argument.
getPreviousBlocks :: MonadBlockfrost m => Either Integer BlockHash -> m [Block]
getPreviousBlocks :: forall (m :: * -> *).
MonadBlockfrost m =>
Either Integer BlockHash -> m [Block]
getPreviousBlocks Either Integer BlockHash
a = forall (m :: * -> *).
MonadBlockfrost m =>
Either Integer BlockHash -> Paged -> m [Block]
getPreviousBlocks' Either Integer BlockHash
a forall a. Default a => a
def

getBlockTxs_ :: MonadBlockfrost m => Project -> Either Integer BlockHash -> Paged -> SortOrder -> m [TxHash]
getBlockTxs_ :: forall (m :: * -> *).
MonadBlockfrost m =>
Project
-> Either Integer BlockHash -> Paged -> SortOrder -> m [TxHash]
getBlockTxs_ = 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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' :: forall (m :: * -> *).
MonadBlockfrost m =>
Either Integer BlockHash -> Paged -> SortOrder -> m [TxHash]
getBlockTxs' Either Integer BlockHash
a Paged
pg SortOrder
s = forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go (\Project
p -> 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.
--
-- Queries 100 entries. To query all entries use 'Blockfrost.Client.Core.allPages'
-- with principled variant of this function (suffixed with @'@)
-- that accepts 'Paged' argument.
getBlockTxs :: MonadBlockfrost m => Either Integer BlockHash -> m [TxHash]
getBlockTxs :: forall (m :: * -> *).
MonadBlockfrost m =>
Either Integer BlockHash -> m [TxHash]
getBlockTxs Either Integer BlockHash
a = forall (m :: * -> *).
MonadBlockfrost m =>
Either Integer BlockHash -> Paged -> SortOrder -> m [TxHash]
getBlockTxs' Either Integer BlockHash
a forall a. Default a => a
def forall a. Default a => a
def

getBlockAffectedAddresses_ :: MonadBlockfrost m => Project -> Either Integer BlockHash -> Paged -> m [(Address, [TxHash])]
getBlockAffectedAddresses_ :: forall (m :: * -> *).
MonadBlockfrost m =>
Project
-> Either Integer BlockHash -> Paged -> m [(Address, [TxHash])]
getBlockAffectedAddresses_ = 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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' :: forall (m :: * -> *).
MonadBlockfrost m =>
Either Integer BlockHash -> Paged -> m [(Address, [TxHash])]
getBlockAffectedAddresses' Either Integer BlockHash
a Paged
pg = forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go (\Project
p -> 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.
--
-- Queries 100 entries. To query all entries use 'Blockfrost.Client.Core.allPages'
-- with principled variant of this function (suffixed with @'@)
-- that accepts 'Paged' argument.
getBlockAffectedAddresses :: MonadBlockfrost m => Either Integer BlockHash -> m [(Address, [TxHash])]
getBlockAffectedAddresses :: forall (m :: * -> *).
MonadBlockfrost m =>
Either Integer BlockHash -> m [(Address, [TxHash])]
getBlockAffectedAddresses Either Integer BlockHash
a = forall (m :: * -> *).
MonadBlockfrost m =>
Either Integer BlockHash -> Paged -> m [(Address, [TxHash])]
getBlockAffectedAddresses' Either Integer BlockHash
a forall a. Default a => a
def