/** * XLayer SDK — ERC-4337 AA 类型定义 * * 包含 ERC-4337 UserOperation、AA 账户、AA Gas 策略、handleOps 结果等核心类型 */ import type { HexString } from './constants.js'; /** * UserOperation v0.6 结构 * 用于 ERC-4337 EntryPoint v0.6 */ export interface UserOperation { sender: string; nonce: HexString | string; initCode: HexString | string; callData: HexString | string; callGasLimit: HexString | string; verificationGasLimit: HexString | string; preVerificationGas: HexString | string; maxFeePerGas: HexString | string; maxPriorityFeePerGas: HexString | string; paymasterAndData: HexString | string; signature: HexString | string; } /** * 签名后的 UserOperation(包含额外元数据) */ export interface SignedUserOp { userOp: UserOperation; userOpHash: string; prefundWei: bigint; sender: string; ownerName?: string; } /** * Gas 估算结果(来自 Bundler) */ export interface GasEstimate { callGasLimit: HexString; verificationGasLimit: HexString; preVerificationGas: HexString; maxFeePerGas?: HexString; maxPriorityFeePerGas?: HexString; } export type GasPolicy = 'fixed' | 'localEstimate' | 'bundlerEstimate'; export interface FixedGasConfig { /** callGasLimit(若不提供,SDK 会使用较保守的默认值) */ callGasLimit?: bigint; /** 已部署 sender 的 verificationGasLimit(默认用 SDK 常量) */ verificationGasLimitDeployed?: bigint; /** 未部署 sender 的 verificationGasLimit(默认用 SDK 常量) */ verificationGasLimitUndeployed?: bigint; /** preVerificationGas(默认用 SDK 常量) */ preVerificationGas?: bigint; /** ✅ 固定 gasPrice(若不提供,SDK 会调用 getFeeData 获取) */ gasPrice?: bigint; } /** * XLayer SDK 基础配置 */ export interface XLayerConfig { /** RPC URL(默认使用公共节点) */ rpcUrl?: string; /** Bundler URL(默认使用 Particle) */ bundlerUrl?: string; /** Chain ID(默认 196) */ chainId?: number; /** EntryPoint 地址 */ entryPoint?: string; /** SimpleAccountFactory 地址 */ factory?: string; /** Salt(用于 AA 账户派生) */ salt?: bigint; /** Paymaster 地址(如果指定,不需要往 AA 转手续费) */ paymaster?: string; /** Paymaster Data(与 paymaster 一起使用) */ paymasterData?: string; /** 默认超时时间(毫秒) */ timeoutMs?: number; /** Gas 估算安全余量倍数 */ gasLimitMultiplier?: number; /** Payer handleOps 交易的 gasLimit(手动覆盖) */ gasLimit?: bigint; /** Payer handleOps 交易的最小 gasPrice (Gwei) */ minGasPriceGwei?: number; /** 利润尾笔原生转账 gasLimit(默认 21000n) */ profitTailGasLimit?: bigint; /** * AA Gas 策略(用于大规模地址时减少 RPC) * - fixed:固定 gas(不 estimate) * - localEstimate:eth_estimateGas(不走 bundler) * - bundlerEstimate:eth_estimateUserOperationGas(最慢但最稳) */ gasPolicy?: GasPolicy; /** fixed 策略的默认 gas 配置(可被每次调用覆盖) */ fixedGas?: FixedGasConfig; /** * 是否提取利润(默认 true) * - 对齐 BSC:默认会从“卖出得到的 OKB / 归集金额”中按比例刮取利润 */ extractProfit?: boolean; /** * 利润比例(基点 bps,1 bps = 0.01%) * - 默认使用 `getProfitRateBps('normal')` */ profitBps?: number; /** * 利润接收地址 * - 默认使用 `getProfitRecipient()` */ profitRecipient?: string; /** * ✅ 安全边际百分比(默认 80) * - 只使用报价的 X% 进行分发,防止价格下跌导致失败 * - 例如:80 表示只使用报价的 80%,允许价格下跌 20% * - 范围:50-100,越低越安全但买入金额越少 */ safetyMarginPercent?: number; } /** * AA 账户信息 */ export interface AAAccount { /** Owner EOA 地址 */ owner: string; /** AA Sender 地址 */ sender: string; /** 是否已部署 */ deployed: boolean; /** OKB 余额 */ balance: bigint; /** Nonce */ nonce: bigint; } /** * Owner 钱包信息 */ export interface OwnerInfo { name: string; privateKey: string; address?: string; sender?: string; } /** * handleOps 执行结果 */ export interface HandleOpsResult { /** 交易哈希 */ txHash: string; /** 区块号 */ blockNumber: number; /** 交易状态 */ status: number; /** UserOperationEvent 列表 */ userOpEvents: UserOpEventResult[]; } /** * UserOperationEvent 解析结果 */ export interface UserOpEventResult { userOpHash: string; sender: string; paymaster: string; success: boolean; actualGasCost: bigint; actualGasUsed: bigint; } /** * 深度 Partial 类型 */ export type DeepPartial = { [P in keyof T]?: T[P] extends object ? DeepPartial : T[P]; }; /** * 必需的配置字段 */ export type RequiredConfig = Required>;