# Damlのtemplateとchoice

> IOUの例でDARを入れてコントラクトを動かし、templateで作り、choiceで変え、choiceの本体の式で台帳を更新し、keyで今のコントラクトを引き、期限と条件をコントラクトに書く所を、章の中の図解で追います。

- URL: https://cardene.dev/diagrams/canton-daml-templates-choices
- 公開日: 2026-11-22
- 更新日: 2026-11-22
- トピック: Blockchain
- タグ: Canton
- 親の図解: [Canton Networkの全体像](https://cardene.dev/diagrams/canton-network.md): トランザクションの中身を関わるpartyにだけ見せるCanton Networkについて、部品ごとの役割と、1つのトランザクションが部品の間をどう流れるかを段ごとに追います。
- 子の図解: [IOUの例でコントラクトを動かす](https://cardene.dev/diagrams/canton-iou-tutorial.md): IOUの例で、DARを2つのParticipant Nodeに入れ、Ledger APIのコマンドでIOUと塗装の申し出を作って受けるまでと、Alice、Bob、Bankに何が見えるかを追います。
- 子の図解: [templateでコントラクトを作る](https://cardene.dev/diagrams/canton-daml-template-choice.md): Damlのtemplateのフィールドとsignatoryとobserverとensureとkeyが、誰の同意でコントラクトを作り、誰に見せるかをどう決めるかを、コントラクトの状態の図とtemplateの部品のつながりの図で追います。
- 子の図解: [choiceでコントラクトを変える](https://cardene.dev/diagrams/canton-daml-contract-choice.md): Damlのchoiceのcontrollerと消費の種類が誰に何を許すかと、exerciseが台帳にactionの木としてどう残るかを、Contactの例とDaml Language Referenceのchoiceの節で追います。
- 子の図解: [choiceの本体で台帳を更新する](https://cardene.dev/diagrams/canton-daml-update-actions.md): Damlのchoiceの本体でコントラクトをcreate、fetch、exercise、archiveする式が、何を受け取って何を返し、誰の承認が要るかと、keyで指す式とPreludeの型を追います。
- 子の図解: [keyで今のコントラクトを引く](https://cardene.dev/diagrams/canton-contract-keys.md): contract keyで、IDが変わるコントラクトの今の状態を引く式と要る承認と、同じkeyのコントラクトが重なる時に返る順を、Contract Keysの節とCanton 3.5.1、3.5.2、3.6.1のリリースノートで追います。
- 子の図解: [期限と条件をコントラクトに書く](https://cardene.dev/diagrams/canton-daml-time.md): Damlのensureとassertでコントラクトのデータと変え方に条件を付け、ledger timeとrecord timeの違いを踏まえて、期限と遅れと時間の窓でchoiceの受け入れを限る形を、2つの図で追います。

前の章「dpmのプロジェクトとビルド」では、Cantonの開発のコマンドラインの道具の`dpm`で、プロジェクトを整えて作る所を見ました。
この章では、Damlのtemplateでコントラクトを作り、choiceでコントラクトを変える所を追います。

まず、Cantonの手引きのGetting Started TutorialのIOU(I-Owe-You)の例で、DARを2つのParticipant Nodeに入れ、Ledger APIのコマンドでIOUと塗装の申し出を作って受けるまでを見ます。
IOUは、例の筋書きで銀行が発行するデジタルの銀行券です。
誰として、どのParticipant Nodeから送るとIOUを作れるかと、Alice、Bob、Bankのそれぞれに何が見えるかも追います。

https://cardene.dev/diagrams/canton-iou-tutorial

続いて、Damlのtemplateのフィールドとsignatoryとobserverとensureとkeyが、誰の同意でコントラクトを作り、誰に見せるかをどう決めるかを見ます。
コントラクトがactiveになりarchiveされるまでの状態と、templateの部品どうしのつながりを追います。

https://cardene.dev/diagrams/canton-daml-template-choice

さらに、choiceのcontrollerと消費の種類が誰に何を許し、exerciseが台帳にactionの木としてどう残るかを見ます。
AliceとBobが`Contact`のchoiceをexerciseする順と、transaction viewのactionの木を追います。

https://cardene.dev/diagrams/canton-daml-contract-choice

そのうえで、choiceの本体でコントラクトをcreate、fetch、exercise、archiveする式が、何を受け取って何を返し、誰の承認が要るかを見ます。
式が受け取る物と返す物と、式ごとに要る承認と役を追い、keyでコントラクトを指す式とPreludeの型とtypeclassも見ます。

https://cardene.dev/diagrams/canton-daml-update-actions

次に、contract keyでコントラクトの今の状態をどう引くかと、一意でないkeyが重なる時にどのコントラクトが返るかを見ます。
keyで引く式に要る承認と、同じkeyのコントラクトが返る順を追います。

https://cardene.dev/diagrams/canton-contract-keys

最後に、ensureとassertでコントラクトのデータと変え方に条件を付け、ledger timeで期限と時間の窓を守る形を見ます。
営業時間の終わりの直前に送った償還のledger timeと記憶域への書き込みの時刻と、Coinの移転の受け入れに足す時間の確認を追います。

https://cardene.dev/diagrams/canton-daml-time

図の語は、それぞれの頁が説明します。

下の図は、templateとledger timeとDARを置き、templateの中にsignatory、key、observer、choiceを、choiceの中に引数、本体、controllerを置きます。
虫眼鏡の印が付いた部品を押すと、その部品の中へ入ります。
外側へ戻る時は、右上の「戻る」か、上に並んだ名前を押します。
部品の中へ入ると、名前の並びの下にその部品を説明する頁の題が出て、押すとその頁を開きます。

```dragon
architecture Damlのtemplateとchoiceを外から内へ
group Top as Damlのtemplateとchoice [icon: net]
  box Tpl as template [note: 作れるコントラクトの型を定める, color: blue, link: /diagrams/canton-daml-template-choice]
  box Lt as ledger time [note: トランザクションごとに決まる時刻, color: coin, link: /diagrams/canton-daml-time]
  box Dar as DAR [note: main packageと依存するpackageからなる, color: slate, link: /diagrams/canton-iou-tutorial]
end
group TplIn as templateの中 [color: blue]
  box Sig as signatory [note: 作成に同意が要るparty, color: slate, link: /diagrams/canton-daml-template-choice]
  box Key as key [note: IDが変わっても今のコントラクトを指す, color: rose, link: /diagrams/canton-contract-keys]
  box Obs as observer [note: 見られるparty, color: slate, link: /diagrams/canton-daml-template-choice]
  box Ch as choice [note: 権限の付いた関数, color: coin, link: /diagrams/canton-daml-contract-choice]
end
group ChIn as choiceの中 [color: coin]
  box Arg as 引数 [note: 追加の情報が要る時だけ付ける, color: slate, link: /diagrams/canton-daml-contract-choice]
  box Body as 本体 [note: exerciseされた時に起きることを定める, color: blue, link: /diagrams/canton-daml-update-actions]
  box Ctl as controller [note: exerciseできるpartyを決める, color: rose, link: /diagrams/canton-daml-contract-choice]
end

step Damlのtemplateとchoiceを外から内へ
  say templateは、main packageを1つと、main packageが依存する全てのpackageからなる、DARのファイルにまとめます。ledger timeは、トランザクションごとに決まる、マイクロ秒単位の時刻です。choiceの本体では、今の時刻を取る関数でledger timeを取れます。この関数は、choiceをexerciseできる時を限るのに、ふつう使います。
  Tpl -> Dar: DARにまとめる
  source CantonのGetting Started Tutorial [url: https://docs.canton.network/global-synchronizer/canton-console/getting-started-tutorial]
  source CantonのChoices [url: https://docs.canton.network/appdev/modules/m3-choices]
  source CantonのContract Templates [url: https://docs.canton.network/appdev/modules/m3-contract-templates]
  source CantonのDevelopment Environment Setup [url: https://docs.canton.network/appdev/modules/m3-dev-environment]
  source CantonのDaml Language Reference [url: https://docs.canton.network/appdev/reference/daml-language-reference]
  source CantonのWorking with Time [url: https://docs.canton.network/appdev/modules/m3-working-with-time]
  source CantonのDA.Time [url: https://docs.canton.network/appdev/reference/daml-standard-library/da-time]
  source CantonのDA.Fail [url: https://docs.canton.network/appdev/reference/daml-standard-library/da-fail]

step templateの中
  say コントラクトは、全てのsignatoryに見えます。つまり、コンパイラが、signatoryを自動でobserverに加えます。signatoryがコントラクトの作成に同意すると、このコントラクトで実行できるchoiceのconsequencesも承認したことになります。contract keyは変わらないので、コントラクトIDが変わっても、keyで今activeなコントラクトを指せます。
  Sig -> Obs: 自動で加わる
  Sig -> Ch: consequencesを承認
  source CantonのContract Templates [url: https://docs.canton.network/appdev/modules/m3-contract-templates]
  source CantonのDevelopment Environment Setup [url: https://docs.canton.network/appdev/modules/m3-dev-environment]
  source CantonのDaml Language Reference [url: https://docs.canton.network/appdev/reference/daml-language-reference]
  source CantonのContract Keys [url: https://docs.canton.network/appdev/modules/m3-contract-keys]
  source Canton 3.5.1のリリースノート [url: https://docs.canton.network/global-synchronizer/release-notes/canton-releases/3-5-1]
  source Canton 3.5.2のリリースノート [url: https://docs.canton.network/global-synchronizer/release-notes/canton-releases/3-5-2]
  source Canton 3.6.1のリリースノート [url: https://docs.canton.network/global-synchronizer/release-notes/canton-releases/3-6-1]
  source CantonのChoices [url: https://docs.canton.network/appdev/modules/m3-choices]

step choiceの中
  say controllerは、choiceをexerciseできるpartyを決めます。Daml Language Referenceのchoiceの節では、choiceの引数にpartyを入れると、そのpartyをchoiceのcontrollerにできます。そうすると、controllerを、コントラクトを作る時でなく、choiceをexerciseする時に決められます。本体は、choiceがexerciseされた時に起きることを定めます。
  Arg -> Ctl: controllerにできる
  source CantonのChoices [url: https://docs.canton.network/appdev/modules/m3-choices]
  source CantonのDaml Language Reference [url: https://docs.canton.network/appdev/reference/daml-language-reference]
  source CantonのPrelude [url: https://docs.canton.network/appdev/reference/daml-standard-library/prelude]
```

一番外の階では、templateをDARのファイルにまとめます。
DARのファイルは、main packageを1つと、main packageが依存する全てのpackageからなります。

ledger timeは、トランザクションごとに決まる、マイクロ秒単位の時刻です。
choiceの本体では、`getTime`でledger timeを取れます。
`getTime`は、choiceをexerciseできる時を限るのに、ふつう使います。

templateの中では、コントラクトは全てのsignatoryに見えます。
つまり、コンパイラが、signatoryを自動でobserverに加えます。
signatoryがコントラクトの作成に同意すると、このコントラクトで実行できるchoiceのconsequencesも承認したことになります。
contract keyは変わらないので、コントラクトIDが変わっても、keyで今activeなコントラクトを指せます。

choiceを実行した時に、その中で続けて起きるcreateやarchiveのようなactionを、そのchoiceのconsequencesと呼びます。

choiceの中では、controllerが、choiceをexerciseできるpartyを決めます。
Daml Language Referenceのchoiceの節では、choiceの引数に`Party`を入れると、その`Party`をchoiceのcontrollerにできます。
そうすると、controllerを、コントラクトを作る時でなく、choiceをexerciseする時に決められます。
本体は、choiceがexerciseされた時に起きることを定めます。

## 出典

https://docs.canton.network/global-synchronizer/canton-console/getting-started-tutorial

https://docs.canton.network/appdev/modules/m3-contract-templates

https://docs.canton.network/appdev/modules/m3-dev-environment

https://docs.canton.network/appdev/modules/m3-choices

https://docs.canton.network/appdev/reference/daml-language-reference

https://docs.canton.network/appdev/reference/daml-standard-library/prelude

https://docs.canton.network/appdev/modules/m3-contract-keys

https://docs.canton.network/global-synchronizer/release-notes/canton-releases/3-5-1

https://docs.canton.network/global-synchronizer/release-notes/canton-releases/3-5-2

https://docs.canton.network/global-synchronizer/release-notes/canton-releases/3-6-1

https://docs.canton.network/appdev/modules/m3-working-with-time

https://docs.canton.network/appdev/reference/daml-standard-library/da-time

https://docs.canton.network/appdev/reference/daml-standard-library/da-fail
