-- | Address queries

module Blockfrost.Client.Cardano.Addresses
  ( getAddressInfo
  , getAddressDetails
  , getAddressUtxos
  , getAddressUtxos'
  , getAddressUtxosAsset
  , getAddressUtxosAsset'
  , getAddressTransactions
  , getAddressTransactions'
  ) where

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

addressesClient :: MonadBlockfrost m => Project -> AddressesAPI (AsClientT m)
addressesClient :: Project -> AddressesAPI (AsClientT m)
addressesClient = (((Address -> m AddressInfo) :<|> (Address -> m AddressDetails))
 :<|> ((Address -> Paged -> SortOrder -> m [AddressUtxo])
       :<|> ((Address -> AssetId -> Paged -> SortOrder -> m [AddressUtxo])
             :<|> (Address
                   -> Paged
                   -> SortOrder
                   -> Maybe BlockIndex
                   -> Maybe BlockIndex
                   -> m [AddressTransaction]))))
-> AddressesAPI (AsClientT m)
forall (routes :: * -> *) mode.
GenericServant routes mode =>
ToServant routes mode -> routes mode
fromServant ((((Address -> m AddressInfo) :<|> (Address -> m AddressDetails))
  :<|> ((Address -> Paged -> SortOrder -> m [AddressUtxo])
        :<|> ((Address -> AssetId -> Paged -> SortOrder -> m [AddressUtxo])
              :<|> (Address
                    -> Paged
                    -> SortOrder
                    -> Maybe BlockIndex
                    -> Maybe BlockIndex
                    -> m [AddressTransaction]))))
 -> AddressesAPI (AsClientT m))
-> (Project
    -> ((Address -> m AddressInfo) :<|> (Address -> m AddressDetails))
       :<|> ((Address -> Paged -> SortOrder -> m [AddressUtxo])
             :<|> ((Address -> AssetId -> Paged -> SortOrder -> m [AddressUtxo])
                   :<|> (Address
                         -> Paged
                         -> SortOrder
                         -> Maybe BlockIndex
                         -> Maybe BlockIndex
                         -> m [AddressTransaction]))))
-> Project
-> AddressesAPI (AsClientT m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CardanoAPI (AsClientT m)
-> ((Address -> m AddressInfo) :<|> (Address -> m AddressDetails))
   :<|> ((Address -> Paged -> SortOrder -> m [AddressUtxo])
         :<|> ((Address -> AssetId -> Paged -> SortOrder -> m [AddressUtxo])
               :<|> (Address
                     -> Paged
                     -> SortOrder
                     -> Maybe BlockIndex
                     -> Maybe BlockIndex
                     -> m [AddressTransaction])))
forall route.
CardanoAPI route
-> route
   :- ("addresses"
       :> (Tag "Cardano \187 Addresses" :> ToServantApi AddressesAPI))
_addresses (CardanoAPI (AsClientT m)
 -> ((Address -> m AddressInfo) :<|> (Address -> m AddressDetails))
    :<|> ((Address -> Paged -> SortOrder -> m [AddressUtxo])
          :<|> ((Address -> AssetId -> Paged -> SortOrder -> m [AddressUtxo])
                :<|> (Address
                      -> Paged
                      -> SortOrder
                      -> Maybe BlockIndex
                      -> Maybe BlockIndex
                      -> m [AddressTransaction]))))
-> (Project -> CardanoAPI (AsClientT m))
-> Project
-> ((Address -> m AddressInfo) :<|> (Address -> m AddressDetails))
   :<|> ((Address -> Paged -> SortOrder -> m [AddressUtxo])
         :<|> ((Address -> AssetId -> Paged -> SortOrder -> m [AddressUtxo])
               :<|> (Address
                     -> Paged
                     -> SortOrder
                     -> Maybe BlockIndex
                     -> Maybe BlockIndex
                     -> m [AddressTransaction])))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Project -> CardanoAPI (AsClientT m)
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> CardanoAPI (AsClientT m)
cardanoClient

getAddressInfo_ :: MonadBlockfrost m => Project -> Address -> m AddressInfo
getAddressInfo_ :: Project -> Address -> m AddressInfo
getAddressInfo_ = AddressesAPI (AsClientT m) -> Address -> m AddressInfo
forall route.
AddressesAPI route
-> route
   :- (Summary "Specific address"
       :> (Description "Obtain information about a specific address."
           :> (Capture "address" Address :> Get '[JSON] AddressInfo)))
_addressInfo (AddressesAPI (AsClientT m) -> Address -> m AddressInfo)
-> (Project -> AddressesAPI (AsClientT m))
-> Project
-> Address
-> m AddressInfo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Project -> AddressesAPI (AsClientT m)
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> AddressesAPI (AsClientT m)
addressesClient

-- | Obtain information about a specific address.
getAddressInfo :: MonadBlockfrost m => Address -> m AddressInfo
getAddressInfo :: Address -> m AddressInfo
getAddressInfo Address
a = (Project -> m AddressInfo) -> m AddressInfo
forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go (Project -> Address -> m AddressInfo
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> Address -> m AddressInfo
`getAddressInfo_` Address
a)

getAddressDetails_ :: MonadBlockfrost m => Project -> Address -> m AddressDetails
getAddressDetails_ :: Project -> Address -> m AddressDetails
getAddressDetails_ = AddressesAPI (AsClientT m) -> Address -> m AddressDetails
forall route.
AddressesAPI route
-> route
   :- (Summary "Address details"
       :> (Description "Obtain details about an address."
           :> (Capture "address" Address
               :> ("total" :> Get '[JSON] AddressDetails))))
_addressDetails (AddressesAPI (AsClientT m) -> Address -> m AddressDetails)
-> (Project -> AddressesAPI (AsClientT m))
-> Project
-> Address
-> m AddressDetails
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Project -> AddressesAPI (AsClientT m)
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> AddressesAPI (AsClientT m)
addressesClient

-- | Obtain details about an address.
getAddressDetails :: MonadBlockfrost m => Address -> m AddressDetails
getAddressDetails :: Address -> m AddressDetails
getAddressDetails Address
a = (Project -> m AddressDetails) -> m AddressDetails
forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go (Project -> Address -> m AddressDetails
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> Address -> m AddressDetails
`getAddressDetails_` Address
a)

getAddressUtxos_ :: MonadBlockfrost m => Project -> Address -> Paged -> SortOrder -> m [AddressUtxo]
getAddressUtxos_ :: Project -> Address -> Paged -> SortOrder -> m [AddressUtxo]
getAddressUtxos_ = AddressesAPI (AsClientT m)
-> Address -> Paged -> SortOrder -> m [AddressUtxo]
forall route.
AddressesAPI route
-> route
   :- (Summary "Address UTXOs"
       :> (Description "UTXOs of the address."
           :> (Capture "address" Address
               :> ("utxos"
                   :> (Pagination :> (Sorting :> Get '[JSON] [AddressUtxo]))))))
_addressUtxos (AddressesAPI (AsClientT m)
 -> Address -> Paged -> SortOrder -> m [AddressUtxo])
-> (Project -> AddressesAPI (AsClientT m))
-> Project
-> Address
-> Paged
-> SortOrder
-> m [AddressUtxo]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Project -> AddressesAPI (AsClientT m)
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> AddressesAPI (AsClientT m)
addressesClient

-- | UTXOs of the address.
-- Allows custom paging and ordering using @Paged@ and @SortOrder@.
getAddressUtxos' :: MonadBlockfrost m => Address -> Paged -> SortOrder -> m [AddressUtxo]
getAddressUtxos' :: Address -> Paged -> SortOrder -> m [AddressUtxo]
getAddressUtxos' Address
a Paged
pg SortOrder
s = (Project -> m [AddressUtxo]) -> m [AddressUtxo]
forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go (\Project
p -> Project -> Address -> Paged -> SortOrder -> m [AddressUtxo]
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> Address -> Paged -> SortOrder -> m [AddressUtxo]
getAddressUtxos_ Project
p Address
a Paged
pg SortOrder
s)

-- | UTXOs of the address.
getAddressUtxos :: MonadBlockfrost m => Address -> m [AddressUtxo]
getAddressUtxos :: Address -> m [AddressUtxo]
getAddressUtxos Address
a = Address -> Paged -> SortOrder -> m [AddressUtxo]
forall (m :: * -> *).
MonadBlockfrost m =>
Address -> Paged -> SortOrder -> m [AddressUtxo]
getAddressUtxos' Address
a Paged
forall a. Default a => a
def SortOrder
forall a. Default a => a
def

getAddressUtxosAsset_ :: MonadBlockfrost m => Project -> Address -> AssetId -> Paged -> SortOrder -> m [AddressUtxo]
getAddressUtxosAsset_ :: Project
-> Address -> AssetId -> Paged -> SortOrder -> m [AddressUtxo]
getAddressUtxosAsset_ = AddressesAPI (AsClientT m)
-> Address -> AssetId -> Paged -> SortOrder -> m [AddressUtxo]
forall route.
AddressesAPI route
-> route
   :- (Summary "Address UTXOs of a given asset"
       :> (Description "UTXOs of the address."
           :> (Capture "address" Address
               :> ("utxos"
                   :> (Capture "asset" AssetId
                       :> (Pagination :> (Sorting :> Get '[JSON] [AddressUtxo])))))))
_addressUtxosAsset (AddressesAPI (AsClientT m)
 -> Address -> AssetId -> Paged -> SortOrder -> m [AddressUtxo])
-> (Project -> AddressesAPI (AsClientT m))
-> Project
-> Address
-> AssetId
-> Paged
-> SortOrder
-> m [AddressUtxo]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Project -> AddressesAPI (AsClientT m)
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> AddressesAPI (AsClientT m)
addressesClient

-- | UTXOs of the address containing specific asset.
-- Allows custom paging and ordering using @Paged@ and @SortOrder@.
getAddressUtxosAsset' :: MonadBlockfrost m => Address -> AssetId-> Paged -> SortOrder -> m [AddressUtxo]
getAddressUtxosAsset' :: Address -> AssetId -> Paged -> SortOrder -> m [AddressUtxo]
getAddressUtxosAsset' Address
addr AssetId
asset Paged
pg SortOrder
s = (Project -> m [AddressUtxo]) -> m [AddressUtxo]
forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go (\Project
p -> Project
-> Address -> AssetId -> Paged -> SortOrder -> m [AddressUtxo]
forall (m :: * -> *).
MonadBlockfrost m =>
Project
-> Address -> AssetId -> Paged -> SortOrder -> m [AddressUtxo]
getAddressUtxosAsset_ Project
p Address
addr AssetId
asset Paged
pg SortOrder
s)

-- | UTXOs of the address containing specific asset.
getAddressUtxosAsset :: MonadBlockfrost m => Address -> AssetId -> m [AddressUtxo]
getAddressUtxosAsset :: Address -> AssetId -> m [AddressUtxo]
getAddressUtxosAsset Address
addr AssetId
asset = Address -> AssetId -> Paged -> SortOrder -> m [AddressUtxo]
forall (m :: * -> *).
MonadBlockfrost m =>
Address -> AssetId -> Paged -> SortOrder -> m [AddressUtxo]
getAddressUtxosAsset' Address
addr AssetId
asset Paged
forall a. Default a => a
def SortOrder
forall a. Default a => a
def

getAddressTransactions_ ::
     MonadBlockfrost m
  => Project
  -> Address
  -> Paged
  -> SortOrder
  -> Maybe BlockIndex
  -> Maybe BlockIndex
  -> m [AddressTransaction]
getAddressTransactions_ :: Project
-> Address
-> Paged
-> SortOrder
-> Maybe BlockIndex
-> Maybe BlockIndex
-> m [AddressTransaction]
getAddressTransactions_ = AddressesAPI (AsClientT m)
-> Address
-> Paged
-> SortOrder
-> Maybe BlockIndex
-> Maybe BlockIndex
-> m [AddressTransaction]
forall route.
AddressesAPI route
-> route
   :- (Summary "Address transactions"
       :> (Description "Transactions on the address."
           :> (Capture "address" Address
               :> ("transactions"
                   :> (Pagination
                       :> (Sorting
                           :> (QueryParam "from" BlockIndex
                               :> (QueryParam "to" BlockIndex
                                   :> Get '[JSON] [AddressTransaction]))))))))
_addressTransactions (AddressesAPI (AsClientT m)
 -> Address
 -> Paged
 -> SortOrder
 -> Maybe BlockIndex
 -> Maybe BlockIndex
 -> m [AddressTransaction])
-> (Project -> AddressesAPI (AsClientT m))
-> Project
-> Address
-> Paged
-> SortOrder
-> Maybe BlockIndex
-> Maybe BlockIndex
-> m [AddressTransaction]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Project -> AddressesAPI (AsClientT m)
forall (m :: * -> *).
MonadBlockfrost m =>
Project -> AddressesAPI (AsClientT m)
addressesClient

-- | Transactions on the address.
-- Allows custom paging and ordering using @Paged@ and @SortOrder@.
-- Also allows support for limiting block ranges using `from`/`to`
-- @BlockIndex@es.
getAddressTransactions' ::
     MonadBlockfrost m
  => Address
  -> Paged
  -> SortOrder
  -> Maybe BlockIndex
  -> Maybe BlockIndex
  -> m [AddressTransaction]
getAddressTransactions' :: Address
-> Paged
-> SortOrder
-> Maybe BlockIndex
-> Maybe BlockIndex
-> m [AddressTransaction]
getAddressTransactions' Address
a Paged
pg SortOrder
s Maybe BlockIndex
from Maybe BlockIndex
to = (Project -> m [AddressTransaction]) -> m [AddressTransaction]
forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go (\Project
p -> Project
-> Address
-> Paged
-> SortOrder
-> Maybe BlockIndex
-> Maybe BlockIndex
-> m [AddressTransaction]
forall (m :: * -> *).
MonadBlockfrost m =>
Project
-> Address
-> Paged
-> SortOrder
-> Maybe BlockIndex
-> Maybe BlockIndex
-> m [AddressTransaction]
getAddressTransactions_ Project
p Address
a Paged
pg SortOrder
s Maybe BlockIndex
from Maybe BlockIndex
to)

-- | Transactions on the address.
getAddressTransactions :: MonadBlockfrost m => Address -> m [AddressTransaction]
getAddressTransactions :: Address -> m [AddressTransaction]
getAddressTransactions Address
a = Address
-> Paged
-> SortOrder
-> Maybe BlockIndex
-> Maybe BlockIndex
-> m [AddressTransaction]
forall (m :: * -> *).
MonadBlockfrost m =>
Address
-> Paged
-> SortOrder
-> Maybe BlockIndex
-> Maybe BlockIndex
-> m [AddressTransaction]
getAddressTransactions' Address
a Paged
forall a. Default a => a
def SortOrder
forall a. Default a => a
def Maybe BlockIndex
forall a. Maybe a
Nothing Maybe BlockIndex
forall a. Maybe a
Nothing