/
Api.hs
175 lines (162 loc) · 6.59 KB
/
Api.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
module Plutus.ChainIndex.Api
( API
, FromHashAPI
, FullAPI
, IsUtxoResponse(..)
, SwaggerAPI
, UtxoAtAddressRequest(..)
, UtxosResponse(..)
, UtxoWithCurrencyRequest(..)
, swagger
, TxoAtAddressRequest(..)
, TxosResponse(..)
) where
import Control.Monad.Freer.Extras.Pagination (Page, PageQuery)
import Data.Aeson (FromJSON, ToJSON, Value)
import Data.OpenApi qualified as OpenApi
import Data.Proxy (Proxy (..))
import GHC.Generics (Generic)
import Ledger (AssetClass, TxId)
import Ledger.Credential (Credential)
import Ledger.Tx (ChainIndexTxOut, TxOutRef)
import Plutus.ChainIndex.Tx (ChainIndexTx)
import Plutus.ChainIndex.Types (Diagnostics, Tip)
import Plutus.V1.Ledger.Api (Datum, DatumHash, MintingPolicy, MintingPolicyHash, Redeemer, RedeemerHash, StakeValidator,
StakeValidatorHash, Validator, ValidatorHash)
import Servant qualified
import Servant.API (Description, Get, JSON, NoContent, Post, Put, ReqBody, (:<|>), (:>))
import Servant.OpenApi (toOpenApi)
import Servant.Swagger.UI (SwaggerSchemaUI, SwaggerSchemaUI', swaggerSchemaUIServer)
-- | When requesting UTxOs of a given address, you need to provide the address,
-- and optionnally the number of elements per page and the last item of the last
-- requested page.
--
-- Here's an example for requesting the first page:
--
-- {
-- "credential": {
-- "tag": "PubKeyCredential",
-- "contents": {
-- "getPubKeyHash": "88ff402b0522f27649ac742238c697c579beeb344eb723099d1f16ce"
-- }
-- }
-- }
--
-- or
--
-- {
-- "pageQuery": {
-- "pageQuerySize": {
-- "getPageSize": 10
-- }
-- },
-- "credential": {
-- "tag": "PubKeyCredential",
-- "contents": {
-- "getPubKeyHash": "88ff402b0522f27649ac742238c697c579beeb344eb723099d1f16ce"
-- }
-- }
-- }
--
-- Here's an example for requesting the next page:
--
-- {
-- "pageQuery": {
-- "pageQuerySize": {
-- "getPageSize": 10
-- },
-- "pageQueryLastItem": {
-- "txOutRefId": {
-- "getTxId": "009b8c674b878cc68bd1d40562c5f14cdbb21be9266f605cfb68ed978e1a965b"
-- },
-- "txOutRefIdx": 0
-- }
-- },
-- "credential": {
-- "tag": "PubKeyCredential",
-- "contents": {
-- "getPubKeyHash": "88ff402b0522f27649ac742238c697c579beeb344eb723099d1f16ce"
-- }
-- }
-- }
data UtxoAtAddressRequest = UtxoAtAddressRequest
{ pageQuery :: Maybe (PageQuery TxOutRef)
, credential :: Credential
}
deriving (Show, Eq, Generic, FromJSON, ToJSON, OpenApi.ToSchema)
-- | See the comment on 'UtxoAtAddressRequest'.
--
-- The difference is using @currency@ field instead of @credential@.
-- {
-- "pageQuery": {
-- ...
-- },
-- "currency": {
-- "unAssetClass": [
-- {
-- "unCurrencySymbol": ""
-- },
-- {
-- "unTokenName": ""
-- }
-- ]
-- }
-- }
data UtxoWithCurrencyRequest = UtxoWithCurrencyRequest
{ pageQuery :: Maybe (PageQuery TxOutRef)
, currency :: AssetClass
}
deriving (Show, Eq, Generic, FromJSON, ToJSON, OpenApi.ToSchema)
-- | Response type for the utxo-{at-address|with-currency} endpoints.
data UtxosResponse = UtxosResponse
{ currentTip :: Tip
, page :: Page TxOutRef
}
deriving (Show, Eq, Generic, FromJSON, ToJSON, OpenApi.ToSchema)
-- | Response type for the is-utxo endpoint.
data IsUtxoResponse = IsUtxoResponse
{ currentTip :: Tip
, isUtxo :: Bool
}
deriving (Show, Eq, Generic, FromJSON, ToJSON, OpenApi.ToSchema)
data TxoAtAddressRequest = TxoAtAddressRequest
{ pageQuery :: Maybe (PageQuery TxOutRef)
, credential :: Credential
}
deriving (Show, Eq, Generic, FromJSON, ToJSON, OpenApi.ToSchema)
-- | Response type for the txo-at-address endpoint.
data TxosResponse = TxosResponse
{ paget :: Page TxOutRef
}
deriving (Show, Eq, Generic, FromJSON, ToJSON, OpenApi.ToSchema)
type API
= "healthcheck" :> Description "Is the server alive?" :> Get '[JSON] NoContent
:<|> "from-hash" :> FromHashAPI
:<|> "tx-out" :> Description "Get a transaction output from its reference." :> ReqBody '[JSON] TxOutRef :> Post '[JSON] ChainIndexTxOut
:<|> "unspent-tx-out" :> Description "Get a unspent transaction output from its reference." :> ReqBody '[JSON] TxOutRef :> Post '[JSON] ChainIndexTxOut
:<|> "tx" :> Description "Get a transaction from its id." :> ReqBody '[JSON] TxId :> Post '[JSON] ChainIndexTx
:<|> "is-utxo" :> Description "Check if the reference is an UTxO." :> ReqBody '[JSON] TxOutRef :> Post '[JSON] IsUtxoResponse
:<|> "utxo-at-address" :> Description "Get all UTxOs at an address." :> ReqBody '[JSON] UtxoAtAddressRequest :> Post '[JSON] UtxosResponse
:<|> "utxo-with-currency" :> Description "Get all UTxOs with a currency." :> ReqBody '[JSON] UtxoWithCurrencyRequest :> Post '[JSON] UtxosResponse
:<|> "txs" :> Description "Get transactions from a list of their ids." :> ReqBody '[JSON] [TxId] :> Post '[JSON] [ChainIndexTx]
:<|> "txo-at-address" :> Description "Get TxOs at an address." :> ReqBody '[JSON] TxoAtAddressRequest :> Post '[JSON] TxosResponse
:<|> "tip" :> Description "Get the current synced tip." :> Get '[JSON] Tip
:<|> "collect-garbage" :> Description "Collect chain index garbage to free up space." :> Put '[JSON] NoContent
:<|> "diagnostics" :> Description "Get the current stats of the chain index." :> Get '[JSON] Diagnostics
type FromHashAPI =
"datum" :> Description "Get a datum from its hash." :> ReqBody '[JSON] DatumHash :> Post '[JSON] Datum
:<|> "validator" :> Description "Get a validator script from its hash." :> ReqBody '[JSON] ValidatorHash :> Post '[JSON] Validator
:<|> "minting-policy" :> Description "Get a minting policy from its hash." :> ReqBody '[JSON] MintingPolicyHash :> Post '[JSON] MintingPolicy
:<|> "stake-validator" :> Description "Get a stake validator from its hash." :> ReqBody '[JSON] StakeValidatorHash :> Post '[JSON] StakeValidator
:<|> "redeemer" :> Description "Get a redeemer from its hash." :> ReqBody '[JSON] RedeemerHash :> Post '[JSON] Redeemer
type SwaggerAPI = "swagger" :> SwaggerSchemaUI "swagger-ui" "swagger.json"
swagger :: forall dir api. Servant.Server api ~ Servant.Handler Value => Servant.Server (SwaggerSchemaUI' dir api)
swagger = swaggerSchemaUIServer (toOpenApi (Proxy @API))
-- We don't include `SwaggerAPI` into `API` to exclude it from the effects code.
type FullAPI = API :<|> SwaggerAPI