# allocationを束でsettleする

> V2のAllocationがauthorizerの承認を表し、executorがSettlementFactoryでallocationの束を正味でsettleし、繰り返しのsettlementを続ける仕組みを追います。

- URL: https://cardene.dev/diagrams/canton-allocation-v2
- 公開日: 2026-11-24
- 更新日: 2026-11-24
- トピック: Blockchain
- タグ: Canton
- 親の図解: [allocationによる決済](https://cardene.dev/diagrams/canton-ext-allocation.md): DvPや支払いのsettlementのために、アプリが利用者へallocationを求め、walletがregistryに作らせ、V1の取り置いた資産とV2の承認をsettleするまでを、章の中の図解で追います。

前の頁では、V1のallocationが、legごとに資産を取り置き、締め切りの中でsettleされる所を見ました。
この頁は、V2の`Allocation`が、authorizerの承認をどう表し、executorが、allocationの束をどう正味でsettleし、繰り返しのsettlementをどう続けるかを追います。
`Splice.Api.Token.AllocationV2`が定める型も、図の後に表で並べます。

https://cardene.dev/diagrams/canton-allocation-v1

settlementは、アプリが実行したい、受け渡しのまとまりで、settleは、それを実行する事です。
transfer legは、settlementの中の、2つのaccountの間の1つの受け渡しで、legは、その短い呼び名です。
executorは、settlementを実行するparty、adminは、資産を管理するregistryのアプリを表すpartyです。
authorizerは、settlementのtransferを承認するaccountで、accountは、章「Token StandardのDaml API」の頁「holdingとmetadataの型を知る」が追います。

https://cardene.dev/diagrams/canton-holding-metadata

authorizerのaccountのpartyは、そのaccountに関わる、accountの所有者と提供者のpartyです。
正味は、同じinstrumentについて、accountごとに全てのtransferの入金と引き落としを差し引いた結果の量です。

choiceとconsumingとnonconsumingは、章「Damlのtemplateとchoice」の頁「choiceでコントラクトを変える」が追っています。

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

## V2のallocationは承認を表す

V2の`Allocation`は、settlementの一部として、transfer legの正味の量の資産を送るか受け取る事への、authorizerの承認を表すコントラクトです。
V2のsettlementでは、executorと、instrumentのadminが、全てのtransfer legに、相手の側からの合う承認がある事を確かめます。
instrumentは、registryが管理するトークンの種類です。

V1の`Allocation`は資産の取り置きを表し、V2の`Allocation`は承認を表します。
同じinstrumentのadminを持つallocationの束を正味でsettleする仕組みは、2つ後の節で見ます。

## 3つのchoiceを呼ぶ者

V2の`Allocation`は、`Allocation_Settle`と`Allocation_Cancel`と`Allocation_Withdraw`の3つのchoiceを持ち、どれも`actors`をcontrollerにします。
`actors`は、そのchoiceを実行するpartyの集まりで、accountそのものではありません。

| choice | 行う事 | 呼ぶ者 | 返す物 |
|---|---|---|---|
| `Allocation_Settle` | transfer legの正味の量を動かして、allocationをsettleする | 既定では、`actors`がallocationの`admin`と`executors`に等しい事を求める | `AllocationResult` |
| `Allocation_Cancel` | allocationを取り消して資金を放し、settlementを実行する承認を無効にする | 普通は、settlementが実行されないと分かった時のexecutorと、期限の切れたallocationを取り消すadmin | `AllocationResult` |
| `Allocation_Withdraw` | authorizerがallocationを引き出して資金を放し、settlementを実行する承認を無効にする。誤って作ったallocationを取り消すのに使える | 既定では、authorizerのaccountのparty | `AllocationResult` |

3つのchoiceで、実装は、`actors`を確かめて、許されない操作を防ぐ決まりです。
`Allocation_Cancel`では、既定で、`actors`がallocationの`executors`に等しい事を求めるべきです。
`Allocation_Settle`では、既定で、`actors`がallocationの`admin`と`executors`に等しい事を求め、両者が共にatomicなsettlementを保証できるようにするべきです。
atomicは、全体が成功するか、何も起きないかのどちらかになる事です。

`Allocation_Withdraw`では、既定で、authorizerのaccountのpartyに引き出しを許すべきです。

`Allocation_Settle`の承認は、普通、`SettlementFactory_SettleBatch`のchoiceの一部として与えられ、executorは、V2のallocationをsettleするのに、この`SettlementFactory_SettleBatch`を使うべきです。
settlementの締め切りを設けた時、実装は、締め切りが過ぎた後に`Allocation_Settle`を呼ばせてはいけません。
`committed`が`True`のallocationは、settlementの締め切りが過ぎた後にしか、`Allocation_Withdraw`をexerciseできません。

V2の`Allocation_Cancel`と`Allocation_Settle`と`Allocation_Withdraw`は、nonconsumingです。
nonconsumingにするのは、トランザクションの読み取りの互換のために、consumingのV1のchoiceの`Allocation_Cancel`と`Allocation_ExecuteTransfer`と`Allocation_Withdraw`を呼ぶなど、別の消し方を許すためです。
そのため、3つのchoiceのどれでも、実装は、choiceの本体でallocationを必ず消費します。

`Allocation_Settle`には、繰り返しのsettlementのための2つの欄があります。

| 欄 | 中身 | 設定できる時 |
|---|---|---|
| `extraTransferLegSides` | settlementの一部としてsettleする、追加のtransfer legの側 | allocationのauthorizerが、繰り返しのsettlementを有効にしている時だけ |
| `nextIterationFunding` | この回の後に、次のsettlementの回のために取り置く資金として渡す値。`None`は、この回の後にもう回が無い事を表す | 同上 |

この欄は、`AllocationSpecification`の同名の欄とは別の値です。
`AllocationSpecification`の欄は、元のallocationで繰り返しのsettlementを有効にしたかと、取り置いた資金を表し、次の節の後で見ます。

実装は、V2の`Allocation`に、`Archive`のchoiceと、3つのchoiceの中身を与える`allocation_cancelImpl`と`allocation_settleImpl`と`allocation_withdrawImpl`のmethod、各choiceの追加のobserverを返す3つの`*ExtraObservers`のmethodを持たせます。
`AllocationAction`は、allocationに対して取れる操作で、4つの形を持ちます。

| 形 | 表す物 |
|---|---|
| `AA_Settle` | settle |
| `AA_Cancel` | 取り消し |
| `AA_Withdraw` | 引き出し |
| `AA_Custom` | 名前空間を付けたIDで表す、registryごとの操作 |

## 束を正味でsettleする

`SettlementFactory`のinterfaceは、同じinstrumentのadminのallocationの束を、正味でsettleできるようにします。
`SettlementFactory_SettleBatch`のchoiceは、同じinstrumentのadminを持つinstrumentのallocationの束をsettleします。
controllerは`actors`で、`SettlementFactory_SettleBatchResult`を返します。

この形なのは、効率とプライバシーのためです。
instrumentのadminがsettleする全てのtransferをまたいで、accountごとの正味の引き落としと入金だけを行えます。
また、各入金と引き落としが見える範囲を、executorとadminと、影響を受けるaccountのpartyだけに制限できます。

`SettlementFactory`は、`Archive`のchoiceと、`actors`がcontrollerで`SettlementFactoryView`を返す`SettlementFactory_PublicFetch`と、`SettlementFactory_SettleBatch`を持ちます。
methodは、`settlementFactory_publicFetchImpl`と`settlementFactory_settleBatchExtraObservers`と`settlementFactory_settleBatchImpl`を持ちます。

次の図は、executorがallocationの束を、factoryでsettleして、正味の入金と引き落としが行われるまでを、正味で動かす場合の例として追います。
個々のallocationはauthorizer側の承認で、渡す`allocations`の一覧全体が、送り手と受け手の両方の承認の証明になります。
allocationごとの承認の確かめ方は、実装が決めます。
正味の入金と引き落としの段は、factoryの段として置きます。

```dragon
sequence executorがallocationの束を正味でsettleする例

box Exec as executor [note: settlementを実行する, color: rose, icon: user]
box Factory as settlement factory [note: 同じadminの束をsettleする, color: slate, icon: server]
box Alloc as allocationの束 [note: authorizerの承認, color: blue, icon: lock]

step executorが束を確定させる
  say executorは、settlementのために確定させたallocationの束を、factoryのchoiceに渡します。束の中のtransfer legは、少なくとも1つ要り、全てfactoryと同じinstrumentのadminを持ちます。
  Exec -> Factory: 束をsettleする
  source CantonのSplice.Api.Token.AllocationV2 [url: https://docs.canton.network/sdks-tools/api-reference/splice-daml/splice-api-token-allocation-v2/splice-api-token-allocationv2]

step 承認を確かめる
  say 個々のallocationは、authorizer側の承認です。渡された一覧全体が、送り手と受け手の両方が、settlementの一部として行う全てのtransferを承認した証明になります。holderが作ったallocationをsettleする時にadminが何を確かめるかは、choiceの実装が決めます。
  Factory -> Alloc: 承認を確かめる
  source CantonのSplice.Api.Token.AllocationV2 [url: https://docs.canton.network/sdks-tools/api-reference/splice-daml/splice-api-token-allocation-v2/splice-api-token-allocationv2]

step 正味で動かす
  say この例では、instrumentのadminがsettleする全てのtransferをまたいで、accountごとの正味の引き落としと入金だけを行います。この形は、各入金と引き落としが見える範囲を、executorとadminと、影響を受けるaccountのpartyだけに制限できる設計です。
  act Factory: accountごとの正味で動かす
  source CantonのSplice.Api.Token.AllocationV2 [url: https://docs.canton.network/sdks-tools/api-reference/splice-daml/splice-api-token-allocation-v2/splice-api-token-allocationv2]

step 結果が束の順に返る
  say 結果は、束の中の各allocationをsettleした結果を、choiceの引数と同じ順で持ちます。
  Factory -> Exec: 結果を返す
  source CantonのSplice.Api.Token.AllocationV2 [url: https://docs.canton.network/sdks-tools/api-reference/splice-daml/splice-api-token-allocation-v2/splice-api-token-allocationv2]
```

次の表は、`SettlementFactory_SettleBatch`の欄と、関わる型を並べます。

| 項目 | 中身 |
|---|---|
| `settlement` | settlement |
| `transferLegs` | 少なくとも1つ要り、全てのtransfer legが、factoryと同じinstrumentのadminを持つ。この全てのlegをまたいで、accountごとの正味を求める |
| `allocations` | `FinalizedAllocation`のlist。個々のallocationはauthorizer側の承認で、一覧全体が、settlementの一部として行う全てのtransferを、送り手と受け手の両方が承認した証明 |
| `actors` | 実装ごとの承認の形に、既定でないcontrollerを設定できる。実装は、許されない実行を防ぐため、これを確かめる決まりで、既定では、アプリとの互換を最大にするため、`settlement.executors`に等しい事を確かめるべき |
| `extraArgs` | 追加の引数 |
| `FinalizedAllocation` | executorがsettlementのために確定させたallocation。欄は`allocationCid`と`extraTransferLegSides`と`nextIterationFunding`。`extraTransferLegSides`は、authorizerが繰り返しのsettlementを有効にしていない限り空にする決まりで、`nextIterationFunding`も、有効にしていない限り設定しない |
| `SettlementFactoryView` | この`SettlementFactory`を使えるinstrumentを管理するregistryのアプリを表す`admin`と、拡張のための`meta` |
| `SettlementFactory_SettleBatchResult` | 束の中の各allocationをsettleした結果`allocationSettleResults`を、choiceの引数の`allocations`と同じ順で持つ。`meta`も持つ |

## allocationの指定と繰り返しのsettlement

V2の`AllocationSpecification`は、settlementの一部として、資産を受け取るか送る事への、authorizerの承認です。
欄は、次の7つです。

| 欄 | 中身 |
|---|---|
| `admin` | settlementで移すinstrumentの資産のadmin |
| `authorizer` | settlementのtransferを承認する`Account` |
| `transferLegSides` | 承認するlegの側 |
| `settlementDeadline` | executorとauthorizerが合意した、allocationの有効期限 |
| `nextIterationFunding` | 次のsettlementの回の資金として取り置く量 |
| `committed` | authorizerがallocationに責任を持つかを表す |
| `meta` | 繰り返しのsettlementで使うallocationの情報を蓄える、追加のデータ |

`settlementDeadline`を設定した時は、この時刻の後にallocationをsettleできず、authorizerは、allocationを引き出して資金を放せます。

`committed`は、executorがallocationをsettleするか、executorが取り消すか、settlementの締め切りが過ぎるか、adminが期限を切るまで、authorizerがallocationに責任を持つかを表します。
`True`なら、authorizerは、settlementの締め切りまで、allocationを引き出せません。
executorが、settlementまでallocationがある事の保証を要する場合に、committedのallocationを使います。

`AllocationSpecification`の`nextIterationFunding`は、次のsettlementの回の資金として取り置く量で、instrumentのIDをkeyにし、量は必ず正にします。
この欄は、元のallocationで繰り返しのsettlementを有効にしたかを表し、`Allocation_Settle`や`FinalizedAllocation`に渡す、同名の欄とは別の値です。
次の図は、`AllocationSpecification`の`nextIterationFunding`の3つの値が、繰り返しのsettlementをどう変えるかを追います。

```dragon
mind allocationの指定の次の回の資金で繰り返しが決まる

step 値で繰り返しが変わる
  say allocationの指定の次の回の資金は、繰り返しのsettlementを使うか、次の回の資金を取り置くかを決めます。
  idea root as "allocationの指定の次の回の資金" [color: slate]
  source CantonのSplice.Api.Token.AllocationV2 [url: https://docs.canton.network/sdks-tools/api-reference/splice-daml/splice-api-token-allocation-v2/splice-api-token-allocationv2]

step 値が無い
  say 値が無い時は、繰り返しのsettlementは無効で、allocationは、指定したtransfer legで、ちょうど1回だけsettleできます。
  idea none as "値が無い" of root [color: blue]
  idea none1 as "繰り返しは無効" of none [color: blue]
  idea none2 as "ちょうど1回だけsettleできる" of none [color: blue]
  source CantonのSplice.Api.Token.AllocationV2 [url: https://docs.canton.network/sdks-tools/api-reference/splice-daml/splice-api-token-allocation-v2/splice-api-token-allocationv2]

step 空のmap
  say 空のmapなら、繰り返しのsettlementは有効ですが、authorizerは次の回の資金を取り置きません。次の回に入ってくるtransferを見込むauthorizerが使えます。
  idea empty as "空のmap" of root [color: blue]
  idea empty1 as "繰り返しは有効" of empty [color: blue]
  idea empty2 as "次の回の資金は取り置かない" of empty [color: blue]
  source CantonのSplice.Api.Token.AllocationV2 [url: https://docs.canton.network/sdks-tools/api-reference/splice-daml/splice-api-token-allocation-v2/splice-api-token-allocationv2]

step 量を入れる
  say 量を入れた時は、その量を、instrumentのIDをkeyにして、次の回の資金として取り置きます。量は必ず正にします。
  idea funded as "正の量" of root [color: blue]
  idea funded1 as "次の回の資金を取り置く" of funded [color: blue]
  source CantonのSplice.Api.Token.AllocationV2 [url: https://docs.canton.network/sdks-tools/api-reference/splice-daml/splice-api-token-allocation-v2/splice-api-token-allocationv2]
```

## 結果とviewの欄

`AllocationResult`は、allocationのchoiceの結果で、`output`と、settlementでauthorizerに作った新しいholdingをinstrumentのIDをkeyに持つ`authorizerHoldingCids`と、`meta`を持ちます。
`AllocationResult_Output`は、4つの形を持ちます。

| 形 | 伝える事 |
|---|---|
| `AllocationResult_Pending` | allocationが、さらに段を待つ事。処理が続く、今のallocationのコントラクトIDを`allocationCid`で伝える |
| `AllocationResult_Settled` | `Allocation_Settle`でsettleした結果。settleが終わった後に、次のsettlementの回のために作った新しいallocationがあれば、そのコントラクトIDを`nextIterationAllocationCid`に持つ |
| `AllocationResult_Cancelled` | `Allocation_Cancel`が十分に承認された時の結果 |
| `AllocationResult_Withdrawn` | `Allocation_Withdraw`が十分に承認された時の結果 |

V2の`AllocationView`は、settleできる状態のallocationのviewです。

| 欄 | 中身 |
|---|---|
| `originalAllocationCid` | 元のallocationのコントラクトID。元のallocation自身では`None`になる。walletは、状態の更新をまたいで同じallocationを結び付けるのに使うべきです。同じsettlementの別のallocationを結び付ける時は、view直下の`settlement`を使います |
| `settlement` | settlement |
| `allocation` | allocationの指定 |
| `holdingCids` | allocationを裏付けるholding。walletがallocationとholdingを結び付けるために示し、holdingを台帳の上に表さないregistryでは、空でもかまわない |
| `createdAt` | allocationを最初に作った時刻 |
| `numIterations` | これまでに、このallocationで行ったsettlementの回の数 |
| `expiresAt` | `Optional Time`で、省略できる。時刻が指定された場合は、動きの無いallocationが期限を迎える時刻 |
| `availableActions` | 操作を、どのpartyの組が行えるかを、集合の集合で表す |
| `meta` | 拡張のための追加のデータ |

`expiresAt`が指定されている時、その後に、registryは、allocationの期限を切り、固めた資金をauthorizerに返してもかまいません。
期限を切ると、使われていないallocationが占有する保存領域を回収できます。
また、denial-of-serviceの攻撃から身を守れます。
denial-of-serviceの攻撃は、正規の利用者によるサービスの利用を妨げる攻撃です。

registryは、期限をsettlementの締め切りにできるだけ近づける事と、settlementの回ごとに期限を延ばす事で、要らない更新を避けるべきです。

`availableActions`は、walletの利用者に、操作を取れるかと、誰を待つかを知らせます。
共同の承認を要する操作には、registryごとの委任のコントラクトが要ります。

## settlementとlegの型

V2の`SettlementInfo`は、settlementを一意に指す参照で、walletが、同じsettlementへのallocationを結び付けるのに使えます。
executorは、`(id, cid, meta)`の3つの組が、settlementをまたいで一意である事を保証する決まりです。

| 型 | 欄 | 中身 |
|---|---|---|
| `SettlementInfo` | `executors`、`id`、`cid`、`meta` | `executors`は、settlementを実行して、資産のadminをまたぐatomicなsettlementを保証するparty。`id`は文字列の識別子、`cid`は`Optional AnyContractId` |
| `TransferLeg` | `transferLegId`、`sender`、`receiver`、`amount`、`instrumentId`、`meta` | `sender`と`receiver`は`Account`。`instrumentId`は、instrumentのadminが使う識別子で、`Text` |
| `TransferLegSide` | `transferLegId`、`side`、`otherside`、`amount`、`instrumentId`、`meta` | transfer legの片側で、allocationが承認するのはこの片側。`side`は`TransferSide`で、`otherside`は相手の側のaccountで、`ReceiverSide`なら送り手、`SenderSide`なら受け手 |
| `TransferSide` | `SenderSide`、`ReceiverSide` | 資産を送る外向きの側と、資産を受け取る内向きの側の2つの形 |

## 出典

https://docs.canton.network/sdks-tools/api-reference/splice-daml/splice-api-token-allocation-v2/splice-api-token-allocationv2
