Skip to content

Commit

Permalink
JSON representation of CBOR data
Browse files Browse the repository at this point in the history
  • Loading branch information
berewt committed Jun 2, 2023
1 parent 8acce35 commit 18cc5c8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 40 deletions.
18 changes: 6 additions & 12 deletions marconi-chain-index/src/Marconi/ChainIndex/Indexers/MintBurn.hs
Expand Up @@ -107,16 +107,10 @@ txbMints txb = case txb of
C.ShelleyTxBody era shelleyTx _ _ _ _ -> case era of
C.ShelleyBasedEraShelley -> []
C.ShelleyBasedEraAllegra -> []
C.ShelleyBasedEraMary -> []
C.ShelleyBasedEraAlonzo -> do
(policyId, assetName, quantity, index', redeemer) <- getPolicyData txb $ LA.atbMint shelleyTx
pure $ MintAsset policyId assetName quantity index' redeemer
C.ShelleyBasedEraBabbage -> do
(policyId, assetName, quantity, index', redeemer) <- getPolicyData txb $ LB.btbMint shelleyTx
pure $ MintAsset policyId assetName quantity index' redeemer
C.ShelleyBasedEraConway -> do
(policyId, assetName, quantity, index', redeemer) <- getPolicyData txb $ LC.ctbMint shelleyTx
pure $ MintAsset policyId assetName quantity index' redeemer
C.ShelleyBasedEraMary -> []
C.ShelleyBasedEraAlonzo -> getPolicyData txb $ LA.atbMint shelleyTx
C.ShelleyBasedEraBabbage -> getPolicyData txb $ LB.btbMint shelleyTx
C.ShelleyBasedEraConway -> getPolicyData txb $ LC.ctbMint shelleyTx
_byronTxBody -> [] -- ByronTxBody is not exported but as it's the only other data constructor then _ matches it.

-- * Helpers
Expand All @@ -125,14 +119,14 @@ getPolicyData
:: forall era. Ledger.Era (C.ShelleyLedgerEra era)
=> C.TxBody era
-> LM.MultiAsset OEra.StandardCrypto
-> [(C.PolicyId, C.AssetName, C.Quantity, Word64, C.ScriptData)]
-> [MintAsset]
getPolicyData txb (LM.MultiAsset m) = do
let
policyIdList = Map.toList m
getPolicyId index' = policyIdList !! fromIntegral index'
((maryPolicyID, assets), index'', (redeemer, _)) <- map (\(index', data_) -> (getPolicyId index', index', data_)) mintRedeemers
(assetName, quantity) :: (LM.AssetName, Integer) <- Map.toList assets
pure (fromMaryPolicyID maryPolicyID, fromMaryAssetName assetName, C.Quantity quantity, index'', fromAlonzoData redeemer)
pure $ MintAsset (fromMaryPolicyID maryPolicyID) (fromMaryAssetName assetName) (C.Quantity quantity) index'' (fromAlonzoData redeemer)
where
mintRedeemers = txRedeemers txb
& Map.toList
Expand Down
11 changes: 3 additions & 8 deletions marconi-chain-index/src/Marconi/ChainIndex/Orphans.hs
Expand Up @@ -14,12 +14,10 @@ import Codec.CBOR.Read qualified as CBOR
import Codec.Serialise (Serialise (decode, encode))
import Data.Aeson (FromJSON, ToJSON)
import Data.Aeson qualified as Aeson
import Data.ByteString.Base16 qualified as Base16
import Data.ByteString.Lazy (toStrict)
import Data.Coerce (coerce)
import Data.Proxy (Proxy (Proxy))
import Data.SOP.Strict (K (K), NP (Nil, (:*)), fn, type (:.:) (Comp))
import Data.Text.Encoding qualified as Text
import Database.SQLite.Simple qualified as SQL
import Database.SQLite.Simple.FromField qualified as SQL
import Database.SQLite.Simple.FromRow (FromRow (fromRow))
Expand Down Expand Up @@ -137,18 +135,15 @@ instance SQL.ToField C.ScriptData where
toField = SQL.SQLBlob . C.serialiseToCBOR

instance FromJSON C.ScriptData where
parseJSON (Aeson.String v) =
either (const mempty) pure $ do
base16Val <- Base16.decode $ Text.encodeUtf8 v
mapLeft show $ C.deserialiseFromCBOR C.AsScriptData base16Val
parseJSON _ = mempty
parseJSON = either (fail . show) (pure . C.getScriptData)
. C.scriptDataFromJson C.ScriptDataJsonDetailedSchema

mapLeft :: (a -> b) -> Either a c -> Either b c
mapLeft f (Left v) = Left $ f v
mapLeft _ (Right v) = Right v

instance ToJSON C.ScriptData where
toJSON v = Aeson.String $ Text.decodeLatin1 $ Base16.encode $ C.serialiseToCBOR v
toJSON = C.scriptDataToJson C.ScriptDataJsonDetailedSchema . C.unsafeHashableScriptData

-- * C.TxIn

Expand Down
Expand Up @@ -12,8 +12,10 @@
{
"address": "addr_test1vpfwv0ezc5g8a4mkku8hhy3y3vp92t7s3ul8g778g5yegsgalc6gc",
"blockHeaderHash": "6161616161616161616161616161616161616161616161616161616161616161",
"datum": "34",
"datumHash": "eb8649214997574e20c464388a172420d25403682bbbb80c496831c8cc1f8f0d",
"datum": {
"int": 34
},
"datumHash": "6d683f9bd0c3b874cdf3da1793b5eb0ea73a074d3e4b66bc62279b09d387fa8d",
"slotNo": 1,
"spentBy": {
"slot": 12,
Expand Down
Expand Up @@ -3,8 +3,10 @@
"blockHeaderHash": "6161616161616161616161616161616161616161616161616161616161616161",
"blockNo": 1047,
"quantity": -10,
"redeemer": "34",
"redeemerHash": "eb8649214997574e20c464388a172420d25403682bbbb80c496831c8cc1f8f0d",
"redeemer": {
"int": 34
},
"redeemerHash": "6d683f9bd0c3b874cdf3da1793b5eb0ea73a074d3e4b66bc62279b09d387fa8d",
"slotNo": 1,
"txId": "ec7d3bd7c6a3a31368093b077af0db46ceac77956999eb842373e08c6420f000",
"txIx": 0
Expand All @@ -13,8 +15,10 @@
"blockHeaderHash": "6161616161616161616161616161616161616161616161616161616161616161",
"blockNo": 1047,
"quantity": 10,
"redeemer": "34",
"redeemerHash": "eb8649214997574e20c464388a172420d25403682bbbb80c496831c8cc1f8f0d",
"redeemer": {
"int": 34
},
"redeemerHash": "6d683f9bd0c3b874cdf3da1793b5eb0ea73a074d3e4b66bc62279b09d387fa8d",
"slotNo": 1,
"txId": "ec7d3bd7c6a3a31368093b077af0db46ceac77956999eb842373e08c6420f000",
"txIx": 1
Expand Down
16 changes: 2 additions & 14 deletions marconi-sidechain/test/Spec/Marconi/Sidechain/Routes.hs
Expand Up @@ -190,13 +190,7 @@ goldenAddressUtxoResult = do
pure
$ C.deserialiseFromBech32 (C.AsAddress C.AsShelleyAddr) addressBech32

let datumCbor = "4"
datum <-
either
(error . show)
pure
$ C.deserialiseFromCBOR C.AsScriptData datumCbor

let datum = C.ScriptDataNumber 34
let txIdRawBytes = "ec7d3bd7c6a3a31368093b077af0db46ceac77956999eb842373e08c6420f000"
txId <-
either
Expand Down Expand Up @@ -241,13 +235,7 @@ goldenAddressUtxoResult = do

goldenMintingPolicyHashTxResult :: IO ByteString
goldenMintingPolicyHashTxResult = do
let redeemerCbor = "4"
redeemerData <-
either
(error . show)
pure
$ C.deserialiseFromCBOR C.AsScriptData redeemerCbor

let redeemerData = C.ScriptDataNumber 34
let txIdRawBytes = "ec7d3bd7c6a3a31368093b077af0db46ceac77956999eb842373e08c6420f000"
txId <-
either
Expand Down

0 comments on commit 18cc5c8

Please sign in to comment.