-- | Utility queries

module Blockfrost.Client.Cardano.Utils
  ( deriveShelleyAddress
  , txEvaluate
  , txEvaluateUTXOs
  ) where

import Data.Text (Text)
import Blockfrost.API
import Blockfrost.Client.Types
import Blockfrost.Types

utilsClient :: MonadBlockfrost m => Project -> UtilsAPI (AsClientT m)
utilsClient :: forall (m :: * -> *).
MonadBlockfrost m =>
Project -> UtilsAPI (AsClientT m)
utilsClient = 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
   :- ("utils"
       :> (Tag "Cardano \187 Utilities" :> ToServantApi UtilsAPI))
_utils forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
MonadBlockfrost m =>
Project -> CardanoAPI (AsClientT m)
cardanoClient

-- | Derive Shelley address from xpub key
deriveShelleyAddress_
  :: MonadBlockfrost m
  => Project
  -> Text -- ^ Hexadecimal xpub
  -> Integer -- ^ Account role
  -> Integer -- ^ Address index
  -> m DerivedAddress
deriveShelleyAddress_ :: forall (m :: * -> *).
MonadBlockfrost m =>
Project -> Text -> Integer -> Integer -> m DerivedAddress
deriveShelleyAddress_ = forall route.
UtilsAPI route
-> route
   :- (Summary "Derive an address"
       :> (Description "Derive Shelley address from an xpub."
           :> ("addresses"
               :> ("xpub"
                   :> (Capture "xpub" Text
                       :> (Capture "role" Integer
                           :> (Capture "Index" Integer :> Get '[JSON] DerivedAddress)))))))
_deriveAddr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
MonadBlockfrost m =>
Project -> UtilsAPI (AsClientT m)
utilsClient

-- | Derive Shelley address from xpub key
deriveShelleyAddress
  :: MonadBlockfrost m
  => Text -- ^ Hexadecimal xpub
  -> Integer -- ^ Account role
  -> Integer -- ^ Address index
  -> m DerivedAddress
deriveShelleyAddress :: forall (m :: * -> *).
MonadBlockfrost m =>
Text -> Integer -> Integer -> m DerivedAddress
deriveShelleyAddress Text
xpub Integer
role Integer
index = forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go (\Project
p -> forall (m :: * -> *).
MonadBlockfrost m =>
Project -> Text -> Integer -> Integer -> m DerivedAddress
deriveShelleyAddress_ Project
p Text
xpub Integer
role Integer
index)

txEvaluate_
  :: MonadBlockfrost m
  => Project
  -> CBORString
  -> m TxEval
txEvaluate_ :: forall (m :: * -> *).
MonadBlockfrost m =>
Project -> CBORString -> m TxEval
txEvaluate_ = forall route.
UtilsAPI route
-> route
   :- (Summary "Submit a transaction for execution units evaluation"
       :> (Description
             "Submit an already serialized transaction to evaluate how much execution units it requires."
           :> ("txs"
               :> ("evaluate"
                   :> (ReqBody '[CBOR] CBORString :> Post '[JSON] TxEval)))))
_txEvaluate forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
MonadBlockfrost m =>
Project -> UtilsAPI (AsClientT m)
utilsClient

-- | Submit a transaction for execution units evaluation
txEvaluate
  :: MonadBlockfrost m
  => CBORString
  -> m TxEval
txEvaluate :: forall (m :: * -> *). MonadBlockfrost m => CBORString -> m TxEval
txEvaluate CBORString
txCbor = forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go (forall (m :: * -> *).
MonadBlockfrost m =>
Project -> CBORString -> m TxEval
`txEvaluate_` CBORString
txCbor)

txEvaluateUTXOs_
  :: MonadBlockfrost m
  => Project
  -> TxEvalInput
  -> m TxEval
txEvaluateUTXOs_ :: forall (m :: * -> *).
MonadBlockfrost m =>
Project -> TxEvalInput -> m TxEval
txEvaluateUTXOs_ = forall route.
UtilsAPI route
-> route
   :- (Summary
         "Submit a transaction for execution units evaluation (additional UTXO set)"
       :> (Description
             "Submit a JSON payload with transaction CBOR and additional UTXO set to evaluate how much execution units it requires."
           :> ("txs"
               :> ("evaluate"
                   :> ("utxos"
                       :> (ReqBody '[JSON] TxEvalInput :> Post '[JSON] TxEval))))))
_txEvaluateUTXOs forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
MonadBlockfrost m =>
Project -> UtilsAPI (AsClientT m)
utilsClient

-- | Submit a transaction for execution units evaluation (additional UTXO set)
txEvaluateUTXOs
  :: MonadBlockfrost m
  => TxEvalInput
  -> m TxEval
txEvaluateUTXOs :: forall (m :: * -> *). MonadBlockfrost m => TxEvalInput -> m TxEval
txEvaluateUTXOs TxEvalInput
txei = forall (m :: * -> *) a.
MonadBlockfrost m =>
(Project -> m a) -> m a
go (forall (m :: * -> *).
MonadBlockfrost m =>
Project -> TxEvalInput -> m TxEval
`txEvaluateUTXOs_` TxEvalInput
txei)