Skip to main content

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.yaml file.

Command line configuration

To view all available command line configuration options for Daml Codegen for JavaScript, run dpm codegen-js --help in your terminal:

Project file configuration

Specify the above settings in the codegen 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.js

References

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>
Types:
DamlTypeScriptTypeScript definition
()Unit{}
BoolBoolboolean
IntIntstring
DecimalDecimalstring
Numeric νNumericstring
TextTextstring
TimeTimestring
PartyPartystring
[τ]List<τ>τ[]
DateDatestring
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.
Given the above definition, the generated TypeScript code will be as follows.
Variants (a.k.a. sum types)
This is a Daml type for a language of additive expressions.
In TypeScript, it is represented as a discriminated union.
Sum of products
Let’s slightly modify the Expr a type of the last section into the following.
Compared to the earlier definition, the Add case is now in terms of a record with fields lhs and rhs. This renders in TypeScript like so.
Note how the definition of the Add case has given rise to a record type definition Expr.Add.
Enums
Given a Daml enumeration like this,
the generated TypeScript will consist of a type declaration and the definition of an associated companion object.
Templates and Choices
Here is a Daml template of a basic ‘IOU’ contract.
The dpm codegen-js command generates types for each of the choices defined on the template as well as the template itself.
Each template results in the generation of an interface and a companion object. Here, is a schematic of the one generated from the Iou template1,2.
See the Use contracts and transactions in JavaScript for details on how to use generated code to interact with the JSON Ledger API.

Footnotes

  1. The undefined type parameter captures the fact that Iou has no contract key.
  2. The never type parameter captures the fact that Iou does not implement directly any interface.