Documentation Index
Fetch the complete documentation index at: https://docs.canton.network/llms.txt
Use this file to discover all available pages before exploring further.
Daml Codegen for JavaScript
Use the Daml Codegen for JavaScript (dpm codegen-js) to generate JavaScript/TypeScript code representing all Daml data types defined in a Daml Archive (.dar) file.
The generated code makes it easier to construct types and work with JSON when using the JSON Ledger API.
See Get started with Canton and the JSON Ledger API for details on how to use the generated code to interact with JSON Ledger API. See the sections below for guidance on setting up and invoking the codegen.
Install
Install the Daml Codegen for JavaScript by installing DPM.Configure
To configure the Daml Codegen, choose one of the two following methods:- Command line configuration: Specify all settings directly in the command line.
- Project file configuration: Define all settings in the
daml.yamlfile.
Command line configuration
To view all available command line configuration options for Daml Codegen for JavaScript, rundpm codegen-js --help in your terminal:
Project file configuration
Specify the above settings in thecodegen element of the Daml project file daml.yaml.
Here is an example:
sdk-version: 3.4.9
name: quickstart
source: daml
init-script: Main:initialize
parties:
- Alice
- Bob
- USD_Bank
- EUR_Bank version: 0.0.1 exposed-modules:
- Main dependencies:
- daml-prim
- daml-stdlib codegen: js: output-directory: ui/daml.js npm-scope: daml.js
Operate
Run the Daml Codegen using project file configuration with: $ dpm codegen-js or using command line configuration with: $ dpm codegen-js ./.daml/dist/quickstart-0.0.1.dar -o ui/daml.js -s daml.jsReferences
Generated JavaScript/TypeScript code
Daml primitives to TypeScript
Daml built-in types are translated to the following equivalent types in TypeScript. The TypeScript equivalents of the primitive Daml types are provided by the @daml/types. Interfaces:interface Template<T extends object, K = unknown, I extends string = string>interface Choice<T extends object, C, R, K = unknown>
| Daml | TypeScript | TypeScript definition |
|---|---|---|
() | Unit | {} |
Bool | Bool | boolean |
Int | Int | string |
Decimal | Decimal | string |
Numeric ν | Numeric | string |
Text | Text | string |
Time | Time | string |
Party | Party | string |
[τ] | List<τ> | τ[] |
Date | Date | string |
ContractId τ | ContractId<τ> | string |
Optional τ | Optional<τ> | null | (null extends τ ? [] | [Exclude<τ, null>] : τ) |
TextMap τ | TextMap<τ> | { [key: string]: τ } |
(τ₁, τ₂) | Tuple₂<τ₁, τ₂> | {_1: τ₁; _2: τ₂} |
The types given in the TypeScript column are defined in @daml/types.
For n-tuples where n ≥ 3, representation is analogous with the pair case (the last line of the table).
The TypeScript types
Time, Decimal, Numeric and Int all alias to string. These choices relate to the avoidance of precision loss under serialization over the JSON Ledger API.The TypeScript definition of type
Optional<τ> in the above table might look complicated. It accounts for differences in the encoding of optional values when nested versus when they are not (i.e. “top-level”). For example, null and "foo" are two possible values of Optional<Text> whereas, [] and ["foo"] are two possible values of type Optional<Optional<Text>> (null is another possible value, [null] is not).Generated TypeScript mappings
The mappings from user-defined data types in Daml to TypeScript are best explained by example.Records (a.k.a. product types)
In Daml, we might model a person like this.Variants (a.k.a. sum types)
This is a Daml type for a language of additive expressions.Sum of products
Let’s slightly modify theExpr a type of the last section into the following.
Add case is now in terms of a record with fields lhs and rhs. This renders in TypeScript like so.
Add case has given rise to a record type definition Expr.Add.
Enums
Given a Daml enumeration like this,Templates and Choices
Here is a Daml template of a basic ‘IOU’ contract.dpm codegen-js command generates types for each of the choices defined on the template as well as the template itself.
Iou template1,2.