Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Debug module to run actions in REPL #407

Merged
merged 3 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ blockfrost-config.json
.direnv
secrets/
.vscode
tests/mock-blueprints/complex-blueprint-rt.json
tests/mock-blueprints/complex-blueprint-rt.json
log.txt
1 change: 1 addition & 0 deletions atlas-cardano.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ library
GeniusYield.Api.TestTokens
GeniusYield.CardanoApi.EraHistory
GeniusYield.CardanoApi.Query
GeniusYield.Debug
GeniusYield.Examples.Common
GeniusYield.Examples.Gift
GeniusYield.Examples.Limbo
Expand Down
50 changes: 50 additions & 0 deletions src/GeniusYield/Debug.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module GeniusYield.Debug (
coreConfigIO
, startDebugCps
, testCps
, stopDebugCps
, eval'
, GYNetworkId (..)
) where

import Control.Concurrent (MVar, newEmptyMVar, forkIO, putMVar, readMVar, takeMVar)
import GeniusYield.Types (GYProviders, GYNetworkId (..))
import GeniusYield.GYConfig (GYCoreConfig, coreConfigIO, withCfgProviders)
import GeniusYield.TxBuilder (GYTxGameMonadIO, runGYTxGameMonadIO)
import Control.Monad (void)

{-

Utils to use with ghci to run Atlas' operations from repl.
Based on https://stackoverflow.com/questions/42425939/how-can-i-use-repl-with-cps-function

ghci> :m + GeniusYield.Debug GeniusYield.Types.UTxO GeniusYield.Types.Address GeniusYield.TxBuilder
ghci> cfg <- coreConfigIO "maestro-config.json"
ghci> (providers, ctrl) <- startDebugCps @_ @() $ testCps cfg
ghci> eval = eval' GYTestnetPreview providers
ghci> take 1 . utxosToList <$> eval (utxosAtAddresses [unsafeAddressFromText "addr_test1wqtcz4vq80zxr3dskdcuw7wtfq0vwssd7rrpnnvcvrjhp5sx7leew"] )
ghci> stopDebugCps ctrl ()

-}

startDebugCps :: forall a b. ((a -> IO b) -> IO b) -> IO (a, MVar b)
startDebugCps cps = do
cpsVal <- newEmptyMVar @a
stopAndRet <- newEmptyMVar @b
void . forkIO $
void . cps $ \c -> do
putMVar cpsVal c
b <- readMVar stopAndRet
putStrLn "Done"
pure b
s <- takeMVar cpsVal
return (s, stopAndRet)

stopDebugCps :: MVar b -> b -> IO ()
stopDebugCps = putMVar

testCps :: GYCoreConfig -> (GYProviders -> IO b) -> IO b
testCps c = withCfgProviders c "test"

eval' :: GYNetworkId -> GYProviders -> GYTxGameMonadIO a -> IO a
eval' = runGYTxGameMonadIO
Loading