# allocationの文脈を受け取る

> allocationを作り、settleし、cancelやwithdrawをする前に、registryかScan Proxyからfactoryとchoice contextを受け取るエンドポイントと、V1とV2の違いを2つの図で追います。

- URL: https://cardene.dev/diagrams/canton-allocation-choices
- 公開日: 2026-11-24
- 更新日: 2026-11-24
- トピック: Blockchain
- タグ: Canton
- 親の図解: [registryのAPI](https://cardene.dev/diagrams/canton-ext-registry-api.md): walletがregistryかScan Proxyから、instrumentの情報とtransferとallocationのchoice contextを受け取る所を、章の中の図解で追います。

前の頁「transferの文脈を受け取る」では、transferのchoiceの前にfactoryとchoice contextを受け取るエンドポイントを見ました。

https://cardene.dev/diagrams/canton-transfer-choice-context

この頁では、同じ形のエンドポイントが、allocationのchoiceの前にもあることを追います。
registryが自ら出す版とScan Proxyの版があり、パスが`/registry/`の下か、`/v0/scan-proxy/registry/`の下かだけが違います。

## allocationの語

legは、決済の中の1つの受け渡しで、送り手、受け手、量、instrumentを持ちます。
V1の`Allocation`のコントラクトは、ロックした保有に裏付けられた、決済の特定のlegへの資産の予約を表し、`AllocationFactory`の`AllocationFactory_Allocate`をexerciseして作ります。
V2の`Allocation`は、account間の受け渡しに対する、送る側か受け取る側のaccountの承認を表します。
allocationは、複数のlegを持つDvPの決済を支えます。

:::details DvP
DvPは、Delivery versus Paymentの略で、証券などの資産の受け渡しと代金の支払いを同時に行い、片方だけが実行されることを防ぐ決済の方式です。
資産を渡す側と代金を払う側の、それぞれの受け渡しをlegと呼びます。
:::

allocation instructionと、allocationのV1とV2の働きは、章「allocationによる決済」が追っています。
allocation instructionは、頁「allocationの作成を頼む」が追っています。

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

## choice contextを返すエンドポイント

choice contextを返すエンドポイントは、allocationを作るエンドポイントと、settleするエンドポイントと、allocationを動かすエンドポイントと、allocation instructionを動かすエンドポイントに分かれます。
どのエンドポイントも、registryの台帳の外のAPIで、choiceを実行するのに要るchoice contextを返します。
V2のエンドポイントは、版0.6.11で足されました。

図Aは、エンドポイントを目的ごとの枝に分けて追います。
この図では、枝はエンドポイントの置き場だけを示し、エンドポイントどうしの関係は示しません。

```dragon
mind allocationのchoice contextを返すエンドポイント

step allocationを作るエンドポイント
  say allocationを作るには、factoryのchoiceを使い、その前にfactoryとchoice contextを返すエンドポイントを呼びます。V1とV2の2つがあります。
  idea root as "allocationの文脈" [color: slate]
  idea make as "allocationを作る" of root [color: blue]
  idea mk1 as "V1のfactory" of make [color: blue]
  idea mk2 as "V2のfactory" of make [color: blue]
  source CantonのPOST /registry/allocation-instruction/v1/allocation-factory [url: https://docs.canton.network/reference/splice-allocation-instruction-api/post-registryallocation-instructionv1allocation-factory]

step settleするエンドポイント
  say settleには、settleするchoiceを使い、その前にfactoryとchoice contextを返すエンドポイントを呼びます。V2だけにあります。
  idea settle as "settleする" of root [color: blue]
  idea st2 as "V2のsettlement-factory" of settle [color: blue]
  source CantonのPOST /registry/allocation/v2/settlement-factory [url: https://docs.canton.network/reference/splice-allocation-v2-api/post-registryallocationv2settlement-factory]

step allocationを動かすエンドポイント
  say walletは、作ったallocationのtransferの実行とcancelとwithdrawのchoiceの前に、choice contextを返すエンドポイントを呼びます。execute-transfer専用のエンドポイントは、この図ではV1にだけあります。
  idea move as "allocationを動かす" of root [color: blue]
  idea mv1 as "V1のexecute-transfer" of move [color: blue]
  idea mv2 as "V1のcancel" of move [color: blue]
  idea mv3 as "V1のwithdraw" of move [color: blue]
  idea mv4 as "V2のcancel" of move [color: blue]
  idea mv5 as "V2のwithdraw" of move [color: blue]
  source CantonのPOST /registry/allocations/v1/:allocationId/choice-contexts/execute-transfer [url: https://docs.canton.network/reference/splice-allocation-api/post-registryallocationsv1:allocationidchoice-contextsexecute-transfer]

step allocation instructionを動かすエンドポイント
  say allocation instructionには、acceptとwithdrawのchoice contextを返すエンドポイントがあり、どちらもV2です。
  idea ins as "instructionを動かす" of root [color: blue]
  idea in1 as "V2のaccept" of ins [color: blue]
  idea in2 as "V2のwithdraw" of ins [color: blue]
  source CantonのPOST /registry/allocation-instruction/v2/:allocationInstructionId/choice-contexts/accept [url: https://docs.canton.network/reference/splice-allocation-instruction-v2-api/post-registryallocation-instructionv2:allocationinstructionidchoice-contextsaccept]
```

エンドポイントのパスと返す物は、次の表のとおりで、どのパスもHTTPメソッドはPOSTです。
`:allocationId`は、対象のallocationのコントラクトIDで、`:allocationInstructionId`は、対象のallocation instructionのコントラクトIDです。
`execute-transfer`のallocationは、呼び手がtransferを実行したいallocationです。
`execute-transfer`専用のエンドポイントは、表ではV1にだけあり、V2でallocationをsettleするには、表の最後の`settlement-factory`のエンドポイントを使います。

| エンドポイント | 返す物 |
|---|---|
| `allocations/v1/:allocationId/choice-contexts/cancel` | allocationをcancelするchoiceのchoice context |
| `allocations/v1/:allocationId/choice-contexts/execute-transfer` | allocationのtransferを実行するchoiceのchoice context |
| `allocations/v1/:allocationId/choice-contexts/withdraw` | allocationをwithdrawするchoiceのchoice context |
| `allocation-instruction/v1/allocation-factory` | `AllocationFactory_Allocate`でallocationを作るfactoryとchoice context |
| `allocation-instruction/v2/:allocationInstructionId/choice-contexts/accept` | allocation instructionをacceptするchoiceのchoice context |
| `allocation-instruction/v2/:allocationInstructionId/choice-contexts/withdraw` | allocation instructionをwithdrawするchoiceのchoice context |
| `allocation-instruction/v2/allocation-factory` | `AllocationFactory_Allocate`でallocationを作るfactoryとchoice context |
| `allocations/v2/:allocationId/choice-contexts/cancel` | V2のallocationをcancelするchoiceのchoice context |
| `allocations/v2/:allocationId/choice-contexts/withdraw` | V2のallocationをwithdrawするchoiceのchoice context |
| `allocation/v2/settlement-factory` | `SettlementFactory_SettleBatch`でallocationをsettleするfactoryとchoice context |

## V1のfactoryを呼んで作る順

V1のfactoryのエンドポイントは、要求の本文に、必須の`choiceArguments`を持ちます。
`choiceArguments`は、factoryのchoiceに渡すつもりの引数です。
Damlの型の定義を繰り返さないために、JSONオブジェクトで書き、形は、`extraArgs.context`と`extraArgs.meta`を空のオブジェクトにして、Daml JSON APIで符号化した形です。

:::details JSON
JSONは、データを文字で表すための形式で、Webのプログラムどうしのデータのやり取りに広く使われています。
:::

引数を渡すのは、registryが引数に応じたコントラクトも返せるようにするためで、例は特定のinstrument-idの設定です。
応答は、必須の`factoryId`と`choiceContext`を持ちます。
`factoryId`は、factoryのinterfaceを実装するコントラクトのIDです。

`choiceContext`は、interfaceを通してコントラクトのchoiceをexerciseするのに要るcontextで、開示するコントラクトで渡す追加の参照データを取り出すのに使います。
そのコントラクトは、`choiceContextData`の中でコントラクトIDで指されます。

図Bでは、walletがfactoryとchoice contextを受け取り、Participant Nodeでallocationを作る所を追います。
段4は、段1の要求が失敗する別の場合で、この時、walletはchoiceの実行へ進みません。
この図では、registryは、Scan Proxyの版でも自ら出す版でも同じ箱で描きます。

```dragon
sequence factoryを呼んでallocationを作る順と、400か404の別の場合

box R as registry [note: allocationのfactoryを持つ, color: rose, icon: server]
box W as wallet [note: allocationを作る側, color: blue, icon: user]
box P as Participant Node [note: choiceを実行する, color: slate, icon: server]

step 引数を渡す
  say walletは、factoryに渡すつもりの引数を、choiceArgumentsに入れて、registryのfactoryのエンドポイントへ送ります。
  W -> R: 引数を渡す
  source CantonのPOST /registry/allocation-instruction/v1/allocation-factory [url: https://docs.canton.network/reference/splice-allocation-instruction-api/post-registryallocation-instructionv1allocation-factory]

step 文脈を受け取る
  say registryは、factoryのIDのfactoryIdと、choice contextを返します。
  R -> W: factoryと文脈を返す
  source CantonのPOST /registry/allocation-instruction/v1/allocation-factory [url: https://docs.canton.network/reference/splice-allocation-instruction-api/post-registryallocation-instructionv1allocation-factory]

step allocationを作る
  say walletは、factoryIdで指したfactoryのchoiceを、受け取ったchoice contextを使って実行する要求を送ります。このchoiceの実行が成功すると、allocationが作られます。
  W -> P: choiceを実行
  source CantonのPOST /registry/allocation-instruction/v1/allocation-factory [url: https://docs.canton.network/reference/splice-allocation-instruction-api/post-registryallocation-instructionv1allocation-factory]

step 別の場合は400か404
  say 段1の要求が不正なら、HTTPのステータスは400、対象が見つからなければ404になります。どちらも、応答の本文の必須のerrorの欄に文字列が入り、この場合は、choiceの実行へ進みません。
  R -> W: 400か404を返す
  source CantonのPOST /registry/allocation-instruction/v1/allocation-factory [url: https://docs.canton.network/reference/splice-allocation-instruction-api/post-registryallocation-instructionv1allocation-factory]
```

V1のallocationのchoice contextのエンドポイントと、factoryのエンドポイントは、400と404のステータスで、必須の`error`の欄を本文に持つ応答を返します。
V2のエンドポイントは、400と404と409のステータスで、必須の`error`の欄を本文に持つ応答を返します。
choice contextのエンドポイントの200の応答には、`choiceContextData`と`disclosedContracts`があります。

## エンドポイントの要求と応答に気を付ける点

操作別のchoice contextのエンドポイントは、cancelを例にすると、要求の本文に、任意の`meta`と、`excludeDebugFields`を持ちます。
`meta`は、choiceに渡すmetadataで、choice contextに取り込むべき物で、拡張のためにあります。
`excludeDebugFields`は、既定がfalseで、trueにすると、応答は`debug`で始まる欄を持たず、帯域を節約できます。
応答は、最上位に、必須の`choiceContextData`と`disclosedContracts`を持ちます。

factoryのエンドポイントは、`meta`を最上位に持たず、必須の`choiceArguments`と`excludeDebugFields`を受け取り、必須の`factoryId`と`choiceContext`を返します。

開示するコントラクトは、必須の`templateId`と`contractId`と`createdEventBlob`と`synchronizerId`の4つの欄を持ちます。
`contractId`は、開示するコントラクトのコントラクトIDです。
`synchronizerId`は、今コントラクトが割り当てられているSynchronizerの識別子です。

任意の`debugPackageName`と`debugPayload`と`debugCreatedAt`は、それぞれ、コントラクトを作るのに使ったDamlのpackageの名前と、コントラクトの引数と、作った時の台帳の有効時刻です。
3つとも、`createdEventBlob`の中身と一致しないことがあるので、データの提供者を信頼する時だけ使います。

コントラクトがreassignmentの途中なら、V2の応答の一覧に載る409になります。
V1の応答の一覧は400と404で、409はV1では`synchronizerId`の欄の説明にだけ書かれています。
reassignmentは、移す元のSynchronizerでunassignし、移す先のSynchronizerでassignする2段の手順です。

V2の開示するコントラクトは、欄に説明が付きます。
`templateId`は、完全修飾のtemplateの識別子です。
`createdEventBlob`は、JSON Ledger APIから取り出したまま、変えずに渡す、直列化したcreated eventです。

V1のcancelのエンドポイントの呼び出し例は、cURLとPythonとJavaScriptとPHPとGoとJavaとRubyの7種類で、メソッドはPOSTです。
どの例も、同じURLと要求の本文で送ります。

## 扱う大きさと先に取れる文脈

V2のallocation factoryのエンドポイントで、registryは、扱うallocationの大きさを制限してもよいです。
ただしアプリと広く互換にするため、registryは、transfer legが25以下のallocationの作成を、サポートすることが必須です。
settlement factoryのエンドポイントも同じで、registryは、transfer legが25以下のsettlement requestを、サポートすることが必須です。

資産の実装は、choice contextの値が、choiceの引数で渡したコントラクトIDに依らない形にするよう勧められています。
そうすれば、クライアントは、複数のToken Standardの操作を1つのDamlトランザクションでつなぐ時に、choice contextを先に取っておけます。

## 出典

https://docs.canton.network/reference/splice-allocation-api/post-registryallocationsv1:allocationidchoice-contextscancel

https://docs.canton.network/reference/splice-allocation-api/post-registryallocationsv1:allocationidchoice-contextsexecute-transfer

https://docs.canton.network/reference/splice-allocation-api/post-registryallocationsv1:allocationidchoice-contextswithdraw

https://docs.canton.network/reference/splice-allocation-instruction-api/post-registryallocation-instructionv1allocation-factory

https://docs.canton.network/reference/splice-allocation-instruction-v2-api/post-registryallocation-instructionv2:allocationinstructionidchoice-contextsaccept

https://docs.canton.network/reference/splice-allocation-instruction-v2-api/post-registryallocation-instructionv2:allocationinstructionidchoice-contextswithdraw

https://docs.canton.network/reference/splice-allocation-instruction-v2-api/post-registryallocation-instructionv2allocation-factory

https://docs.canton.network/reference/splice-allocation-v2-api/post-registryallocationsv2:allocationidchoice-contextscancel

https://docs.canton.network/reference/splice-allocation-v2-api/post-registryallocationsv2:allocationidchoice-contextswithdraw

https://docs.canton.network/reference/splice-allocation-v2-api/post-registryallocationv2settlement-factory

https://docs.canton.network/reference/splice-scan-proxy-allocation-api/post-v0scan-proxyregistryallocationsv1:allocationidchoice-contextscancel

https://docs.canton.network/reference/splice-scan-proxy-allocation-api/post-v0scan-proxyregistryallocationsv1:allocationidchoice-contextsexecute-transfer

https://docs.canton.network/reference/splice-scan-proxy-allocation-api/post-v0scan-proxyregistryallocationsv1:allocationidchoice-contextswithdraw

https://docs.canton.network/reference/splice-scan-proxy-allocation-instruction-api/post-v0scan-proxyregistryallocation-instructionv1allocation-factory

https://docs.canton.network/reference/splice-scan-proxy-allocation-instruction-v2-api/post-v0scan-proxyregistryallocation-instructionv2:allocationinstructionidchoice-contextsaccept

https://docs.canton.network/reference/splice-scan-proxy-allocation-instruction-v2-api/post-v0scan-proxyregistryallocation-instructionv2:allocationinstructionidchoice-contextswithdraw

https://docs.canton.network/reference/splice-scan-proxy-allocation-instruction-v2-api/post-v0scan-proxyregistryallocation-instructionv2allocation-factory

https://docs.canton.network/reference/splice-scan-proxy-allocation-v2-api/post-v0scan-proxyregistryallocationsv2:allocationidchoice-contextscancel

https://docs.canton.network/reference/splice-scan-proxy-allocation-v2-api/post-v0scan-proxyregistryallocationsv2:allocationidchoice-contextswithdraw

https://docs.canton.network/reference/splice-scan-proxy-allocation-v2-api/post-v0scan-proxyregistryallocationv2settlement-factory
