Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
-- | Cardano Scripts endpoints | ||
|
||
{-# OPTIONS_HADDOCK hide #-} | ||
|
||
module Blockfrost.API.Cardano.Scripts | ||
where | ||
|
||
import Servant.API | ||
import Servant.API.Generic | ||
|
||
import Blockfrost.Types.Cardano.Scripts | ||
import Blockfrost.Types.Shared | ||
import Blockfrost.Util.Pagination | ||
import Blockfrost.Util.Sorting | ||
|
||
data ScriptsAPI route = | ||
ScriptsAPI | ||
{ | ||
_listScripts | ||
:: route | ||
:- Summary "Scripts" | ||
:> Description "List of scripts." | ||
:> Pagination | ||
:> Sorting | ||
:> Get '[JSON] [ScriptHash] | ||
, _getScript | ||
:: route | ||
:- Summary "Specific scripts" | ||
:> Description "Information about a specific script." | ||
:> Capture "script_hash" ScriptHash | ||
:> Get '[JSON] Script | ||
, _getScriptRedeemers | ||
:: route | ||
:- Summary "Redeemers of a specific script" | ||
:> Description "List of redeemers of a specific script." | ||
:> Capture "script_hash" ScriptHash | ||
:> Pagination | ||
:> Sorting | ||
:> "redeemers" | ||
:> Get '[JSON] [ScriptRedeemer] | ||
} deriving (Generic) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
-- | Cardano Scripts responses | ||
|
||
module Blockfrost.Types.Cardano.Scripts | ||
( Script (..) | ||
, ScriptType (..) | ||
, ScriptRedeemer (..) | ||
) where | ||
|
||
import Deriving.Aeson | ||
import Servant.Docs (ToSample (..), samples, singleSample) | ||
|
||
import Blockfrost.Types.Shared | ||
import Blockfrost.Types.Cardano.Transactions (ValidationPurpose(Spend)) | ||
|
||
-- | Script type | ||
data ScriptType = Plutus | Timelock | ||
deriving stock (Show, Eq, Generic) | ||
deriving (FromJSON, ToJSON) | ||
via CustomJSON '[ConstructorTagModifier '[ToLower]] ScriptType | ||
|
||
instance ToSample ScriptType where | ||
toSamples = pure $ samples [ Plutus, Timelock ] | ||
|
||
-- | Script info | ||
data Script = Script | ||
{ _scriptScriptHash :: ScriptHash -- ^ Hash of the script | ||
, _scriptType :: ScriptType -- ^ Type of the script language | ||
, _scriptSerialisedSize :: Maybe Integer -- ^ The size of the CBOR serialised script, if a Plutus script | ||
} | ||
deriving stock (Show, Eq, Generic) | ||
deriving (FromJSON, ToJSON) | ||
via CustomJSON '[FieldLabelModifier '[StripPrefix "_script", CamelToSnake]] Script | ||
|
||
instance ToSample Script where | ||
toSamples = pure $ singleSample | ||
Script | ||
{ _scriptScriptHash = "67f33146617a5e61936081db3b2117cbf59bd2123748f58ac9678656" | ||
, _scriptType = Plutus | ||
, _scriptSerialisedSize = Just 3119 | ||
} | ||
|
||
-- | Script redeemer | ||
data ScriptRedeemer = ScriptRedeemer | ||
{ _scriptRedeemerTxHash :: TxHash -- ^ Hash of the transaction | ||
, _scriptRedeemerTxIndex :: Integer -- ^ Index of the redeemer within a transaction | ||
, _scriptRedeemerPurpose :: ValidationPurpose -- ^ Validation purpose | ||
, _scriptRedeemerUnitMem :: Quantity -- ^ The budget in Memory to run a script | ||
, _scriptRedeemerUnitSteps :: Quantity -- ^ The budget in Steps to run a script | ||
, _scriptRedeemerFee :: Lovelaces -- ^ The fee consumed to run the script | ||
} | ||
deriving stock (Show, Eq, Generic) | ||
deriving (FromJSON, ToJSON) | ||
via CustomJSON '[FieldLabelModifier '[StripPrefix "_scriptRedeemer", CamelToSnake]] ScriptRedeemer | ||
|
||
instance ToSample ScriptRedeemer where | ||
toSamples = pure $ singleSample | ||
ScriptRedeemer | ||
{ _scriptRedeemerTxHash = "1a0570af966fb355a7160e4f82d5a80b8681b7955f5d44bec0dce628516157f0" | ||
, _scriptRedeemerTxIndex = 0 | ||
, _scriptRedeemerPurpose = Spend | ||
, _scriptRedeemerUnitMem = 1700 | ||
, _scriptRedeemerUnitSteps = 476468 | ||
, _scriptRedeemerFee = 172033 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
-- | Script Hash newtype | ||
|
||
module Blockfrost.Types.Shared.ScriptHash | ||
( ScriptHash (..) | ||
) where | ||
|
||
import Data.Aeson (FromJSON (..), ToJSON (..), Value(..), (.=), (.:)) | ||
import Data.String (IsString (..)) | ||
import Data.Text (Text) | ||
import qualified Data.Text | ||
import qualified Data.Vector | ||
import GHC.Generics | ||
import Servant.API (Capture, FromHttpApiData (..), ToHttpApiData (..)) | ||
import Servant.Docs (DocCapture (..), ToCapture (..), ToSample (..), samples) | ||
|
||
-- | Id (hash) of the transaction | ||
newtype ScriptHash = ScriptHash { unScriptHash :: Text } | ||
deriving stock (Show, Eq, Generic) | ||
deriving newtype (FromHttpApiData, ToHttpApiData) | ||
|
||
instance IsString ScriptHash where | ||
fromString = ScriptHash . Data.Text.pack | ||
|
||
instance ToJSON ScriptHash where | ||
toJSON = toJSON . unScriptHash | ||
toEncoding = toEncoding . unScriptHash | ||
instance FromJSON ScriptHash where | ||
parseJSON = fmap ScriptHash <$> parseJSON | ||
|
||
-- Custom instance for list used by script list endpoint | ||
instance {-# OVERLAPS #-} ToJSON [ScriptHash] where | ||
toJSON = Array . Data.Vector.fromList . map (\sh -> Object ("script_hash" .= (toJSON . unScriptHash $ sh))) | ||
instance {-# OVERLAPS #-} FromJSON [ScriptHash] where | ||
parseJSON (Array a) = mapM parseJSON' (Data.Vector.toList a) | ||
where | ||
parseJSON' (Object b) = b .: "script_hash" | ||
parseJSON' _ = fail "Unexpected type for ScriptHash" | ||
parseJSON _ = fail "Expected array for [ScriptHash]" | ||
|
||
instance ToSample ScriptHash where | ||
toSamples _ = samples $ map ScriptHash | ||
[ "67f33146617a5e61936081db3b2117cbf59bd2123748f58ac9678656" | ||
, "e1457a0c47dfb7a2f6b8fbb059bdceab163c05d34f195b87b9f2b30e" | ||
] | ||
|
||
instance ToCapture (Capture "script_hash" ScriptHash) where | ||
toCapture _ = DocCapture "script_hash" "Hash of the script." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
{-# LANGUAGE NumericUnderscores #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE QuasiQuotes #-} | ||
{-# LANGUAGE TemplateHaskell #-} | ||
|
||
module Cardano.Scripts | ||
where | ||
|
||
import Data.Aeson (decode, eitherDecode, encode) | ||
import Data.Text (Text) | ||
import qualified Money | ||
import Test.Hspec | ||
import Test.Tasty.Hspec | ||
import Text.RawString.QQ | ||
|
||
import Blockfrost.Types | ||
|
||
spec_scripts :: Spec | ||
spec_scripts = do | ||
it "parses script list sample" $ do | ||
eitherDecode scriptListSample | ||
`shouldBe` | ||
Right scriptListExpected | ||
|
||
it "parses script info sample" $ do | ||
eitherDecode scriptSample | ||
`shouldBe` | ||
Right scriptSampleExpected | ||
|
||
it "parses script redeemer sample" $ do | ||
eitherDecode scriptRedeemerSample | ||
`shouldBe` | ||
Right scriptRedeemerExpected | ||
|
||
scriptListSample = [r| | ||
[ | ||
{ | ||
"script_hash": "67f33146617a5e61936081db3b2117cbf59bd2123748f58ac9678656" | ||
}, | ||
{ | ||
"script_hash": "e1457a0c47dfb7a2f6b8fbb059bdceab163c05d34f195b87b9f2b30e" | ||
}, | ||
{ | ||
"script_hash": "a6e63c0ff05c96943d1cc30bf53112ffff0f34b45986021ca058ec54" | ||
} | ||
] | ||
|] | ||
|
||
scriptListExpected :: [ScriptHash] | ||
scriptListExpected = | ||
[ "67f33146617a5e61936081db3b2117cbf59bd2123748f58ac9678656" | ||
, "e1457a0c47dfb7a2f6b8fbb059bdceab163c05d34f195b87b9f2b30e" | ||
, "a6e63c0ff05c96943d1cc30bf53112ffff0f34b45986021ca058ec54" | ||
] | ||
|
||
scriptSample = [r| | ||
{ | ||
"script_hash": "67f33146617a5e61936081db3b2117cbf59bd2123748f58ac9678656", | ||
"type": "plutus", | ||
"serialised_size": 3119 | ||
} | ||
|] | ||
|
||
scriptSampleExpected = | ||
Script | ||
{ _scriptScriptHash = "67f33146617a5e61936081db3b2117cbf59bd2123748f58ac9678656" | ||
, _scriptType = Plutus | ||
, _scriptSerialisedSize = Just 3119 | ||
} | ||
|
||
scriptRedeemerSample = [r| | ||
{ | ||
"tx_hash": "1a0570af966fb355a7160e4f82d5a80b8681b7955f5d44bec0dce628516157f0", | ||
"tx_index": 0, | ||
"purpose": "spend", | ||
"unit_mem": "1700", | ||
"unit_steps": "476468", | ||
"fee": "172033" | ||
} | ||
|] | ||
|
||
scriptRedeemerExpected = | ||
ScriptRedeemer | ||
{ _scriptRedeemerTxHash = "1a0570af966fb355a7160e4f82d5a80b8681b7955f5d44bec0dce628516157f0" | ||
, _scriptRedeemerTxIndex = 0 | ||
, _scriptRedeemerPurpose = Spend | ||
, _scriptRedeemerUnitMem = 1700 | ||
, _scriptRedeemerUnitSteps = 476468 | ||
, _scriptRedeemerFee = 172033 | ||
} |