压缩 SDK
用于与"契约"协议互动的TypeScript SDK,为存款,注册和分配操作提供易用的方法.
安装
npm install @epoch-protocol/epoch-intents-sdk
快速启动
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 参考
压缩SDK
与"契约"协议互动的主要SDK类.
构造器
new CompactSDK(config: CompactSDKConfig)
参数:
config.apiBaseUrl- 分配器的基址 APIconfig.walletClient- Viem 钱包客户端,用于签名交易config.publicClient- Viem 读取区块链状态的公共客户端
方法
存款和注册人
交存证书并登记一份链上契约.
async depositAndRegister(params: DepositAndRegisterParams): Promise<DepositResult>
参数:
params.tokenType- 随便'native'或者说'erc20'params.tokenAddress- Token合同地址(ERC20所需)params.amount- 作为字符串存入的金额params.allocatorAddress- 分配者的地址params.sponsorAddress- 提案国的地址params.expiresInSeconds- 过期时间以秒计(默认:600)params.witnessData- 可选证人/任务数据
回返情况:
DepositResult- 交易散列和收款
示例
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",
},
});
存款登记和创建分配
在一个操作中进行存款、登记和创建分配.
async depositRegisterAndCreateAllocation(params: DepositRegisterAndCreateAllocationParams): Promise<AllocationResult>
参数:
- 所有参数来自
depositAndRegister params.sessionToken- API 认证的会话令牌params.sponsorSignature- 可选担保人签名
回返情况:
AllocationResult- 存款结果和拨款答复
示例
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);
获得健康检查
从分配器API那里获取健康检查信息.
async getHealthCheck(): Promise<HealthCheckResponse>
回返情况:
HealthCheckResponse- API 健康状况和配置
类型
存款和注册人Params
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;
};
}
交存结果
interface DepositResult {
hash: Hash;
receipt: {
status: "success" | "reverted";
transactionHash: Hash;
blockNumber: bigint;
gasUsed: bigint;
};
}
分配结果
interface AllocationResult {
depositResult: DepositResult;
allocationResponse: {
hash: string;
signature: string;
digest: string;
};
}
处理错误
SDK为不同的故障情况提供了特定的出错类型:
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);
}
}
高级使用
自定义目击数据
对于复杂的分配方案,您可以提供自定义的目击数据:
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",
},
});
原始托肯存款
土生土长矿床:
const result = await sdk.depositAndRegister({
tokenType: "native",
amount: "1.5", // ETH amount
allocatorAddress: "0xB1c97a33E6441b8C4C8C0E4A8b4C4C4C4C4C4C4C",
sponsorAddress: "0xC2d88a33E6441b8C4C8C0E4A8b4C4C4C4C4C4C4C",
});
测试
SDK包括了综合测试文件 test/ 文件夹 :
test/test.ts- 基本测试套房test/test-runner.ts- 高级测试跑道test/integration-example.ts- 简单整合实例test/test-config.ts- 测试配置test/TESTING.md- 测试文件
以 :
pnpm test
pnpm test:runner
pnpm example
所需资源
- 节点.js 18+
- 类型脚本 5.0+
- 维埃姆 2.x
许可证
麻省理工学院