UNPKG

web3agent-aof

Version:

AI Framework for Onchain Operations and DeFi Interactions

118 lines 3.96 kB
export class GasOptimizer { provider; currentStrategy; constructor(provider) { this.provider = provider; this.currentStrategy = this.getDefaultStrategy(); } getDefaultStrategy() { return { type: 'moderate', maxPriorityFee: BigInt(2000000000), // 2 Gwei maxFeePerGas: BigInt(40000000000), // 40 Gwei flashbotsEnabled: false }; } async setStrategy(type) { const strategies = { aggressive: { type: 'aggressive', maxPriorityFee: BigInt(3000000000), // 3 Gwei maxFeePerGas: BigInt(50000000000), // 50 Gwei flashbotsEnabled: true }, moderate: this.getDefaultStrategy(), safe: { type: 'safe', maxPriorityFee: BigInt(1500000000), // 1.5 Gwei maxFeePerGas: BigInt(30000000000), // 30 Gwei flashbotsEnabled: false } }; this.currentStrategy = strategies[type]; } async estimateGas(tx) { const [baseFee, priorityFee] = await Promise.all([ this.provider.getFeeData(), this.estimatePriorityFee() ]); const maxPriorityFee = priorityFee > this.currentStrategy.maxPriorityFee ? this.currentStrategy.maxPriorityFee : priorityFee; const maxFeePerGas = baseFee.maxFeePerGas || BigInt(0); const estimatedGas = await this.provider.estimateGas(tx); const estimatedCost = maxFeePerGas * estimatedGas; return { maxFeePerGas, maxPriorityFeePerGas: maxPriorityFee, estimatedGas, estimatedCost }; } async simulateTransaction(tx) { try { const result = await this.provider.call(tx); return { success: true, gasUsed: BigInt(0), // Actual gas used would come from trace_call returnValue: result, logs: [] // Actual logs would come from trace_call }; } catch (error) { return { success: false, gasUsed: BigInt(0), returnValue: '0x', logs: [], error: error.message }; } } async estimatePriorityFee() { const feeHistory = await this.provider.send('eth_feeHistory', [ 4, 'latest', [25, 50, 75] ]); const rewards = feeHistory.reward.map((block) => BigInt(block[1])); const medianPriorityFee = rewards.sort((a, b) => { return a < b ? -1 : a > b ? 1 : 0; })[Math.floor(rewards.length / 2)]; return medianPriorityFee; } async enableMEVProtection() { // Implementation would integrate with Flashbots this.currentStrategy.flashbotsEnabled = true; } async optimizeGasPrice(baseGasPrice) { const networkCongestion = await this.getNetworkCongestion(); let multiplier = 1.0; switch (networkCongestion) { case 'high': multiplier = 1.2; break; case 'medium': multiplier = 1.1; break; case 'low': multiplier = 1.0; break; } return BigInt(Math.floor(Number(baseGasPrice) * multiplier)); } async getNetworkCongestion() { const block = await this.provider.getBlock('latest'); if (!block) return 'medium'; const gasUsed = Number(block.gasUsed); const gasLimit = Number(block.gasLimit); const utilization = gasUsed / gasLimit; if (utilization > 0.8) return 'high'; if (utilization > 0.5) return 'medium'; return 'low'; } } //# sourceMappingURL=optimizer.js.map