Skip to content

Commit 30caa53

Browse files
committed
Update the docs and CHANGELOG
1 parent 70c9d7f commit 30caa53

14 files changed

+94
-246
lines changed

CHANGELOG.md

+37
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,43 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
8080

8181
### Removed
8282

83+
- **[IMPORTANT]** Removed use of conditional code rewriting based on `BROWSER_RUNTIME` env variable during bundling ([#1595](https://github.com/Plutonomicon/cardano-transaction-lib/pull/1598))
84+
85+
This change simplifies the bundling process, but it requires a number of updates for all CTL-dependent projects:
86+
87+
- WebPack users should make this change to the webpack config:
88+
89+
```diff
90+
plugins: [
91+
- new webpack.DefinePlugin({
92+
- BROWSER_RUNTIME: isBrowser
93+
- }),
94+
```
95+
- Esbuild users should make this change:
96+
97+
```diff
98+
const config = {
99+
...
100+
- define: {
101+
- BROWSER_RUNTIME: isBrowser ? "true" : '""'
102+
- },
103+
```
104+
105+
- All users should update the runtime dependencies:
106+
107+
```diff
108+
- "@emurgo/cardano-message-signing-browser": "1.0.1",
109+
- "@emurgo/cardano-message-signing-nodejs": "1.0.1",
110+
+ "@mlabs-haskell/cardano-message-signing": "1.0.1",
111+
- "apply-args-browser": "0.0.1",
112+
- "apply-args-nodejs": "0.0.1",
113+
+ "@mlabs-haskell/uplc-apply-args": "1.0.0",
114+
+ "isomorphic-ws": "^5.0.0",
115+
- "ws": "8.4.0",
116+
+ "ws": "^8.16.0",
117+
+ "web-encoding": "^1.1.5",
118+
```
119+
83120
- `ModifyTx` error: made conversion functions total and removed the need to handle it
84121

85122
## [v7.0.0]

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ js-sources := $(shell fd --no-ignore-parent -ejs -ecjs)
1313
ps-entrypoint := Ctl.Examples.ByUrl
1414
# The entry point function in the main PureScript module
1515
ps-entrypoint-function := main
16-
# Whether to bundle for the browser
16+
# Whether to bundle for the browser ("1") or the node ("")
17+
# NOTE: bundling for the node is not necessary, see https://github.com/Plutonomicon/cardano-transaction-lib/blob/develop/doc/using-from-js.md
1718
browser-runtime := 1 # Use "1" for true and "" for false
1819

1920
preview-node-ipc = $(shell docker volume inspect store_node-preview-ipc | jq -r '.[0].Mountpoint')

doc/ctl-as-dependency.md

+3-11
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,17 @@ CTL can be imported as an additional dependency into a Purescript project built
66
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
77
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
88

9-
- [Caveats](#caveats)
109
- [Using CTL's overlays](#using-ctls-overlays)
1110
- [Upgrading CTL](#upgrading-ctl)
1211
- [See also](#see-also)
1312

1413
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
1514

16-
## Caveats
17-
18-
The following caveats alway applies when using CTL from your project:
19-
20-
1. Only bundling with Webpack is supported (this is due to our internal dependency on `cardano-serialization-lib` which uses WASM with top-level `await`; only Webpack is reliably capable of bundling this properly)
21-
2. The environment variable `BROWSER_RUNTIME` determines which version of `cardano-serialization-lib` is loaded by CTL, so you must use it as well (i.e. set it to `1` for the browser; leave it unset for NodeJS)
22-
2315
## Using CTL's overlays
2416

2517
CTL exposes two `overlay`s from its flake. You can use these in the Nix setup of your own project to use the same setup as we do, e.g. the same packages and PS builders:
2618

27-
- `overlays.purescript` contains Purescript builders to compile Purescript sources, build bundles with Webpack (`bundlePursProject`), run unit tests using NodeJS (`runPursTest`), and run CTL contracts on a private testnet using Plutip (`runPlutipTest`).
19+
- `overlays.purescript` contains Purescript builders to compile Purescript sources, build bundles with Webpack/esbuild (`bundlePursProject`), run unit tests using NodeJS (`runPursTest`), and run CTL contracts on a private testnet using Plutip (`runPlutipTest`).
2820
- `overlays.runtime` contains various packages and other tools used in CTL's runtime, including `ogmios`, `kupo`, and `plutip-server`. It also defines `buildCtlRuntime` and `launchCtlRuntime` to help you quickly launch all runtime services (see the [runtime docs](./runtime.md))
2921

3022
We've split the overlays into two components to allow users to more easily choose which parts of CTL's Nix infrastructure they would like to directly consume. For example, some users do not require a pre-packaged runtime and would prefer to build it themselves with more control over its components (e.g. by directly using `ogmios` from their own `inputs`). Such users might still like to use our `purescript` overlay -- splitting the `overlays` allows us to support this. `overlays.runtime` also contains several haskell.nix packages which may cause issues with `hackage.nix` versions in your own project.
@@ -80,9 +72,9 @@ Make sure to perform **all** of the following steps, otherwise you **will** enco
8072
- That is, avoid using the `~` or `^` prefixes (e.g use versions like `"1.6.51"` instead of `"^1.6.51"`)
8173
- If you're using a `package-lock.json` (which is _highly_ recommended), you can update the lockfile with `npm i --package-lock-only`
8274

83-
4. **Update your webpack config**
75+
4. **Update your webpack/esbuild config**
8476

85-
- Sometimes the WebPack configuration also comes with breaking changes. Common source of problems are changes to `resolve.fallback`, `plugins` and `experiments` fields of the WebPack config. Use `git diff old-revision new-revision webpack.config.js` in the root of a cloned CTL repo, or use `git blame`.
77+
- Sometimes the WebPack or esbuild configuration also comes with breaking changes. Use `git diff old-revision new-revision webpack.config.cjs` in the root of a cloned CTL repo, or use `git blame`.
8678

8779
## See also
8880

doc/development.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ To **build** the project **without bundling and for a NodeJS environment**:
7777
- `npm run staking-test` to run [Plutip](./plutip-testing.md)-powered tests for ADA staking functionality - [entry point](../test/Plutip/Staking.purs)
7878
- `npm run blockfrost-test` for [Blockfrost-powered tests](./blockfrost.md) (does not require a runtime, but needs [some setup](./blockfrost.md#setting-up-a-blockfrost-powered-test-suite)) - [entry point](../test/Blockfrost/Contract.purs)
7979
- `npm run blockfrost-local-test` for self-hosted [Blockfrost-powered tests](./blockfrost.md) (requires a [local Blockfrost runtime](./blockfrost.md#running-blockfrost-locally)) - [entry point](../test/Blockfrost/Contract.purs)
80-
- `npm run e2e-test` for [tests with a headless browser](./e2e-testing.md) (requires a runtime and the tests served via HTTP: `npm run start-runtime` and `npm run webpack-serve` or `esbuild-serve`)
80+
- `npm run e2e-test` for [tests with a headless browser](./e2e-testing.md) (requires a runtime and the tests served via HTTP: `npm run start-runtime` and `npm run e2e-serve` or `esbuild-serve`)
8181

8282
#### With Nix
8383

@@ -100,14 +100,14 @@ Here and below, `<SYSTEM>` should be replaced with [one of the supported systems
100100

101101
To run or build/bundle the project for the browser:
102102

103-
- `npm run webpack-serve` will start a Webpack development server at `localhost:4008`, which is required for [E2E tests](./e2e-testing.md)
103+
- `npm run {webpack|esbuild}-serve` will start a Webpack development server at `localhost:4008`, which is required for [E2E tests](./e2e-testing.md)
104104
- `npm run {webpack|esbuild}-bundle` will output a bundled example module to `dist` (or `nix build -L .#ctl-example-bundle-web-{webpack|esbuild}` to build an example module using Nix into `./result/`)
105105

106-
By default, Webpack will build a [Purescript module](../examples/ByUrl.purs) that serves multiple example `Contract`s depending on URL (see [here](./e2e-testing.md#serving-the-contract-to-be-tested)). You can point Webpack to another Purescript entrypoint by changing the `ps-bundle` variable in the Makefile or in the `main` argument in the flake's `packages.ctl-examples-bundle-web`.
106+
By default, the bundler will build a [Purescript module](../examples/ByUrl.purs) that serves multiple example `Contract`s depending on URL (see [here](./e2e-testing.md#serving-the-contract-to-be-tested)). You can point the bundler to another Purescript entrypoint by changing the `ps-bundle` variable in the Makefile or in the `main` argument in the flake's `packages.ctl-examples-bundle-web`.
107107

108-
You will also need a light wallet extension pre-configured to run a `Contract`.
108+
You will also need a light wallet extension pre-configured for the correct Cardano network to run a `Contract`.
109109

110-
**Note**: The `BROWSER_RUNTIME` environment variable must be set to `1` in order to build/bundle the project properly for the browser (e.g. `BROWSER_RUNTIME=1 webpack ...`). For Node environments, leave this variable unset.
110+
**Note**: The `BROWSER_RUNTIME` environment variable must be set to `1` in order to build/bundle the project properly for the browser (e.g. `BROWSER_RUNTIME=1 webpack ...`). For NodeJS environments, leave this variable unset.
111111

112112
**Note**: The `KUPO_HOST` environment variable must be set to the base URL of the Kupo service in order to successfully serve the project for the browser (by default, `KUPO_HOST=http://localhost:1442`).
113113

doc/faq.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ Another thing to keep in mind is that due to [min-ada requirements](https://docs
7878

7979
CTL does not consume wallet collateral normally, but it still can happen.
8080

81-
In order to get the collateral UTxO, CTL uses the wallet and then marks the returned UTxO as locked internally. But some wallets (e.g. Gero) do not return the collateral the moment it is set, waiting for Tx confirmation first. In case a collateral is set right before the contract is started, CTL can accidentally spend the collateral, because we rely on CTL's own query layer to get a list of available UTxOs, and the wallet state may lag behind it, not returning the collateral to filter out at that moment.
81+
In order to get the collateral UTxO, CTL uses the wallet and then marks the returned UTxO as "locked" internally. But some wallets (e.g. Gero) do not return the collateral the moment it is set, waiting for Tx confirmation first. In case a collateral is set right before the contract is started, CTL can accidentally spend the collateral, because we rely on CTL's own query layer to get a list of available UTxOs without consulting with the wallet [in some cases](./query-layers.md), and the wallet state may lag behind the backend state.
82+
83+
It is impossible to lose the collateral UTxO funds completely, though, because CTL always uses [CIP-40 collateral return](https://cips.cardano.org/cip/CIP-0040).
8284

8385
## Time-related
8486

doc/importing-scripts.md

+5-87
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
- [Exporting scripts from Plutus or Plutarch](#exporting-scripts-from-plutus-or-plutarch)
88
- [Using Plutonomy](#using-plutonomy)
9-
- [Importing serialized scripts](#importing-serialized-scripts)
109
- [Serializing Plutus scripts](#serializing-plutus-scripts)
1110
- [PlutusTx](#plutustx)
1211
- [Plutarch](#plutarch)
1312
- [plutarch-ctl-bridge](#plutarch-ctl-bridge)
13+
- [Importing serialized scripts](#importing-serialized-scripts)
1414

1515
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
1616

@@ -31,7 +31,6 @@ The output file should be a Cardano envelope:
3131
- An example of a Plutus exporter can be found [here](https://github.com/Mr-Andersen/ctl-multisign-mre/blob/main/onchain/exporter/Main.hs).
3232
- For Plutarch, see [the-plutus-scaffold](https://github.com/mlabs-haskell/the-plutus-scaffold)'s [exporter](https://github.com/mlabs-haskell/the-plutus-scaffold/tree/main/onchain/exporter).
3333

34-
3534
### Using Plutonomy
3635

3736
It makes sense to use [Plutonomy](https://github.com/well-typed/plutonomy) (an optimizer for UPLC) in the exporter:
@@ -44,91 +43,6 @@ script = fromCompiledCode $
4443
optimizeUPLCWith aggressiveOptimizerOptions $$(PlutusTx.compile [||policy||])
4544
```
4645

47-
## Importing serialized scripts
48-
49-
To use your own scripts, compile them to any subdirectory in the root of your project (where `webpack.config.js` is located) and add a relative path to `webpack.config.js` under the `resolve.alias` section. In CTL, we have the `Scripts` alias for this purpose. Note the capitalization of `Scripts`: it is necessary to disambiguate it from local folders.
50-
51-
First, in your `webpack.config.js`, define an `alias` under `module.exports.resolve.alias` in order to `require` the compiled scripts from JS modules:
52-
53-
```javascript
54-
const path = require("path");
55-
56-
module.exports = {
57-
// ...
58-
resolve: {
59-
modules: [process.env.NODE_PATH],
60-
extensions: [".js"],
61-
fallback: {
62-
// ...
63-
},
64-
alias: {
65-
// You should update this path to the location of your compiled scripts,
66-
// relative to `webpack.config.js`
67-
Scripts: path.resolve(__dirname, "fixtures/scripts"),
68-
},
69-
},
70-
};
71-
```
72-
73-
You must also add the following to `module.exports.module.rules`:
74-
75-
```javascript
76-
module.exports = {
77-
// ...
78-
module: {
79-
rules: [
80-
{
81-
test: /\.plutus$/i,
82-
type: "asset/source",
83-
},
84-
// ...
85-
],
86-
},
87-
};
88-
```
89-
90-
This enables inlining your serialized scripts in `.js` files, to then be loaded in Purescript via the FFI:
91-
92-
```javascript
93-
// inline .plutus file as a string
94-
exports.myscript = require("Scripts/myscript.plutus");
95-
```
96-
97-
And on the purescript side, the script can be loaded like so:
98-
99-
```purescript
100-
foreign import myscript :: String
101-
102-
parseValidator :: Contract Validator
103-
parseValidator = liftMaybe (error "Error decoding myscript") do
104-
envelope <- decodeTextEnvelope myscript
105-
Validator <$> Contract.TextEnvelope.plutusScriptV1FromEnvelope envelope
106-
myContract cfg = runContract_ cfg $ do
107-
validator <- parseValidator
108-
...
109-
```
110-
111-
This way you avoid hardcoding your scripts directly to .purs files which could lead to synchronization issues should your scripts change.
112-
113-
**Note**: The `alias` method above will only work in the browser when bundling with Webpack. In order to load the scripts for both browser and NodeJS environments, you can use the `BROWSER_RUNTIME` environment variable like so:
114-
115-
```javascript
116-
let script;
117-
if (typeof BROWSER_RUNTIME != "undefined" && BROWSER_RUNTIME) {
118-
script = require("Scripts/my-script.plutus");
119-
} else {
120-
const fs = require("fs");
121-
const path = require("path");
122-
script = fs.readFileSync(
123-
path.resolve(__dirname, "../../fixtures/scripts/my-script.plutus"),
124-
"utf8"
125-
);
126-
}
127-
exports.myScript = script;
128-
```
129-
130-
Note that the relative path passed to `path.resolve` for the NodeJS case starts from the `output` directory that the Purescript compiler produces.
131-
13246
## Serializing Plutus scripts
13347

13448
### PlutusTx
@@ -176,3 +90,7 @@ You can use [`ply`](https://github.com/mlabs-haskell/ply) and [`ply-ctl`](https:
17690
### plutarch-ctl-bridge
17791

17892
You can use [`plutarch-ctl-bridge`](https://github.com/mlabs-haskell/plutarch-ctl-bridge) to generate Purescript types from your Haskell type definitions and typed script wrappers from parametrized Plutarch scripts. See [example module](https://github.com/mlabs-haskell/plutarch-ctl-bridge/blob/main/example/Main.hs).
93+
94+
## Importing serialized scripts
95+
96+
To use your own scripts, compile them to any subdirectory in the root of your project and either dynamically load them from file (NodeJS) or use your bundler to include them as fixtures into the bundle ([instructions for WebPack](https://webpack.js.org/guides/asset-modules/), [for esbuild](https://esbuild.github.io/content-types/#external-file)).

0 commit comments

Comments
 (0)