|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Farbcode\LaravelEvm\Commands; |
| 4 | + |
| 5 | +use Farbcode\LaravelEvm\Contracts\FeePolicy; |
| 6 | +use Farbcode\LaravelEvm\Contracts\RpcClient; |
| 7 | +use Farbcode\LaravelEvm\Contracts\Signer; |
| 8 | +use Illuminate\Console\Command; |
| 9 | +use Web3p\EthereumTx\EIP1559Transaction; |
| 10 | + |
| 11 | +class EvmBumpCommand extends Command |
| 12 | +{ |
| 13 | + protected $signature = 'evm:bump {--nonce=} {--original=} {--factor=2.0} {--gas=21000} {--priority=} {--max=} {--dry-run} {--auto}'; |
| 14 | + |
| 15 | + protected $description = 'Send a high-fee empty self transaction to replace a stuck pending transaction (same nonce).'; |
| 16 | + |
| 17 | + public function handle(RpcClient $rpc, Signer $signer, FeePolicy $fees): int |
| 18 | + { |
| 19 | + $address = $signer->getAddress(); |
| 20 | + $chainId = (int) config('evm.chain_id'); |
| 21 | + |
| 22 | + // Fetch latest and pending nonce |
| 23 | + $pendingHex = $rpc->call('eth_getTransactionCount', [$address, 'pending']); |
| 24 | + $latestHex = $rpc->call('eth_getTransactionCount', [$address, 'latest']); |
| 25 | + $pending = hexdec($pendingHex); |
| 26 | + $latest = hexdec($latestHex); |
| 27 | + |
| 28 | + $specifiedNonceOpt = $this->option('nonce'); |
| 29 | + $nonce = null; |
| 30 | + if ($specifiedNonceOpt !== null) { |
| 31 | + $nonce = (int) $specifiedNonceOpt; |
| 32 | + } else { |
| 33 | + if ($pending > $latest) { |
| 34 | + $nonce = $pending - 1; // last pending |
| 35 | + } else { |
| 36 | + $this->error('No pending transactions detected (pending == latest). Use --nonce to force replacement.'); |
| 37 | + return self::FAILURE; |
| 38 | + } |
| 39 | + } |
| 40 | + if ($nonce < 0) { |
| 41 | + $this->error('Calculated nonce is negative; abort.'); |
| 42 | + return self::FAILURE; |
| 43 | + } |
| 44 | + |
| 45 | + // Optional original tx fetch |
| 46 | + $origHash = $this->option('original'); |
| 47 | + $origPriority = null; $origMaxFee = null; $origGas = null; |
| 48 | + if ($origHash) { |
| 49 | + try { |
| 50 | + $orig = $rpc->call('eth_getTransactionByHash', [$origHash]); |
| 51 | + if (is_array($orig) && isset($orig['nonce'])) { |
| 52 | + $origNonce = hexdec($orig['nonce']); |
| 53 | + if ($origNonce !== $nonce) { |
| 54 | + $this->warn('Original tx nonce '.$origNonce.' != target nonce '.$nonce.'; continuing but replacement may fail.'); |
| 55 | + } |
| 56 | + // Legacy or EIP-1559: fields differ; try both |
| 57 | + if (isset($orig['maxPriorityFeePerGas'])) { |
| 58 | + $origPriority = hexdec($orig['maxPriorityFeePerGas']); |
| 59 | + } elseif (isset($orig['gasPrice'])) { |
| 60 | + $origPriority = hexdec($orig['gasPrice']); // treat as priority baseline |
| 61 | + } |
| 62 | + if (isset($orig['maxFeePerGas'])) { |
| 63 | + $origMaxFee = hexdec($orig['maxFeePerGas']); |
| 64 | + } elseif (isset($orig['gasPrice'])) { |
| 65 | + $origMaxFee = hexdec($orig['gasPrice']); |
| 66 | + } |
| 67 | + if (isset($orig['gas'])) { |
| 68 | + $origGas = hexdec($orig['gas']); |
| 69 | + } |
| 70 | + } else { |
| 71 | + $this->warn('Could not fetch original tx or unexpected response shape.'); |
| 72 | + } |
| 73 | + } catch (\Throwable $e) { |
| 74 | + $this->warn('Fetch original tx failed: '.$e->getMessage()); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Base gas price |
| 79 | + $gasPriceHex = $rpc->call('eth_gasPrice'); |
| 80 | + $base = is_string($gasPriceHex) ? hexdec($gasPriceHex) : (int) $gasPriceHex; |
| 81 | + |
| 82 | + $factor = (float) $this->option('factor'); |
| 83 | + $userPrio = $this->option('priority'); |
| 84 | + $userMax = $this->option('max'); |
| 85 | + [$suggestPrio, $suggestMax] = $fees->suggest(fn () => $gasPriceHex); |
| 86 | + |
| 87 | + // Auto mode: derive min bump from original tx if provided |
| 88 | + $priority = $userPrio !== null ? (int) $userPrio : max($suggestPrio, (int) ($base * 0.3)); |
| 89 | + $maxFee = $userMax !== null ? (int) $userMax : max($suggestMax, (int) ($base * $factor)); |
| 90 | + |
| 91 | + if ($this->option('auto') && $origPriority !== null && $origMaxFee !== null) { |
| 92 | + $minPriority = max((int) ($origPriority * 1.125), $origPriority + 1_000_000_000); // +12.5% or +1 gwei |
| 93 | + $minMaxFee = max((int) ($origMaxFee * 1.125), $origMaxFee + 2_000_000_000); // +12.5% or +2 gwei |
| 94 | + $priority = max($priority, $minPriority); |
| 95 | + $maxFee = max($maxFee, $minMaxFee); |
| 96 | + } |
| 97 | + |
| 98 | + if ($maxFee <= $priority) { |
| 99 | + $maxFee = $priority * 2; |
| 100 | + } |
| 101 | + |
| 102 | + $gasLimit = (int) $this->option('gas'); |
| 103 | + if ($gasLimit < 21000) { |
| 104 | + $gasLimit = 21000; |
| 105 | + } |
| 106 | + if ($origGas !== null) { |
| 107 | + // Keep at least original gas limit if higher (avoid underestimating) |
| 108 | + $gasLimit = max($gasLimit, $origGas); |
| 109 | + } |
| 110 | + |
| 111 | + // ChainId sanity check |
| 112 | + $rpcChainHex = $rpc->call('eth_chainId'); |
| 113 | + $rpcChain = is_string($rpcChainHex) ? hexdec($rpcChainHex) : (int) $rpcChainHex; |
| 114 | + if ($rpcChain !== $chainId && ! $this->option('dry-run')) { |
| 115 | + $this->error('ChainId mismatch: local='.$chainId.' remote='.$rpcChain.' (abort)'); |
| 116 | + return self::FAILURE; |
| 117 | + } |
| 118 | + |
| 119 | + $fields = [ |
| 120 | + 'chainId' => $chainId, |
| 121 | + 'nonce' => $nonce, |
| 122 | + 'maxPriorityFeePerGas' => $priority, |
| 123 | + 'maxFeePerGas' => $maxFee, |
| 124 | + 'gas' => $gasLimit, |
| 125 | + 'to' => $address, |
| 126 | + 'value' => 0, |
| 127 | + 'data' => '0x', |
| 128 | + 'accessList' => [], |
| 129 | + ]; |
| 130 | + |
| 131 | + $this->line('Prepared replacement transaction:'); |
| 132 | + $rows = [[ |
| 133 | + $nonce, |
| 134 | + $priority, |
| 135 | + $maxFee, |
| 136 | + $gasLimit, |
| 137 | + ]]; |
| 138 | + $this->table(['nonce','priority','maxFee','gasLimit'], $rows); |
| 139 | + if ($origPriority !== null) { |
| 140 | + $this->info('Original priority: '.$origPriority.' original maxFee: '.$origMaxFee.' original gas: '.$origGas); |
| 141 | + } |
| 142 | + $this->info('Base fee (approx from gasPrice): '.$base); |
| 143 | + |
| 144 | + if ($this->option('dry-run')) { |
| 145 | + try { |
| 146 | + $rawSigned = new EIP1559Transaction($fields)->sign($signer->privateKey()); |
| 147 | + $rawHex = str_starts_with($rawSigned, '0x') ? $rawSigned : '0x'.$rawSigned; |
| 148 | + $this->info('Dry-run signed rawTx (not broadcasted):'); |
| 149 | + $this->line($rawHex); |
| 150 | + } catch (\Throwable $e) { |
| 151 | + $this->error('Dry-run signing failed: '.$e->getMessage()); |
| 152 | + return self::FAILURE; |
| 153 | + } |
| 154 | + return self::SUCCESS; |
| 155 | + } |
| 156 | + |
| 157 | + try { |
| 158 | + $rawSigned = new EIP1559Transaction($fields)->sign($signer->privateKey()); |
| 159 | + $rawHex = str_starts_with($rawSigned, '0x') ? $rawSigned : '0x'.$rawSigned; |
| 160 | + $txHash = $rpc->call('eth_sendRawTransaction', [$rawHex]); |
| 161 | + $this->info('Broadcast replacement txHash: '.$txHash); |
| 162 | + $this->info('Use a block explorer or evm:wait to confirm mining.'); |
| 163 | + } catch (\Throwable $e) { |
| 164 | + $msg = $e->getMessage(); |
| 165 | + $this->error('Broadcast failed: '.$msg); |
| 166 | + if (str_contains($msg, 'could not replace')) { |
| 167 | + $this->warn('Suggestion: Increase --priority and --max or use --auto with --original=<txHash>.'); |
| 168 | + if ($origPriority !== null) { |
| 169 | + $this->line('Try at least priority >= '.max((int) ($origPriority * 1.15), $origPriority + 2_000_000_000) |
| 170 | + .' and maxFee >= '.max((int) ($origMaxFee * 1.15), $origMaxFee + 3_000_000_000)); |
| 171 | + } else { |
| 172 | + $this->line('If original tx fees unknown, start with factor 3-5 or specify --priority / --max manually.'); |
| 173 | + } |
| 174 | + } |
| 175 | + return self::FAILURE; |
| 176 | + } |
| 177 | + |
| 178 | + return self::SUCCESS; |
| 179 | + } |
| 180 | +} |
0 commit comments