-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick-debug.js
More file actions
187 lines (153 loc) · 7.74 KB
/
quick-debug.js
File metadata and controls
187 lines (153 loc) · 7.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
require('dotenv').config();
const { ethers } = require('ethers');
// Fast RPC endpoints
const RPC_URLS = [
'https://rpc.ankr.com/arbitrum',
'https://arbitrum-one-rpc.publicnode.com',
'https://1rpc.io/arb'
];
const ORACLE_ADDRESS = '0xa935db8cb0f45256e3df77cbff1980cf7aeffbe4';
const BEACON_ADDRESS = '0x0d623183d4a0de6e4871dfdd1350312c3b1538b6';
const colors = {
reset: '\x1b[0m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
cyan: '\x1b[36m'
};
async function getFastProvider() {
for (const rpc of RPC_URLS) {
try {
const provider = new ethers.providers.JsonRpcProvider({
url: rpc,
timeout: 8000
});
const block = await Promise.race([
provider.getBlockNumber(),
new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), 3000))
]);
console.log(`${colors.green}✅ Using: ${rpc.split('/')[2]}${colors.reset}`);
return provider;
} catch (e) {
console.log(`${colors.red}❌ ${rpc.split('/')[2]} failed${colors.reset}`);
}
}
throw new Error('All RPCs failed');
}
async function main() {
console.log(`${colors.cyan}🚀 Quick Settlement Debug${colors.reset}\n`);
try {
// Connect
const provider = await getFastProvider();
console.log('');
// Basic state
const [block, timestamp] = await Promise.all([
provider.getBlockNumber(),
provider.getBlock('latest').then(b => b.timestamp)
]);
console.log(`${colors.blue}Chain State:${colors.reset}`);
console.log(`Block: ${block}, Time: ${new Date(timestamp * 1000).toLocaleTimeString()}\n`);
// Oracle state
const oracle = new ethers.Contract(ORACLE_ADDRESS, [
'function nextReportId() view returns (uint256)',
'function reportMeta(uint256) view returns (tuple(address,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,bool))',
'function reportStatus(uint256) view returns (tuple(uint256,uint256,address,address,uint256,uint256,uint256,uint256,bool,bool,bool,uint256,uint256))',
'function extraData(uint256) view returns (address,uint256,address,bytes4,bool,uint256,uint256,bool)',
'function settle(uint256) returns (uint256,uint256)'
], provider);
const nextId = await oracle.nextReportId();
console.log(`${colors.blue}Oracle State:${colors.reset}`);
console.log(`Next Report ID: ${nextId}\n`);
// Check last 10 reports
console.log(`${colors.blue}Scanning Reports ${nextId-10} to ${nextId-1}:${colors.reset}`);
let opportunities = 0;
const gasPrice = await provider.getGasPrice();
console.log(`Gas Price: ${ethers.utils.formatUnits(gasPrice, 'gwei')} gwei\n`);
for (let id = nextId - 1; id >= Math.max(1, nextId - 10); id--) {
try {
const [meta, status, extra] = await Promise.all([
oracle.reportMeta(id),
oracle.reportStatus(id),
oracle.extraData(id)
]);
console.log(`Report #${id}:`);
// Quick checks
if (status[8] || status[9] || status[10]) { // isSettled, disputeOccurred, isDistributed
console.log(` ❌ Already processed\n`);
continue;
}
if (status[2] === ethers.constants.AddressZero) { // currentReporter
console.log(` ❌ No report submitted\n`);
continue;
}
if (extra[2] !== ethers.constants.AddressZero) { // callbackContract
console.log(` ❌ Has callback contract\n`);
continue;
}
// Timing check
const reportTime = status[4].toNumber(); // reportTimestamp
const settlementTime = meta[4].toNumber(); // settlementTime
const isTimeType = meta[12]; // timeType
const currentTime = isTimeType ? timestamp : block;
const deadline = reportTime + settlementTime;
console.log(` ${isTimeType ? 'Time' : 'Block'} based settlement`);
console.log(` Current: ${currentTime}, Deadline: ${deadline}`);
if (currentTime < deadline) {
const remaining = deadline - currentTime;
console.log(` ⏰ ${remaining} ${isTimeType ? 'seconds' : 'blocks'} until settleable\n`);
continue;
}
// Profitability
const reward = meta[10]; // settlerReward
const estimatedGas = 150000;
const gasCost = gasPrice.mul(estimatedGas);
const profit = reward.sub(gasCost);
console.log(` 💰 Reward: ${ethers.utils.formatEther(reward)} ETH`);
console.log(` 💸 Gas Cost: ${ethers.utils.formatEther(gasCost)} ETH`);
console.log(` 📈 Profit: ${ethers.utils.formatEther(profit)} ETH`);
if (profit.gt(0)) {
console.log(` ${colors.green}🎯 SETTLEABLE OPPORTUNITY!${colors.reset}`);
opportunities++;
// Try to estimate actual gas
try {
const gas = await oracle.estimateGas.settle(id);
console.log(` 🔥 Actual gas estimate: ${gas.toString()}`);
} catch (e) {
console.log(` ${colors.red}❌ Would revert: ${e.reason || e.message.substring(0, 50)}${colors.reset}`);
}
} else {
console.log(` ❌ Not profitable`);
}
console.log('');
} catch (e) {
console.log(` ${colors.red}Error: ${e.message.substring(0, 50)}${colors.reset}\n`);
}
}
console.log(`${colors.cyan}=== SUMMARY ===${colors.reset}`);
console.log(`Settlement opportunities found: ${opportunities}`);
// Test SmartAutoBeacon
console.log(`\n${colors.blue}Testing SmartAutoBeacon:${colors.reset}`);
try {
const beacon = new ethers.Contract(BEACON_ADDRESS, [
'function freeMoneyLight() external'
], provider);
const gas = await beacon.estimateGas.freeMoneyLight();
const cost = gasPrice.mul(gas);
console.log(`${colors.green}✅ freeMoneyLight() would succeed${colors.reset}`);
console.log(`Gas needed: ${gas.toString()}, Cost: ${ethers.utils.formatEther(cost)} ETH`);
} catch (e) {
console.log(`${colors.yellow}❌ freeMoneyLight() would fail: ${e.reason || e.message.substring(0, 100)}${colors.reset}`);
}
if (opportunities === 0) {
console.log(`\n${colors.yellow}No opportunities found. This means:${colors.reset}`);
console.log(`- Reports aren't ready for settlement yet`);
console.log(`- All available reports have been settled`);
console.log(`- Gas prices make settlements unprofitable`);
console.log(`- Reports have callback contracts`);
}
} catch (error) {
console.log(`${colors.red}Fatal error: ${error.message}${colors.reset}`);
}
}
main();