Skip to content

Commit a352f62

Browse files
author
euonymos
committed
chore: CEM.Indexing module
1 parent 8f80353 commit a352f62

File tree

14 files changed

+191
-181
lines changed

14 files changed

+191
-181
lines changed

cem-script.cabal

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,7 @@ library
149149
hs-source-dirs: src/
150150
exposed-modules:
151151
Cardano.CEM
152-
Cardano.CEM.Indexing.Event
153-
Cardano.CEM.Indexing.Oura
154-
Cardano.CEM.Indexing.Tx
152+
Cardano.CEM.Indexing
155153
Cardano.CEM.Testing.StateMachine
156154

157155
other-modules:
@@ -160,6 +158,9 @@ library
160158
Cardano.CEM.Documentation
161159
Cardano.CEM.DSL
162160
Cardano.CEM.DSLSmart
161+
Cardano.CEM.Indexing.Event
162+
Cardano.CEM.Indexing.Oura
163+
Cardano.CEM.Indexing.Tx
163164
Cardano.CEM.Monads
164165
Cardano.CEM.Monads.CLB
165166
Cardano.CEM.Monads.L1Commons

src/Cardano/CEM.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Cardano.CEM (
44

55
-- TODO: review
66

7-
import Cardano.CEM.Address as X (scriptCredential)
7+
import Cardano.CEM.Address as X (cemScriptPlutusCredential)
88
import Cardano.CEM.Compile as X
99
import Cardano.CEM.DSL as X (
1010
CEMScript (..),

src/Cardano/CEM/Address.hs

Lines changed: 59 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,62 @@
11
module Cardano.CEM.Address (
2-
AddressBech32 (MkAddressBech32, unAddressBech32),
3-
cardanoAddressBech32,
4-
scriptCredential,
5-
scriptCardanoAddress,
62
cemScriptAddress,
3+
cemScriptPlutusCredential,
4+
cemScriptPlutusAddress,
75
plutusAddressToShelleyAddress,
86
) where
97

10-
import Cardano.Api qualified
11-
import Cardano.Api.Address qualified
12-
import Cardano.Api.Ledger qualified
8+
import Cardano.Api qualified as C
9+
import Cardano.Api.Address qualified as C (Address (..))
10+
import Cardano.Api.Ledger qualified as C
1311
import Cardano.CEM.OnChain (CEMScriptCompiled (cemScriptCompiled))
14-
import Cardano.CEM.OnChain qualified as Compiled
15-
import Cardano.Crypto.Hash qualified as Cardano.Hash
16-
import Cardano.Ledger.BaseTypes qualified as Ledger
17-
import Cardano.Ledger.Credential qualified as Cred
18-
import Cardano.Ledger.Hashes qualified
19-
import Cardano.Ledger.Keys qualified as Ledger.Keys
12+
import Cardano.Crypto.Hash qualified as Crypto
13+
import Cardano.Ledger.BaseTypes qualified as L
14+
import Cardano.Ledger.Credential qualified as L
15+
import Cardano.Ledger.Hashes qualified as L
16+
import Cardano.Ledger.Keys qualified as L
2017
import Data.Proxy (Proxy)
21-
import Data.String (IsString)
22-
import Data.Text qualified as T
2318
import Plutarch.LedgerApi (scriptHash)
2419
import Plutarch.Script (serialiseScript)
2520
import Plutus.Extras (scriptValidatorHash)
26-
import PlutusLedgerApi.V1 qualified
27-
import PlutusLedgerApi.V1.Address (Address, scriptHashAddress)
21+
import PlutusLedgerApi.V1 qualified as P
22+
import PlutusLedgerApi.V1.Address qualified as P (scriptHashAddress)
2823
import Prelude
2924

30-
newtype AddressBech32 = MkAddressBech32 {unAddressBech32 :: T.Text}
31-
deriving newtype (Eq, Show, IsString)
32-
33-
cardanoAddressBech32 :: Cardano.Api.Address Cardano.Api.ShelleyAddr -> AddressBech32
34-
cardanoAddressBech32 = MkAddressBech32 . Cardano.Api.serialiseToBech32
35-
36-
{-# INLINEABLE cemScriptAddress #-}
3725
cemScriptAddress ::
38-
forall script. (CEMScriptCompiled script) => Proxy script -> Address
39-
cemScriptAddress =
40-
scriptHashAddress . scriptValidatorHash . serialiseScript . cemScriptCompiled
41-
42-
scriptCardanoAddress ::
4326
forall script.
44-
(Compiled.CEMScriptCompiled script) =>
45-
Cardano.Api.Ledger.Network ->
27+
(CEMScriptCompiled script) =>
28+
C.Network ->
4629
Proxy script ->
47-
Either String (Cardano.Api.Address Cardano.Api.ShelleyAddr)
48-
scriptCardanoAddress network =
30+
Either String (C.Address C.ShelleyAddr)
31+
cemScriptAddress network =
4932
plutusAddressToShelleyAddress network
50-
. flip PlutusLedgerApi.V1.Address Nothing
51-
. scriptCredential
33+
. flip P.Address Nothing
34+
. cemScriptPlutusCredential
35+
36+
{-# INLINEABLE cemScriptPlutusAddress #-}
37+
cemScriptPlutusAddress ::
38+
forall script. (CEMScriptCompiled script) => Proxy script -> P.Address
39+
cemScriptPlutusAddress =
40+
P.scriptHashAddress
41+
. scriptValidatorHash
42+
. serialiseScript
43+
. cemScriptCompiled
5244

53-
scriptCredential ::
45+
cemScriptPlutusCredential ::
5446
forall script.
55-
(Compiled.CEMScriptCompiled script) =>
47+
(CEMScriptCompiled script) =>
5648
Proxy script ->
57-
PlutusLedgerApi.V1.Credential
58-
scriptCredential =
59-
PlutusLedgerApi.V1.ScriptCredential
49+
P.Credential
50+
cemScriptPlutusCredential =
51+
P.ScriptCredential
6052
. scriptHash
61-
. Compiled.cemScriptCompiled
53+
. cemScriptCompiled
6254

6355
plutusAddressToShelleyAddress ::
64-
Cardano.Api.Ledger.Network ->
65-
PlutusLedgerApi.V1.Address ->
66-
Either String (Cardano.Api.Address Cardano.Api.ShelleyAddr)
67-
plutusAddressToShelleyAddress network (PlutusLedgerApi.V1.Address payment stake) = do
56+
L.Network ->
57+
P.Address ->
58+
Either String (C.Address C.ShelleyAddr)
59+
plutusAddressToShelleyAddress network (P.Address payment stake) = do
6860
paymentCred <-
6961
maybe
7062
(Left "plutusAddressToShelleyAddress:can't decode payment credential")
@@ -75,36 +67,36 @@ plutusAddressToShelleyAddress network (PlutusLedgerApi.V1.Address payment stake)
7567
(Left "plutusAddressToShelleyAddress:can't decode stake credential")
7668
Right
7769
stakeCredential
78-
pure $ Cardano.Api.Address.ShelleyAddress network paymentCred stakeCred
70+
pure $ C.ShelleyAddress network paymentCred stakeCred
7971
where
8072
credentialToCardano
81-
( PlutusLedgerApi.V1.PubKeyCredential
82-
(PlutusLedgerApi.V1.PubKeyHash pkh)
73+
( P.PubKeyCredential
74+
(P.PubKeyHash pkh)
8375
) =
84-
Cred.KeyHashObj
85-
. Ledger.Keys.KeyHash
86-
<$> Cardano.Hash.hashFromBytes
87-
(PlutusLedgerApi.V1.fromBuiltin pkh)
76+
L.KeyHashObj
77+
. L.KeyHash
78+
<$> Crypto.hashFromBytes
79+
(P.fromBuiltin pkh)
8880
credentialToCardano
89-
( PlutusLedgerApi.V1.ScriptCredential
90-
(PlutusLedgerApi.V1.ScriptHash hash)
81+
( P.ScriptCredential
82+
(P.ScriptHash hash')
9183
) =
92-
Cred.ScriptHashObj
93-
. Cardano.Ledger.Hashes.ScriptHash
94-
<$> Cardano.Hash.hashFromBytes
95-
(PlutusLedgerApi.V1.fromBuiltin hash)
84+
L.ScriptHashObj
85+
. L.ScriptHash
86+
<$> Crypto.hashFromBytes
87+
(P.fromBuiltin hash')
9688

9789
paymentCredential = credentialToCardano payment
9890
stakeCredential = case stake of
99-
Nothing -> Just Cardano.Api.Ledger.StakeRefNull
91+
Nothing -> Just L.StakeRefNull
10092
Just ref -> case ref of
101-
PlutusLedgerApi.V1.StakingHash cred ->
102-
Cardano.Api.Ledger.StakeRefBase
93+
P.StakingHash cred ->
94+
L.StakeRefBase
10395
<$> credentialToCardano cred
104-
PlutusLedgerApi.V1.StakingPtr slotNo txIx sertId ->
96+
P.StakingPtr slotNo txIx sertId ->
10597
Just $
106-
Cardano.Api.Ledger.StakeRefPtr $
107-
Cred.Ptr
108-
(Ledger.SlotNo $ fromInteger slotNo)
109-
(Ledger.TxIx $ fromInteger txIx)
110-
(Ledger.CertIx $ fromInteger sertId)
98+
L.StakeRefPtr $
99+
L.Ptr
100+
(L.SlotNo $ fromInteger slotNo)
101+
(L.TxIx $ fromInteger txIx)
102+
(L.CertIx $ fromInteger sertId)

src/Cardano/CEM/Indexing.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Cardano.CEM.Indexing
2+
( module X
3+
) where
4+
5+
import Cardano.CEM.Indexing.Event as X
6+
import Cardano.CEM.Indexing.Oura as X
7+
import Cardano.CEM.Indexing.Tx as X

src/Cardano/CEM/Indexing/Event.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
22

33
-- | Indexer events, i.e. indexer outputs.
4-
module Cardano.CEM.Indexing.Event where
4+
module Cardano.CEM.Indexing.Event (
5+
IndexerEvent (..),
6+
extractEvent,
7+
) where
58

69
import Cardano.Api qualified as C
710
import Cardano.Api.ScriptData qualified as C
@@ -63,7 +66,7 @@ deriving stock instance
6366
(Eq (Spine (Transition script))) =>
6467
(Eq (IndexerEvent script))
6568

66-
{- | The core function, that extracts an Event out of a Oura transaction.
69+
{- | The core function, that extracts an 'Event' out of a Oura transaction.
6770
It might be a pure function, IO here was used mostly to simplify debugging
6871
during its development.
6972
-}
@@ -77,7 +80,7 @@ extractEvent ::
7780
IO (Maybe (IndexerEvent script))
7881
extractEvent network tx = do
7982
-- Script payment credential based predicate
80-
let ~(Right scriptAddr) = Address.scriptCardanoAddress network (Proxy @script)
83+
let ~(Right scriptAddr) = Address.cemScriptAddress network (Proxy @script)
8184
let cPred = hasAddr scriptAddr
8285

8386
-- Source state

src/Cardano/CEM/Indexing/Oura.hs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{- | CEM provides the building blocks to build an indexer for your dApp.
22
Current implementation is based on Oura. This module provides tools to
3-
run Oura.
3+
run Oura daemon, most important being function to buil a config for Oura
4+
for a particular CEM Script.
45
-}
56
module Cardano.CEM.Indexing.Oura (
67
SourcePath (MkSourcePath, unSourcePath),
@@ -10,9 +11,14 @@ module Cardano.CEM.Indexing.Oura (
1011
selectByAddress,
1112
ouraMonitoringScript,
1213
configToText,
14+
15+
-- * Addresses
16+
AddressBech32 (MkAddressBech32, unAddressBech32),
17+
cardanoAddressBech32,
1318
) where
1419

15-
import Cardano.CEM.Address qualified as Address
20+
import Cardano.Api qualified as C
21+
import Cardano.CEM.Address (cemScriptAddress)
1622
import Cardano.CEM.OnChain (CEMScriptCompiled)
1723
import Cardano.Ledger.BaseTypes qualified as Ledger
1824
import Data.Data (Proxy)
@@ -45,8 +51,8 @@ daemonConfig filters sourcePath sinkPath =
4551
]
4652

4753
-- | A oura *filter* that selects by address
48-
selectByAddress :: Address.AddressBech32 -> Filter
49-
selectByAddress (Address.MkAddressBech32 addressBech32) =
54+
selectByAddress :: AddressBech32 -> Filter
55+
selectByAddress (MkAddressBech32 addressBech32) =
5056
MkFilter $
5157
Toml.ToValue.table
5258
[ "predicate" .= Toml.Text addressBech32 -- "addr1qx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3n0d3vllmyqwsx5wktcd8cc3sq835lu7drv2xwl2wywfgse35a3x"
@@ -67,8 +73,8 @@ ouraMonitoringScript p network sourcePath sinkPath =
6773
(\filters -> daemonConfig filters sourcePath sinkPath)
6874
. pure
6975
. selectByAddress
70-
. Address.cardanoAddressBech32
71-
<$> Address.scriptCardanoAddress network p
76+
. cardanoAddressBech32
77+
<$> cemScriptAddress network p
7278

7379
cursor :: Toml.Table
7480
cursor =
@@ -108,3 +114,9 @@ source (MkSourcePath socketPath) =
108114

109115
configToText :: Table -> T.Text
110116
configToText = T.pack . show . Toml.Pretty.prettyToml
117+
118+
newtype AddressBech32 = MkAddressBech32 {unAddressBech32 :: T.Text}
119+
deriving newtype (Eq, Show, IsString)
120+
121+
cardanoAddressBech32 :: C.Address C.ShelleyAddr -> AddressBech32
122+
cardanoAddressBech32 = MkAddressBech32 . C.serialiseToBech32

0 commit comments

Comments
 (0)