完整文档页面(中文翻译)。文末附有来源说明。

阅读英文版

integrationsintegration-patterns

集成模式

Common patterns for integrating Canton Network building blocks into your 应用

本页说明 common patterns for integrating Canton Network building blocks into 应用. These patterns help you design effective 集成 while respecting Canton’s privacy model.

Pattern 概览

flowchart TB
    subgraph Patterns[Integration Patterns]
        WAL[Wallet Integration<br>CC management]
        TOKEN[Token Operations<br>Standard tokens]
        APP[App Integration<br>Third-party apps]
        DATA[Data Access<br>Ledger queries]
    end

    subgraph Your App[Your Application]
        FE[Frontend]
        BE[Backend]
        SC[Smart Contracts]
    end

    WAL --> FE
    TOKEN --> SC
    APP --> BE
    DATA --> BE

钱包 集成 Pattern

Use Case

添加 Canton Coin(CC) management to your 应用, allowing 用户 to:

  • View their CC 余额
  • 转账 CC to other Party
  • Top up 流量 for 交易

架构

flowchart LR
    subgraph YourApp[Your Application]
        UI[User Interface]
        AUTH[Auth Service]
        API[Your API]
    end

    subgraph Wallet[Wallet Integration]
        SDK[Wallet SDK]
        LAPI[Ledger API]
    end

    UI --> AUTH
    UI --> API
    API --> SDK
    SDK --> LAPI

Key Considerations

ConsiderationApproach
认证Integrate with your auth system; map 用户 to Party
余额 display查询 via 钱包 SDK; cache appropriately
Transfers提交 via Ledger API; handle async confirmation
错误 handlingHandle insufficient 余额, 网络 错误

Privacy Implications

  • 用户 see only their own 余额
  • 转账 details visible only to sender and receiver
  • Your 应用 backend sees what it’s authorized to see

Token Operations Pattern

Use Case

创建, manage, and transfer tokens following the Canton Token Standard (CIP-0056).

架构

flowchart TB
    subgraph Contracts[Smart Contracts]
        TOKEN[Your Token Template]
        HOLDING[Holding Interface]
        TRANSFER[Transfer Interface]
    end

    subgraph Standard[Token Standard]
        CIP[CIP-0056 Interfaces]
    end

    TOKEN --> CIP
    HOLDING --> CIP
    TRANSFER --> CIP

Implementation Approach

-- Implement token standard interfaces
template MyToken
  with
    issuer : Party
    holder : Party
    amount : Decimal
  where
    signatory issuer
    observer holder

    -- Implement standard holding interface
    interface instance HoldingInterface for MyToken where
      view = HoldingView with
        custodian = issuer
        owner = holder
        amount = amount

    -- Standard transfer choice
    choice Transfer : ContractId MyToken
      with
        newHolder : Party
      controller holder
      do
        create this with holder = newHolder

Key Considerations

ConsiderationApproach
InteroperabilityFollow CIP-0056 for 钱包 compatibility
授权Define clear signatory/controller roles
PrivacyToken balances visible only to holders

应用 集成 Pattern

Use Case

Integrate with other Canton Network 应用, such as:

  • DeFi protocols
  • Identity 服务
  • Data feeds

架构

flowchart LR
    subgraph YourApp[Your Application]
        SC1[Your Contracts]
        BE1[Your Backend]
    end

    subgraph ThirdParty[Third-Party App]
        SC2[Their Contracts]
        API2[Their API]
    end

    SC1 <--> |Daml interfaces| SC2
    BE1 <--> |REST/gRPC| API2

集成 Approaches

ApproachWhen to Use
On-ledgerMulti-party 工作流 requiring atomic execution
Off-ledger API读取-only queries, non-transactional operations
HybridCombine both for complete 集成

Key Considerations

ConsiderationApproach
Interface compatibilityUse published Daml interfaces
授权Understand party requirements
PrivacyKnow what data is shared through composition
VersioningHandle 合约 upgrades

Data Access Pattern

Use Case

查询 ledger data for reporting, analytics, or 应用 state.

Options

方法Use CasePerformance
Ledger APIReal-time queries, streamingGood
PQSComplex queries, analyticsBest for reads
交易 treesHistorical data, auditDepends on volume

架构 with PQS

flowchart TB
    subgraph YourApp[Your Application]
        API[Your API]
        REPORT[Reporting]
    end

    subgraph Canton[Canton Infrastructure]
        PART[Validator Node]
        LAPI[Ledger API]
        PQS[PQS]
        PQSDB[(PQS PostgreSQL)]
    end

    API --> LAPI
    LAPI --> PART
    PART --> PQS
    REPORT --> PQSDB
    PQS --> PQSDB
PQS connects to the 验证者节点 via the Ledger API to receive 交易 updates, then stores the data in its own PostgreSQL database for querying.

Key Considerations

ConsiderationApproach
查询 complexitySimple: Ledger API; Complex: PQS
Latency requirementsReal-time: Ledger API; Batch: PQS
Data volumeHigh volume: PQS with indexing
Privacy scopeOnly query data for authorized Party

Privacy-Aware Design

All 集成 patterns must 账户 for Canton’s privacy model:

Design Principles

Principle应用
Minimal visibility请求 only necessary observer rights
Party designDon’t create Party unnecessarily
Divulgence awarenessUnderstand what composing 合约 reveals
Audit considerationPlan for audit visibility from the start

Common Mistakes

MistakeImpactSolution
Over-observingUnnecessary data exposureMinimize observers
Public queriesExpecting global state查询 party-scoped data
Ignoring divulgenceUnintended data sharingMap 交易 composition

错误 Handling Patterns

集成 should handle common 错误 scenarios:

错误CauseHandling
Insufficient 流量Not enough CC for feesPrompt for top-up
授权 failureParty not authorized检查 party setup
Timeout网络 or validator issuesRetry with backoff
合约 not foundArchived or never existedRefresh state

Retry Strategy

async function submitWithRetry<T>(command: Command, maxRetries = 3): Promise<T> {
  for (let attempt = 0; attempt < maxRetries; attempt++) {
    try {
      return await ledgerApi.submit(command);
    } catch (error) {
      if (isRetryable(error) && attempt < maxRetries - 1) {
        await sleep(Math.pow(2, attempt) * 1000);
        continue;
      }
      throw error;
    }
  }
  throw new Error("Max retries exceeded");
}

下一步

Detailed 钱包 集成 guide. Implement the Canton Token Standard.

本文由 CC Privacy Club 根据 Canton Network 官方文档(CC-BY-4.0)整理翻译,仅供学习;实现细节以官方最新版本为准。