Skip to content

Commit 25326a3

Browse files
committed
docs update
1 parent 866349f commit 25326a3

File tree

7 files changed

+64
-4
lines changed

7 files changed

+64
-4
lines changed

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ This format is based on [Keep A Changelog](https://keepachangelog.com/en/1.0.0).
77
- Wallets with Base Address support
88
- Lookups for wallets in tasty integration
99

10+
## [1.3.1] - 2022-11-04
11+
12+
### Fixed
13+
14+
- collateral creation - happens now before user contract execution
15+
16+
### Added
17+
18+
- collateral handling documentation
19+
1020
## [1.3.0] - 2022-10-26
1121

1222
### Added

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ If your project is importing and making use of `Plutip`s library you will need t
3636

3737
And the following ghc flag must to be set for the test execution: `-Wall -threaded -rtsopts`
3838

39-
NOTE: This branch launches local network in `Vasil`. It was tested with node `1.35.3` (this node version used in nix environment as well). Please use appropriate node version when setting up own binaries in `PATH`.
39+
## NOTES
40+
41+
⚠️ This branch launches local network in `Vasil`. It was tested with node `1.35.3` (this node version used in nix environment as well). Please use appropriate node version when setting up own binaries in `PATH`.
42+
43+
⚠️ [Collateral handling](./docs/collateral-handling.md)
4044

4145
## Tutorials
4246

contract-execution/Main.hs

+4-1
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ main = do
4141
extraConf = def {ecSlotLength = slotLen}
4242
plutipConfig = def {extraConfig = extraConf}
4343

44+
addSomeWalletWithCollateral funds =
45+
addSomeWallet (toAda 10 : funds)
46+
4447
putStrLn "Starting cluster..."
4548
(st, _) <- startCluster plutipConfig $ do
46-
w <- addSomeWallet [toAda 10]
49+
w <- addSomeWalletWithCollateral [toAda 100]
4750
liftIO $ putStrLn "Waiting for wallets to be funded..."
4851
CI.awaitWalletFunded w slotLen
4952

docs/collateral-handling.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Collateral handling
2+
3+
Before running *any* contract `Plutip` under the hood creates dedicated UTxO at "own" wallet address to be used *only* as collateral. This UTxO is created by submitting transaction and spending "own" wallet's funds . For collateral `Plutip` always uses **10 Ada** at this point and if wallet address already has UTxO with 10 Ada on it, then Plutip will use it as collateral.
4+
5+
UTxO that was created or picked for collateral is stored in memory, so during Contract execution `Plutip` will always use this exact same UTxO. Collateral UTxO has special properties - to guard it from being consumed by accident it is not accessible from Contract. I.e. calls like `utxosAt` ***will not return*** UTxO used for collateral. This means, that users don't have to care really about collateral UTxO during contract execution.
6+
7+
The only place where collateral "sticks out" is the moment of wallet creation. Ii is also visible for `cardano-cli` queries.
8+
9+
## Cluster runner
10+
11+
With [cluster runners](../local-cluster/README.md), when creating wallet with `addSomeWallet [100_000_000]`, if you want to have UTxO with exactly 100 Ada while running the Contract, you should add 10 Ada more to wallet's initial distribution, or UTxO with 100 Ada will be used to create collateral.
12+
13+
E.g.:
14+
15+
```haskell
16+
main :: IO ()
17+
main = do
18+
let executeContract wallet contract =
19+
ask >>= \cEnv -> runContract cEnv wallet contract
20+
21+
(st, _) <- startCluster def $ do
22+
w <- addSomeWallet [100_000_000, 10_000_000] -- 10 Ada will be used as collateral
23+
awaitWalletFunded w 1
24+
result <- executeContract w someContract
25+
doSomething result
26+
stopCluster st
27+
```
28+
29+
Or just make helper function:
30+
31+
```haskell
32+
addSomeWalletWithCollateral funds =
33+
addSomeWallet (toAda 10 : funds)
34+
```
35+
36+
## Tasty integration
37+
38+
For collateral handling in tasty integration see [Collateral handling section](./tasty-integration.md#collateral-handling).

docs/tasty-integration.md

+2
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ To assert the final `Value` which `wallet` will have after contract execution sp
126126
* `initAdaAssertValue [100] 133` - initialize `wallet` with single UTxO with 100 Ada and check that after contract execution final `Value` of all `wallet`'s UTxOs is equal to 133 Ada.
127127
* `initAndAssertLovelaceWith [1_000_000] VGt 2_000_000` - initialize `wallet` with single UTxO with 1000000 Lovelace and check that after contract execution final `Value` of all `wallet`'s UTxOs is *greater than* 2000000 Lovelace.
128128

129+
#### Collateral handling
130+
129131
***One important note*** is that Plutip creates dedicated UTxO to be used *only* as collateral under the hood. This UTxO would normally be created by spending wallets funds, and the transaction fee and Ada amount used for collateral UTxO would mess up balance assertions. So when using any kind of assertions for `Value` it is advised to wrap `wallets` initialization with `withCollateral` function. This simply adds a small UTxO to the `wallets`'s balance during network setup that is then picked up for collateral instead avoiding the problem. Use it like so:
130132

131133
```haskell

local-cluster/Main.hs

+4-1
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,11 @@ main = do
7979
amt -> Right $ fromInteger . toInteger $ amt
8080

8181
initWallets numWallets numUtxos amt dirWallets = do
82+
let collateralAmount = 10_000_000
8283
replicateM (max 0 numWallets) $
83-
addSomeWalletDir (replicate numUtxos amt) dirWallets
84+
addSomeWalletDir
85+
(collateralAmount : replicate numUtxos amt)
86+
dirWallets
8487

8588
printWallet (w, n) = do
8689
putStrLn $ "Wallet " ++ show n ++ " PKH: " ++ show (walletPkh w)

local-cluster/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ main = do
5555
ask >>= \cEnv -> runContract cEnv wallet contract
5656

5757
(st, _) <- startCluster def $ do
58-
w <- addSomeWallet [100_000_000]
58+
w <- addSomeWallet [100_000_000, 10_000_000] -- 10 Ada will be used as collateral
5959
awaitWalletFunded w 1
6060
result <- executeContract w someContract
6161
doSomething result

0 commit comments

Comments
 (0)