{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_HADDOCK hide #-}
module Cometa.Provider.Blockfrost.Internal.Provider (providerWithTransport) where
import qualified Cometa.Address as Address
import qualified Cometa.Address.Reward as Reward
import Cometa.AssetId (AssetId (..))
import qualified Cometa.Credential as Credential
import qualified Cometa.Crypto.Blake2b as Blake2b
import Cometa.Error (Error (..))
import qualified Cometa.Evaluation as Evaluation
import qualified Cometa.Internal.Hex as Hex
import qualified Cometa.PlutusData as Data
import qualified Cometa.Provider as Provider
import Cometa.Provider.Blockfrost.Internal.Config (Config)
import qualified Cometa.Provider.Blockfrost.Internal.Config as Config
import qualified Cometa.Provider.Blockfrost.Internal.Http as Http
import qualified Cometa.Provider.Blockfrost.Internal.Json as Json
import qualified Cometa.Provider.Blockfrost.Internal.Protocol as Protocol
import qualified Cometa.Script as Script
import qualified Cometa.Script.Plutus as Plutus
import qualified Cometa.Transaction as Tx
import qualified Cometa.Transaction.Datum as Datum
import qualified Cometa.Transaction.Input as Input
import qualified Cometa.Transaction.Output as Output
import qualified Cometa.Transaction.Utxo as Utxo
import qualified Cometa.TransactionId as TxId
import qualified Cometa.Value as Amount
import Control.Concurrent (threadDelay)
import Control.Monad (foldM, unless, when)
import Control.Monad.Trans.Except (ExceptT (..), except, runExceptT)
import Data.Aeson (Value (..), encode, (.:))
import Data.Aeson.Types (Parser, withObject, withText)
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as Lazy
import qualified Data.Map.Strict as Map
import Data.Maybe (mapMaybe)
import qualified Data.Set as Set
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Word (Word64)
import GHC.Clock (getMonotonicTimeNSec)
providerWithTransport :: Config -> Http.Transport -> Provider.Provider
providerWithTransport :: Config -> Transport -> Provider
providerWithTransport Config
config Transport
transport =
(Text -> Word32 -> Provider
Provider.unavailable (Text
"Blockfrost " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack (Network -> String
forall a. Show a => a -> String
show (Config -> Network
Config.network Config
config))) (Network -> Word32
Config.networkMagic (Config -> Network
Config.network Config
config)))
{ Provider.protocolParameters = guarded transport "blockfrost.protocolParameters" $ runExceptT $ do
value <- requiredJSON transport "blockfrost.protocolParameters" ["epochs", "latest", "parameters"] []
except (Protocol.decodeProtocolParameters value),
Provider.utxosAt = \Address
address -> Transport
-> Text -> IO (Either Error [Utxo]) -> IO (Either Error [Utxo])
forall a.
Transport -> Text -> IO (Either Error a) -> IO (Either Error a)
guarded Transport
transport Text
"blockfrost.utxosAt" (Transport
-> Text -> Address -> Maybe AssetId -> IO (Either Error [Utxo])
atAddress Transport
transport Text
"blockfrost.utxosAt" Address
address Maybe AssetId
forall a. Maybe a
Nothing),
Provider.utxosWithAsset = \Address
address AssetId
asset -> Transport
-> Text -> IO (Either Error [Utxo]) -> IO (Either Error [Utxo])
forall a.
Transport -> Text -> IO (Either Error a) -> IO (Either Error a)
guarded Transport
transport Text
"blockfrost.utxosWithAsset" (Transport
-> Text -> Address -> Maybe AssetId -> IO (Either Error [Utxo])
atAddress Transport
transport Text
"blockfrost.utxosWithAsset" Address
address (AssetId -> Maybe AssetId
forall a. a -> Maybe a
Just AssetId
asset)),
Provider.utxoWithAsset = guarded transport "blockfrost.utxoWithAsset" . uniqueAsset transport,
Provider.resolveInputs = guarded transport "blockfrost.resolveInputs" . resolve transport,
Provider.resolveDatum = guarded transport "blockfrost.resolveDatum" . datum transport,
Provider.rewardBalance = guarded transport "blockfrost.rewardBalance" . rewards transport,
Provider.submitTransaction = guarded transport "blockfrost.submitTransaction" . submit transport,
Provider.evaluateTransaction = \Transaction
transaction [Utxo]
inputs -> Transport
-> Text -> IO (Either Error Budgets) -> IO (Either Error Budgets)
forall a.
Transport -> Text -> IO (Either Error a) -> IO (Either Error a)
guarded Transport
transport Text
"blockfrost.evaluateTransaction" (Transport -> Transaction -> [Utxo] -> IO (Either Error Budgets)
evaluate Transport
transport Transaction
transaction [Utxo]
inputs),
Provider.awaitTransaction = \TransactionId
identifier Word64
timeout -> Transport
-> Text -> IO (Either Error Bool) -> IO (Either Error Bool)
forall a.
Transport -> Text -> IO (Either Error a) -> IO (Either Error a)
guarded Transport
transport Text
"blockfrost.awaitTransaction" (Config
-> Transport -> TransactionId -> Word64 -> IO (Either Error Bool)
await Config
config Transport
transport TransactionId
identifier Word64
timeout)
}
guarded :: Http.Transport -> Text -> IO (Either Error a) -> IO (Either Error a)
guarded :: forall a.
Transport -> Text -> IO (Either Error a) -> IO (Either Error a)
guarded Transport
transport Text
operation IO (Either Error a)
action = do
Either HttpFailure ()
current <- Transport -> IO (Either HttpFailure ())
Http.checkOpen Transport
transport
case Either HttpFailure ()
current of
Left HttpFailure
failure -> Either Error a -> IO (Either Error a)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error a
forall a b. a -> Either a b
Left (Text -> HttpFailure -> Error
transportError Text
operation HttpFailure
failure))
Right () -> IO (Either Error a)
action
transportError :: Text -> Http.HttpFailure -> Error
transportError :: Text -> HttpFailure -> Error
transportError Text
operation HttpFailure
failure = case HttpFailure
failure of
HttpFailure
Http.Closed -> Error
RuntimeClosed
HttpFailure
Http.Timeout -> Text -> Text -> Error
ProviderTransportError Text
operation Text
"timeout"
HttpFailure
Http.ConnectionFailure -> Text -> Text -> Error
ProviderTransportError Text
operation Text
"connection failure"
HttpFailure
Http.ResponseTooLarge -> Text -> Text -> Error
ProviderTransportError Text
operation Text
"response too large"
getRequest :: [Text] -> [(Text, Text)] -> Http.Request
getRequest :: [Text] -> [(Text, Text)] -> Request
getRequest [Text]
path [(Text, Text)]
query = Method
-> [Text]
-> [(Text, Text)]
-> ByteString
-> ByteString
-> Maybe Int
-> Request
Http.Request Method
Http.Get [Text]
path [(Text, Text)]
query ByteString
"" ByteString
"" Maybe Int
forall a. Maybe a
Nothing
send :: Http.Transport -> Text -> Http.Request -> IO (Either Error Http.Response)
send :: Transport -> Text -> Request -> IO (Either Error Response)
send Transport
transport Text
operation Request
request = (HttpFailure -> Either Error Response)
-> (Response -> Either Error Response)
-> Either HttpFailure Response
-> Either Error Response
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Error -> Either Error Response
forall a b. a -> Either a b
Left (Error -> Either Error Response)
-> (HttpFailure -> Error) -> HttpFailure -> Either Error Response
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> HttpFailure -> Error
transportError Text
operation) Response -> Either Error Response
forall a b. b -> Either a b
Right (Either HttpFailure Response -> Either Error Response)
-> IO (Either HttpFailure Response) -> IO (Either Error Response)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Transport -> Request -> IO (Either HttpFailure Response)
Http.send Transport
transport Request
request
getJSON :: Http.Transport -> Text -> [Text] -> [(Text, Text)] -> Bool -> IO (Either Error (Maybe Value))
getJSON :: Transport
-> Text
-> [Text]
-> [(Text, Text)]
-> Bool
-> IO (Either Error (Maybe Value))
getJSON Transport
transport Text
operation [Text]
path [(Text, Text)]
query Bool
missing = do
Either Error Response
result <- Transport -> Text -> Request -> IO (Either Error Response)
send Transport
transport Text
operation ([Text] -> [(Text, Text)] -> Request
getRequest [Text]
path [(Text, Text)]
query)
Either Error (Maybe Value) -> IO (Either Error (Maybe Value))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error (Maybe Value) -> IO (Either Error (Maybe Value)))
-> Either Error (Maybe Value) -> IO (Either Error (Maybe Value))
forall a b. (a -> b) -> a -> b
$ do
Response
response <- Either Error Response
result
case Response -> Int
Http.status Response
response of
Int
200 -> Value -> Maybe Value
forall a. a -> Maybe a
Just (Value -> Maybe Value)
-> Either Error Value -> Either Error (Maybe Value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> ByteString -> Either Error Value
Json.decodeValue Text
operation (Response -> ByteString
Http.bodyBytes Response
response)
Int
404 | Bool
missing -> Maybe Value -> Either Error (Maybe Value)
forall a b. b -> Either a b
Right Maybe Value
forall a. Maybe a
Nothing
Int
code -> Error -> Either Error (Maybe Value)
forall a b. a -> Either a b
Left (Text -> Int -> Error
ProviderHttpError Text
operation Int
code)
requiredJSON :: Http.Transport -> Text -> [Text] -> [(Text, Text)] -> ExceptT Error IO Value
requiredJSON :: Transport
-> Text -> [Text] -> [(Text, Text)] -> ExceptT Error IO Value
requiredJSON Transport
transport Text
operation [Text]
path [(Text, Text)]
query = do
Maybe Value
value <- IO (Either Error (Maybe Value)) -> ExceptT Error IO (Maybe Value)
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (Transport
-> Text
-> [Text]
-> [(Text, Text)]
-> Bool
-> IO (Either Error (Maybe Value))
getJSON Transport
transport Text
operation [Text]
path [(Text, Text)]
query Bool
False)
ExceptT Error IO Value
-> (Value -> ExceptT Error IO Value)
-> Maybe Value
-> ExceptT Error IO Value
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Either Error Value -> ExceptT Error IO Value
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Error -> Either Error Value
forall a b. a -> Either a b
Left (Text -> Error
InvalidProviderResponse Text
operation))) Value -> ExceptT Error IO Value
forall a. a -> ExceptT Error IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Value
value
pages :: Http.Transport -> Text -> [Text] -> IO (Either Error [Json.Row])
pages :: Transport -> Text -> [Text] -> IO (Either Error [Row])
pages Transport
transport Text
operation [Text]
path = ExceptT Error IO [Row] -> IO (Either Error [Row])
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (Int -> Set Input -> [[Row]] -> ExceptT Error IO [Row]
go Int
1 Set Input
forall a. Set a
Set.empty [])
where
go :: Int -> Set Input -> [[Row]] -> ExceptT Error IO [Row]
go Int
page Set Input
seen [[Row]]
chunks = do
Bool -> ExceptT Error IO () -> ExceptT Error IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
page Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10000) (Either Error () -> ExceptT Error IO ()
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
InvalidProviderResponse Text
operation)))
Maybe Value
response <- IO (Either Error (Maybe Value)) -> ExceptT Error IO (Maybe Value)
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (Transport
-> Text
-> [Text]
-> [(Text, Text)]
-> Bool
-> IO (Either Error (Maybe Value))
getJSON Transport
transport Text
operation [Text]
path [(Text
"count", Text
"100"), (Text
"page", String -> Text
Text.pack (Int -> String
forall a. Show a => a -> String
show Int
page)), (Text
"order", Text
"asc")] (Int
page Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== (Int
1 :: Int)))
[Row]
current <- ExceptT Error IO [Row]
-> (Value -> ExceptT Error IO [Row])
-> Maybe Value
-> ExceptT Error IO [Row]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([Row] -> ExceptT Error IO [Row]
forall a. a -> ExceptT Error IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []) (Either Error [Row] -> ExceptT Error IO [Row]
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Either Error [Row] -> ExceptT Error IO [Row])
-> (Value -> Either Error [Row]) -> Value -> ExceptT Error IO [Row]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> (Value -> Parser [Row]) -> Value -> Either Error [Row]
forall a. Text -> (Value -> Parser a) -> Value -> Either Error a
Json.parseValue Text
operation Value -> Parser [Row]
Json.rows) Maybe Value
response
let identities :: [Input]
identities = (Row -> Input) -> [Row] -> [Input]
forall a b. (a -> b) -> [a] -> [b]
map Row -> Input
Json.rowInput [Row]
current
Bool -> ExceptT Error IO () -> ExceptT Error IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([Row] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Row]
current Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
100 Bool -> Bool -> Bool
&& Set Input -> Int
forall a. Set a -> Int
Set.size ([Input] -> Set Input
forall a. Ord a => [a] -> Set a
Set.fromList [Input]
identities) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== [Input] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Input]
identities Bool -> Bool -> Bool
&& (Input -> Bool) -> [Input] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Input -> Set Input -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.notMember` Set Input
seen) [Input]
identities) (Either Error () -> ExceptT Error IO ()
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
InvalidProviderResponse Text
operation)))
let completed :: [[Row]]
completed = [Row]
current [Row] -> [[Row]] -> [[Row]]
forall a. a -> [a] -> [a]
: [[Row]]
chunks
if [Row] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Row]
current Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
100
then [Row] -> ExceptT Error IO [Row]
forall a. a -> ExceptT Error IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([[Row]] -> [Row]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Row]] -> [[Row]]
forall a. [a] -> [a]
reverse [[Row]]
completed))
else Int -> Set Input -> [[Row]] -> ExceptT Error IO [Row]
go (Int
page Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Set Input -> Set Input -> Set Input
forall a. Ord a => Set a -> Set a -> Set a
Set.union Set Input
seen ([Input] -> Set Input
forall a. Ord a => [a] -> Set a
Set.fromList [Input]
identities)) [[Row]]
completed
atAddress :: Http.Transport -> Text -> Address.Address -> Maybe AssetId -> IO (Either Error [Utxo.Utxo])
atAddress :: Transport
-> Text -> Address -> Maybe AssetId -> IO (Either Error [Utxo])
atAddress Transport
transport Text
operation Address
address Maybe AssetId
asset = ExceptT Error IO [Utxo] -> IO (Either Error [Utxo])
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT Error IO [Utxo] -> IO (Either Error [Utxo]))
-> ExceptT Error IO [Utxo] -> IO (Either Error [Utxo])
forall a b. (a -> b) -> a -> b
$ do
let suffix :: [Text]
suffix = case Maybe AssetId
asset of Just value :: AssetId
value@Native {} -> [AssetId -> Text
Json.assetText AssetId
value]; Maybe AssetId
_ -> []
[Row]
found <- IO (Either Error [Row]) -> ExceptT Error IO [Row]
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (Transport -> Text -> [Text] -> IO (Either Error [Row])
pages Transport
transport Text
operation ([Text
"addresses", Address -> Text
Address.toText Address
address, Text
"utxos"] [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> [Text]
suffix))
Bool -> ExceptT Error IO () -> ExceptT Error IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ((Row -> Bool) -> [Row] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ((Address -> Address -> Bool
forall a. Eq a => a -> a -> Bool
== Address
address) (Address -> Bool) -> (Row -> Address) -> Row -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Row -> Address
Json.rowAddress) [Row]
found) (Either Error () -> ExceptT Error IO ()
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
InvalidProviderResponse Text
operation)))
[Utxo]
values <- IO (Either Error [Utxo]) -> ExceptT Error IO [Utxo]
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (Transport -> Text -> [Row] -> IO (Either Error [Utxo])
materialize Transport
transport Text
operation [Row]
found)
[Utxo] -> ExceptT Error IO [Utxo]
forall a. a -> ExceptT Error IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Utxo] -> ExceptT Error IO [Utxo])
-> [Utxo] -> ExceptT Error IO [Utxo]
forall a b. (a -> b) -> a -> b
$ case Maybe AssetId
asset of Maybe AssetId
Nothing -> [Utxo]
values; Just AssetId
value -> (Utxo -> Bool) -> [Utxo] -> [Utxo]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
0) (Integer -> Bool) -> (Utxo -> Integer) -> Utxo -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AssetId -> Value -> Integer
Amount.quantity AssetId
value (Value -> Integer) -> (Utxo -> Value) -> Utxo -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Output -> Value
Output.value (Output -> Value) -> (Utxo -> Output) -> Utxo -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Utxo -> Output
Utxo.output) [Utxo]
values
uniqueAsset :: Http.Transport -> AssetId -> IO (Either Error (Maybe Utxo.Utxo))
uniqueAsset :: Transport -> AssetId -> IO (Either Error (Maybe Utxo))
uniqueAsset Transport
_ AssetId
Ada = Either Error (Maybe Utxo) -> IO (Either Error (Maybe Utxo))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error (Maybe Utxo)
forall a b. a -> Either a b
Left (Text -> Error
UnsupportedOperation Text
"NFT lookup requires a native asset"))
uniqueAsset Transport
transport AssetId
asset = ExceptT Error IO (Maybe Utxo) -> IO (Either Error (Maybe Utxo))
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT Error IO (Maybe Utxo) -> IO (Either Error (Maybe Utxo)))
-> ExceptT Error IO (Maybe Utxo) -> IO (Either Error (Maybe Utxo))
forall a b. (a -> b) -> a -> b
$ do
[Row]
found <- IO (Either Error [Row]) -> ExceptT Error IO [Row]
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (Transport -> Text -> [Text] -> IO (Either Error [Row])
pages Transport
transport Text
operation [Text
"assets", AssetId -> Text
Json.assetText AssetId
asset, Text
"utxos"])
[Utxo]
values <- IO (Either Error [Utxo]) -> ExceptT Error IO [Utxo]
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (Transport -> Text -> [Row] -> IO (Either Error [Utxo])
materialize Transport
transport Text
operation [Row]
found)
case [Utxo]
values of
[] -> Maybe Utxo -> ExceptT Error IO (Maybe Utxo)
forall a. a -> ExceptT Error IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Utxo
forall a. Maybe a
Nothing
[Utxo
value] | AssetId -> Value -> Integer
Amount.quantity AssetId
asset (Output -> Value
Output.value (Utxo -> Output
Utxo.output Utxo
value)) Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
1 -> Maybe Utxo -> ExceptT Error IO (Maybe Utxo)
forall a. a -> ExceptT Error IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Utxo -> Maybe Utxo
forall a. a -> Maybe a
Just Utxo
value)
[Utxo]
_ -> Either Error (Maybe Utxo) -> ExceptT Error IO (Maybe Utxo)
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Error -> Either Error (Maybe Utxo)
forall a b. a -> Either a b
Left (Text -> Error
InvalidProviderResponse Text
operation))
where
operation :: Text
operation = Text
"blockfrost.utxoWithAsset"
materialize :: Http.Transport -> Text -> [Json.Row] -> IO (Either Error [Utxo.Utxo])
materialize :: Transport -> Text -> [Row] -> IO (Either Error [Utxo])
materialize Transport
transport Text
operation [Row]
found = ExceptT Error IO [Utxo] -> IO (Either Error [Utxo])
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT Error IO [Utxo] -> IO (Either Error [Utxo]))
-> ExceptT Error IO [Utxo] -> IO (Either Error [Utxo])
forall a b. (a -> b) -> a -> b
$ [Utxo] -> [Utxo]
forall a. [a] -> [a]
reverse ([Utxo] -> [Utxo])
-> ((Map ScriptHash Script, [Utxo]) -> [Utxo])
-> (Map ScriptHash Script, [Utxo])
-> [Utxo]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Map ScriptHash Script, [Utxo]) -> [Utxo]
forall a b. (a, b) -> b
snd ((Map ScriptHash Script, [Utxo]) -> [Utxo])
-> ExceptT Error IO (Map ScriptHash Script, [Utxo])
-> ExceptT Error IO [Utxo]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Map ScriptHash Script, [Utxo])
-> Row -> ExceptT Error IO (Map ScriptHash Script, [Utxo]))
-> (Map ScriptHash Script, [Utxo])
-> [Row]
-> ExceptT Error IO (Map ScriptHash Script, [Utxo])
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM (Map ScriptHash Script, [Utxo])
-> Row -> ExceptT Error IO (Map ScriptHash Script, [Utxo])
step (Map ScriptHash Script
forall k a. Map k a
Map.empty, []) [Row]
found
where
step :: (Map ScriptHash Script, [Utxo])
-> Row -> ExceptT Error IO (Map ScriptHash Script, [Utxo])
step (Map ScriptHash Script
scripts, [Utxo]
values) Row
row = do
case (Row -> Datum
Json.rowDatum Row
row, Row -> Maybe Hash32
Json.rowDatumHash Row
row) of
(Datum.InlineDatum EncodedPlutusData
encoded, Just Hash32
expected) -> Text -> Hash32 -> EncodedPlutusData -> ExceptT Error IO ()
verifyDatum Text
operation Hash32
expected EncodedPlutusData
encoded
(Datum, Maybe Hash32)
_ -> () -> ExceptT Error IO ()
forall a. a -> ExceptT Error IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
(Map ScriptHash Script
updated, Maybe Script
script) <- case Row -> Maybe ScriptHash
Json.rowScriptHash Row
row of
Maybe ScriptHash
Nothing -> (Map ScriptHash Script, Maybe Script)
-> ExceptT Error IO (Map ScriptHash Script, Maybe Script)
forall a. a -> ExceptT Error IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Map ScriptHash Script
scripts, Maybe Script
forall a. Maybe a
Nothing)
Just ScriptHash
hash -> case ScriptHash -> Map ScriptHash Script -> Maybe Script
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup ScriptHash
hash Map ScriptHash Script
scripts of
Just Script
known -> (Map ScriptHash Script, Maybe Script)
-> ExceptT Error IO (Map ScriptHash Script, Maybe Script)
forall a. a -> ExceptT Error IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Map ScriptHash Script
scripts, Script -> Maybe Script
forall a. a -> Maybe a
Just Script
known)
Maybe Script
Nothing -> do
Script
fetched <- Transport -> Text -> ScriptHash -> ExceptT Error IO Script
fetchScript Transport
transport Text
operation ScriptHash
hash
(Map ScriptHash Script, Maybe Script)
-> ExceptT Error IO (Map ScriptHash Script, Maybe Script)
forall a. a -> ExceptT Error IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ScriptHash
-> Script -> Map ScriptHash Script -> Map ScriptHash Script
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert ScriptHash
hash Script
fetched Map ScriptHash Script
scripts, Script -> Maybe Script
forall a. a -> Maybe a
Just Script
fetched)
Output
output <- Either Error Output -> ExceptT Error IO Output
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Text -> Either Error Output -> Either Error Output
forall a. Text -> Either Error a -> Either Error a
mapResponse Text
operation (Address -> Value -> Datum -> Maybe Script -> Either Error Output
Output.mkOutput (Row -> Address
Json.rowAddress Row
row) (Row -> Value
Json.rowValue Row
row) (Row -> Datum
Json.rowDatum Row
row) Maybe Script
script))
(Map ScriptHash Script, [Utxo])
-> ExceptT Error IO (Map ScriptHash Script, [Utxo])
forall a. a -> ExceptT Error IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Map ScriptHash Script
updated, Input -> Output -> Utxo
Utxo.utxo (Row -> Input
Json.rowInput Row
row) Output
output Utxo -> [Utxo] -> [Utxo]
forall a. a -> [a] -> [a]
: [Utxo]
values)
mapResponse :: Text -> Either Error a -> Either Error a
mapResponse :: forall a. Text -> Either Error a -> Either Error a
mapResponse Text
operation = (Error -> Either Error a)
-> (a -> Either Error a) -> Either Error a -> Either Error a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either Error a -> Error -> Either Error a
forall a b. a -> b -> a
const (Error -> Either Error a
forall a b. a -> Either a b
Left (Text -> Error
InvalidProviderResponse Text
operation))) a -> Either Error a
forall a b. b -> Either a b
Right
fetchScript :: Http.Transport -> Text -> Credential.ScriptHash -> ExceptT Error IO Script.Script
fetchScript :: Transport -> Text -> ScriptHash -> ExceptT Error IO Script
fetchScript Transport
transport Text
operation ScriptHash
expected = do
let path :: [Text]
path = [Text
"scripts", ByteString -> Text
Hex.encodeHex (ScriptHash -> ByteString
Credential.scriptHashBytes ScriptHash
expected)]
Value
metadata <- Transport
-> Text -> [Text] -> [(Text, Text)] -> ExceptT Error IO Value
requiredJSON Transport
transport Text
operation [Text]
path []
(ByteString
hash, Text
kind) <- Either Error (ByteString, Text)
-> ExceptT Error IO (ByteString, Text)
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Text
-> (Value -> Parser (ByteString, Text))
-> Value
-> Either Error (ByteString, Text)
forall a. Text -> (Value -> Parser a) -> Value -> Either Error a
Json.parseValue Text
operation (String
-> (Object -> Parser (ByteString, Text))
-> Value
-> Parser (ByteString, Text)
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"script metadata" (\Object
entry -> (,) (ByteString -> Text -> (ByteString, Text))
-> Parser ByteString -> Parser (Text -> (ByteString, Text))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Object
entry Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"script_hash" Parser Text -> (Text -> Parser ByteString) -> Parser ByteString
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Parser ByteString
Json.parseHex) Parser (Text -> (ByteString, Text))
-> Parser Text -> Parser (ByteString, Text)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
entry Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"type")) Value
metadata)
Bool -> ExceptT Error IO () -> ExceptT Error IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ByteString
hash ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ScriptHash -> ByteString
Credential.scriptHashBytes ScriptHash
expected) (Either Error () -> ExceptT Error IO ()
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
InvalidProviderResponse Text
operation)))
Script
script <- case Text
kind :: Text of
Text
"timelock" -> do
Value
payload <- Transport
-> Text -> [Text] -> [(Text, Text)] -> ExceptT Error IO Value
requiredJSON Transport
transport Text
operation ([Text]
path [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> [Text
"json"]) []
NativeScript -> Script
Script.Native (NativeScript -> Script)
-> ExceptT Error IO NativeScript -> ExceptT Error IO Script
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Either Error NativeScript -> ExceptT Error IO NativeScript
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Text
-> (Value -> Parser NativeScript)
-> Value
-> Either Error NativeScript
forall a. Text -> (Value -> Parser a) -> Value -> Either Error a
Json.parseValue Text
operation (String
-> (Object -> Parser NativeScript) -> Value -> Parser NativeScript
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"script JSON" (\Object
entry -> Object
entry Object -> Key -> Parser Value
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"json" Parser Value
-> (Value -> Parser NativeScript) -> Parser NativeScript
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Value -> Parser NativeScript
Json.nativeScript)) Value
payload)
Text
_ -> do
Version
version <- Either Error Version -> ExceptT Error IO Version
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Either Error Version -> ExceptT Error IO Version)
-> Either Error Version -> ExceptT Error IO Version
forall a b. (a -> b) -> a -> b
$ case Text
kind of
Text
"plutusV1" -> Version -> Either Error Version
forall a b. b -> Either a b
Right Version
Plutus.V1
Text
"plutusV2" -> Version -> Either Error Version
forall a b. b -> Either a b
Right Version
Plutus.V2
Text
"plutusV3" -> Version -> Either Error Version
forall a b. b -> Either a b
Right Version
Plutus.V3
Text
_ -> Error -> Either Error Version
forall a b. a -> Either a b
Left (Text -> Error
UnsupportedOperation Text
"Blockfrost returned an unsupported reference script language")
Value
payload <- Transport
-> Text -> [Text] -> [(Text, Text)] -> ExceptT Error IO Value
requiredJSON Transport
transport Text
operation ([Text]
path [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> [Text
"cbor"]) []
ByteString
bytes <- Either Error ByteString -> ExceptT Error IO ByteString
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Text
-> (Value -> Parser ByteString) -> Value -> Either Error ByteString
forall a. Text -> (Value -> Parser a) -> Value -> Either Error a
Json.parseValue Text
operation Value -> Parser ByteString
cborField Value
payload)
PlutusScript -> Script
Script.Plutus (PlutusScript -> Script)
-> ExceptT Error IO PlutusScript -> ExceptT Error IO Script
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Either Error PlutusScript -> ExceptT Error IO PlutusScript
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Text -> Either Error PlutusScript -> Either Error PlutusScript
forall a. Text -> Either Error a -> Either Error a
mapResponse Text
operation (Version -> ByteString -> Either Error PlutusScript
Plutus.fromBytes Version
version ByteString
bytes))
ScriptHash
actual <- IO (Either Error ScriptHash) -> ExceptT Error IO ScriptHash
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (Script -> IO (Either Error ScriptHash)
Script.hash Script
script)
Bool -> ExceptT Error IO () -> ExceptT Error IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ScriptHash
actual ScriptHash -> ScriptHash -> Bool
forall a. Eq a => a -> a -> Bool
== ScriptHash
expected) (Either Error () -> ExceptT Error IO ()
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
InvalidProviderResponse Text
operation)))
Script -> ExceptT Error IO Script
forall a. a -> ExceptT Error IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Script
script
cborField :: Value -> Parser ByteString
cborField :: Value -> Parser ByteString
cborField = String
-> (Object -> Parser ByteString) -> Value -> Parser ByteString
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"CBOR response" (\Object
entry -> Object
entry Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"cbor" Parser Text -> (Text -> Parser ByteString) -> Parser ByteString
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Parser ByteString
Json.parseHex)
verifyDatum :: Text -> Datum.Hash32 -> Data.EncodedPlutusData -> ExceptT Error IO ()
verifyDatum :: Text -> Hash32 -> EncodedPlutusData -> ExceptT Error IO ()
verifyDatum Text
operation Hash32
expected EncodedPlutusData
encoded = do
Blake2bHash
actual <- IO (Either Error Blake2bHash) -> ExceptT Error IO Blake2bHash
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (EncodedPlutusData -> IO (Either Error Blake2bHash)
Data.hash EncodedPlutusData
encoded)
Bool -> ExceptT Error IO () -> ExceptT Error IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Blake2bHash -> ByteString
Blake2b.toBytes Blake2bHash
actual ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== Hash32 -> ByteString
Datum.hashBytes Hash32
expected) (Either Error () -> ExceptT Error IO ()
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
InvalidProviderResponse Text
operation)))
datum :: Http.Transport -> Datum.Hash32 -> IO (Either Error (Maybe Data.EncodedPlutusData))
datum :: Transport -> Hash32 -> IO (Either Error (Maybe EncodedPlutusData))
datum Transport
transport Hash32
hash = ExceptT Error IO (Maybe EncodedPlutusData)
-> IO (Either Error (Maybe EncodedPlutusData))
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT Error IO (Maybe EncodedPlutusData)
-> IO (Either Error (Maybe EncodedPlutusData)))
-> ExceptT Error IO (Maybe EncodedPlutusData)
-> IO (Either Error (Maybe EncodedPlutusData))
forall a b. (a -> b) -> a -> b
$ do
Maybe Value
response <- IO (Either Error (Maybe Value)) -> ExceptT Error IO (Maybe Value)
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (Transport
-> Text
-> [Text]
-> [(Text, Text)]
-> Bool
-> IO (Either Error (Maybe Value))
getJSON Transport
transport Text
operation [Text
"scripts", Text
"datum", ByteString -> Text
Hex.encodeHex (Hash32 -> ByteString
Datum.hashBytes Hash32
hash), Text
"cbor"] [] Bool
True)
case Maybe Value
response of
Maybe Value
Nothing -> Maybe EncodedPlutusData
-> ExceptT Error IO (Maybe EncodedPlutusData)
forall a. a -> ExceptT Error IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe EncodedPlutusData
forall a. Maybe a
Nothing
Just Value
value -> do
ByteString
bytes <- Either Error ByteString -> ExceptT Error IO ByteString
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Text
-> (Value -> Parser ByteString) -> Value -> Either Error ByteString
forall a. Text -> (Value -> Parser a) -> Value -> Either Error a
Json.parseValue Text
operation Value -> Parser ByteString
cborField Value
value)
EncodedPlutusData
encoded <- Either Error EncodedPlutusData
-> ExceptT Error IO EncodedPlutusData
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Text
-> Either Error EncodedPlutusData -> Either Error EncodedPlutusData
forall a. Text -> Either Error a -> Either Error a
mapResponse Text
operation (ByteString -> Either Error EncodedPlutusData
Data.decodeCbor ByteString
bytes))
Text -> Hash32 -> EncodedPlutusData -> ExceptT Error IO ()
verifyDatum Text
operation Hash32
hash EncodedPlutusData
encoded
Maybe EncodedPlutusData
-> ExceptT Error IO (Maybe EncodedPlutusData)
forall a. a -> ExceptT Error IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (EncodedPlutusData -> Maybe EncodedPlutusData
forall a. a -> Maybe a
Just EncodedPlutusData
encoded)
where
operation :: Text
operation = Text
"blockfrost.resolveDatum"
rewards :: Http.Transport -> Reward.RewardAddress -> IO (Either Error Word64)
rewards :: Transport -> RewardAddress -> IO (Either Error Word64)
rewards Transport
transport RewardAddress
account = ExceptT Error IO Word64 -> IO (Either Error Word64)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT Error IO Word64 -> IO (Either Error Word64))
-> ExceptT Error IO Word64 -> IO (Either Error Word64)
forall a b. (a -> b) -> a -> b
$ do
Maybe Value
response <- IO (Either Error (Maybe Value)) -> ExceptT Error IO (Maybe Value)
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (Transport
-> Text
-> [Text]
-> [(Text, Text)]
-> Bool
-> IO (Either Error (Maybe Value))
getJSON Transport
transport Text
operation [Text
"accounts", RewardAddress -> Text
Reward.toText RewardAddress
account] [] Bool
True)
ExceptT Error IO Word64
-> (Value -> ExceptT Error IO Word64)
-> Maybe Value
-> ExceptT Error IO Word64
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Word64 -> ExceptT Error IO Word64
forall a. a -> ExceptT Error IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Word64
0) (Either Error Word64 -> ExceptT Error IO Word64
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Either Error Word64 -> ExceptT Error IO Word64)
-> (Value -> Either Error Word64)
-> Value
-> ExceptT Error IO Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> (Value -> Parser Word64) -> Value -> Either Error Word64
forall a. Text -> (Value -> Parser a) -> Value -> Either Error a
Json.parseValue Text
operation Value -> Parser Word64
parser) Maybe Value
response
where
operation :: Text
operation = Text
"blockfrost.rewardBalance"
parser :: Value -> Parser Word64
parser = String -> (Object -> Parser Word64) -> Value -> Parser Word64
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"reward account" ((Object -> Parser Word64) -> Value -> Parser Word64)
-> (Object -> Parser Word64) -> Value -> Parser Word64
forall a b. (a -> b) -> a -> b
$ \Object
entry -> do
Text
text <- Object
entry Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"stake_address"
RewardAddress
actual <- (Error -> Parser RewardAddress)
-> (RewardAddress -> Parser RewardAddress)
-> Either Error RewardAddress
-> Parser RewardAddress
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Parser RewardAddress -> Error -> Parser RewardAddress
forall a b. a -> b -> a
const (String -> Parser RewardAddress
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid reward account")) RewardAddress -> Parser RewardAddress
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> Either Error RewardAddress
Reward.fromText Text
text)
Bool -> Parser () -> Parser ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (RewardAddress
actual RewardAddress -> RewardAddress -> Bool
forall a. Eq a => a -> a -> Bool
== RewardAddress
account) (String -> Parser ()
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Account identity mismatch")
Object
entry Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"withdrawable_amount" Parser Text -> (Text -> Parser Word64) -> Parser Word64
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Parser Word64
Json.parseQuantity
resolve :: Http.Transport -> [Input.Input] -> IO (Either Error [Utxo.Utxo])
resolve :: Transport -> [Input] -> IO (Either Error [Utxo])
resolve Transport
transport [Input]
requested = ExceptT Error IO [Utxo] -> IO (Either Error [Utxo])
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT Error IO [Utxo] -> IO (Either Error [Utxo]))
-> ExceptT Error IO [Utxo] -> IO (Either Error [Utxo])
forall a b. (a -> b) -> a -> b
$ do
let unique :: [Input]
unique = [Input] -> [Input]
forall a. Ord a => [a] -> [a]
distinct [Input]
requested
transactionIds :: [TransactionId]
transactionIds = [TransactionId] -> [TransactionId]
forall a. Ord a => [a] -> [a]
distinct ((Input -> TransactionId) -> [Input] -> [TransactionId]
forall a b. (a -> b) -> [a] -> [b]
map Input -> TransactionId
Input.transactionId [Input]
unique)
[(Input, Address)]
discovered <- [[(Input, Address)]] -> [(Input, Address)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[(Input, Address)]] -> [(Input, Address)])
-> ExceptT Error IO [[(Input, Address)]]
-> ExceptT Error IO [(Input, Address)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (TransactionId -> ExceptT Error IO [(Input, Address)])
-> [TransactionId] -> ExceptT Error IO [[(Input, Address)]]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse TransactionId -> ExceptT Error IO [(Input, Address)]
discover [TransactionId]
transactionIds
let addresses :: [Address]
addresses = [Address] -> [Address]
forall a. Ord a => [a] -> [a]
distinct (((Input, Address) -> Address) -> [(Input, Address)] -> [Address]
forall a b. (a -> b) -> [a] -> [b]
map (Input, Address) -> Address
forall a b. (a, b) -> b
snd [(Input, Address)]
discovered)
eligible :: Set Input
eligible = [Input] -> Set Input
forall a. Ord a => [a] -> Set a
Set.fromList (((Input, Address) -> Input) -> [(Input, Address)] -> [Input]
forall a b. (a -> b) -> [a] -> [b]
map (Input, Address) -> Input
forall a b. (a, b) -> a
fst [(Input, Address)]
discovered)
[[Utxo]]
snapshots <- (Address -> ExceptT Error IO [Utxo])
-> [Address] -> ExceptT Error IO [[Utxo]]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (\Address
address -> IO (Either Error [Utxo]) -> ExceptT Error IO [Utxo]
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (Transport
-> Text -> Address -> Maybe AssetId -> IO (Either Error [Utxo])
atAddress Transport
transport Text
operation Address
address Maybe AssetId
forall a. Maybe a
Nothing)) [Address]
addresses
let found :: Map Input Utxo
found = [(Input, Utxo)] -> Map Input Utxo
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(Utxo -> Input
Utxo.input Utxo
value, Utxo
value) | Utxo
value <- [[Utxo]] -> [Utxo]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[Utxo]]
snapshots, Input -> Set Input -> Bool
forall a. Ord a => a -> Set a -> Bool
Set.member (Utxo -> Input
Utxo.input Utxo
value) Set Input
eligible]
[Utxo] -> ExceptT Error IO [Utxo]
forall a. a -> ExceptT Error IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Input -> Maybe Utxo) -> [Input] -> [Utxo]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Input -> Map Input Utxo -> Maybe Utxo
forall k a. Ord k => k -> Map k a -> Maybe a
`Map.lookup` Map Input Utxo
found) [Input]
unique)
where
operation :: Text
operation = Text
"blockfrost.resolveInputs"
discover :: TransactionId -> ExceptT Error IO [(Input, Address)]
discover TransactionId
transaction = do
Maybe Value
response <- IO (Either Error (Maybe Value)) -> ExceptT Error IO (Maybe Value)
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (Transport
-> Text
-> [Text]
-> [(Text, Text)]
-> Bool
-> IO (Either Error (Maybe Value))
getJSON Transport
transport Text
operation [Text
"txs", TransactionId -> Text
TxId.toHex TransactionId
transaction, Text
"utxos"] [] Bool
True)
[(Input, Address)]
entries <- ExceptT Error IO [(Input, Address)]
-> (Value -> ExceptT Error IO [(Input, Address)])
-> Maybe Value
-> ExceptT Error IO [(Input, Address)]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([(Input, Address)] -> ExceptT Error IO [(Input, Address)]
forall a. a -> ExceptT Error IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []) (Either Error [(Input, Address)]
-> ExceptT Error IO [(Input, Address)]
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Either Error [(Input, Address)]
-> ExceptT Error IO [(Input, Address)])
-> (Value -> Either Error [(Input, Address)])
-> Value
-> ExceptT Error IO [(Input, Address)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text
-> (Value -> Parser [(Input, Address)])
-> Value
-> Either Error [(Input, Address)]
forall a. Text -> (Value -> Parser a) -> Value -> Either Error a
Json.parseValue Text
operation (TransactionId -> Value -> Parser [(Input, Address)]
Json.transactionAddresses TransactionId
transaction)) Maybe Value
response
[(Input, Address)] -> ExceptT Error IO [(Input, Address)]
forall a. a -> ExceptT Error IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [(Input
reference, Address
address) | (Input
reference, Address
address) <- [(Input, Address)]
entries, Input
reference Input -> [Input] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Input]
requested]
distinct :: (Ord a) => [a] -> [a]
distinct :: forall a. Ord a => [a] -> [a]
distinct = Set a -> [a] -> [a]
forall {a}. Ord a => Set a -> [a] -> [a]
go Set a
forall a. Set a
Set.empty
where
go :: Set a -> [a] -> [a]
go Set a
_ [] = []
go Set a
seen (a
value : [a]
rest)
| a -> Set a -> Bool
forall a. Ord a => a -> Set a -> Bool
Set.member a
value Set a
seen = Set a -> [a] -> [a]
go Set a
seen [a]
rest
| Bool
otherwise = a
value a -> [a] -> [a]
forall a. a -> [a] -> [a]
: Set a -> [a] -> [a]
go (a -> Set a -> Set a
forall a. Ord a => a -> Set a -> Set a
Set.insert a
value Set a
seen) [a]
rest
submit :: Http.Transport -> Tx.Transaction -> IO (Either Error TxId.TransactionId)
submit :: Transport -> Transaction -> IO (Either Error TransactionId)
submit Transport
transport Transaction
transaction = ExceptT Error IO TransactionId -> IO (Either Error TransactionId)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT Error IO TransactionId -> IO (Either Error TransactionId))
-> ExceptT Error IO TransactionId
-> IO (Either Error TransactionId)
forall a b. (a -> b) -> a -> b
$ do
TransactionId
expected <- IO (Either Error TransactionId) -> ExceptT Error IO TransactionId
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (Transaction -> IO (Either Error TransactionId)
Tx.transactionId Transaction
transaction)
Response
response <- IO (Either Error Response) -> ExceptT Error IO Response
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (Transport -> Text -> Request -> IO (Either Error Response)
send Transport
transport Text
operation (Method
-> [Text]
-> [(Text, Text)]
-> ByteString
-> ByteString
-> Maybe Int
-> Request
Http.Request Method
Http.Post [Text
"tx", Text
"submit"] [] (Transaction -> ByteString
Tx.encodeCbor Transaction
transaction) ByteString
"application/cbor" Maybe Int
forall a. Maybe a
Nothing))
Bool -> ExceptT Error IO () -> ExceptT Error IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Response -> Int
Http.status Response
response Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
200) (Either Error () -> ExceptT Error IO ()
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Int -> Error
ProviderHttpError Text
operation (Response -> Int
Http.status Response
response))))
Value
value <- Either Error Value -> ExceptT Error IO Value
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Text -> ByteString -> Either Error Value
Json.decodeValue Text
operation (Response -> ByteString
Http.bodyBytes Response
response))
TransactionId
identifier <- Either Error TransactionId -> ExceptT Error IO TransactionId
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Text
-> (Value -> Parser TransactionId)
-> Value
-> Either Error TransactionId
forall a. Text -> (Value -> Parser a) -> Value -> Either Error a
Json.parseValue Text
operation (String
-> (Text -> Parser TransactionId) -> Value -> Parser TransactionId
forall a. String -> (Text -> Parser a) -> Value -> Parser a
withText String
"transaction hash" ((Error -> Parser TransactionId)
-> (TransactionId -> Parser TransactionId)
-> Either Error TransactionId
-> Parser TransactionId
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Parser TransactionId -> Error -> Parser TransactionId
forall a b. a -> b -> a
const (String -> Parser TransactionId
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid transaction hash")) TransactionId -> Parser TransactionId
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error TransactionId -> Parser TransactionId)
-> (Text -> Either Error TransactionId)
-> Text
-> Parser TransactionId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either Error TransactionId
TxId.fromHex)) Value
value)
Bool -> ExceptT Error IO () -> ExceptT Error IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (TransactionId
identifier TransactionId -> TransactionId -> Bool
forall a. Eq a => a -> a -> Bool
== TransactionId
expected) (Either Error () -> ExceptT Error IO ()
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
InvalidProviderResponse Text
operation)))
TransactionId -> ExceptT Error IO TransactionId
forall a. a -> ExceptT Error IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TransactionId
identifier
where
operation :: Text
operation = Text
"blockfrost.submitTransaction"
evaluate :: Http.Transport -> Tx.Transaction -> [Utxo.Utxo] -> IO (Either Error Evaluation.Budgets)
evaluate :: Transport -> Transaction -> [Utxo] -> IO (Either Error Budgets)
evaluate Transport
transport Transaction
transaction [Utxo]
inputs = ExceptT Error IO Budgets -> IO (Either Error Budgets)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT Error IO Budgets -> IO (Either Error Budgets))
-> ExceptT Error IO Budgets -> IO (Either Error Budgets)
forall a b. (a -> b) -> a -> b
$ do
Value
payload <- Either Error Value -> ExceptT Error IO Value
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Transaction -> [Utxo] -> Either Error Value
Json.evaluationPayload Transaction
transaction [Utxo]
inputs)
Response
response <- IO (Either Error Response) -> ExceptT Error IO Response
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (Transport -> Text -> Request -> IO (Either Error Response)
send Transport
transport Text
operation (Method
-> [Text]
-> [(Text, Text)]
-> ByteString
-> ByteString
-> Maybe Int
-> Request
Http.Request Method
Http.Post [Text
"utils", Text
"txs", Text
"evaluate", Text
"utxos"] [(Text
"version", Text
"5")] (ByteString -> ByteString
Lazy.toStrict (Value -> ByteString
forall a. ToJSON a => a -> ByteString
encode Value
payload)) ByteString
"application/json" Maybe Int
forall a. Maybe a
Nothing))
Bool -> ExceptT Error IO () -> ExceptT Error IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Response -> Int
Http.status Response
response Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
200) (Either Error () -> ExceptT Error IO ()
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Int -> Error
ProviderHttpError Text
operation (Response -> Int
Http.status Response
response))))
Value
value <- Either Error Value -> ExceptT Error IO Value
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Text -> ByteString -> Either Error Value
Json.decodeValue Text
operation (Response -> ByteString
Http.bodyBytes Response
response))
Either Error Budgets -> ExceptT Error IO Budgets
forall (m :: * -> *) e a. Monad m => Either e a -> ExceptT e m a
except (Transaction -> Value -> Either Error Budgets
Json.decodeBudgets Transaction
transaction Value
value)
where
operation :: Text
operation = Text
"blockfrost.evaluateTransaction"
await :: Config -> Http.Transport -> TxId.TransactionId -> Word64 -> IO (Either Error Bool)
await :: Config
-> Transport -> TransactionId -> Word64 -> IO (Either Error Bool)
await Config
_ Transport
_ TransactionId
_ Word64
0 = Either Error Bool -> IO (Either Error Bool)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> Either Error Bool
forall a b. b -> Either a b
Right Bool
False)
await Config
config Transport
transport TransactionId
identifier Word64
milliseconds = do
Word64
started <- IO Word64
getMonotonicTimeNSec
Integer -> IO (Either Error Bool)
loop (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
started Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
milliseconds Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
1000000)
where
operation :: Text
operation = Text
"blockfrost.awaitTransaction"
loop :: Integer -> IO (Either Error Bool)
loop Integer
deadline = do
Integer
now <- Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word64 -> Integer) -> IO Word64 -> IO Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO Word64
getMonotonicTimeNSec
if Integer
now Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
deadline
then Either Error Bool -> IO (Either Error Bool)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> Either Error Bool
forall a b. b -> Either a b
Right Bool
False)
else do
let remaining :: Integer
remaining = Integer -> Integer -> Integer
forall a. Ord a => a -> a -> a
max Integer
1 ((Integer
deadline Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
now Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
999999) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`div` Integer
1000000)
request :: Request
request = ([Text] -> [(Text, Text)] -> Request
getRequest [Text
"txs", TransactionId -> Text
TxId.toHex TransactionId
identifier] []) {Http.requestTimeoutMillis = Just (fromInteger (min remaining (toInteger (Config.responseTimeoutMillis config))))}
Either Error Response
result <- Transport -> Text -> Request -> IO (Either Error Response)
send Transport
transport Text
operation Request
request
case Either Error Response
result of
Left Error
failure -> do
Integer
finished <- Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word64 -> Integer) -> IO Word64 -> IO Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO Word64
getMonotonicTimeNSec
Either Error Bool -> IO (Either Error Bool)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error Bool -> IO (Either Error Bool))
-> Either Error Bool -> IO (Either Error Bool)
forall a b. (a -> b) -> a -> b
$ if Integer
finished Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
deadline Bool -> Bool -> Bool
&& Error
failure Error -> Error -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> Text -> Error
ProviderTransportError Text
operation Text
"timeout" then Bool -> Either Error Bool
forall a b. b -> Either a b
Right Bool
False else Error -> Either Error Bool
forall a b. a -> Either a b
Left Error
failure
Right Response
response -> case Response -> Int
Http.status Response
response of
Int
200 -> Either Error Bool -> IO (Either Error Bool)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error Bool -> IO (Either Error Bool))
-> Either Error Bool -> IO (Either Error Bool)
forall a b. (a -> b) -> a -> b
$ do
Value
value <- Text -> ByteString -> Either Error Value
Json.decodeValue Text
operation (Response -> ByteString
Http.bodyBytes Response
response)
Text -> (Value -> Parser ()) -> Value -> Either Error ()
forall a. Text -> (Value -> Parser a) -> Value -> Either Error a
Json.parseValue Text
operation Value -> Parser ()
confirmed Value
value
Bool -> Either Error Bool
forall a. a -> Either Error a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
Int
404 -> do
Integer
finished <- Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word64 -> Integer) -> IO Word64 -> IO Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO Word64
getMonotonicTimeNSec
let delay :: Integer
delay = Integer -> Integer -> Integer
forall a. Ord a => a -> a -> a
min (Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Config -> Int
Config.pollIntervalMillis Config
config) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
1000) (Integer -> Integer -> Integer
forall a. Ord a => a -> a -> a
max Integer
0 ((Integer
deadline Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
finished) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`div` Integer
1000))
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Integer
delay Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
0) (Int -> IO ()
threadDelay (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
delay))
Integer -> IO (Either Error Bool)
loop Integer
deadline
Int
code -> Either Error Bool -> IO (Either Error Bool)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error Bool
forall a b. a -> Either a b
Left (Text -> Int -> Error
ProviderHttpError Text
operation Int
code))
confirmed :: Value -> Parser ()
confirmed = String -> (Object -> Parser ()) -> Value -> Parser ()
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"confirmed transaction" ((Object -> Parser ()) -> Value -> Parser ())
-> (Object -> Parser ()) -> Value -> Parser ()
forall a b. (a -> b) -> a -> b
$ \Object
entry -> do
Text
text <- Object
entry Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"hash"
TransactionId
actual <- (Error -> Parser TransactionId)
-> (TransactionId -> Parser TransactionId)
-> Either Error TransactionId
-> Parser TransactionId
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Parser TransactionId -> Error -> Parser TransactionId
forall a b. a -> b -> a
const (String -> Parser TransactionId
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid transaction hash")) TransactionId -> Parser TransactionId
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> Either Error TransactionId
TxId.fromHex Text
text)
Bool -> Parser () -> Parser ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (TransactionId
actual TransactionId -> TransactionId -> Bool
forall a. Eq a => a -> a -> Bool
== TransactionId
identifier) (String -> Parser ()
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Transaction identity mismatch")
ByteString
block <- Object
entry Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"block" Parser Text -> (Text -> Parser ByteString) -> Parser ByteString
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Parser ByteString
Json.parseHex
Bool -> Parser () -> Parser ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ByteString -> Int
B.length ByteString
block Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
32) (String -> Parser ()
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid block hash")
Word64
_ <- Object
entry Object -> Key -> Parser Value
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"block_height" Parser Value -> (Value -> Parser Word64) -> Parser Word64
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Value -> Parser Word64
Json.parseNatural
() -> Parser ()
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()