/
Api.hs
123 lines (118 loc) · 4.17 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
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE TypeOperators #-}
module Plutus.ChainIndex.Api
( API
, FromHashAPI
, UtxoAtAddressRequest(..)
, UtxoWithCurrencyRequest(..)
) where
import Control.Monad.Freer.Extras.Pagination (Page, PageQuery)
import Data.Aeson (FromJSON, ToJSON)
import GHC.Generics (Generic)
import Ledger (AssetClass, Datum, DatumHash, MintingPolicy, MintingPolicyHash,
Redeemer, RedeemerHash, StakeValidator, StakeValidatorHash,
TxId, Validator, ValidatorHash)
import Ledger.Credential (Credential)
import Ledger.Tx (ChainIndexTxOut, TxOutRef)
import Plutus.ChainIndex.Tx (ChainIndexTx)
import Plutus.ChainIndex.Types (Diagnostics, Tip)
import Servant.API (Get, JSON, NoContent, Post, Put, ReqBody, (:<|>), (:>))
-- | 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)
-- | 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)
type API
= "healthcheck" :> Get '[JSON] NoContent
:<|> "from-hash" :> FromHashAPI
:<|> "tx-out" :> ReqBody '[JSON] TxOutRef :> Post '[JSON] ChainIndexTxOut
:<|> "tx" :> ReqBody '[JSON] TxId :> Post '[JSON] ChainIndexTx
:<|> "is-utxo" :> ReqBody '[JSON] TxOutRef :> Post '[JSON] (Tip, Bool)
:<|> "utxo-at-address" :> ReqBody '[JSON] UtxoAtAddressRequest :> Post '[JSON] (Tip, Page TxOutRef)
:<|> "utxo-with-currency" :> ReqBody '[JSON] UtxoWithCurrencyRequest :> Post '[JSON] (Tip, Page TxOutRef)
:<|> "tip" :> Get '[JSON] Tip
:<|> "collect-garbage" :> Put '[JSON] NoContent
:<|> "diagnostics" :> Get '[JSON] Diagnostics
type FromHashAPI =
"datum" :> ReqBody '[JSON] DatumHash :> Post '[JSON] Datum
:<|> "validator" :> ReqBody '[JSON] ValidatorHash :> Post '[JSON] Validator
:<|> "minting-policy" :> ReqBody '[JSON] MintingPolicyHash :> Post '[JSON] MintingPolicy
:<|> "stake-validator" :> ReqBody '[JSON] StakeValidatorHash :> Post '[JSON] StakeValidator
:<|> "redeemer" :> ReqBody '[JSON] RedeemerHash :> Post '[JSON] Redeemer