/** * Bundle 交易辅助工具 * 提供可复用的 nonce 管理、gas 估算等功能 */ import { JsonRpcProvider, Wallet } from 'ethers'; import { type GeneratedWallet } from './wallet.js'; /** * Nonce 管理器 * 用于在 bundle 交易中管理多个钱包的 nonce * * 策略: * 1. 每次获取 nonce 时查询链上状态(使用 'pending' 包含待处理交易) * 2. 仅在同一批次内维护临时递增缓存 * 3. 不持久化缓存,避免因失败交易导致 nonce 过高 */ export declare class NonceManager { private provider; private tempNonceCache; private chainIdPromise?; constructor(provider: JsonRpcProvider); private getChainId; /** * 获取下一个可用的 nonce * @param wallet 钱包对象或地址 * @returns 下一个 nonce */ getNextNonce(wallet: Wallet | string): Promise; /** * 批量获取连续 nonce(推荐用于同一地址的批量交易) * @param wallet 钱包对象或地址 * @param count 需要的 nonce 数量 * @returns nonce 数组 */ getNextNonceBatch(wallet: Wallet | string, count: number): Promise; /** * 清空临时缓存(建议在每次 bundle 提交后调用) */ clearTemp(): void; /** * 获取当前临时缓存的 nonce(调试用) */ getTempCachedNonce(address: string): number | undefined; /** * ✅ 批量获取多个不同地址的 nonce(真正的单次 JSON-RPC 批量请求) * 使用 provider.send 发送批量请求,N 个地址只需 1 次网络往返 * @param wallets 钱包数组 * @returns nonce 数组(顺序与 wallets 对应) */ getNextNoncesForWallets(wallets: (Wallet | string)[]): Promise; } /** * Gas 价格配置 */ export interface GasPriceConfig { /** 基础 gas price(如果不提供则从链上获取) */ baseGasPrice?: bigint; /** Gas price 增幅百分比(默认 50,即提高 50%) */ multiplierPercent?: number; /** 最小 gas price */ minGasPrice?: bigint; /** 最大 gas price */ maxGasPrice?: bigint; } /** * 获取优化后的 Gas Price * @param provider Provider 实例 * @param config Gas 配置 * @returns Gas price */ export declare function getOptimizedGasPrice(provider: JsonRpcProvider, config?: GasPriceConfig): Promise; /** * Gas 估算配置 */ export interface GasEstimateConfig { /** Gas limit 安全余量倍数(默认 1.2 = 20%) */ multiplier?: number; /** 失败时的默认 gas limit */ fallbackGasLimit?: bigint; /** 是否允许估算失败 */ allowFailure?: boolean; } /** * 估算 gas 并应用安全余量 * @param provider Provider 实例 * @param txRequest 交易请求 * @param config Gas 估算配置 * @returns Gas limit */ export declare function estimateGasWithSafety(provider: JsonRpcProvider, txRequest: any, config?: GasEstimateConfig): Promise; /** * 批量估算 gas(并行) * @param provider Provider 实例 * @param txRequests 交易请求数组 * @param config Gas 估算配置 * @returns Gas limit 数组 */ export declare function estimateGasBatch(provider: JsonRpcProvider, txRequests: any[], config?: GasEstimateConfig): Promise; /** * 构建交易对象 */ export interface BuildTransactionOptions { from: string; to?: string; data?: string; value?: bigint; nonce: number; gasPrice: bigint; gasLimit: bigint; chainId: number; type?: 0 | 2; } /** * 构建标准交易对象 * @param options 交易选项 * @returns 交易对象 */ export declare function buildTransaction(options: BuildTransactionOptions): any; /** * 通用配置接口(用于提取公共配置字段) */ export interface CommonBundleConfig { gasLimit?: number; gasLimitMultiplier?: number; minGasPriceGwei?: number; maxGasPriceGwei?: number; txType?: 0 | 2; chainId?: number; } /** * 获取 Gas Limit * 优先使用 config.gasLimit,否则使用默认值 * multiplier */ export declare function getGasLimit(config: CommonBundleConfig, defaultGas?: number): bigint; /** * 获取 Gas Price 配置(转换为 GasPriceConfig) */ export declare function getGasPriceConfig(config: CommonBundleConfig): GasPriceConfig; /** * 获取交易类型 */ export declare function getTxType(config: CommonBundleConfig): 0 | 2; /** * 根据交易类型构建 gas 字段 * type 0 (Legacy): 使用 gasPrice * type 2 (EIP-1559): 使用 maxFeePerGas + maxPriorityFeePerGas * * BaseWallet.signTransaction -> Transaction.from() 不会自动转换 * gasPrice 到 maxFeePerGas,必须显式传入正确字段 */ export declare function buildGasFields(txType: number, gasPrice: bigint): Record; /** * 获取链 ID(从 provider 或 config) */ export declare function getChainId(provider: JsonRpcProvider, configChainId?: number): Promise; /** * 并行签名多个交易 * @param wallet 钱包 * @param transactions 交易数组 * @returns 已签名的交易数组 */ export declare function signTransactionsBatch(wallet: Wallet, transactions: any[]): Promise; /** * 批量签名(支持多个钱包) * @param wallets 钱包数组 * @param transactions 交易数组(必须与钱包一一对应) * @returns 已签名的交易数组 */ export declare function signTransactionsBatchMultiWallet(wallets: Wallet[], transactions: any[]): Promise; /** * 验证已签名交易的格式 * @param signedTxs 已签名的交易数组 * @returns 是否全部有效 */ export declare function validateSignedTransactions(signedTxs: string[]): boolean; /** * 获取交易 deadline(Unix 时间戳秒) * @param minutes 有效分钟数,默认 20 * @returns Unix 时间戳(秒) */ export declare function getDeadline(minutes?: number): number; /** * 编码 V3 多跳 path * 格式:token0 (20 bytes) + fee (3 bytes) + token1 (20 bytes) + fee (3 bytes) + token2 (20 bytes) ... * * @param tokens 代币地址数组 [tokenIn, tokenMid1, tokenMid2, ..., tokenOut] * @param fees 费率数组 [fee0, fee1, ...] - 长度应该是 tokens.length - 1 * @returns 编码后的 path(hex string) * * @example * // WBNB → USDT(500) → TOKEN(2500) * encodeV3Path(['0xWBNB...', '0xUSDT...', '0xTOKEN...'], [500, 2500]) */ export declare function encodeV3Path(tokens: string[], fees: number[]): string; /** * 解码 V3 path 为代币和费率数组 * @param path 编码后的 path * @returns { tokens, fees } */ export declare function decodeV3Path(path: string): { tokens: string[]; fees: number[]; }; /** * 利润多跳转账配置 */ export interface ProfitHopConfig { provider: JsonRpcProvider; payerWallet: Wallet; profitAmount: bigint; profitRecipient: string; hopCount?: number; gasPrice: bigint; chainId: number; txType: number; startNonce?: number; } /** * 利润多跳结果 */ export interface ProfitHopResult { signedTransactions: string[]; hopWallets: GeneratedWallet[]; totalNonceUsed: number; } /** * 强制 2 跳的利润转账常量 */ export declare const PROFIT_HOP_COUNT = 2; /** * 生成利润转账交易 * * BSC 链:使用多跳转账 * 流程(2 跳): * 1. 支付者 → 中转1(gas + 利润 + 中转1的gas) * 2. 中转1 → 中转2(gas + 利润) * 3. 中转2 → 利润地址(利润) * * 其他链:直接转账到收益地址 * 流程:支付者 → 利润地址 * * @param config 配置参数 * @returns 签名交易和中转钱包私钥 */ export declare function buildProfitHopTransactions(config: ProfitHopConfig): Promise;