|
| 1 | +/** |
| 2 | + * Recovery script: deploy phantom proxy wallet via factory and transfer stuck USDC back to EOA. |
| 3 | + * |
| 4 | + * The polymarket-cli derived a proxy address via CREATE2 but never deployed the contract. |
| 5 | + * 55.49 USDC.e + 2.5 USDC are stuck at that address. |
| 6 | + * |
| 7 | + * This script calls the ProxyWalletFactory.proxy() which: |
| 8 | + * 1. Auto-deploys the proxy wallet (CREATE2) if it doesn't exist |
| 9 | + * 2. Executes ERC20 transfer calls through the proxy to send funds back to the EOA |
| 10 | + * |
| 11 | + * Usage: |
| 12 | + * DRY_RUN=1 node recover-proxy-funds.js # simulate only |
| 13 | + * node recover-proxy-funds.js # execute for real |
| 14 | + */ |
| 15 | + |
| 16 | +const { ethers } = require("ethers"); |
| 17 | + |
| 18 | +// ─── Configuration ─────────────────────────────────────────────────────────── |
| 19 | + |
| 20 | +const RPC_URL = "https://polygon.drpc.org"; |
| 21 | +const PRIVATE_KEY = "0x67369faeaf47af03ea0519a3e40431d3bdf947454c2f121a7672593ebdc657a9"; |
| 22 | + |
| 23 | +const FACTORY_ADDRESS = "0xaB45c5A4B0c941a2F231C04C3f49182e1A254052"; |
| 24 | +const PHANTOM_PROXY = "0x17a535cdd1cdb870b0e8d735c1dcc95e14727caf"; |
| 25 | + |
| 26 | +// Tokens stuck at the phantom proxy |
| 27 | +const USDC_E = "0x2791bca1f2de4661ed88a30c99a7a9449aa84174"; // USDC.e (PoS bridged) |
| 28 | +const USDC = "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359"; // native USDC |
| 29 | + |
| 30 | +// ─── ABIs ──────────────────────────────────────────────────────────────────── |
| 31 | + |
| 32 | +const FACTORY_ABI = [ |
| 33 | + "function proxy(tuple(uint8 operation, address to, uint256 value, bytes data)[] calls) payable returns (bytes[])", |
| 34 | +]; |
| 35 | + |
| 36 | +const ERC20_ABI = [ |
| 37 | + "function transfer(address to, uint256 amount) returns (bool)", |
| 38 | + "function balanceOf(address account) view returns (uint256)", |
| 39 | +]; |
| 40 | + |
| 41 | +// ─── Main ──────────────────────────────────────────────────────────────────── |
| 42 | + |
| 43 | +async function main() { |
| 44 | + const dryRun = process.env.DRY_RUN === "1"; |
| 45 | + |
| 46 | + const provider = new ethers.JsonRpcProvider(RPC_URL); |
| 47 | + const wallet = new ethers.Wallet(PRIVATE_KEY, provider); |
| 48 | + const eoa = wallet.address; |
| 49 | + |
| 50 | + console.log("EOA address: ", eoa); |
| 51 | + console.log("Phantom proxy: ", PHANTOM_PROXY); |
| 52 | + console.log("Factory: ", FACTORY_ADDRESS); |
| 53 | + console.log("Dry run: ", dryRun); |
| 54 | + console.log(); |
| 55 | + |
| 56 | + // Verify EOA matches expected |
| 57 | + if (eoa.toLowerCase() !== "0x7b75418121134dce9d0a40a80762a8d8f379d49d") { |
| 58 | + throw new Error(`EOA mismatch! Got ${eoa}, expected 0x7b754...`); |
| 59 | + } |
| 60 | + |
| 61 | + // Check phantom proxy has no code (not yet deployed) |
| 62 | + const code = await provider.getCode(PHANTOM_PROXY); |
| 63 | + if (code !== "0x") { |
| 64 | + console.log("WARNING: Proxy already has code deployed. It may have been deployed already."); |
| 65 | + console.log("Code:", code.slice(0, 40) + "..."); |
| 66 | + } else { |
| 67 | + console.log("Confirmed: no contract at phantom proxy (will be deployed by factory)"); |
| 68 | + } |
| 69 | + |
| 70 | + // Check token balances at phantom proxy |
| 71 | + const usdcE = new ethers.Contract(USDC_E, ERC20_ABI, provider); |
| 72 | + const usdc = new ethers.Contract(USDC, ERC20_ABI, provider); |
| 73 | + |
| 74 | + const balUsdcE = await usdcE.balanceOf(PHANTOM_PROXY); |
| 75 | + const balUsdc = await usdc.balanceOf(PHANTOM_PROXY); |
| 76 | + |
| 77 | + console.log(`USDC.e balance: ${ethers.formatUnits(balUsdcE, 6)} USDC.e`); |
| 78 | + console.log(`USDC balance: ${ethers.formatUnits(balUsdc, 6)} USDC`); |
| 79 | + console.log(); |
| 80 | + |
| 81 | + if (balUsdcE === 0n && balUsdc === 0n) { |
| 82 | + console.log("Nothing to recover — both balances are zero."); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // Build ERC20 transfer calldata |
| 87 | + const erc20Iface = new ethers.Interface(ERC20_ABI); |
| 88 | + const calls = []; |
| 89 | + |
| 90 | + if (balUsdcE > 0n) { |
| 91 | + calls.push({ |
| 92 | + operation: 1, // CALL through proxy |
| 93 | + to: USDC_E, |
| 94 | + value: 0, |
| 95 | + data: erc20Iface.encodeFunctionData("transfer", [eoa, balUsdcE]), |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + if (balUsdc > 0n) { |
| 100 | + calls.push({ |
| 101 | + operation: 1, // CALL through proxy |
| 102 | + to: USDC, |
| 103 | + value: 0, |
| 104 | + data: erc20Iface.encodeFunctionData("transfer", [eoa, balUsdc]), |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + console.log(`Prepared ${calls.length} transfer call(s) through factory.proxy()`); |
| 109 | + for (const c of calls) { |
| 110 | + console.log(` → transfer to ${eoa} via token ${c.to}`); |
| 111 | + } |
| 112 | + console.log(); |
| 113 | + |
| 114 | + const factory = new ethers.Contract(FACTORY_ADDRESS, FACTORY_ABI, wallet); |
| 115 | + |
| 116 | + if (dryRun) { |
| 117 | + // Simulate with eth_call |
| 118 | + console.log("=== DRY RUN: simulating transaction ==="); |
| 119 | + try { |
| 120 | + const result = await factory.proxy.staticCall(calls); |
| 121 | + console.log("Simulation SUCCESS!"); |
| 122 | + console.log("Return values:", result); |
| 123 | + } catch (err) { |
| 124 | + console.error("Simulation FAILED:", err.message); |
| 125 | + if (err.data) console.error("Revert data:", err.data); |
| 126 | + } |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + // Execute for real |
| 131 | + console.log("=== EXECUTING TRANSACTION ==="); |
| 132 | + const tx = await factory.proxy(calls, { |
| 133 | + gasLimit: 500_000, // generous limit for proxy deploy + 2 transfers |
| 134 | + }); |
| 135 | + console.log("Transaction sent:", tx.hash); |
| 136 | + console.log("Waiting for confirmation..."); |
| 137 | + |
| 138 | + const receipt = await tx.wait(); |
| 139 | + console.log(); |
| 140 | + console.log("Transaction confirmed!"); |
| 141 | + console.log("Block: ", receipt.blockNumber); |
| 142 | + console.log("Gas used: ", receipt.gasUsed.toString()); |
| 143 | + console.log("Status: ", receipt.status === 1 ? "SUCCESS" : "FAILED"); |
| 144 | + console.log(`Polygonscan: https://polygonscan.com/tx/${tx.hash}`); |
| 145 | + |
| 146 | + // Verify funds arrived at EOA |
| 147 | + console.log(); |
| 148 | + console.log("=== Post-recovery balances ==="); |
| 149 | + const newBalProxy_E = await usdcE.balanceOf(PHANTOM_PROXY); |
| 150 | + const newBalProxy_U = await usdc.balanceOf(PHANTOM_PROXY); |
| 151 | + const newBalEoa_E = await usdcE.balanceOf(eoa); |
| 152 | + const newBalEoa_U = await usdc.balanceOf(eoa); |
| 153 | + |
| 154 | + console.log(`Phantom proxy: ${ethers.formatUnits(newBalProxy_E, 6)} USDC.e, ${ethers.formatUnits(newBalProxy_U, 6)} USDC`); |
| 155 | + console.log(`EOA: ${ethers.formatUnits(newBalEoa_E, 6)} USDC.e, ${ethers.formatUnits(newBalEoa_U, 6)} USDC`); |
| 156 | +} |
| 157 | + |
| 158 | +main().catch((err) => { |
| 159 | + console.error("FATAL:", err); |
| 160 | + process.exit(1); |
| 161 | +}); |
0 commit comments