# walletからtokenを送る

> walletが、registryからfactoryを取り、Ledger APIでchoiceを実行してトークンのtransferを送り、1段で終わったかを結果から知る手順を追います。

- URL: https://cardene.dev/diagrams/canton-token-wallet-transfer
- 公開日: 2026-11-24
- 更新日: 2026-11-24
- トピック: Blockchain
- タグ: Canton
- 親の図解: [walletのtokenの扱い](https://cardene.dev/diagrams/canton-ext-wallet-token.md): walletを作る人が、Token Standardのtokenの残高と履歴を読み、transferを送って受け、保有を少なく保つ手順を、章の中の図解で追います。

前の頁「transferを受け取る2つの形」は、Amuletのtransferを2段と1段で受け取る形を、保有とlockの動きで追いました。

https://cardene.dev/diagrams/canton-wallet-transfer-kinds

この頁は、walletがトークンのtransferを送る側と受ける側として、registryとLedger APIを使う手順を追います。
続けて、transferが1段で終わったかを、walletがどこで知るかを確かめます。

registryは、トークンのinstrumentを管理し、所有の記録を持ち、台帳の上のDamlのinterfaceと、台帳の外のHTTPのAPIを出すアプリです。
factoryは、transferの流れを始めるchoiceを出す、registryのコントラクトで、choice contextは、そのchoiceを実行するために要る追加のデータです。
Token StandardのAPIの全体像は、V1がCIP-0056の本文、V2がCIP-0112の本文が示し、使い方の背景は、Spliceのソースコードの`token-standard`のREADMEが示します。

## factoryを取ってchoiceを実行する

Token Standardのfactoryでchoiceを実行するには、先に、対応するregistryからfactoryを取ります。
instrumentの`admin`のpartyのIDからregistryのURLへの対応は、CNS(Canton Name Service)を元にする見込みの汎用の仕組みができるまで、walletが自分で保ちます。
registryは、該当するエンドポイントで、`TransferFactory`か`AllocationFactory`を返します。

次の図Aは、walletがregistryとLedger APIを使ってtransferを送り、結果を受ける流れを、4つの段で追います。

```dragon
sequence walletがtransferを送る流れ

box R as registry [note: factoryと文脈を返す側, color: slate, icon: server]
box W as wallet [note: transferを送る側, color: rose, icon: user]
box L as Ledger API [note: validatorのノード, color: blue, icon: server]

step factoryを求める
  say walletは、まず対応するregistryに、factoryを求めます。
  W -> R: factoryを求める
  source CantonのToken Standard [url: https://docs.canton.network/appdev/deep-dives/token-standard]

step factoryと文脈が返る
  say registryの応答は、factoryのコントラクトのIDと、開示するコントラクトと、choiceの文脈のデータを持ちます。
  R -> W: 文脈を返す
  source CantonのToken Standard [url: https://docs.canton.network/appdev/deep-dives/token-standard]

step choiceを実行する
  say walletは、factoryのIDを指すchoiceの実行の要求に、開示するコントラクトを添えて、Ledger APIに送ります。external partyは、準備して署名してから送ります。
  W -> L: choiceを実行する
  source CantonのToken Standard [url: https://docs.canton.network/appdev/deep-dives/token-standard]

step 結果を受ける
  say 台帳への効果を含むトランザクションが返り、可視なexerciseの結果のoutputが、transferが1段で終わったかを示します。
  L -> W: 結果を返す
  source CantonのToken Standard [url: https://docs.canton.network/appdev/deep-dives/token-standard]
```

registryの応答の`factoryId`は、factoryのコントラクトのIDです。
`disclosedContracts`は、choiceの引数とは別の物で、factoryのchoiceのexerciseに渡さないと、choiceが動きません。
`choiceContextData`は、choiceの引数の`context`として渡します。
`context`の位置は、choiceの引数の直下ではなく、transferのchoiceでは`extraArgs`の中で、CLIの例は`choiceArgument.extraArgs.context`に置きます。

実行の要求は、external partyでないpartyでは`POST /v2/commands/submit-and-wait-for-transaction`、external partyでは`prepare`で準備して署名した後の`POST /v2/interactive-submission/executeAndWaitForTransaction`に送ります。
`disclosedContracts`は、choiceの引数に入れず、この準備か送信の要求に渡します。
素の`submit-and-wait`の応答は`updateId`と`completionOffset`だけで、choiceの`output`を含みません。
素の`execute`も署名済みの要求への応答で、`output`を返さないので、結果を読む時は、上の2つの台帳への効果を含む形式を指定できるエンドポイントを使います。

どちらの場合も、要求に`ExerciseCommand`を入れ、`templateId`にfactoryのinterfaceのID、`contractId`にregistryから得た`factoryId`を書きます。
interfaceのIDの例は`#splice-api-token-transfer-instruction-v2:Splice.Api.Token.TransferInstructionV2:TransferFactory`です。
`choice`は実行するchoiceの名前で、例は`TransferFactory_Transfer`です。
`choiceArgument`は、JSONから変換されてDamlのchoiceに渡る引数で、`TransferFactory_Transfer`では、`transfer`に送り手と受け手と額を含み、`actors`と`extraArgs`も持ちます。

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

external partyの署名の流れは、頁「interactive submission」が追っています。

https://cardene.dev/diagrams/canton-interactive-submission

## factoryでないchoiceも同じ道で実行する

Token Standardのinterfaceを実装したコントラクトでchoiceを実行する時も、factoryのchoiceと同じ要求の道を使います。
external partyでないpartyは`submit-and-wait-for-transaction`、external partyは準備して署名した後の`executeAndWaitForTransaction`のエンドポイントで、結果のトランザクションまで受け取ります。
`ExerciseCommand`の`templateId`はコントラクトのinterfaceのIDで、例は`#splice-api-token-transfer-instruction-v2:Splice.Api.Token.TransferInstructionV2:TransferInstruction`です。
`contractId`は対象のコントラクトのIDで、ふつうpartyの今のACSから得ます。

ACSは、active contract setの略で、Participant Nodeが今持っている有効なコントラクトの集まりです。
読み方は、前の頁「walletで残高と履歴を読む」が追っています。

https://cardene.dev/diagrams/canton-token-wallet-read

choiceの例は`TransferInstruction_Accept`で、`choiceArgument`は、JSONから変換されます。
transferのchoiceと受け入れのchoiceは、`extraArgs`を持ち、`context`は、その`extraArgs`の中に置きます。

`choiceArgument`に`context`が要るのは、次の表の5つの場合で、対応するregistryから取れます。

| 対象 | choice |
|---|---|
| TransferInstruction | accept、reject、withdraw |
| Allocation | withdraw、cancel |

contextを返すregistryのエンドポイントの応答は、2つの欄を持ちます。
`choiceContextData`は`choiceArgument`の`context`に、`disclosedContracts`は、choiceの引数ではなく、準備か送信の要求に渡します。
`AllocationRequest_Reject`と`AllocationRequest_Withdraw`は、空のchoice contextで呼びます。
この2つのchoiceの`ChoiceContext`は、実装の振る舞いを将来広げる余地を残すために置かれています。

## 1段で終わったかは結果で知る

`TransferFactory_Transfer`と`TransferInstruction_Accept`は、どちらも`TransferInstructionResult`を返し、その`output`の欄が、何が起きたかを示します。
次の図Bは、最初の`TransferFactory_Transfer`の結果として、`output`の3つの値と、walletが次にする事を追います。
この図では、3つの値を、`output`の型が挙げる順に並べます。

```dragon
mind transferの結果の見分け方

step 結果の欄
  say transferを頼んだ最初のchoiceの結果は、outputの欄で、何が起きたかを示します。
  idea root as "transferの結果" [color: slate]
  source CantonのToken Standard [url: https://docs.canton.network/appdev/deep-dives/token-standard]

step 保留
  say 保留は、transferに追加の段が要る事を表します。返ったtransferInstructionCidの申し出で、次の操作を確かめます。
  idea p as "保留" of root [color: blue]
  idea p1 as "申し出で次の操作を確かめる" of p [color: blue]
  source CantonのV2のtransferInstruction [url: https://docs.canton.network/sdks-tools/api-reference/splice-daml/splice-api-token-transfer-instruction-v2/splice-api-token-transferinstructionv2]

step 完了
  say 完了は、transferが成功した事を表し、受け手が受け取った保有が並びます。
  idea c as "完了" of root [color: blue]
  idea c1 as "受け手の保有が並ぶ" of c [color: blue]
  source CantonのV2のtransferInstruction [url: https://docs.canton.network/sdks-tools/api-reference/splice-daml/splice-api-token-transfer-instruction-v2/splice-api-token-transferinstructionv2]

step 失敗
  say 失敗は、transferが成功せず、手数料を除いて保有が送り手へ戻った事を表します。
  idea f as "失敗" of root [color: blue]
  idea f1 as "手数料を除いて送り手へ戻る" of f [color: blue]
  source CantonのV2のtransferInstruction [url: https://docs.canton.network/sdks-tools/api-reference/splice-daml/splice-api-token-transfer-instruction-v2/splice-api-token-transferinstructionv2]
```

`output`が`TransferInstructionResult_Pending`なら、返った`transferInstructionCid`の申し出で、次に要る操作を確かめます。
次の操作は、受け手の受け入れとは限らず、受け入れの代わりに、拒否か取り下げも使えます。

`output`が`TransferInstructionResult_Completed`なら、transferは成功し、`receiverHoldingCids`が受け手の新しい保有を並べます。
台帳に保有を表さない実装では、`receiverHoldingCids`が空の場合があります。

`output`が`TransferInstructionResult_Failed`なら、transferは成功せず、手数料を除いて、保有は送り手へ戻ります。

後続の`TransferInstruction_Accept`も同じ型の結果を返し、実装ごとに3つのどれも返せます。
そのため、`Completed`だけを見ても、最初の要求から1段で終わったかは分かりません。

1段で終わったかは、台帳でchoiceを実行した結果として見ます。
V1の`TransferFactory`の応答は、実行の前に、registryが選んだtransferの方式を`transferKind`で示します。
この方式は、実行後に台帳で確かめる`output`とは別の物です。

external partyでないpartyでは、台帳への効果を含むトランザクションの形式で送ると、結果のトランザクションに、可視な`ExercisedEvent`の`exerciseResult`として`output`が入ります。
external partyでは、署名して実行した後に、同じ形式のトランザクションから`output`を読みます。

`prepare`の応答は、署名前のトランザクションで、choiceの結果を直接読む欄を持ちません。

V2の`TransferFactory_Transfer`は、`actors`の全てが承認に要る全てのpartyを含む時に、1段で終わらせる実装が推奨されています。
Canton Coinでは、受け手が有効な事前承認を持ち、実装がそれを受け手の代わりの承認として使う時に、`actors`は足ります。
このため、受け取るtransferには、既に入金された残高として出る物と、保留の申し出として出る物があります。

## 6つのAPIの参照

Token StandardのAPIの参照は、次の表の6つに分かれます。
V1の背景はCIP-0056、V2の背景はCIP-0112の、detailsの節が補います。
Damlの参照の欄は台帳の上のDamlの型の仕様で、OpenAPIの参照の欄は台帳の外のHTTPのAPIの定義です。
どちらの参照も、章「Token StandardのDaml API」と章「registryのAPI」の頁が追っています。

| API | Damlの参照 | OpenAPIの参照 |
|---|---|---|
| Token Metadata | `splice-api-token-metadata-v1` | Token Metadata Service |
| Holding | V2が`splice-api-token-holding-v2`、V1が`splice-api-token-holding-v1` | なし |
| Transfer Instruction | `splice-api-token-transfer-instruction-v2`と`splice-api-token-transfer-instruction-v1` | Transfer Instruction V2 APIとV1 API |
| Allocation | `splice-api-token-allocation-v2`と`splice-api-token-allocation-v1` | Allocation V2 APIとV1 API |
| Allocation Instruction | `splice-api-token-allocation-instruction-v2`と`splice-api-token-allocation-instruction-v1` | Allocation Instruction V2 APIとV1 API |
| Allocation Request | `splice-api-token-allocation-request-v2`と`splice-api-token-allocation-request-v1` | なし |

Holdingは、CIP-0056の複数のregistryにまたがる保有を表示するportfolio viewの実装を可能にします。
Transfer Instructionは、1段か2段で、partyからpartyへ資産だけを渡すFOPのtransferの流れを可能にします。
Allocationは、Allocation InstructionとAllocation Requestと合わせて、DvPのtransferの流れを可能にします。

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

FOPは、Free of Paymentの略で、代金の支払いと結び付けずに資産だけを渡す送金です。
FOPとDvPの流れは、頁「Token Standardで資産を送る」が追っています。

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

## registryのトークンのDARを入れ忘れると解釈できない

validatorは、registryのトークンのDARのパッケージを入れないと、そのトークンに関わるコントラクトとトランザクションを解釈できません。
DARは、main packageと、それが依存する全てのpackageからなるファイルです。
トークンのDARをvalidatorに入れる事は、台帳の外のregistryのAPIと話すWallet SDKやCLIのコードを持つ事とは別です。

registryが出したトークンのtransferが、`UNRESOLVED_PACKAGE_NAME`のようなエラーで解決できない時は、該当するDARがvalidatorにuploadされ、vettingされているかを確かめます。
vettingは、章「Damlのpackage」の頁「packageをvettingして使う」が追っています。

https://cardene.dev/diagrams/canton-dar-vetting

## 次に読む頁

次の頁「holdingのUTXOを少なく保つ」は、walletの提供者が、userの保有の数を少なく保ち、`MergeDelegation`で自動でまとめる手順を追います。

:::details UTXO
UTXOは、Bitcoinが使う残高の記録の方式で、まだ使われていないトランザクションの出力を1つずつ記録します。
出力は1度使うと消え、新しい出力が作られます。
:::

https://cardene.dev/diagrams/canton-token-holding-merge

## 出典

https://docs.canton.network/appdev/deep-dives/token-standard

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

https://docs.canton.network/reference/json-api-reference/post-v2commandssubmit-and-wait-for-transaction

https://docs.canton.network/reference/json-api-reference/post-v2interactive-submissionexecuteandwaitfortransaction
