Skip to content
DAML By Example

First Contract

-- FirstContract.daml
module FirstContract where

-- A template defines the structure and rules of a contract.
-- `with` declares the contract's fields.
-- `where` contains the rules: signatories, observers, choices.
template Asset
  with
    issuer  : Party    -- who created the asset
    owner   : Party    -- current holder
    name    : Text     -- human-readable label
    amount  : Decimal  -- quantity
  where
    -- At least one signatory is required.
    -- Signatories must authorize the creation of this contract.
    signatory issuer

    -- Observers can see the contract but cannot act on it.
    observer owner

    -- `ensure` is a contract invariant checked on every creation.
    -- If False, the transaction is aborted.
    ensure amount > 0.0

    -- The `agreement` field is an optional human-readable string.
    agreement
      show issuer <> " issued " <> show amount
      <> " of " <> name <> " to " <> show owner

Key points

Template structure

  • A template produces a record type of the same name that is used to construct create commands.
  • DAML is not object-oriented. Templates do not inherit from each other; use interfaces for polymorphism.

Contract lifecycle

  • this refers to the current contract's data inside a choice body.
  • self refers to the ContractId of the current contract inside a choice body.
  • A ContractId T is a typed reference to an active contract. It becomes stale (unusable) once the contract is archived.

Configuration

  • signatory, observer, ensure, and agreement are evaluated when the contract is created.