Skip to content

Commit 1f60f27

Browse files
committed
Resolves #12:
- bot interfaces switched from dry-mode - wallet to wallet payment test contract - integrations tests
1 parent a0b681d commit 1f60f27

File tree

9 files changed

+104
-38
lines changed

9 files changed

+104
-38
lines changed

example/Main.hs

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ import Control.Monad.Reader (ask)
66
import DSL (
77
ada,
88
addSomeWallet,
9+
ledgerPaymentPkh,
910
mkMainnetAddress,
1011
report,
1112
runContract,
1213
runUsingCluster,
1314
waitSeconds,
1415
)
1516
import Data.Text (Text, unpack)
16-
import DebugContract.DebugGet qualified as DebugContract
17+
import DebugContract.GetUtxos qualified as DebugContract
18+
import DebugContract.PayToWallet qualified as DebugContract
1719
import LocalCluster.Types (supportDir)
1820
import System.Environment (setEnv)
1921
import Tools.DebugCli qualified as CLI
@@ -31,15 +33,20 @@ main = do
3133
w2 <- addSomeWallet (ada 202)
3234

3335
debugWallets (sequence [w1, w2]) --temporary, for debugging
34-
testWallet <- either (error . show) pure w1
35-
runContract testWallet DebugContract.getUtxos
36+
testW1 <- either (error . show) pure w1
37+
runContract testW1 DebugContract.getUtxos
3638
>>= report
37-
runContract testWallet DebugContract.getUtxosThrowsErr
39+
runContract testW1 DebugContract.getUtxosThrowsErr
3840
>>= report
39-
runContract testWallet DebugContract.getUtxosThrowsEx
41+
runContract testW1 DebugContract.getUtxosThrowsEx
4042
>>= report
4143

42-
liftIO $ putStrLn "Done. Debug awaiting - interrupt to exit" >> forever (waitSeconds 60)
44+
testW2 <- either (error . show) pure w2
45+
runContract testW1 (DebugContract.payTo (ledgerPaymentPkh testW2) 10_000_000)
46+
>>= report
47+
>> debugWallets (sequence [w1, w2])
48+
49+
liftIO $ putStrLn "Done. Debug awaiting - Enter to exit" >> void getLine
4350
where
4451
debugWallets ws = do
4552
cEnv <- ask
@@ -50,22 +57,3 @@ main = do
5057
(error . ("Err: " <>) . show)
5158
(mapM_ (liftIO . CLI.utxoAtAddress cEnv . mkMainnetAddress))
5259
ws
53-
54-
testMnemonic :: [Text]
55-
testMnemonic =
56-
[ "radar"
57-
, "scare"
58-
, "sense"
59-
, "winner"
60-
, "little"
61-
, "jeans"
62-
, "blue"
63-
, "spell"
64-
, "mystery"
65-
, "sketch"
66-
, "omit"
67-
, "time"
68-
, "tiger"
69-
, "leave"
70-
, "load"
71-
]

plutip.cabal

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ common common-imports
4646
, plutus-contract
4747
, plutus-ledger
4848
, plutus-ledger-api
49+
, plutus-ledger-constraints
4950
, plutus-pab
5051
, prettyprinter
5152
, row-types
@@ -119,7 +120,8 @@ library
119120
BotInterface.Types
120121
BotInterface.Wallet
121122
DSL
122-
DebugContract.DebugGet
123+
DebugContract.GetUtxos
124+
DebugContract.PayToWallet
123125
LocalCluster.Cluster
124126
LocalCluster.Types
125127
LocalCluster.Wallet

src/BotInterface/Run.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ runContract bpiWallet contract =
7676
, pcScriptFileDir = pack $ BIS.scriptsDir cEnv
7777
, pcSigningKeyFileDir = pack $ BIS.keysDir cEnv
7878
, pcTxFileDir = pack $ BIS.txsDir cEnv
79-
, pcDryRun = True
79+
, pcDryRun = False
8080
, pcProtocolParamsFile = pack $ BIS.pParamsFile cEnv
8181
, pcLogLevel = Info
8282
, pcOwnPubKeyHash = ledgerPkh bpiWallet

src/BotInterface/Setup.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module BotInterface.Setup (
77
txsDir,
88
) where
99

10+
import Cardano.Launcher.Node (nodeSocketFile)
1011
import Data.Aeson (encodeFile)
1112
import LocalCluster.Types (ClusterEnv (supportDir), nodeSocket)
1213
import System.Directory (createDirectoryIfMissing, doesDirectoryExist)
@@ -34,6 +35,7 @@ runSetup cEnv = do
3435
setSocketPathEnv
3536
where
3637
setSocketPathEnv =
38+
-- required by `cardano-cli` used by bot interface
3739
setEnv "CARDANO_NODE_SOCKET_PATH" (nodeSocketFile $ nodeSocket cEnv)
3840
createRequiredDirs =
3941
mapM_

src/BotInterface/Wallet.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module BotInterface.Wallet (
55
mkMainnetAddress,
66
cardanoMainnetAddress,
77
ledgerPkh,
8+
ledgerPaymentPkh,
89
) where
910

1011
import BotInterface.Setup qualified as Setup
@@ -22,7 +23,7 @@ import Control.Monad.Reader (MonadReader (ask), ReaderT (runReaderT))
2223
import Data.Bool (bool)
2324
import Data.Text (Text, pack, unpack)
2425
import GHC.Natural (Natural)
25-
import Ledger (PubKey (PubKey), PubKeyHash, pubKeyHash)
26+
import Ledger (PaymentPubKeyHash (PaymentPubKeyHash), PubKey (PubKey), PubKeyHash, pubKeyHash)
2627
import LocalCluster.Types (ClusterEnv, nodeSocket, supportDir)
2728
import Plutus.V1.Ledger.Api qualified as LAPI
2829
import System.FilePath ((<.>), (</>))
@@ -109,6 +110,9 @@ ledgerPkh =
109110
. CAPI.serialiseToRawBytes
110111
. vrfKey
111112

113+
ledgerPaymentPkh :: BpiWallet -> PaymentPubKeyHash
114+
ledgerPaymentPkh = PaymentPubKeyHash . ledgerPkh
115+
112116
-- whateverJsonYouNeed :: BpiWallet -> String
113117
-- whateverJsonYouNeed wallet =
114118
-- (C8.unpack $ encode $ addressCredential $ pubKeyHashAddress (PaymentPubKeyHash $ ledgerPkh wallet) Nothing)

src/DSL.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module DSL (
22
BpiWallet,
3+
RunResult (RunSuccess, RunFailed),
34
addSomeWallet,
45
runContract,
56
runContract_,
@@ -10,17 +11,20 @@ module DSL (
1011
report,
1112
mkMainnetAddress,
1213
cardanoMainnetAddress,
14+
ledgerPaymentPkh,
1315
) where
1416

1517
import BotInterface.Run (runContract, runContract_)
1618
import BotInterface.Wallet (
1719
BpiWallet,
1820
addSomeWallet,
1921
cardanoMainnetAddress,
22+
ledgerPaymentPkh,
2023
mkMainnetAddress,
2124
)
2225
import Control.Monad.IO.Class (MonadIO, liftIO)
2326
import LocalCluster.Cluster (runUsingCluster, runUsingCluster')
27+
import LocalCluster.Types (RunResult (RunFailed, RunSuccess))
2428
import Utils (ada, waitSeconds)
2529

2630
{- | Stand-in for upcoming report functionality
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module DebugContract.DebugGet (getUtxos, getUtxosThrowsErr, getUtxosThrowsEx) where
1+
module DebugContract.GetUtxos (getUtxos, getUtxosThrowsErr, getUtxosThrowsEx) where
22

33
import Data.Map (Map)
44
import Data.Text (Text, pack)

src/DebugContract/PayToWallet.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module DebugContract.PayToWallet (payTo) where
2+
3+
import Data.Text (Text)
4+
import Ledger (CardanoTx, PaymentPubKeyHash, getCardanoTxId)
5+
import Ledger.Ada qualified as Ada
6+
import Plutus.Contract (Contract, awaitTxConfirmed, ownPaymentPubKeyHash, submitTx)
7+
import Plutus.PAB.Effects.Contract.Builtin (EmptySchema)
8+
9+
import Ledger.Constraints qualified as Constraints
10+
import Ledger.Constraints qualified as Contract
11+
12+
payTo :: PaymentPubKeyHash -> Integer -> Contract () EmptySchema Text CardanoTx
13+
payTo toPkh amt = do
14+
uwnPkh <- ownPaymentPubKeyHash
15+
tx <- submitTx (Constraints.mustPayToPubKey toPkh (Ada.lovelaceValueOf amt) <> Contract.mustBeSignedBy uwnPkh)
16+
awaitTxConfirmed $ getCardanoTxId tx
17+
pure tx

test/Test/Integration.hs

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,87 @@
11
module Test.Integration (test) where
22

3-
import Cardano.Api (AssetId (AdaAssetId), Quantity (Quantity), TxOut (TxOut), UTxO (unUTxO), txOutValueToValue, valueToList)
3+
import Cardano.Api (AssetId (AdaAssetId), Quantity (Quantity), TxOut (TxOut), UTxO (UTxO, unUTxO), txOutValueToValue, valueToList)
44
import Data.Map qualified as Map
55
import System.Environment (setEnv)
66
import Test.Tasty (TestTree)
7-
import Test.Tasty.HUnit (assertFailure, testCase, (@?=))
7+
import Test.Tasty.HUnit (assertBool, assertFailure, testCase, (@?=))
88
import Tools.CardanoApi (utxosAtAddress)
99

1010
import Control.Monad.IO.Class (liftIO)
1111
import Control.Monad.Reader (ask)
1212
import DSL (
13+
RunResult (RunFailed, RunSuccess),
1314
ada,
1415
addSomeWallet,
1516
cardanoMainnetAddress,
17+
runContract,
1618
runUsingCluster,
1719
waitSeconds,
1820
)
1921

22+
import DebugContract.GetUtxos qualified as DebugContract
23+
import DebugContract.PayToWallet qualified as DebugContract
24+
25+
import BotInterface.Wallet (ledgerPaymentPkh)
26+
2027
-- FIXME: something prints node configs polluting test outputs
2128
test :: TestTree
2229
test = do
23-
testCase "Basic integration: launch and add wallet" $ do
30+
testCase "Basic integration: launch, add wallet, tx from wallet to wallet" $ do
2431
withTestConf . runUsingCluster $ do
25-
ws <- addSomeWallet (ada 101)
26-
case ws of
27-
Left e -> liftIO $ assertFailure $ "Error: " <> show e
28-
Right wallet -> checkFunds wallet
32+
w1 <- addSomeWallet (ada 101) >>= either (error . show) pure
33+
checkFunds w1 (ada 101)
34+
w2 <- addSomeWallet (ada 102) >>= either (error . show) pure
35+
checkFunds w2 (ada 102)
36+
37+
assertSucceeds
38+
"Get utxos"
39+
(runContract w1 DebugContract.getUtxos)
40+
assertFails
41+
"Get utxos throwing error"
42+
(runContract w1 DebugContract.getUtxosThrowsErr)
43+
assertFails
44+
"Get utxos throwing exception"
45+
(runContract w1 DebugContract.getUtxosThrowsEx)
46+
assertFails
47+
"Pay negative amount"
48+
(runContract w1 (DebugContract.payTo (ledgerPaymentPkh w2) (-10_000_000)))
49+
50+
checkAdaTxFromTo w1 w2
2951
where
30-
checkFunds wallet' =
52+
checkFunds wallet' expectedAmt = do
53+
let expectedAmt' = toInteger expectedAmt
3154
ask >>= \cEnv -> liftIO $ do
3255
waitSeconds 2
3356
res <- utxosAtAddress cEnv (cardanoMainnetAddress wallet')
3457
let resultValue = toCombinedFlatValue <$> res
35-
resultValue @?= Right [(AdaAssetId, Quantity 101000000)]
58+
resultValue @?= Right [(AdaAssetId, Quantity expectedAmt')]
59+
60+
assertSucceeds tag act = do
61+
act >>= liftIO . assertBool (tag <> " did not succeed") . isSuccess
62+
63+
assertFails tag act = do
64+
act >>= liftIO . assertBool (tag <> " did not fail") . not . isSuccess
65+
66+
checkAdaTxFromTo w1 w2 = do
67+
res <- runContract w1 (DebugContract.payTo (ledgerPaymentPkh w2) 10_000_000)
68+
cEnv <- ask
69+
liftIO $ do
70+
assertBool ("Wallet to wallet tx failed: " <> show res) (isSuccess res)
71+
waitSeconds 1 -- todo: some "wait tx processed" could be handy
72+
utxosAtAddress cEnv (cardanoMainnetAddress w2)
73+
>>= \case
74+
Left e ->
75+
assertFailure $ "Failed ot get wallet UTxO: " <> show e
76+
Right (UTxO utxo) ->
77+
let utxoCnt = Map.size utxo
78+
in assertBool
79+
("Should be 2 UTxO at destination wallet, but request returned " <> show utxoCnt)
80+
(utxoCnt == 2)
81+
82+
isSuccess = \case
83+
RunSuccess _ _ -> True
84+
RunFailed _ -> False
3685

3786
withTestConf :: IO b -> IO b
3887
withTestConf runTest = do

0 commit comments

Comments
 (0)