@epoch-protocol/epoch-intents-sdk

中文文档
2.2.0发布于 2026/10/27MITTypeScriptES Module近 30 天下载 3万

TypeScript SDK for Epoch Intents protocol

原始使用文档
文档目录18 节
  1. Installation
  2. Quick Start
  3. API Reference
  4. CompactSDK
  5. depositAndRegister
  6. depositRegisterAndCreateAllocation
  7. getHealthCheck
  8. Types
  9. DepositAndRegisterParams
  10. DepositResult
  11. AllocationResult
  12. Error Handling
  13. Advanced Usage
  14. Custom Witness Data
  15. Native Token Deposits
  16. Testing
  17. Requirements
  18. License

Compact SDK

A TypeScript SDK for interacting with The Compact protocol, providing easy-to-use methods for deposit, register, and allocation operations.

Installation

npm install @epoch-protocol/epoch-intents-sdk

Quick Start

import { CompactSDK } from "@epoch-protocol/epoch-intents-sdk";
import { createWalletClient, createPublicClient, http } from "viem";
import { mainnet } from "viem/chains";

// Initialize clients
const walletClient = createWalletClient({
  chain: mainnet,
  transport: http(),
  // ... your wallet configuration
});

const publicClient = createPublicClient({
  chain: mainnet,
  transport: http(),
});

// Initialize SDK
const sdk = new CompactSDK({
  apiBaseUrl: "http://localhost:3000", // Your allocator API URL
  walletClient,
  publicClient,
});

// Deposit and register a compact
const result = await sdk.depositAndRegister({
  tokenType: "erc20",
  tokenAddress: "0xA0b86a33E6441b8C4C8C0E4A8b4C4C4C4C4C4C4C",
  amount: "100",
  allocatorAddress: "0xB1c97a33E6441b8C4C8C0E4A8b4C4C4C4C4C4C4C",
  sponsorAddress: "0xC2d88a33E6441b8C4C8C0E4A8b4C4C4C4C4C4C4C",
  expiresInSeconds: 600, // 10 minutes
});

console.log("Transaction hash:", result.hash);
console.log("Receipt:", result.receipt);

API Reference

CompactSDK

The main SDK class for interacting with The Compact protocol.

Constructor

new CompactSDK(config: CompactSDKConfig)

Parameters:

  • config.apiBaseUrl - Base URL of your allocator API
  • config.walletClient - Viem wallet client for signing transactions
  • config.publicClient - Viem public client for reading blockchain state

Methods

depositAndRegister

Deposits tokens and registers a compact on-chain.

async depositAndRegister(params: DepositAndRegisterParams): Promise<DepositResult>

Parameters:

  • params.tokenType - Either 'native' or 'erc20'
  • params.tokenAddress - Token contract address (required for ERC20)
  • params.amount - Amount to deposit as string
  • params.allocatorAddress - Address of the allocator
  • params.sponsorAddress - Address of the sponsor
  • params.expiresInSeconds - Expiration time in seconds (default: 600)
  • params.witnessData - Optional witness/mandate data

Returns:

  • DepositResult - Transaction hash and receipt

Example:

const result = await sdk.depositAndRegister({
  tokenType: "erc20",
  tokenAddress: "0xA0b86a33E6441b8C4C8C0E4A8b4C4C4C4C4C4C4C",
  amount: "100",
  allocatorAddress: "0xB1c97a33E6441b8C4C8C0E4A8b4C4C4C4C4C4C4C",
  sponsorAddress: "0xC2d88a33E6441b8C4C8C0E4A8b4C4C4C4C4C4C4C",
  witnessData: {
    tokenIn: "0xA0b86a33E6441b8C4C8C0E4A8b4C4C4C4C4C4C4C",
    tokenInAmount: "100",
    tokenOut: "0xD3e99a33E6441b8C4C8C0E4A8b4C4C4C4C4C4C4C",
    minTokenOut: "95",
    destinationChainId: 1,
    protocolHashIdentifier:
      "0x0000000000000000000000000000000000000000000000000000000000000000",
    recipient: "0x0000000000000000000000000000000000000000",
  },
});

depositRegisterAndCreateAllocation

Performs deposit, register, and creates an allocation in one operation.

async depositRegisterAndCreateAllocation(params: DepositRegisterAndCreateAllocationParams): Promise<AllocationResult>

Parameters:

  • All parameters from depositAndRegister
  • params.sessionToken - Session token for API authentication
  • params.sponsorSignature - Optional sponsor signature

Returns:

  • AllocationResult - Both deposit result and allocation response

Example:

const result = await sdk.depositRegisterAndCreateAllocation({
  tokenType: "erc20",
  tokenAddress: "0xA0b86a33E6441b8C4C8C0E4A8b4C4C4C4C4C4C4C",
  amount: "100",
  allocatorAddress: "0xB1c97a33E6441b8C4C8C0E4A8b4C4C4C4C4C4C4C",
  sponsorAddress: "0xC2d88a33E6441b8C4C8C0E4A8b4C4C4C4C4C4C4C",
  sessionToken: "your-session-token",
  witnessData: {
    tokenIn: "0xA0b86a33E6441b8C4C8C0E4A8b4C4C4C4C4C4C4C",
    tokenInAmount: "100",
    tokenOut: "0xD3e99a33E6441b8C4C8C0E4A8b4C4C4C4C4C4C4C",
    minTokenOut: "95",
    destinationChainId: 1,
  },
});

console.log("Deposit hash:", result.depositResult.hash);
console.log("Allocation hash:", result.allocationResponse.hash);

getHealthCheck

Gets health check information from the allocator API.

async getHealthCheck(): Promise<HealthCheckResponse>

Returns:

  • HealthCheckResponse - API health status and configuration

Types

DepositAndRegisterParams

interface DepositAndRegisterParams {
  tokenType: "native" | "erc20";
  tokenAddress?: Address; // Required for ERC20
  amount: string;
  allocatorAddress: Address;
  sponsorAddress: Address;
  expiresInSeconds?: number; // Default: 600
  witnessData?: {
    tokenIn: Address;
    tokenInAmount: string;
    tokenOut: Address;
    minTokenOut: string;
    destinationChainId: number;
    protocolHashIdentifier?: Hex;
    recipient?: Address;
  };
}

DepositResult

interface DepositResult {
  hash: Hash;
  receipt: {
    status: "success" | "reverted";
    transactionHash: Hash;
    blockNumber: bigint;
    gasUsed: bigint;
  };
}

AllocationResult

interface AllocationResult {
  depositResult: DepositResult;
  allocationResponse: {
    hash: string;
    signature: string;
    digest: string;
  };
}

Error Handling

The SDK provides specific error types for different failure scenarios:

import {
  CompactSDKError,
  TransactionError,
  APIError,
} from "@epoch-protocol/epoch-intents-sdk";

try {
  const result = await sdk.depositAndRegister(params);
} catch (error) {
  if (error instanceof TransactionError) {
    console.error("Transaction failed:", error.message);
    console.error("Transaction hash:", error.txHash);
  } else if (error instanceof APIError) {
    console.error("API error:", error.message);
    console.error("Status:", error.status);
  } else if (error instanceof CompactSDKError) {
    console.error("SDK error:", error.message);
  }
}

Advanced Usage

Custom Witness Data

For complex allocation scenarios, you can provide custom witness data:

const result = await sdk.depositRegisterAndCreateAllocation({
  tokenType: "erc20",
  tokenAddress: "0xA0b86a33E6441b8C4C8C0E4A8b4C4C4C4C4C4C4C",
  amount: "1000",
  allocatorAddress: "0xB1c97a33E6441b8C4C8C0E4A8b4C4C4C4C4C4C4C",
  sponsorAddress: "0xC2d88a33E6441b8C4C8C0E4A8b4C4C4C4C4C4C4C",
  sessionToken: "session-token",
  witnessData: {
    tokenIn: "0xA0b86a33E6441b8C4C8C0E4A8b4C4C4C4C4C4C4C",
    tokenInAmount: "1000",
    tokenOut: "0xD3e99a33E6441b8C4C8C0E4A8b4C4C4C4C4C4C4C",
    minTokenOut: "950",
    destinationChainId: 10, // Optimism
    protocolHashIdentifier:
      "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
    recipient: "0x0000000000000000000000000000000000000000",
  },
});

Native Token Deposits

For native ETH deposits:

const result = await sdk.depositAndRegister({
  tokenType: "native",
  amount: "1.5", // ETH amount
  allocatorAddress: "0xB1c97a33E6441b8C4C8C0E4A8b4C4C4C4C4C4C4C",
  sponsorAddress: "0xC2d88a33E6441b8C4C8C0E4A8b4C4C4C4C4C4C4C",
});

Testing

The SDK includes comprehensive test files in the test/ folder:

  • test/test.ts - Basic test suite
  • test/test-runner.ts - Advanced test runner
  • test/integration-example.ts - Simple integration example
  • test/test-config.ts - Test configuration
  • test/TESTING.md - Testing documentation

Run tests with:

pnpm test
pnpm test:runner
pnpm example

Requirements

  • Node.js 18+
  • TypeScript 5.0+
  • Viem 2.x

License

MIT

版本与发布记录

@epoch-protocol/epoch-intents-sdk 共发布 55 个版本,首次发布于 2026/01/16;过去 12 个月发布了 55 个版本。

版本发布日期距今
2.2.0latest2026/10/2717 天前
2.1.92026/10/2717 天前
2.0.12026/10/2618 天前
2.1.02026/10/2618 天前
2.0.02026/10/2519 天前
1.0.40-mainnet.02026/10/2222 天前

关于 @epoch-protocol/epoch-intents-sdk 的常见问题

@epoch-protocol/epoch-intents-sdk 是什么?

TypeScript SDK for Epoch Intents protocol

如何安装 @epoch-protocol/epoch-intents-sdk?

在项目目录运行 npm install @epoch-protocol/epoch-intents-sdk,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:viem(^2.x)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@epoch-protocol/epoch-intents-sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@epoch-protocol/epoch-intents-sdk 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@epoch-protocol/epoch-intents-sdk 可以商用吗?

@epoch-protocol/epoch-intents-sdk 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@epoch-protocol/epoch-intents-sdk 的下载量有多少?

根据 npm 官方下载统计,@epoch-protocol/epoch-intents-sdk 在 2026/10/14 – 2026/11/12 这 30 天内被下载 29,607 次,日均约 987 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@epoch-protocol/epoch-intents-sdk 还在维护吗?

最新版本 2.2.0 发布于 2026/10/27(17 天前);至今共发布 55 个版本,其中过去 12 个月发布了 55 个;首次发布于 2026/01/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。