Skip to content

Commit

Permalink
feat: parse transition spines from transactions WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
euonymos committed Nov 30, 2024
1 parent baee6d0 commit e3c5c63
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 64 deletions.
2 changes: 2 additions & 0 deletions src/Cardano/CEM/Examples/Auction.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# OPTIONS_GHC -Wno-unrecognised-pragmas -ddump-splices #-}
{-# LANGUAGE NoPolyKinds #-}

module Cardano.CEM.Examples.Auction where
Expand Down Expand Up @@ -36,6 +37,7 @@ data SimpleAuctionStageParams
| CanCloseAt POSIXTime
deriving stock (Prelude.Eq, Prelude.Show)


instance Stages SimpleAuctionStage where
type StageParams SimpleAuctionStage = SimpleAuctionStageParams
stageToOnChainInterval NoControl _ = Interval.always
Expand Down
1 change: 1 addition & 0 deletions test/Oura.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{- TODO: move to the Indexing folder? -}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE OverloadedRecordDot #-}

Expand Down
129 changes: 65 additions & 64 deletions test/Oura/Config.hs
Original file line number Diff line number Diff line change
@@ -1,81 +1,82 @@
{- FIXME: remove -}
{-# LANGUAGE BlockArguments #-}

module Oura.Config (
filtersL,
predicateL,
tableL,
atKey,
_Table,
_Integer,
_Bool,
_Text,
-- filtersL,
-- predicateL,
-- tableL,
-- atKey,
-- _Table,
-- _Integer,
-- _Bool,
-- _Text,
) where

import Prelude
-- import Prelude

import Cardano.CEM.Indexing qualified as Config
import Control.Lens (
At (at),
Each (each),
Iso',
Lens',
Prism',
Traversal',
from,
iso,
mapping,
partsOf,
prism',
_Just,
)
import Data.Map (Map)
import Data.Text qualified as T
import Toml qualified
-- import Cardano.CEM.Indexing qualified as Config
-- import Control.Lens (
-- At (at),
-- Each (each),
-- Iso',
-- Lens',
-- Prism',
-- Traversal',
-- from,
-- iso,
-- mapping,
-- partsOf,
-- prism',
-- _Just,
-- )
-- import Data.Map (Map)
-- import Data.Text qualified as T
-- import Toml qualified

-- * Config
-- -- * Config

filterL :: Iso' Config.Filter Toml.Table
filterL = iso Config.unFilter Config.MkFilter
-- filterL :: Iso' Config.Filter Toml.Table
-- filterL = iso Config.unFilter Config.MkFilter

predicateL :: Traversal' Config.Filter T.Text
predicateL = filterL . atKey "predicate" . _Just . _Text
-- predicateL :: Traversal' Config.Filter T.Text
-- predicateL = filterL . atKey "predicate" . _Just . _Text

filtersL :: Traversal' Toml.Table [Config.Filter]
filtersL =
atKey "filters"
. _Just
. _List
. partsOf (each . _Table . from filterL)
-- filtersL :: Traversal' Toml.Table [Config.Filter]
-- filtersL =
-- atKey "filters"
-- . _Just
-- . _List
-- . partsOf (each . _Table . from filterL)

atKey :: T.Text -> Traversal' Toml.Table (Maybe Toml.Value)
atKey key = tableL . at key
-- atKey :: T.Text -> Traversal' Toml.Table (Maybe Toml.Value)
-- atKey key = tableL . at key

tableL :: Lens' Toml.Table (Map T.Text Toml.Value)
tableL =
iso (\(Toml.MkTable t) -> t) Toml.MkTable
. mapping (iso snd ((),))
-- tableL :: Lens' Toml.Table (Map T.Text Toml.Value)
-- tableL =
-- iso (\(Toml.MkTable t) -> t) Toml.MkTable
-- . mapping (iso snd ((),))

_Table :: Prism' Toml.Value Toml.Table
_Table = prism' Toml.Table \case
Toml.Table table -> Just table
_ -> Nothing
-- _Table :: Prism' Toml.Value Toml.Table
-- _Table = prism' Toml.Table \case
-- Toml.Table table -> Just table
-- _ -> Nothing

_Text :: Prism' Toml.Value T.Text
_Text = prism' Toml.Text \case
Toml.Text t -> Just t
_ -> Nothing
-- _Text :: Prism' Toml.Value T.Text
-- _Text = prism' Toml.Text \case
-- Toml.Text t -> Just t
-- _ -> Nothing

_List :: Prism' Toml.Value [Toml.Value]
_List = prism' Toml.List \case
Toml.List xs -> Just xs
_ -> Nothing
-- _List :: Prism' Toml.Value [Toml.Value]
-- _List = prism' Toml.List \case
-- Toml.List xs -> Just xs
-- _ -> Nothing

_Bool :: Prism' Toml.Value Bool
_Bool = prism' Toml.Bool \case
Toml.Bool b -> Just b
_ -> Nothing
-- _Bool :: Prism' Toml.Value Bool
-- _Bool = prism' Toml.Bool \case
-- Toml.Bool b -> Just b
-- _ -> Nothing

_Integer :: Prism' Toml.Value Integer
_Integer = prism' Toml.Integer \case
Toml.Integer n -> Just n
_ -> Nothing
-- _Integer :: Prism' Toml.Value Integer
-- _Integer = prism' Toml.Integer \case
-- Toml.Integer n -> Just n
-- _ -> Nothing
29 changes: 29 additions & 0 deletions test/OuraFilters/Mock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Data.ByteString.Base16 qualified as Base16
import Data.ByteString.Base64 qualified as Base64
import Data.ByteString.Lazy qualified as LBS
import Data.Functor ((<&>))
import Data.Map.Strict qualified as Map
import Data.Text qualified as T
import Data.Vector qualified as Vec
import GHC.Generics (Generic (Rep))
Expand All @@ -29,6 +30,11 @@ import PlutusLedgerApi.V1 qualified
import Safe qualified
import Utils (digits)
import Prelude
import Cardano.CEM (Transition, CEMScript, transitionStage, State)
import Data.Data (Proxy(Proxy))
import Data.Spine (getSpine, Spine)
import Data.Tuple (swap)
import Data.Bifunctor (first)

newtype WithoutUnderscore a = MkWithoutUnderscore a
deriving newtype (Generic)
Expand Down Expand Up @@ -261,6 +267,28 @@ data Tx = MkTx
makeLenses ''Tx
makeLensesFor [("collateral", "txCollateral")] ''Tx


-- ---

type Event script = Spine (Transition script)


extractEvent :: forall script. (CEMScript script) => Tx -> Maybe (Event script)
extractEvent tx = do
let mOwnInput :: Maybe TxInput = undefined
let mSourceState :: Maybe (State script) = _ mOwnInput
let mSourceSpine :: Maybe (Spine (State script)) = getSpine <$> mSourceState

let mOwnOutput :: Maybe TxInput = undefined
let mTargetState :: Maybe (State script) = _ mOwnInput
let mSourceSpine :: Maybe (Spine (State script)) = getSpine <$> mSourceState

let transitions = first (\(_,b,c) -> (b,c)) . swap <$> Map.toList (transitionStage $ Proxy @script)
lookup (mSourceSpine, mSourceSpine) transitions

-- ---


data TxEvent = MkTxEvent
{ _parsed_tx :: Tx
, _point :: String -- "Origin"
Expand Down Expand Up @@ -369,3 +397,4 @@ plutusAddressToOuraAddress =
. SerialiseRaw.serialiseToRawBytes
. either error id
. Address.plutusAddressToShelleyAddress Ledger.Mainnet

0 comments on commit e3c5c63

Please sign in to comment.