Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions src/Blockfrost/Service.purs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ module Cardano.Blockfrost.Service
, runBlockfrostServiceTestM
, submitTx
, utxosAt
, utxosAtWithPageLimit
) where

import Prelude
Expand Down Expand Up @@ -490,21 +491,31 @@ handle404AsMempty = map (fromMaybe mempty) <<< handle404AsNothing
--------------------------------------------------------------------------------

utxosAt :: Address -> BlockfrostServiceM (Either ClientError UtxoMap)
utxosAt address = runExceptT $
ExceptT (utxosAtAddressOnPage 1)
>>= (ExceptT <<< resolveBlockfrostUtxosAtAddress)
where
utxosAtAddressOnPage
:: Int -> BlockfrostServiceM (Either ClientError BlockfrostUtxosAtAddress)
utxosAtAddressOnPage page = runExceptT do
-- Maximum number of results per page supported by Blockfrost:
let maxNumResultsOnPage = 100
utxos <- ExceptT $
blockfrostGetRequest (UtxosAtAddress address page maxNumResultsOnPage)
<#> handle404AsMempty <<< handleBlockfrostResponse
case Array.length (unwrap utxos) < maxNumResultsOnPage of
true -> pure utxos
false -> append utxos <$> ExceptT (utxosAtAddressOnPage $ page + 1)
utxosAt = utxosAtWithPageLimit { maxPages: Nothing }

utxosAtWithPageLimit
:: { maxPages :: Maybe Int }
-> Address
-> BlockfrostServiceM (Either ClientError UtxoMap)
utxosAtWithPageLimit { maxPages } address
| maybe false (_ <= 0) maxPages = pure $ Right Map.empty
| otherwise =
runExceptT do
ExceptT (utxosAtAddressOnPage 1)
>>= (ExceptT <<< resolveBlockfrostUtxosAtAddress)
where
utxosAtAddressOnPage
:: Int -> BlockfrostServiceM (Either ClientError BlockfrostUtxosAtAddress)
utxosAtAddressOnPage page = runExceptT do
-- Maximum number of results per page supported by Blockfrost:
let maxNumResultsOnPage = 100
utxos <- ExceptT $
blockfrostGetRequest (UtxosAtAddress address page maxNumResultsOnPage)
<#> handle404AsMempty <<< handleBlockfrostResponse
let pageLimitReached = maybe false (page >= _) maxPages
case (Array.length (unwrap utxos) < maxNumResultsOnPage) || pageLimitReached of
true -> pure utxos
false -> append utxos <$> ExceptT (utxosAtAddressOnPage $ page + 1)

getUtxoByOref
:: TransactionInput
Expand Down
3 changes: 2 additions & 1 deletion src/BlockfrostProvider.purs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ import Cardano.Blockfrost.Service
, runBlockfrostServiceTestM
, submitTx
, utxosAt
, utxosAtWithPageLimit
) as X
import Cardano.Blockfrost.BlockfrostProtocolParameters
( BlockfrostProtocolParameters(BlockfrostProtocolParameters)
) as X
import Cardano.Blockfrost.Provider (providerForBlockfrostBackend) as X
import Cardano.Blockfrost.Provider (providerForBlockfrostBackend) as X