module Morley.Client.RPC.API
( NodeMethods (..)
, nodeMethods
, monitorHeads
) where
import Data.Coerce (coerce)
import Network.HTTP.Types.Status (Status(..))
import Servant.API
(Capture, Get, JSON, NewlineFraming, Post, QueryParam, ReqBody, SourceIO, StreamGet,
ToHttpApiData(..), (:<|>)(..), (:>))
import Servant.Client.Core
(ResponseF(..), RunClient, RunStreamingClient, clientIn, pattern FailureResponse)
import Morley.Client.RPC.QueryFixedParam
import Morley.Client.RPC.Types
import Morley.Micheline (Expression, TezosInt64, TezosMutez, TezosNat)
import Morley.Tezos.Address
import Morley.Tezos.Address.Kinds
import Morley.Tezos.Crypto (KeyHash, PublicKey)
import Morley.Util.ByteString
type NodeAPI =
"chains" :> "main" :> "blocks" :> (
Capture "block_id" BlockId :> "hash" :> Get '[JSON] Text :<|>
Capture "block_id" BlockId :> "context" :> "contracts" :> Capture "contract" ImplicitAddress'
:> "counter" :> Get '[JSON] TezosInt64 :<|>
Capture "block_id" BlockId :> "context" :> "contracts" :> Capture "contract" ContractAddress'
:> "script" :> Get '[JSON] OriginationScript :<|>
Capture "block_id" BlockId :> "context" :> "contracts" :> Capture "contract" ContractAddress'
:> "storage" :> Get '[JSON] Expression :<|>
Capture "block_id" BlockId :> "context" :> "contracts" :> Capture "contract" Address'
:> "ticket_balance" :> ReqBody '[JSON] GetTicketBalance :> Post '[JSON] TezosNat :<|>
Capture "block_id" BlockId :> "context" :> "contracts" :> Capture "contract" ContractAddress'
:> "all_ticket_balances" :> Get '[JSON] [GetAllTicketBalancesResponse] :<|>
Capture "block_id" BlockId :> Get '[JSON] BlockConstants :<|>
Capture "block_id" BlockId :> "header" :> Get '[JSON] BlockHeader :<|>
Capture "block_id" BlockId :> "context" :> "constants" :> Get '[JSON] ProtocolParameters :<|>
Capture "block_id" BlockId :> "operations" :> Get '[JSON] [[BlockOperation]] :<|>
Capture "block_id" BlockId :> "operation_hashes" :> Get '[JSON] [[OperationHash]] :<|>
Capture "block_id" BlockId :> "context" :> "contracts" :> Capture "contract" ContractAddress'
:> "big_map_get" :> ReqBody '[JSON] GetBigMap :> Post '[JSON] GetBigMapResult :<|>
Capture "block_id" BlockId :> "context" :> "big_maps"
:> Capture "big_map_id" Natural
:> Capture "script_expr" Text
:> Get '[JSON] Expression :<|>
Capture "block_id" BlockId :> "context" :> "big_maps"
:> Capture "big_map_id" Natural
:> QueryParam "offset" Natural
:> QueryParam "length" Natural
:> Get '[JSON] Expression :<|>
Capture "block_id" BlockId :> "context" :> "contracts"
:> Capture "contract" Address' :> "balance" :> Get '[JSON] TezosMutez :<|>
Capture "block_id" BlockId :> "context" :> "contracts"
:> Capture "contract" Address' :> "delegate" :> Get '[JSON] KeyHash :<|>
Capture "block_id" BlockId :> "context" :> "contracts" :> Capture "address" ImplicitAddress'
:> "manager_key" :> Get '[JSON] (Maybe PublicKey) :<|>
Capture "block_id" BlockId :> "helpers" :> "scripts" :> "script_size"
:> ReqBody '[JSON] CalcSize :> Post '[JSON] ScriptSize :<|>
Capture "block_id" BlockId :> "helpers" :> "forge" :> "operations"
:> ReqBody '[JSON] ForgeOperation :> Post '[JSON] HexJSONByteString :<|>
Capture "block_id" BlockId :> "helpers" :> "scripts" :> "run_operation"
:> ReqBody '[JSON] RunOperation :> Post '[JSON] RunOperationResult :<|>
Capture "block_id" BlockId :> "helpers" :> "preapply" :> "operations"
:> ReqBody '[JSON] [PreApplyOperation] :> Post '[JSON] [RunOperationResult] :<|>
Capture "block_id" BlockId :> "helpers" :> "scripts" :> "run_code"
:> ReqBody '[JSON] RunCode :> Post '[JSON] RunCodeResult :<|>
Capture "block_id" BlockId :> "helpers" :> "scripts" :> "pack_data"
:> ReqBody '[JSON] PackData :> Post '[JSON] PackDataResult
) :<|>
"chains" :> "main" :> "chain_id" :> Get '[JSON] Text :<|>
"injection" :> "operation" :> QueryFixedParam "chain" "main"
:> ReqBody '[JSON] HexJSONByteString
:> Post '[JSON] OperationHash
type StreamingAPI =
"monitor" :> "heads" :> "main"
:> StreamGet NewlineFraming JSON (SourceIO BlockHeader)
nodeAPI :: Proxy NodeAPI
nodeAPI :: Proxy NodeAPI
nodeAPI = Proxy NodeAPI
forall {k} (t :: k). Proxy t
Proxy
data NodeMethods m = NodeMethods
{ forall (m :: * -> *). NodeMethods m -> BlockId -> m Text
getBlockHash :: BlockId -> m Text
, forall (m :: * -> *).
NodeMethods m -> BlockId -> ImplicitAddress -> m TezosInt64
getCounter :: BlockId -> ImplicitAddress -> m TezosInt64
, forall (m :: * -> *).
NodeMethods m -> BlockId -> ContractAddress -> m OriginationScript
getScript :: BlockId -> ContractAddress -> m OriginationScript
, forall (m :: * -> *).
NodeMethods m -> BlockId -> ContractAddress -> m Expression
getStorageAtBlock :: BlockId -> ContractAddress -> m Expression
, forall (m :: * -> *).
NodeMethods m
-> BlockId -> Address -> GetTicketBalance -> m TezosNat
getTicketBalanceAtBlock :: BlockId -> Address -> GetTicketBalance -> m TezosNat
, forall (m :: * -> *).
NodeMethods m
-> BlockId -> ContractAddress -> m [GetAllTicketBalancesResponse]
getAllTicketBalancesAtBlock :: BlockId -> ContractAddress -> m [GetAllTicketBalancesResponse]
, forall (m :: * -> *). NodeMethods m -> BlockId -> m BlockConstants
getBlockConstants :: BlockId -> m BlockConstants
, :: BlockId -> m BlockHeader
, forall (m :: * -> *).
NodeMethods m -> BlockId -> m ProtocolParameters
getProtocolParameters :: BlockId -> m ProtocolParameters
, forall (m :: * -> *).
NodeMethods m -> BlockId -> m [[BlockOperation]]
getBlockOperations :: BlockId -> m [[BlockOperation]]
, forall (m :: * -> *).
NodeMethods m -> BlockId -> m [[OperationHash]]
getBlockOperationHashes :: BlockId -> m [[OperationHash]]
, forall (m :: * -> *).
NodeMethods m
-> BlockId -> ContractAddress -> GetBigMap -> m GetBigMapResult
getBigMap :: BlockId -> ContractAddress -> GetBigMap -> m GetBigMapResult
, forall (m :: * -> *).
NodeMethods m -> BlockId -> Natural -> Text -> m Expression
getBigMapValueAtBlock :: BlockId -> Natural -> Text -> m Expression
, forall (m :: * -> *).
NodeMethods m
-> BlockId
-> Natural
-> Maybe Natural
-> Maybe Natural
-> m Expression
getBigMapValuesAtBlock :: BlockId -> Natural -> Maybe Natural -> Maybe Natural -> m Expression
, forall (m :: * -> *).
NodeMethods m -> BlockId -> Address -> m TezosMutez
getBalance :: BlockId -> Address -> m TezosMutez
, forall (m :: * -> *).
NodeMethods m -> BlockId -> L1Address -> m (Maybe KeyHash)
getDelegate :: BlockId -> L1Address -> m (Maybe KeyHash)
, forall (m :: * -> *).
NodeMethods m -> BlockId -> CalcSize -> m ScriptSize
getScriptSizeAtBlock :: BlockId -> CalcSize -> m ScriptSize
, forall (m :: * -> *).
NodeMethods m -> BlockId -> ForgeOperation -> m HexJSONByteString
forgeOperation :: BlockId -> ForgeOperation -> m HexJSONByteString
, forall (m :: * -> *).
NodeMethods m -> BlockId -> RunOperation -> m RunOperationResult
runOperation :: BlockId -> RunOperation -> m RunOperationResult
, forall (m :: * -> *).
NodeMethods m
-> BlockId -> [PreApplyOperation] -> m [RunOperationResult]
preApplyOperations :: BlockId -> [PreApplyOperation] -> m [RunOperationResult]
, forall (m :: * -> *).
NodeMethods m -> BlockId -> RunCode -> m RunCodeResult
runCode :: BlockId -> RunCode -> m RunCodeResult
, forall (m :: * -> *).
NodeMethods m -> BlockId -> ImplicitAddress -> m (Maybe PublicKey)
getManagerKey :: BlockId -> ImplicitAddress -> m (Maybe PublicKey)
, forall (m :: * -> *). NodeMethods m -> m Text
getChainId :: m Text
, forall (m :: * -> *).
NodeMethods m -> HexJSONByteString -> m OperationHash
injectOperation :: HexJSONByteString -> m OperationHash
, forall (m :: * -> *).
NodeMethods m -> BlockId -> PackData -> m PackDataResult
packData :: BlockId -> PackData -> m PackDataResult
}
monitorHeads :: forall m. (RunStreamingClient m) => m (SourceIO BlockHeader)
monitorHeads :: forall (m :: * -> *).
RunStreamingClient m =>
m (SourceIO BlockHeader)
monitorHeads = forall {t}. Proxy t
forall {k} (t :: k). Proxy t
Proxy @StreamingAPI Proxy StreamingAPI -> Proxy m -> Client m StreamingAPI
forall (m :: * -> *) api.
HasClient m api =>
Proxy api -> Proxy m -> Client m api
`clientIn` forall {k} (t :: k). Proxy t
forall {t :: * -> *}. Proxy t
Proxy @m
nodeMethods :: forall m. (MonadCatch m, RunClient m) => NodeMethods m
nodeMethods :: forall (m :: * -> *). (MonadCatch m, RunClient m) => NodeMethods m
nodeMethods = NodeMethods :: forall (m :: * -> *).
(BlockId -> m Text)
-> (BlockId -> ImplicitAddress -> m TezosInt64)
-> (BlockId -> ContractAddress -> m OriginationScript)
-> (BlockId -> ContractAddress -> m Expression)
-> (BlockId -> Address -> GetTicketBalance -> m TezosNat)
-> (BlockId -> ContractAddress -> m [GetAllTicketBalancesResponse])
-> (BlockId -> m BlockConstants)
-> (BlockId -> m BlockHeader)
-> (BlockId -> m ProtocolParameters)
-> (BlockId -> m [[BlockOperation]])
-> (BlockId -> m [[OperationHash]])
-> (BlockId -> ContractAddress -> GetBigMap -> m GetBigMapResult)
-> (BlockId -> Natural -> Text -> m Expression)
-> (BlockId
-> Natural -> Maybe Natural -> Maybe Natural -> m Expression)
-> (BlockId -> Address -> m TezosMutez)
-> (BlockId -> L1Address -> m (Maybe KeyHash))
-> (BlockId -> CalcSize -> m ScriptSize)
-> (BlockId -> ForgeOperation -> m HexJSONByteString)
-> (BlockId -> RunOperation -> m RunOperationResult)
-> (BlockId -> [PreApplyOperation] -> m [RunOperationResult])
-> (BlockId -> RunCode -> m RunCodeResult)
-> (BlockId -> ImplicitAddress -> m (Maybe PublicKey))
-> m Text
-> (HexJSONByteString -> m OperationHash)
-> (BlockId -> PackData -> m PackDataResult)
-> NodeMethods m
NodeMethods
{ getDelegate :: BlockId -> L1Address -> m (Maybe KeyHash)
getDelegate = \BlockId
block (Constrained KindedAddress a
addr) -> do
Either ClientError KeyHash
result <- m KeyHash -> m (Either ClientError KeyHash)
forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> m (Either e a)
try (m KeyHash -> m (Either ClientError KeyHash))
-> m KeyHash -> m (Either ClientError KeyHash)
forall a b. (a -> b) -> a -> b
$ BlockId -> Address' -> m KeyHash
getDelegate' BlockId
block (Address -> Address'
Address' (Address -> Address') -> Address -> Address'
forall a b. (a -> b) -> a -> b
$ KindedAddress a -> Address
forall (kind :: AddressKind). KindedAddress kind -> Address
MkAddress KindedAddress a
addr)
case Either ClientError KeyHash
result of
Left (FailureResponse RequestF () (BaseUrl, ByteString)
_ Response{responseStatusCode :: forall a. ResponseF a -> Status
responseStatusCode=Status{statusCode :: Status -> Int
statusCode = Int
404}})
-> Maybe KeyHash -> m (Maybe KeyHash)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe KeyHash
forall a. Maybe a
Nothing
Left ClientError
err -> ClientError -> m (Maybe KeyHash)
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM ClientError
err
Right KeyHash
res -> Maybe KeyHash -> m (Maybe KeyHash)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe KeyHash -> m (Maybe KeyHash))
-> Maybe KeyHash -> m (Maybe KeyHash)
forall a b. (a -> b) -> a -> b
$ KeyHash -> Maybe KeyHash
forall a. a -> Maybe a
Just KeyHash
res
, m Text
HexJSONByteString -> m OperationHash
BlockId -> m [[BlockOperation]]
BlockId -> m [[OperationHash]]
BlockId -> m Text
BlockId -> m ProtocolParameters
BlockId -> m BlockHeader
BlockId -> m BlockConstants
BlockId
-> Natural -> Maybe Natural -> Maybe Natural -> m Expression
BlockId -> Natural -> Text -> m Expression
BlockId -> [PreApplyOperation] -> m [RunOperationResult]
BlockId -> ImplicitAddress -> m (Maybe PublicKey)
BlockId -> ImplicitAddress -> m TezosInt64
BlockId -> ContractAddress -> m [GetAllTicketBalancesResponse]
BlockId -> ContractAddress -> m Expression
BlockId -> ContractAddress -> m OriginationScript
BlockId -> ContractAddress -> GetBigMap -> m GetBigMapResult
BlockId -> Address -> m TezosMutez
BlockId -> Address -> GetTicketBalance -> m TezosNat
BlockId -> PackData -> m PackDataResult
BlockId -> CalcSize -> m ScriptSize
BlockId -> RunCode -> m RunCodeResult
BlockId -> RunOperation -> m RunOperationResult
BlockId -> ForgeOperation -> m HexJSONByteString
injectOperation :: HexJSONByteString -> m OperationHash
getChainId :: m Text
packData :: BlockId -> PackData -> m PackDataResult
runCode :: BlockId -> RunCode -> m RunCodeResult
preApplyOperations :: BlockId -> [PreApplyOperation] -> m [RunOperationResult]
runOperation :: BlockId -> RunOperation -> m RunOperationResult
forgeOperation :: BlockId -> ForgeOperation -> m HexJSONByteString
getScriptSizeAtBlock :: BlockId -> CalcSize -> m ScriptSize
getManagerKey :: BlockId -> ImplicitAddress -> m (Maybe PublicKey)
getBalance :: BlockId -> Address -> m TezosMutez
getBigMapValuesAtBlock :: BlockId
-> Natural -> Maybe Natural -> Maybe Natural -> m Expression
getBigMapValueAtBlock :: BlockId -> Natural -> Text -> m Expression
getBigMap :: BlockId -> ContractAddress -> GetBigMap -> m GetBigMapResult
getBlockOperationHashes :: BlockId -> m [[OperationHash]]
getBlockOperations :: BlockId -> m [[BlockOperation]]
getProtocolParameters :: BlockId -> m ProtocolParameters
getBlockHeader :: BlockId -> m BlockHeader
getBlockConstants :: BlockId -> m BlockConstants
getAllTicketBalancesAtBlock :: BlockId -> ContractAddress -> m [GetAllTicketBalancesResponse]
getTicketBalanceAtBlock :: BlockId -> Address -> GetTicketBalance -> m TezosNat
getStorageAtBlock :: BlockId -> ContractAddress -> m Expression
getScript :: BlockId -> ContractAddress -> m OriginationScript
getCounter :: BlockId -> ImplicitAddress -> m TezosInt64
getBlockHash :: BlockId -> m Text
packData :: BlockId -> PackData -> m PackDataResult
injectOperation :: HexJSONByteString -> m OperationHash
getChainId :: m Text
getManagerKey :: BlockId -> ImplicitAddress -> m (Maybe PublicKey)
runCode :: BlockId -> RunCode -> m RunCodeResult
preApplyOperations :: BlockId -> [PreApplyOperation] -> m [RunOperationResult]
runOperation :: BlockId -> RunOperation -> m RunOperationResult
forgeOperation :: BlockId -> ForgeOperation -> m HexJSONByteString
getScriptSizeAtBlock :: BlockId -> CalcSize -> m ScriptSize
getBalance :: BlockId -> Address -> m TezosMutez
getBigMapValuesAtBlock :: BlockId
-> Natural -> Maybe Natural -> Maybe Natural -> m Expression
getBigMapValueAtBlock :: BlockId -> Natural -> Text -> m Expression
getBigMap :: BlockId -> ContractAddress -> GetBigMap -> m GetBigMapResult
getBlockOperationHashes :: BlockId -> m [[OperationHash]]
getBlockOperations :: BlockId -> m [[BlockOperation]]
getProtocolParameters :: BlockId -> m ProtocolParameters
getBlockHeader :: BlockId -> m BlockHeader
getBlockConstants :: BlockId -> m BlockConstants
getAllTicketBalancesAtBlock :: BlockId -> ContractAddress -> m [GetAllTicketBalancesResponse]
getTicketBalanceAtBlock :: BlockId -> Address -> GetTicketBalance -> m TezosNat
getStorageAtBlock :: BlockId -> ContractAddress -> m Expression
getScript :: BlockId -> ContractAddress -> m OriginationScript
getCounter :: BlockId -> ImplicitAddress -> m TezosInt64
getBlockHash :: BlockId -> m Text
..
}
where
(BlockId -> m Text
getBlockHash
:<|> ((BlockId -> KindedAddress' 'AddressKindImplicit -> m TezosInt64)
-> BlockId -> ImplicitAddress -> m TezosInt64
coerce -> BlockId -> ImplicitAddress -> m TezosInt64
getCounter)
:<|> ((BlockId
-> KindedAddress' 'AddressKindContract -> m OriginationScript)
-> BlockId -> ContractAddress -> m OriginationScript
coerce -> BlockId -> ContractAddress -> m OriginationScript
getScript)
:<|> ((BlockId -> KindedAddress' 'AddressKindContract -> m Expression)
-> BlockId -> ContractAddress -> m Expression
coerce -> BlockId -> ContractAddress -> m Expression
getStorageAtBlock)
:<|> ((BlockId -> Address' -> GetTicketBalance -> m TezosNat)
-> BlockId -> Address -> GetTicketBalance -> m TezosNat
coerce -> BlockId -> Address -> GetTicketBalance -> m TezosNat
getTicketBalanceAtBlock)
:<|> ((BlockId
-> KindedAddress' 'AddressKindContract
-> m [GetAllTicketBalancesResponse])
-> BlockId -> ContractAddress -> m [GetAllTicketBalancesResponse]
coerce -> BlockId -> ContractAddress -> m [GetAllTicketBalancesResponse]
getAllTicketBalancesAtBlock)
:<|> BlockId -> m BlockConstants
getBlockConstants
:<|> BlockId -> m BlockHeader
getBlockHeader
:<|> BlockId -> m ProtocolParameters
getProtocolParameters
:<|> BlockId -> m [[BlockOperation]]
getBlockOperations
:<|> BlockId -> m [[OperationHash]]
getBlockOperationHashes
:<|> ((BlockId
-> KindedAddress' 'AddressKindContract
-> GetBigMap
-> m GetBigMapResult)
-> BlockId -> ContractAddress -> GetBigMap -> m GetBigMapResult
coerce -> BlockId -> ContractAddress -> GetBigMap -> m GetBigMapResult
getBigMap)
:<|> BlockId -> Natural -> Text -> m Expression
getBigMapValueAtBlock
:<|> BlockId
-> Natural -> Maybe Natural -> Maybe Natural -> m Expression
getBigMapValuesAtBlock
:<|> ((BlockId -> Address' -> m TezosMutez)
-> BlockId -> Address -> m TezosMutez
coerce -> BlockId -> Address -> m TezosMutez
getBalance)
:<|> BlockId -> Address' -> m KeyHash
getDelegate'
:<|> ((BlockId
-> KindedAddress' 'AddressKindImplicit -> m (Maybe PublicKey))
-> BlockId -> ImplicitAddress -> m (Maybe PublicKey)
coerce -> BlockId -> ImplicitAddress -> m (Maybe PublicKey)
getManagerKey)
:<|> BlockId -> CalcSize -> m ScriptSize
getScriptSizeAtBlock
:<|> BlockId -> ForgeOperation -> m HexJSONByteString
forgeOperation
:<|> BlockId -> RunOperation -> m RunOperationResult
runOperation
:<|> BlockId -> [PreApplyOperation] -> m [RunOperationResult]
preApplyOperations
:<|> BlockId -> RunCode -> m RunCodeResult
runCode
:<|> BlockId -> PackData -> m PackDataResult
packData)
:<|> m Text
getChainId
:<|> HexJSONByteString -> m OperationHash
injectOperation
= Proxy NodeAPI
nodeAPI Proxy NodeAPI -> Proxy m -> Client m NodeAPI
forall (m :: * -> *) api.
HasClient m api =>
Proxy api -> Proxy m -> Client m api
`clientIn` (forall {k} (t :: k). Proxy t
forall {t :: * -> *}. Proxy t
Proxy @m)
newtype KindedAddress' kind = KindedAddress' { forall (kind :: AddressKind).
KindedAddress' kind -> KindedAddress kind
unAddress' :: KindedAddress kind }
type ContractAddress' = KindedAddress' 'AddressKindContract
type ImplicitAddress' = KindedAddress' 'AddressKindImplicit
instance ToHttpApiData (KindedAddress' kind) where
toUrlPiece :: KindedAddress' kind -> Text
toUrlPiece = KindedAddress kind -> Text
forall (kind :: AddressKind). KindedAddress kind -> Text
formatAddress (KindedAddress kind -> Text)
-> (KindedAddress' kind -> KindedAddress kind)
-> KindedAddress' kind
-> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KindedAddress' kind -> KindedAddress kind
forall (kind :: AddressKind).
KindedAddress' kind -> KindedAddress kind
unAddress'
newtype Address' = Address' Address
instance ToHttpApiData Address' where
toUrlPiece :: Address' -> Text
toUrlPiece (Address' (MkAddress KindedAddress kind
addr)) = KindedAddress kind -> Text
forall (kind :: AddressKind). KindedAddress kind -> Text
formatAddress KindedAddress kind
addr