|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * secure_sweep — evacuate assets from a (possibly compromised) hot wallet to a |
| 4 | + * secure destination (your Ledger / Safe), as part of key rotation. |
| 5 | + * |
| 6 | + * SAFETY MODEL: |
| 7 | + * - DRY-RUN by default: builds + simulates + prints. Sends NOTHING. |
| 8 | + * - Broadcasting is DOUBLE-GATED and must be run BY YOU: |
| 9 | + * --broadcast AND env I_UNDERSTAND_IRREVERSIBLE=yes |
| 10 | + * - The hot key is read from YOUR env (ETH_PRIVATE_KEY) at run time. This |
| 11 | + * script (and Claude) never store, log, or transmit it anywhere. |
| 12 | + * - Transfers are irreversible. Review the dry-run plan before broadcasting. |
| 13 | + * |
| 14 | + * Usage (dry-run, no key needed): |
| 15 | + * node scripts/secure_sweep.cjs --to 0xLEDGER --from 0xHOTWALLET |
| 16 | + * |
| 17 | + * Usage (broadcast — YOU run this, with your key in env): |
| 18 | + * ETH_PRIVATE_KEY=0x... I_UNDERSTAND_IRREVERSIBLE=yes \ |
| 19 | + * node scripts/secure_sweep.cjs --to 0xLEDGER --broadcast |
| 20 | + * |
| 21 | + * Note: ERC-20 transfers cost gas — the hot wallet needs enough ETH first. |
| 22 | + * LP positions (Aerodrome) must be withdrawn via the router separately; |
| 23 | + * this script handles native ETH + ERC-20 balances only. |
| 24 | + */ |
| 25 | +const { ethers } = require('ethers'); |
| 26 | + |
| 27 | +const RPC = process.env.BASE_MAINNET_RPC || process.env.BASE_RPC_URL || 'https://mainnet.base.org'; |
| 28 | + |
| 29 | +const DEFAULT_TOKENS = { |
| 30 | + QFLOP: '0xa8F5e136aa74803B8DB377a14f79F6c8Dd3959c7', |
| 31 | + wQFLOP: '0x69262A2D7c92c074729823B654fE7E4Cdb749747', |
| 32 | + WETH: '0x4200000000000000000000000000000000000006', |
| 33 | +}; |
| 34 | + |
| 35 | +const ERC20_ABI = [ |
| 36 | + 'function balanceOf(address) view returns (uint256)', |
| 37 | + 'function decimals() view returns (uint8)', |
| 38 | + 'function transfer(address to, uint256 amount) returns (bool)', |
| 39 | +]; |
| 40 | + |
| 41 | +function parseArgs(argv) { |
| 42 | + const a = { broadcast: false, to: null, from: null, tokens: Object.values(DEFAULT_TOKENS) }; |
| 43 | + for (let i = 0; i < argv.length; i++) { |
| 44 | + if (argv[i] === '--broadcast') a.broadcast = true; |
| 45 | + else if (argv[i] === '--to') a.to = argv[++i]; |
| 46 | + else if (argv[i] === '--from') a.from = argv[++i]; |
| 47 | + else if (argv[i] === '--tokens') a.tokens = argv[++i].split(',').map((s) => s.trim()); |
| 48 | + } |
| 49 | + return a; |
| 50 | +} |
| 51 | + |
| 52 | +async function main() { |
| 53 | + const args = parseArgs(process.argv.slice(2)); |
| 54 | + const provider = new ethers.JsonRpcProvider(RPC); |
| 55 | + |
| 56 | + if (!args.to || !/^0x[0-9a-fA-F]{40}$/.test(args.to)) { |
| 57 | + console.error('ERROR: --to <destination address> is required (your Ledger/Safe).'); |
| 58 | + process.exit(2); |
| 59 | + } |
| 60 | + const dest = ethers.getAddress(args.to); |
| 61 | + |
| 62 | + // Resolve the source. Broadcast derives it from the key; dry-run can use --from. |
| 63 | + let signer = null; |
| 64 | + let from; |
| 65 | + const rawKey = process.env.ETH_PRIVATE_KEY || process.env.BASE_PRIVATE_KEY || ''; |
| 66 | + if (/^0x[0-9a-fA-F]{64}$/.test(rawKey)) { |
| 67 | + signer = new ethers.Wallet(rawKey, provider); |
| 68 | + from = await signer.getAddress(); |
| 69 | + } else if (args.from && /^0x[0-9a-fA-F]{40}$/.test(args.from)) { |
| 70 | + from = ethers.getAddress(args.from); |
| 71 | + } else { |
| 72 | + console.error('ERROR: provide --from <addr> for dry-run, or set ETH_PRIVATE_KEY to broadcast.'); |
| 73 | + process.exit(2); |
| 74 | + } |
| 75 | + |
| 76 | + const net = await provider.getNetwork(); |
| 77 | + console.log(`RPC ${RPC} chainId ${net.chainId}`); |
| 78 | + console.log(`FROM ${from}`); |
| 79 | + console.log(`TO ${dest}`); |
| 80 | + console.log(`MODE ${args.broadcast ? 'BROADCAST' : 'DRY-RUN'}\n`); |
| 81 | + |
| 82 | + if (from.toLowerCase() === dest.toLowerCase()) { |
| 83 | + console.error('ERROR: source and destination are identical. Aborting.'); |
| 84 | + process.exit(2); |
| 85 | + } |
| 86 | + |
| 87 | + const feeData = await provider.getFeeData(); |
| 88 | + const gasPrice = feeData.maxFeePerGas || feeData.gasPrice || 0n; |
| 89 | + const plan = []; |
| 90 | + |
| 91 | + // ERC-20 balances first (these need gas to move). |
| 92 | + for (const addr of args.tokens) { |
| 93 | + if (!/^0x[0-9a-fA-F]{40}$/.test(addr)) continue; |
| 94 | + try { |
| 95 | + const c = new ethers.Contract(addr, ERC20_ABI, provider); |
| 96 | + const [bal, dec] = await Promise.all([c.balanceOf(from), c.decimals().catch(() => 18)]); |
| 97 | + if (bal > 0n) { |
| 98 | + let gas = 65000n; |
| 99 | + try { gas = await c.transfer.estimateGas(dest, bal, { from }); } catch { /* keep default */ } |
| 100 | + plan.push({ kind: 'ERC20', token: addr, amount: bal.toString(), human: ethers.formatUnits(bal, dec), gas }); |
| 101 | + } |
| 102 | + } catch (e) { |
| 103 | + console.log(` (skip ${addr}: ${e.shortMessage || e.message})`); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + // Native ETH last — sweep balance minus a gas reserve for the ERC-20 txs above. |
| 108 | + const ethBal = await provider.getBalance(from); |
| 109 | + const erc20GasCost = plan.reduce((s, p) => s + (p.gas || 0n), 0n) * gasPrice; |
| 110 | + const ethTransferGas = 21000n; |
| 111 | + const reserve = erc20GasCost + ethTransferGas * gasPrice; |
| 112 | + const ethToSend = ethBal > reserve ? ethBal - reserve : 0n; |
| 113 | + |
| 114 | + console.log('PLANNED TRANSFERS:'); |
| 115 | + for (const p of plan) console.log(` ERC20 ${p.human} (${p.token}) ~gas ${p.gas}`); |
| 116 | + console.log(` ETH ${ethers.formatEther(ethToSend)} (after gas reserve ${ethers.formatEther(reserve)})`); |
| 117 | + |
| 118 | + // Sufficiency check — the classic trap: tokens present, no gas to move them. |
| 119 | + if (plan.length > 0 && ethBal < erc20GasCost) { |
| 120 | + console.log(`\n⚠️ INSUFFICIENT GAS: holding ${plan.length} token balance(s) but only ` + |
| 121 | + `${ethers.formatEther(ethBal)} ETH; need ~${ethers.formatEther(erc20GasCost)} ETH to move them. ` + |
| 122 | + `Fund ${from} with a little ETH first.`); |
| 123 | + } |
| 124 | + |
| 125 | + if (!args.broadcast) { |
| 126 | + console.log('\nDRY-RUN complete. Nothing sent. Re-run with --broadcast (and the safety env) to execute.'); |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + // ── BROADCAST PATH (gated; intended to be run by the asset owner only) ── |
| 131 | + if (process.env.I_UNDERSTAND_IRREVERSIBLE !== 'yes') { |
| 132 | + console.error('\nREFUSED: --broadcast requires env I_UNDERSTAND_IRREVERSIBLE=yes. ' + |
| 133 | + 'These transfers are irreversible. Aborting.'); |
| 134 | + process.exit(1); |
| 135 | + } |
| 136 | + if (!signer) { |
| 137 | + console.error('REFUSED: no signing key in env. Set ETH_PRIVATE_KEY to broadcast.'); |
| 138 | + process.exit(1); |
| 139 | + } |
| 140 | + |
| 141 | + for (const p of plan) { |
| 142 | + const c = new ethers.Contract(p.token, ERC20_ABI, signer); |
| 143 | + const tx = await c.transfer(dest, BigInt(p.amount)); |
| 144 | + console.log(` sent ERC20 ${p.human} (${p.token}) tx ${tx.hash}`); |
| 145 | + await tx.wait(); |
| 146 | + } |
| 147 | + if (ethToSend > 0n) { |
| 148 | + const tx = await signer.sendTransaction({ to: dest, value: ethToSend }); |
| 149 | + console.log(` sent ETH ${ethers.formatEther(ethToSend)} tx ${tx.hash}`); |
| 150 | + await tx.wait(); |
| 151 | + } |
| 152 | + console.log('\nSweep complete. Verify on BaseScan, then retire the old key.'); |
| 153 | +} |
| 154 | + |
| 155 | +main().catch((e) => { console.error(e); process.exit(1); }); |
0 commit comments