# allocationの作成を頼む

> walletがregistryのfactoryにallocationの作成を頼み、時間のかかる作成をAllocationInstructionで追い、V2で受け入れて取り下げ、期限切れで終わるまでを追います。

- URL: https://cardene.dev/diagrams/canton-allocation-instruction
- 公開日: 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するまでを、章の中の図解で追います。

前の頁では、アプリが利用者のwalletへallocationを求める依頼を見ました。
この頁は、求められたwalletが、registryにallocationをどう作らせるかを追います。
registryがallocationを直に作らず、複数の段で作る時は、`AllocationInstruction`で状態を追い、送り手が取り下げられます。

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

registryは、トークンを管理するアプリで、instrumentは、registryが管理するトークンの種類です。
holdingは、partyの持つ1つのトークンの保有で、allocationを作る時の資金になります。
adminは、registryのアプリを表すpartyです。
これらの語は、章「Token StandardのDaml API」の頁「Token Standardで資産を送る」と「holdingとmetadataの型を知る」が追います。

https://cardene.dev/diagrams/canton-token-standard

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

## walletがfactoryでallocationを作らせる

`Splice.Api.Token.AllocationInstructionV1`は、walletがregistryにallocationを作るよう指示するためのinterfaceをまとめます。
`AllocationFactory`を実装するコントラクトは、registryのアプリから取り出し、walletが、allocation instructionか、直にallocationを作るのに使います。
factoryの取り出し方は、章「registryのAPI」の頁「allocationの文脈を受け取る」が追います。

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

view`AllocationFactoryView`は、このfactoryを使えるinstrumentを管理するregistryのアプリを表すparty`admin`と、拡張のための`meta`を持ちます。

factoryの`AllocationFactory_Allocate`は、送り手のwalletが、settlementの特定のlegに資産のallocationを求める、汎用のchoiceです。
controllerは`allocation.transferLeg.sender`です。
結果として、allocationが直に作られるか、代わりにallocation instructionが作られるかは、registryしだいです。

次の表は、V1の`AllocationFactory_Allocate`の引数と返す物を並べます。

| 欄 | 中身 |
|---|---|
| `expectedAdmin` | factoryを出すadminのpartyの見込み |
| `allocation` | 作る`AllocationSpecification` |
| `requestedAt` | 依頼の時刻 |
| `inputHoldingCids` | allocationの資金に使うholding |
| `extraArgs` | 追加の引数 |
| 返す物 | `AllocationInstructionResult` |

`AllocationSpecification`は、settlementの特定のlegへの資産のallocationの指定で、頁「資産を取り置いてsettleする」が追います。

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

実装は、`expectedAdmin`がfactoryのadminと一致する事を必ず確かめます。
呼ぶ側は、`expectedAdmin`を、自分のParticipant Nodeから読むなど、信頼できる出どころから得るべきです。
そうすれば、vetした全てのDamlのpackageが、expected adminを確かめるinterfaceの実装だけを持つ限り、信頼できない出どころから得たfactoryでも、choiceを安全にexerciseできます。
vetは、packageをvettingすることです。

vettingは、Participant Nodeが他のParticipant Nodeに、どのpackageを支えるかを伝える手段です。
Participant NodeがSynchronizerにVettedPackagesのtopology transactionを登録して行います。
章「packageのuploadとvetting」が追っています。

https://cardene.dev/diagrams/canton-package-upload-vetting

`inputHoldingCids`は、allocationの資金に使うべきholdingです。
holdingを台帳の上に表さないregistryや、allocationのためのholdingを自動で選ぶregistryでは、空でもかまいません。
`inputHoldingCids`を指定した時は、成功したallocationが、それらを全てarchiveする決まりです。
そのため、同じholdingを指定した別のallocationは、先に成功した方がholdingを消費していて、作れなくなります。

送り手は、同じholdingを指定して成功時に消費する事で、同じ資金で両方のallocationが作られる事を防ぎ、allocationの重複を防げます。

`AllocationFactory_PublicFetch`は、`actor`がcontrollerのchoiceで、`expectedAdmin`と`actor`を受け取り、`AllocationFactoryView`を返します。
各interfaceは、実装するtemplateのsignatoryがcontrollerの`Archive`のchoiceを持ちます。
実装は、`allocationFactory_allocateImpl`と`allocationFactory_publicFetchImpl`と`allocationInstruction_updateImpl`と`allocationInstruction_withdrawImpl`などのmethodで、choiceの中身を与えます。

次の図は、walletがregistryのアプリからfactoryを取り出し、台帳の上のfactoryに作成を求め、allocation instructionができて、registryが状態を更新し、送り手が取り下げるまでを追います。
この図は、registryが複数の段で進める場合を描き、直にallocationを作る場合は、段の説明で触れます。
registryが状態を更新する段と、送り手が取り下げる段は、allocation instructionのコントラクトの段として置きます。

```dragon
sequence walletがallocationの作成を頼んで追う

box Reg as registryのアプリ [note: factoryを出す, color: rose, icon: server]
box Wallet as 送り手のwallet [note: allocationを求める, color: blue, icon: card]
box Fac as factory [note: 台帳のコントラクト, color: slate, icon: doc]
box Inst as allocation instruction [note: 作成の状態を追う, color: slate, icon: doc]

step factoryを取り出す
  say walletは、registryのアプリから、allocationを作るためのfactoryを取り出します。
  Wallet -> Reg: factoryを取り出す
  source CantonのSplice.Api.Token.AllocationInstructionV1 [url: https://docs.canton.network/sdks-tools/api-reference/splice-daml/splice-api-token-allocation-instruction-v1/splice-api-token-allocationinstructionv1]

step 作成を求める
  say 送り手のwalletは、台帳の上のfactoryに、settlementの特定のlegのために、allocationの作成を求めます。結果として、allocationが直に作られるか、allocation instructionが作られるかは、registryしだいです。
  Wallet -> Fac: 作成を求める
  source CantonのSplice.Api.Token.AllocationInstructionV1 [url: https://docs.canton.network/sdks-tools/api-reference/splice-daml/splice-api-token-allocation-instruction-v1/splice-api-token-allocationinstructionv1]

step allocation instructionができる
  say registryが複数の段で進める時は、allocation instructionが作られ、作成の状態を追います。
  Fac -> Inst: 作る
  source CantonのSplice.Api.Token.AllocationInstructionV1 [url: https://docs.canton.network/sdks-tools/api-reference/splice-daml/splice-api-token-allocation-instruction-v1/splice-api-token-allocationinstructionv1]

step registryが段を進める
  say registryは、内部の処理の段を進めて、allocation instructionの状態を更新します。理由はmetadataで伝えられます。
  act Inst: registryが状態を更新する
  source CantonのSplice.Api.Token.AllocationInstructionV1 [url: https://docs.canton.network/sdks-tools/api-reference/splice-daml/splice-api-token-allocation-instruction-v1/splice-api-token-allocationinstructionv1]

step 送り手が取り下げる
  say 送り手は、送り手として、allocation instructionのコントラクトに取り下げのchoiceを行えます。
  act Inst: 送り手が取り下げる
  source CantonのSplice.Api.Token.AllocationInstructionV1 [url: https://docs.canton.network/sdks-tools/api-reference/splice-daml/splice-api-token-allocation-instruction-v1/splice-api-token-allocationinstructionv1]
```

## V1のallocation instructionを追う

`AllocationInstruction`のinterfaceは、registryのアプリにallocationを作るよう求めた、allocation instructionの状態を追います。
registryは、allocation instructionを複数の段で進めてもかまいません。
その時は、このinterfaceのchoiceだけを使うべきです。
interfaceのchoiceだけで進めるのは、walletがトランザクションの履歴を確実に読み、allocationが最後に成功したか失敗したかを判断できるようにするためです。

V1は、allocation instructionに2つのchoiceを持ちます。

| choice | controller | 使う場面 | 受け取る物 | 返す物 |
|---|---|---|---|---|
| `AllocationInstruction_Update` | instrumentのadminと`extraActors` | registryが内部の処理の段を進めて、状態を更新する。理由はmetadataで伝える | `extraActors`と`extraArgs` | `AllocationInstructionResult` |
| `AllocationInstruction_Withdraw` | 送り手 | 送り手としてallocation instructionを取り下げる | `extraArgs` | `AllocationInstructionResult` |

`AllocationInstruction_Update`では、実装は、`extraActors`が、その更新に見込むactorを含む事を必ず確かめます。

`AllocationInstructionResult`は、allocationを指示するか、instructionの状態を進めた結果です。
`output`と`senderChangeCids`と`meta`を持ち、`meta`は、たとえば、かかった手数料を伝えます。
`senderChangeCids`は、資金に使わなかった残額として送り手に返す新しいholdingです。
呼ぶ側は、`senderChangeCids`を使って、1つのDamlトランザクションで、複数のallocation instructionをまとめて作るか更新できます。

`AllocationInstructionResult_Output`は、3つの形を持ちます。
`AllocationInstructionResult_Pending`は、作成がまだ終わらず、さらに段を待つ事を伝えます。
`AllocationInstructionResult_Completed`と`AllocationInstructionResult_Failed`は、作成が終わった結果を伝えます。

| 形 | 伝える事 |
|---|---|
| `AllocationInstructionResult_Pending` | allocationの作成が、さらに段を待つ事。`allocationInstructionCid`と共に伝える |
| `AllocationInstructionResult_Completed` | allocationが作られた事。新しいallocationのコントラクトID`allocationCid`と共に伝える |
| `AllocationInstructionResult_Failed` | allocationの作成が成功せず、資金のために取り置いたholdingを全て放した事 |

view`AllocationInstructionView`は、6つの欄を持ちます。

| 欄 | 中身 |
|---|---|
| `originalInstructionCid` | 元のallocation instructionのコントラクトID。walletが、複数の段を通した更新の流れを追うのに使い、registryが複数の段で進める時だけ入る |
| `allocation` | 作るallocation |
| `pendingActions` | allocationを作るために、partyごとに残る作業の`Map Party Text`。allocationの準備に要る、registryごとの処理の進み具合を伝えるのに使える |
| `requestedAt` | 依頼の時刻 |
| `inputHoldingCids` | 資金に使うholding。holdingを台帳の上に表さないregistryでは、空でもかまわない |
| `meta` | より細かい状態の情報 |

## V2は受け入れと取り下げを持つ

V2の`AllocationFactory_Allocate`は、特定のsettlementのためのallocationの作成を求めます。
controllerは`actors`で、直にallocationを作るか、allocation instructionを作るかは、registryしだいです。
V2の`AllocationFactory_PublicFetch`は、fetchを行うpartyの集まり`actors`がcontrollerで、`AllocationFactoryView`を返します。

V1との違いを、次の表に並べます。

| 項目 | V1 | V2 |
|---|---|---|
| `AllocationFactory_Allocate`の欄 | `expectedAdmin`と`allocation`と`requestedAt`と`inputHoldingCids`と`extraArgs` | `settlement`と`allocation`と`requestedAt`と`inputHoldingCids`と`extraArgs`と`actors` |
| `AllocationFactory_Allocate`の返す物 | `AllocationInstructionResult` | `AllocationInstructionResult` |
| adminの確かめ | `expectedAdmin`がfactoryのadminと一致する事を確かめる | `expectedAdmin`の欄が無い。allocationのadminのpartyがfactoryのadminと一致する事を確かめる |
| allocation instructionのchoice | `AllocationInstruction_Update`と`AllocationInstruction_Withdraw`と`Archive` | `AllocationInstruction_Accept`と`AllocationInstruction_Withdraw`と`Archive`の3つで、`Update`は無い |
| 資金に使わなかった残額の欄 | `senderChangeCids` | `authorizerChangeCids` |

V2の実装は、資金に使わなかった入力のholdingと残額を、全て`AllocationInstructionResult`の`authorizerChangeCids`で返す決まりです。
また、`actors`の実際の値を確かめて、許されないallocationの作成を防ぐ決まりです。
`actors`は`[Party]`の型で、`authorizer`は`Account`の型なので、実装は、`actors`が`authorizer`のaccountを承認できるpartyの組かを確かめ、そうなら、allocationの開始を許すべきです。
accountの提供者が資金を割り当てる権利を持つaccountでは、`actors`がaccountの提供者のpartyの組の時も、許すべきです。

V2の`AllocationInstruction_Accept`は、allocation instructionを認める必要のある者として受け入れます。
controllerは`actors`で、`actors`と`extraArgs`を受け取り、`AllocationInstructionResult`を返します。
受け入れに要る権限は、registryが自由に決められますが、単独で受け入れられるpartyを、`AllocationInstructionView.availableActions`で必ず報告します。
単独で動けるpartyを報告させるのは、walletが、利用者に受け入れの選択肢を見せられるようにするためです。

V2の`AllocationInstruction_Withdraw`は、allocation instructionを取り下げ、controllerは`actors`です。
実装は、許されない取り下げを防ぐため、`actors`を必ず確かめます。
取り下げに要る権限もregistryが決められますが、単独で取り下げられるpartyを`availableActions`で必ず報告し、walletが取り下げの選択肢を見せられるようにします。

V2の`AllocationInstructionAction`は、allocation instructionの状態を進める操作で、3つの形を持ちます。

| 形 | 表す物 |
|---|---|
| `AIA_Withdraw` | 取り下げ |
| `AIA_Accept` | 受け入れ |
| `AIA_Custom` | 名前空間を付けた操作のIDで、metadataのkeyと同じように付ける。allocationを作るために要る、registryごとの操作 |

V2のinterfaceは、各choiceの追加のobserverを返す、`allocationFactory_allocateExtraObservers`と`allocationInstruction_acceptExtraObservers`と`allocationInstruction_withdrawExtraObservers`のmethodを足します。

V2の`AllocationInstructionResult`は、`output`と`authorizerChangeCids`と`meta`を持ちます。
`output`は、V1と同じく、`AllocationInstructionResult_Pending`と`AllocationInstructionResult_Completed`と`AllocationInstructionResult_Failed`の3つの形のどれかです。
`authorizerChangeCids`は、資金に使わなかったか、残額として作ったauthorizerのholdingを、`instrumentId.id`をkeyにした`TextMap`で持ちます。
V1の`senderChangeCids`と同じく、1つのDamlトランザクションで複数のallocation instructionをまとめて扱うのに使えます。

## V2のviewと期限切れ

V2のview`AllocationInstructionView`は、8つの欄を持ちます。

| 欄 | 中身 |
|---|---|
| `originalInstructionCid` | 処理の最初の段でない時に入る、最初のallocation instructionのコントラクトID。walletが、状態の更新をまたいで同じinstructionを結び付けるのに使うべき。同じsettlementの別のallocation instructionの結び付けには使わず、view直下の`settlement`の欄を使う |
| `settlement` | settlement |
| `allocation` | 作るallocation |
| `requestedAt` | 依頼の時刻 |
| `inputHoldingCids` | 資金の要らないallocationでは空。holdingを台帳の上に表さないregistryでも、空でもかまわない |
| `expiresAt` | `Optional Time`で、省略できる。設定された場合は、動きの無いallocation instructionが期限を迎える時刻 |
| `availableActions` | どの操作を、どのpartyの組が行えるか |
| `meta` | 拡張のための追加のデータ |

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

registryは、期限をallocationのsettlementの締め切りにできるだけ近づける事と、allocation instructionへの操作のたびに期限を延ばす事で、要らない期限切れを避けるべきです。

`availableActions`は、どの操作を、どのpartyの組が行えるかを表します。
外側のlistは集合の集合として読み、内側のlistのpartyが共に行えば、操作を行えます。
walletは、`availableActions`を使って、利用者に操作を取れるかどうかと、どのほかのpartyの操作を待つかを知らせられます。
共同の承認が要る操作には複数のpartyを指定でき、行うには、registryごとの委任のコントラクトが揃っている事が前提です。

次の図は、allocation instructionの戻り値を、まだ終わらない待機中と、終わりの作成済みと失敗に分け、最後に、V2で期限が設定された時だけ起き得る期限切れを別の処理として追います。
この図は、作成済みと失敗と期限切れを、別々の場合として並べます。
資金に使ったholdingが放される段と、期限を切る段は、それぞれ対象のコントラクトの段として置きます。

```dragon
sequence allocation instructionの戻り値と期限切れ

box Inst as allocation instruction [note: 作成を待つ, color: slate, icon: doc]
box Reg as registry [note: 作成を進める, color: rose, icon: server]
box Alloc as allocation [note: 作られる物, color: coin, icon: lock]
box Hold as 資金に使うholding [note: 取り置いた保有, color: blue, icon: card]

step 待機中を返す
  say walletが作成を求めると、registryが複数の段で進める時は、allocation instructionが、さらに段を待つ待機中の戻り値で始まります。待機中は、作成の終わりではありません。V1もV2も同じ形です。
  Reg -> Inst: 待機中を返す
  source CantonのSplice.Api.Token.AllocationInstructionV1 [url: https://docs.canton.network/sdks-tools/api-reference/splice-daml/splice-api-token-allocation-instruction-v1/splice-api-token-allocationinstructionv1]

step 作成済みを返す場合
  say allocationが作られた事は、新しいallocationのコントラクトIDと共に伝えられます。V1もV2も同じ形です。
  Reg -> Alloc: 作る
  source CantonのSplice.Api.Token.AllocationInstructionV1 [url: https://docs.canton.network/sdks-tools/api-reference/splice-daml/splice-api-token-allocation-instruction-v1/splice-api-token-allocationinstructionv1]

step 失敗を返す場合
  say allocationの作成が成功せず、資金のために取り置いたholdingを全て放した事を伝えます。V1もV2も同じ形です。
  act Hold: 全て放される
  source CantonのSplice.Api.Token.AllocationInstructionV1 [url: https://docs.canton.network/sdks-tools/api-reference/splice-daml/splice-api-token-allocation-instruction-v1/splice-api-token-allocationinstructionv1]

step V2だけ 期限を切る
  say V2で期限が設定されている時は、動きの無いallocation instructionの期限を、registryが期限の時刻の後に切ってもかまいません。これは、戻り値とは別の処理です。
  act Inst: 期限を切る
  source CantonのSplice.Api.Token.AllocationInstructionV2 [url: https://docs.canton.network/sdks-tools/api-reference/splice-daml/splice-api-token-allocation-instruction-v2/splice-api-token-allocationinstructionv2]
```

## 次に読む頁

次の頁「資産を取り置いてsettleする」は、V1のallocationが、資産を取り置き、締め切りの中でsettleされ、引き出され、取り消されるまでを追います。

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

## 出典

https://docs.canton.network/sdks-tools/api-reference/splice-daml/splice-api-token-allocation-instruction-v1/splice-api-token-allocationinstructionv1

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