Skip to content

Commit 9b33876

Browse files
committed
Update docs.
1 parent 43785cd commit 9b33876

File tree

6 files changed

+80
-50
lines changed

6 files changed

+80
-50
lines changed

docs/interactive-plutip.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ setup = do
4242
waitSeconds 2
4343
pure (env, ownWallet)
4444

45-
addWalletWithAdas :: [Ada] -> ReaderT ClusterEnv IO BpiWallet
46-
addWalletWithAdas = addSomeWallet . map (fromInteger . Ada.toLovelace)
45+
addWalletWithAdas :: [Ada] -> ReaderT ClusterEnv IO (BpiWallet k)
46+
addWalletWithAdas funds = addSomeWallet $ testWallet'
47+
(map (fromInteger . Ada.toLovelace) funds)
48+
Nothing
49+
(PkhTag ())
4750
```
4851

4952
> Aside: Feel free to choose the amount of ada you want to fund your wallet with. Just remember: `addSomeWallet` takes a list of _lovelace_ amounts. Here, I've actually made my custom `Ada` type as well some helper utilities (not the same as `Plutus.V1.Ledger.Ada` as that is removed in newer `plutus-ledger-api` versions).
@@ -89,7 +92,7 @@ Once you have that, you can simply use `runContract` from `import Test.Plutip.In
8992
runContract ::
9093
(ToJSON w, Monoid w, MonadIO m) =>
9194
ClusterEnv ->
92-
BpiWallet ->
95+
BpiWallet k ->
9396
Contract w s e a ->
9497
m (ExecutionResult w e a)
9598
```
@@ -179,7 +182,7 @@ import Test.Plutip.Contract.Types (TestContractConstraints)
179182
newtype ContractRunner = ContrRunner
180183
{ runContr ::
181184
forall w e a.
182-
TestContractConstraints w e a =>
185+
TestContractConstraints w e Int a =>
183186
Contract w EmptySchema e a ->
184187
IO (Either (FailureReason e) a)
185188
}
@@ -192,7 +195,7 @@ begin = do
192195
setup = do
193196
env <- ask
194197
-- Gotta have all those utxos for the collaterals.
195-
ownWallet <- addWalletWithAdas $ 300 : replicate 50 10
198+
ownWallet <- addWalletWithAdas $ testWallet' (300 : replicate 50 10) Nothing (PkhTag 0)
196199
-- Wait for faucet funds to be added.
197200
waitSeconds 2
198201
pure (env, ownWallet)

docs/tasty-integration.md

+52-30
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ test =
1010
withConfiguredCluster def -- 1
1111
"Basic integration: launch, add wallet, tx from wallet to wallet" -- 2
1212
$ [ assertExecution "Contract 1" -- 3
13-
(initAda [100,200] <> initLovelace 10_000_000) -- 3.1
14-
(withContract $ \[wallet2pkh] -> someContract) -- 3.2
13+
(initAda (PkhTag (0 :: Int)) [100,200] <> initLovelace (BaseTag 1) 10_000_000) -- 3.1
14+
(withContract $ \wl -> someContract) -- 3.2
1515
[ shouldSucceed -- 3.3
1616
]
1717
]
@@ -20,8 +20,8 @@ test =
2020
1. Will start the local network with default config (more on configuring below)
2121
2. Description of test group that will be run on current instance of the network
2222
3. Test scenario that will be performed on the local network with it's description. Scenario includes:
23-
1. (3.1.) Initialization of `wallets`. In this case two addresses will be funded: first will have 2 UTxOs with 100 and 200 Ada, second - single UTxO with 10 Ada.
24-
2. (3.2) Execution of "`someContract :: Contract w s e a`". `PaymentPubKeyHash` of *first wallet* will be accessible in `someContract` as "own PaymentPubKeyHash". e.g with `ownFirstPaymentPubKeyHash`. `PaymentPubKeyHash` of *second* initiated wallet is brought into scope by `wallet2pkh` during pattern match on list (more on that below).
23+
1. (3.1.) Initialization of `wallets`. In this case two addresses will be funded: first - enterprise address - will have 2 UTxOs with 100 and 200 Ada, second - base address - single UTxO with 10 Ada.
24+
2. (3.2) Execution of "`someContract :: Contract w s e a`". `PaymentPubKeyHash` of *first wallet* will be accessible in `someContract` as "own PaymentPubKeyHash". e.g with `ownFirstPaymentPubKeyHash`. `PaymentPubKeyHash` of *second* initiated wallet is accessible through `wl :: WalletLookups` (more on that below).
2525
3. (3.3) List of checks or `predicates` which will be performed for the result of `someContract` execution.
2626

2727
It is possible to run several scenarios on single network instance - note that `withConfiguredCluster` accepts list of `assertExecution`'s.
@@ -33,39 +33,61 @@ It is possible to initialize arbitrary number of `wallets` in second argument of
3333
E.g. if `wallets` initialized like
3434

3535
```haskell
36-
(initAda [100] <> initAda [200] <> initAda [300])
36+
(initAda (PkhTag (0 :: Int)) [100] <> initAda (PkhTag 1) [200] <> initAda (BaseTag 2) [300])
3737
```
3838

3939
we will get 3 funded addresses represented by 3 corresponding `wallets`:
4040

41-
* `PaymentPubKeyHash` of wallet `initAda [100]` will be "own" `PaymentPubKeyHash` for contract executed it test case.
42-
* `PaymentPubKeyHash` of `wallets` `initAda [200]` and `initAda [300]` will be available via lambda argument. I.e.:
41+
* `PaymentPubKeyHash` of wallet 0 will be "own" `PaymentPubKeyHash` for contract executed it test case.
42+
* `PaymentPubKeyHash` of `wallets` 1 and 2 will be available via lambda wallet lookups argument. I.e.:
4343

4444

4545
```haskell
46-
withContract $ \[pkh1, pkh2] -> someContract
46+
withContract $ \wl -> do
47+
PkhWallet pkh1 <- lookupWallet wl (PkhTag 1)
48+
BaseWallet pkh2 spkh2 <- lookupWallet wl (BaseTag 2)
49+
someContract
4750
```
4851

49-
where
52+
note that the lookup return type depends on a query tag. Unfortunetely the type hint is needed to avoid cryptic error message.
5053

51-
* `pkh1` is `PaymentPubKeyHash` of `wallet` `initAda [200]`
52-
* `pkh2` is `PaymentPubKeyHash` of `wallet` `initAda [300]`
5354

54-
`PaymentPubKeyHash` of `wallet` `initAda [100]` is meant to be `pkh0` and not presented in the list.
55+
* `pkh1` is `PaymentPubKeyHash` of `wallet` `initAda (PkhTag 1) [200]`
56+
* `pkh2` is `PaymentPubKeyHash` of `wallet` `initAda (BaseTag 2) [300]` and `spkh2` is its `StakePubKeyHash`
57+
58+
`PaymentPubKeyHash` of `wallet` `initAda (PkhTag 0) [100]` is meant to be `pkh0` and not presented in the lookups.
59+
60+
61+
You can execute a contract with base address as contracts address:
62+
```haskell
63+
(initAda (BaseTag 0) [100])
64+
```
65+
66+
and witness in contract
67+
68+
```haskell
69+
withContract $ \_ -> do
70+
ourAddr :| _ <- Contract.ownAddresses
71+
case addr of
72+
Address (PubKeyCredential ourPkh) (Just (StakingHash (PubKeyCredential ourSpkh))) -> logInfo "This is the address we will get."
73+
_ -> error "Nothing else matters"
74+
```
75+
76+
Use `mustPayToPubKeyAddress` instead of `mustPayToPubKey` when your address has staking keys.
5577

5678
## Executing contracts
5779

5880
It is possible to run arbitrary number of contracts in 3d argument of `assertExecution` using its monadic nature. E.g.:
5981

6082
```haskell
6183
assertExecution "Some description"
62-
( initAda [100])
84+
( initAda (PkhTag ()) [100])
6385
( do
6486
void $
6587
withContract $
66-
\pkhs -> contract1
88+
\wl -> contract1
6789
withContractAs 1 $
68-
\pkhs -> contract2
90+
\wl -> contract2
6991
)
7092
[shouldSucceed]
7193
```
@@ -85,29 +107,29 @@ For example, consider the following scenario:
85107

86108
```haskell
87109
assertExecution "Some description"
88-
( initAda [100] -- walletA
89-
<> initAda [200] -- walletB
90-
<> initAda [300] -- walletC
110+
( initAda (PkhTag 'a') [100] -- walletA
111+
<> initAda (PkhTag 'b') [200] -- walletB
112+
<> initAda (PkhTag 'c') [300] -- walletC
91113
)
92114
( do
93115
void $
94-
withContractAs 1 $ -- running contract with walletB
95-
\[walletA_PKH, walletC_PKH] -> setupContract1
116+
withContractAs 'b' $ -- running contract with walletB
117+
\wl -> do
118+
wallA <- lookupWallet wl (PkhTag 'a')
119+
setupContract1
96120
void $
97-
withContractAs 2 $ -- running contract with walletC
98-
\[walletA_PKH, walletB_PKH] -> setupContract2
99-
withContract $
100-
\pkhs -> theContract
121+
withContractAs 'c' $ -- running contract with walletC
122+
\wl -> do
123+
wallB <- lookupWallet wl (PkhTag 'b')
124+
setupContract2
125+
withContract $ -- uses first wallet, walletA
126+
\wl -> theContract
101127
)
102128
[shouldSucceed]
103129
```
104130

105-
Under the hood, test runner builds list of wallets like this `[walletA, walletB, walletC]` and by calling `withContractAs` we can refer to an index (0 based) of specific wallet in this list. In that case, `PaymentPubKeyHash` of referenced `wallet` becomes "own" `PaymentPubKeyHash` of the contract, and `PaymentPubKeyHash`'es in argument of lambda will be rearranged. E.g. in case of `withContractAs 1`:
106-
107-
* `PaymentPubKeyHash` of `walletB` will become own `PaymentPubKeyHash`
108-
* argument of lambada will contain `PaymentPubKeyHash`'es of `walletA` and `walletC`.
109-
110-
Actually, `withContract` is just shortcut for `withContractAs 0`.
131+
`withContractAs` asks explicitly for the name of a wallet to be used as contract's.
132+
Instead `withContract` uses the first wallet, first in the order of how the initializations are written.
111133

112134
## Assertions
113135

src/Test/Plutip/Contract.hs

+10-7
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
-- > (initAda (PkhTag 0) 100 <> initAda (PkhTag 1) 101)
100100
-- > ( do
101101
-- > void $ -- run something prior to the contract which result will be checked
102-
-- > withContract $ \wl ->
102+
-- > withContract $ \wl ->
103103
-- > PkhWallet pkh1 <- lookupWallet wl (PkhTag 1)
104104
-- > payTo pkh1 10_000_000
105105
-- > withContractAs 1 $ \wl -> do -- run the contract which result will be checked
@@ -182,19 +182,22 @@ import Test.Plutip.Contract.Types (
182182
)
183183
import Test.Plutip.Contract.Values (assertValues, valueAt)
184184
import Test.Plutip.Internal.BotPlutusInterface.Lookups (WalletLookups, lookupsMap, makeWalletInfo, makeWalletLookups)
185-
import Test.Plutip.Internal.BotPlutusInterface.Run
186-
( runContract, runContract, runContractWithLogLvl )
185+
import Test.Plutip.Internal.BotPlutusInterface.Run (
186+
runContract,
187+
runContractWithLogLvl,
188+
)
187189
import Test.Plutip.Internal.BotPlutusInterface.Types (
188190
BpiWallet (bwTag),
189191
TestWallet (twExpected, twTag),
190192
TestWallet' (TestWallet'),
191193
TestWallets (unTestWallets),
194+
WalletInfo,
192195
getTag,
193196
ownAddress,
194-
WalletInfo,
195197
)
196-
import Test.Plutip.Internal.BotPlutusInterface.Wallet
197-
( walletPaymentPkh )
198+
import Test.Plutip.Internal.BotPlutusInterface.Wallet (
199+
walletPaymentPkh,
200+
)
198201
import Test.Plutip.Internal.Types (
199202
ClusterEnv,
200203
ExecutionResult (contractLogs, outcome),
@@ -321,7 +324,7 @@ maybeAddValuesCheck ioRes tws =
321324
f _ _ = error "All left are This and all right are That."
322325
in Map.unionWith f (This <$> mb) (That <$> mc)
323326

324-
-- | Run a contract using the first wallet as own wallet, and return `ExecutionResult`.
327+
-- | Run a contract using the first wallet (in the order of how initializations are written) as own wallet, and return `ExecutionResult`.
325328
-- This could be used by itself, or combined with multiple other contracts.
326329
--
327330
-- @since 0.2

src/Test/Plutip/Internal/BotPlutusInterface/Types.hs

+7-4
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ import Test.Plutip.Internal.BotPlutusInterface.Keys (KeyPair, StakeKeyPair)
3131

3232
-- | Name for the wallet (k) together with information on what we expect the wallet to be.
3333
-- Used in wallet initialization specifies requested wallet's type, used in lookups specifies expected returned wallet type.
34-
--
34+
--
3535
-- Don't use the same name `k` for two wallets, even with different tag constructors.
36+
-- `t` type parameter is the type of wallet that will be accessible from WalletLookups.
3637
data WalletTag t k where
3738
-- | Base address wallet: has both payment and staking keys
3839
BaseTag :: k -> WalletTag BaseWallet k
@@ -69,17 +70,19 @@ newtype TestWallets k = TestWallets {unTestWallets :: NonEmpty (TestWallet' k)}
6970

7071
data TestWallet' k = forall t. TestWallet' (TestWallet t k)
7172

73+
-- | Make TestWallet', takes utxo distribution, value assertions and WalletTag as arguments.
74+
testWallet' :: [Positive] -> Maybe (ValueOrdering, Value) -> WalletTag t k -> TestWallet' k
75+
testWallet' twInitDistribiution twExpected twTag = TestWallet' $ TestWallet twInitDistribiution twExpected twTag
76+
7277
data SomeTestWallet' = forall k. SomeTestWallet' (TestWallet' k)
7378

79+
-- | Description of wallet to initialize
7480
data TestWallet t k = TestWallet
7581
{ twInitDistribiution :: [Positive]
7682
, twExpected :: Maybe (ValueOrdering, Value)
7783
, twTag :: WalletTag t k
7884
}
7985

80-
testWallet' :: [Positive] -> Maybe (ValueOrdering, Value) -> WalletTag t k -> TestWallet' k
81-
testWallet' twInitDistribiution twExpected twTag = TestWallet' $ TestWallet twInitDistribiution twExpected twTag
82-
8386
data ValueOrdering = VEq | VGt | VLt | VGEq | VLEq
8487

8588
-- | Value doesn't have an Ord instance, so we cannot use `compare`

src/Test/Plutip/LocalCluster.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ waitSeconds n = liftIO $ threadDelay (fromEnum n * 1_000_000)
5858
-- > test =
5959
-- > withCluster
6060
-- > "Tests with local cluster"
61-
-- > [ assertExecution "Get utxos" (initAda (PkhTag 0) 100) (withContract $ const getUtxos) [shouldSucceed]]
61+
-- > [ assertExecution "Get utxos" (initAda (PkhTag ()) 100) (withContract $ const getUtxos) [shouldSucceed]]
6262
-- > ...
6363
--
6464
-- @since 0.2
@@ -78,7 +78,7 @@ withCluster = withConfiguredCluster def
7878
-- > let myConfig = PlutipConfig ...
7979
-- > withConfiguredCluster myConfig
8080
-- > "Tests with local cluster"
81-
-- > [ assertExecution "Get utxos" (initAda (PkhTag 0) 100) (withContract $ const getUtxos) [shouldSucceed]]
81+
-- > [ assertExecution "Get utxos" (initAda (PkhTag ()) 100) (withContract $ const getUtxos) [shouldSucceed]]
8282
-- > ...
8383
--
8484
-- @since 0.2

test/Spec/Integration.hs

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import Spec.TestContract.SimpleContracts (
3131
import Spec.TestContract.ValidateTimeRange (failingTimeContract, successTimeContract)
3232
import Test.Plutip.Contract (
3333
ClusterTest,
34-
ValueOrdering (VLt),
3534
assertExecution,
3635
assertExecutionWith,
3736
initAda,
@@ -45,7 +44,7 @@ import Test.Plutip.Contract (
4544
)
4645
import Test.Plutip.Contract.Types (WalletTag (BaseTag, PkhTag))
4746
import Test.Plutip.Internal.BotPlutusInterface.Lookups (WalletLookups (lookupWallet), lookupAddress)
48-
import Test.Plutip.Internal.BotPlutusInterface.Types (PkhWallet (PkhWallet))
47+
import Test.Plutip.Internal.BotPlutusInterface.Types (PkhWallet (PkhWallet), ValueOrdering (VLt))
4948
import Test.Plutip.Internal.Types (
5049
FailureReason (CaughtException, ContractExecutionError),
5150
isException,

0 commit comments

Comments
 (0)