Skip to content

Commit 457490d

Browse files
committed
Resolves # 12
- running contracts with bot plutus interface
1 parent ca6ffe8 commit 457490d

File tree

12 files changed

+147
-101
lines changed

12 files changed

+147
-101
lines changed

example/Main.hs

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,54 @@
11
module Main (main) where
22

3-
import BotInterface.Wallet qualified as BW
43
import Control.Monad (forever, replicateM_, void)
4+
import Control.Monad.IO.Class (liftIO)
5+
import Control.Monad.Reader (ask)
6+
import DSL (
7+
ada,
8+
addSomeWallet,
9+
mkMainnetAddress,
10+
report,
11+
runContract,
12+
runUsingCluster,
13+
waitSeconds,
14+
)
515
import Data.Text (Text, unpack)
6-
import LocalCluster.Cluster (runUsingCluster)
16+
import DebugContract.DebugGet qualified as DebugContract
17+
import LocalCluster.Types (supportDir)
718
import System.Environment (setEnv)
819
import Tools.DebugCli qualified as CLI
9-
import Utils (ada, waitSeconds)
10-
11-
import Data.Either (fromRight)
12-
import LocalCluster.Types (ClusterEnv (chainIndexUrl), supportDir)
13-
14-
import BotInterface.Run qualified as Bot
15-
import DebugContract.DebugGet qualified as DebugContract
16-
17-
-- debug stuff
18-
19-
import DebugContract.DebugGet qualified as DebugContract
20-
import Tools.ChainIndex qualified as CIX
21-
22-
-- debug stuff - END
2320

2421
main :: IO ()
2522
main = do
2623
-- todo: maybe some better configuring procedure should be introduced
2724
setEnv "SHELLEY_TEST_DATA" "cluster-data/cardano-node-shelley"
2825
setEnv "NO_POOLS" "1"
29-
setEnv "CARDANO_NODE_TRACING_MIN_SEVERITY" "Debug"
30-
31-
runUsingCluster $ \cEnv -> do
32-
ws <- -- ? maybe it will be more ergonomic to get rid of `Ether` and just fail hard
33-
BW.usingEnv cEnv . fmap sequence . sequence $
34-
[ BW.addSomeWallet (ada 101)
35-
, BW.addSomeWallet (ada 202)
36-
, BW.addSomeWallet (ada 303)
37-
]
3826

39-
debugWallets ws cEnv
27+
runUsingCluster $ do
28+
w1 <- -- ? maybe it will be more ergonomic to get rid of `Ether` and just fail hard
29+
-- as there is no reason to continue if wallet can't be set up
30+
addSomeWallet (ada 101)
31+
w2 <- addSomeWallet (ada 202)
4032

41-
let wallet = head $ tail $ fromRight (error "Ouch") ws
42-
Bot.runWrapped cEnv wallet DebugContract.getUtxos >>= print
43-
Bot.runWrapped cEnv wallet DebugContract.getUtxosThrowsErr >>= print
44-
Bot.runWrapped cEnv wallet DebugContract.getUtxosThrowsEx >>= print
33+
debugWallets (sequence [w1, w2]) --temporary, for debugging
34+
testWallet <- either (error . show) pure w1
35+
runContract testWallet DebugContract.getUtxos
36+
>>= report
37+
runContract testWallet DebugContract.getUtxosThrowsErr
38+
>>= report
39+
runContract testWallet DebugContract.getUtxosThrowsEx
40+
>>= report
4541

46-
putStrLn "Done. Debug awaiting - interrupt to exit" >> forever (waitSeconds 60)
42+
putStrLn "Done. Debug awaiting - interrupt to exit" >> forever (waitSeconds 60)
4743
where
48-
debugWallets ws cEnv = do
49-
putStrLn "\nDebug check:"
50-
putStrLn $ "Cluster dir: " <> show (supportDir cEnv)
51-
waitSeconds 2
44+
debugWallets ws = do
45+
cEnv <- ask
46+
liftIO $ putStrLn "\nDebug check:"
47+
liftIO $ putStrLn $ "Cluster dir: " <> show (supportDir cEnv)
48+
liftIO $ waitSeconds 2
5249
either
5350
(error . ("Err: " <>) . show)
54-
(mapM_ (CLI.utxoAtAddress cEnv . BW.mkMainnetAddress))
51+
(mapM_ (liftIO . CLI.utxoAtAddress cEnv . mkMainnetAddress))
5552
ws
5653

5754
testMnemonic :: [Text]

plutip.cabal

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,17 @@ library
113113

114114
hs-source-dirs: src/
115115
exposed-modules:
116-
Address
117116
BotInterface.Keys
118117
BotInterface.Run
119118
BotInterface.Setup
120119
BotInterface.Types
121120
BotInterface.Wallet
121+
DSL
122122
DebugContract.DebugGet
123123
LocalCluster.Cluster
124124
LocalCluster.Types
125125
LocalCluster.Wallet
126+
Tools.Address
126127
Tools.CardanoApi
127128
Tools.ChainIndex
128129
Tools.DebugCli
@@ -135,6 +136,7 @@ executable plutip-example
135136
, base
136137
, plutip
137138
, text
139+
, mtl
138140

139141
test-suite plutip-tests
140142
import: common-imports

src/BotInterface/Run.hs

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{-# LANGUAGE AllowAmbiguousTypes #-}
22

3-
module BotInterface.Run (execute, runWrapped) where
3+
module BotInterface.Run (runContract, runContract_) where
44

55
import BotInterface.Setup qualified as BIS
66
import BotInterface.Wallet (BpiWallet, ledgerPkh)
7-
import BotPlutusInterface.Contract (runContract)
7+
import BotPlutusInterface.Contract qualified as BIC
88
import BotPlutusInterface.Types (
99
CLILocation (Local),
1010
ContractEnvironment (ContractEnvironment),
@@ -32,39 +32,27 @@ import BotPlutusInterface.Types (
3232
)
3333
import Cardano.Api.ProtocolParameters (ProtocolParameters)
3434
import Control.Concurrent.STM (newTVarIO, readTVarIO)
35+
import Control.Monad (void)
3536
import Control.Monad.Catch (MonadCatch, handleAll)
3637
import Control.Monad.IO.Class (MonadIO, liftIO)
37-
import Control.Monad.Reader (MonadReader (ask), ReaderT (runReaderT))
38+
import Control.Monad.Reader (MonadReader (ask), ReaderT)
3839
import Data.Aeson (ToJSON, eitherDecodeFileStrict')
3940
import Data.Kind (Type)
4041
import Data.Row (Row)
41-
import Data.Text (Text, pack)
42+
import Data.Text (pack)
4243
import Data.UUID.V4 qualified as UUID
43-
import LocalCluster.Types (ClusterEnv (chainIndexUrl, networkId))
44+
import LocalCluster.Types (ClusterEnv (chainIndexUrl, networkId), FailReason (ContractErr, OtherErr), RunResult (RunFailed, RunSuccess))
4445
import Plutus.Contract (Contract)
4546
import Plutus.PAB.Core.ContractInstance.STM (Activity (Active))
4647
import Wallet.Types (ContractInstanceId (ContractInstanceId))
4748

48-
data FailReason e
49-
= ContractErr e
50-
| OtherErr Text
51-
deriving stock (Show)
52-
53-
data RunResult w e a
54-
= RunSuccess
55-
{ contractResult :: a
56-
, contractState :: ContractState w
57-
}
58-
| RunFailed {reason :: FailReason e}
59-
deriving stock (Show)
60-
61-
execute ::
49+
runContract ::
6250
forall (w :: Type) (s :: Row Type) (e :: Type) (a :: Type) (m :: Type -> Type).
6351
(ToJSON w, Monoid w, MonadIO m, MonadCatch m) =>
6452
BpiWallet ->
6553
Contract w s e a ->
6654
ReaderT ClusterEnv m (RunResult w e a)
67-
execute bpiWallet contract =
55+
runContract bpiWallet contract =
6856
ask
6957
>>= readProtocolParams
7058
>>= either
@@ -101,15 +89,15 @@ execute bpiWallet contract =
10189
, ceWallet = playGroundWallet
10290
, ceContractInstanceId = contractInstanceID
10391
}
104-
res <- liftIO $ runContract contractEnv playGroundWallet contract
92+
res <- liftIO $ BIC.runContract contractEnv playGroundWallet contract
10593
case res of
10694
Left e -> return $ RunFailed (ContractErr e)
10795
Right a -> RunSuccess a <$> liftIO (readTVarIO contractState)
10896

109-
runWrapped ::
97+
runContract_ ::
98+
forall (w :: Type) (s :: Row Type) (e :: Type) (a :: Type) (m :: Type -> Type).
11099
(ToJSON w, Monoid w, MonadIO m, MonadCatch m) =>
111-
ClusterEnv ->
112100
BpiWallet ->
113101
Contract w s e a ->
114-
m (RunResult w e a)
115-
runWrapped cEnv bpiWallet contract = runReaderT (execute bpiWallet contract) cEnv
102+
ReaderT ClusterEnv m ()
103+
runContract_ bpiWallet contract = void $ runContract bpiWallet contract

src/BotInterface/Wallet.hs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ module BotInterface.Wallet (
55
mkMainnetAddress,
66
cardanoMainnetAddress,
77
ledgerPkh,
8-
whateverJsonYouNeed,
98
) where
109

1110
import BotInterface.Setup qualified as Setup
@@ -20,12 +19,10 @@ import Cardano.Wallet.Shelley.Launch.Cluster (
2019
import Control.Arrow (ArrowChoice (left))
2120
import Control.Monad.IO.Class (MonadIO, liftIO)
2221
import Control.Monad.Reader (MonadReader (ask), ReaderT (runReaderT))
23-
import Data.Aeson (encode)
2422
import Data.Bool (bool)
25-
import Data.ByteString.Lazy.Char8 qualified as C8
2623
import Data.Text (Text, pack, unpack)
2724
import GHC.Natural (Natural)
28-
import Ledger (Address (addressCredential), PaymentPubKeyHash (PaymentPubKeyHash), PubKey (PubKey), PubKeyHash, pubKeyHash, pubKeyHashAddress)
25+
import Ledger (PubKey (PubKey), PubKeyHash, pubKeyHash)
2926
import LocalCluster.Types (ClusterEnv, nodeSocket, supportDir)
3027
import Plutus.V1.Ledger.Api qualified as LAPI
3128
import System.FilePath ((<.>), (</>))
@@ -112,6 +109,6 @@ ledgerPkh =
112109
. CAPI.serialiseToRawBytes
113110
. vrfKey
114111

115-
whateverJsonYouNeed :: BpiWallet -> String
116-
whateverJsonYouNeed wallet =
117-
(C8.unpack $ encode $ addressCredential $ pubKeyHashAddress (PaymentPubKeyHash $ ledgerPkh wallet) Nothing)
112+
-- whateverJsonYouNeed :: BpiWallet -> String
113+
-- whateverJsonYouNeed wallet =
114+
-- (C8.unpack $ encode $ addressCredential $ pubKeyHashAddress (PaymentPubKeyHash $ ledgerPkh wallet) Nothing)

src/DSL.hs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module DSL (
2+
BpiWallet,
3+
addSomeWallet,
4+
runContract,
5+
runContract_,
6+
runUsingCluster,
7+
runUsingCluster',
8+
ada,
9+
waitSeconds,
10+
report,
11+
mkMainnetAddress,
12+
cardanoMainnetAddress,
13+
) where
14+
15+
import BotInterface.Run (runContract, runContract_)
16+
import BotInterface.Wallet (
17+
BpiWallet,
18+
addSomeWallet,
19+
cardanoMainnetAddress,
20+
mkMainnetAddress,
21+
)
22+
import Control.Monad.IO.Class (MonadIO, liftIO)
23+
import LocalCluster.Cluster (runUsingCluster, runUsingCluster')
24+
import Utils (ada, waitSeconds)
25+
26+
{- | Stand-in for upcoming report functionality
27+
(just print out for now)
28+
-}
29+
report :: (Show a, MonadIO m) => a -> m ()
30+
report = liftIO . print

src/DebugContract/DebugGet.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
module DebugContract.DebugGet (getUtxos, getUtxosThrowsErr, getUtxosThrowsEx) where
22

3+
import Data.Map (Map)
34
import Data.Text (Text, pack)
4-
import Ledger (pubKeyHashAddress, TxOutRef, ChainIndexTxOut)
5+
import Ledger (ChainIndexTxOut, TxOutRef, pubKeyHashAddress)
56
import Plutus.Contract (Contract, utxosAt)
67
import Plutus.Contract qualified as Contract
78
import Plutus.PAB.Effects.Contract.Builtin (EmptySchema)
89
import Text.Printf (printf)
9-
import Data.Map (Map)
1010

1111
getUtxos :: Contract () EmptySchema Text (Map TxOutRef ChainIndexTxOut)
1212
getUtxos = do
1313
pkh <- Contract.ownPaymentPubKeyHash
1414
Contract.logInfo @String $ printf "Own PKH: %s" (show pkh)
1515
utxosAt $ pubKeyHashAddress pkh Nothing
1616

17-
getUtxosThrowsErr :: Contract () EmptySchema Text (Map TxOutRef ChainIndexTxOut)
17+
getUtxosThrowsErr :: Contract () EmptySchema Text (Map TxOutRef ChainIndexTxOut)
1818
getUtxosThrowsErr = do
1919
pkh <- Contract.ownPaymentPubKeyHash
2020
Contract.logInfo @String $ printf "Own PKH: %s" (show pkh)
@@ -28,7 +28,7 @@ getUtxosThrowsErr = do
2828
, "Debug: Own UTxOs: " <> show utxos <> "\n"
2929
]
3030

31-
getUtxosThrowsEx :: Contract () EmptySchema Text (Map TxOutRef ChainIndexTxOut)
31+
getUtxosThrowsEx :: Contract () EmptySchema Text (Map TxOutRef ChainIndexTxOut)
3232
getUtxosThrowsEx = do
3333
pkh <- Contract.ownPaymentPubKeyHash
3434
Contract.logInfo @String $ printf "Own PKH: %s" (show pkh)

src/LocalCluster/Cluster.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
-- temporary measure while module under development
44
{-# OPTIONS_GHC -Wno-unused-imports #-}
55

6-
module LocalCluster.Cluster (runUsingCluster) where
6+
module LocalCluster.Cluster (runUsingCluster, runUsingCluster') where
77

88
import BotInterface.Setup qualified as BotSetup
99
import Cardano.Api qualified as CAPI
@@ -68,6 +68,7 @@ import Control.Arrow (
6868
import Control.Concurrent (threadDelay)
6969
import Control.Concurrent.Async (async)
7070
import Control.Monad (unless, void, when)
71+
import Control.Monad.Reader (ReaderT (runReaderT))
7172
import Control.Tracer (
7273
Tracer,
7374
contramap,
@@ -111,13 +112,15 @@ import Test.Integration.Faucet (
111112
{- | Start cluster and run action using provided `CalusterEnv`
112113
under development (mostly borrowed from `cardano-wallet`)
113114
-}
115+
runUsingCluster :: ReaderT ClusterEnv IO () -> IO ()
116+
runUsingCluster act = runUsingCluster' (runReaderT act)
114117

115118
{- Examples:
116119
`plutus-apps` local cluster: https://github.com/input-output-hk/plutus-apps/blob/75a581c6eb98d36192ce3d3f86ea60a04bc4a52a/plutus-pab/src/Plutus/PAB/LocalCluster/Run.hs
117120
`cardano-wallet` local cluster: https://github.com/input-output-hk/cardano-wallet/blob/99b13e50f092ffca803fd38b9e435c24dae05c91/lib/shelley/exe/local-cluster.hs
118121
-}
119-
runUsingCluster :: (ClusterEnv -> IO ()) -> IO ()
120-
runUsingCluster action = do
122+
runUsingCluster' :: (ClusterEnv -> IO ()) -> IO ()
123+
runUsingCluster' action = do
121124
checkProcessesAvailable ["cardano-node", "cardano-cli"]
122125
withLocalClusterSetup $ \dir clusterLogs _walletLogs -> do
123126
withLoggingNamed "cluster" clusterLogs $ \(_, (_, trCluster)) -> do

src/LocalCluster/Types.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
module LocalCluster.Types (
22
ClusterEnv (..),
3+
RunResult (..),
4+
FailReason (..),
35
nodeSocket,
46
) where
57

8+
import BotPlutusInterface.Types (ContractState)
69
import Cardano.Api (NetworkId)
710
import Cardano.BM.Tracing (Trace)
811
import Cardano.Launcher.Node (CardanoNodeConn)
@@ -23,3 +26,16 @@ data ClusterEnv = ClusterEnv
2326
-- | Helper function to get socket path from
2427
nodeSocket :: ClusterEnv -> CardanoNodeConn
2528
nodeSocket (ClusterEnv (RunningNode sp _ _) _ _ _ _) = sp
29+
30+
data FailReason e
31+
= ContractErr e
32+
| OtherErr Text
33+
deriving stock (Show)
34+
35+
data RunResult w e a
36+
= RunSuccess
37+
{ contractResult :: a
38+
, contractState :: ContractState w
39+
}
40+
| RunFailed {reason :: FailReason e}
41+
deriving stock (Show)

src/Address.hs renamed to src/Tools/Address.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- | Cardano-Api/Ledger/Cardano-Wallet address conversions
2-
module Address (
2+
module Tools.Address (
33
walletToCardano,
44
walletToCardanoAny,
55
walletToLedger,

src/Tools/DebugCli.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
{-# LANGUAGE ViewPatterns #-}
2-
31
module Tools.DebugCli (
42
debugCli,
53
utxoAtAddress,

0 commit comments

Comments
 (0)