-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost-mortem-analysis.ts
More file actions
416 lines (360 loc) · 16.4 KB
/
Copy pathpost-mortem-analysis.ts
File metadata and controls
416 lines (360 loc) · 16.4 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/**
* Comprehensive Post-Mortem Analysis
* - Find all successful mints from recent window
* - Analyze timing, gas, and inclusion patterns
* - Compare to our bot's performance
* - Identify failure points and improvements
*/
import 'dotenv/config'
import { createPublicClient, http, decodeEventLog, parseAbi } from 'viem'
import { loadConfig, beraChain } from './config.js'
const SACRIFICE_ADDR = '0xAAebaC0E0108846F61183265b764DD40b41A2534' as const
const STEADY_TEDDYS = '0x88888888A9361f15AAdBAca355A6B2938C6A674e' as const
const MIBERA = '0x6666397DFe9a8c469BF65dc744CB1C733416c420' as const
const OUR_WALLET = '0x48BA1467731accdc715026aa792Fb88AeE5f347f' as const
const TRANSFER_ABI = parseAbi([
'event Transfer(address indexed from, address indexed to, uint256 indexed tokenId)',
])
interface MintAnalysis {
txHash: string
blockNumber: bigint
blockTimestamp: number
from: string
gasPrice: bigint
maxFeePerGas: bigint | null
maxPriorityFeePerGas: bigint | null
gasUsed: bigint
gasLimit: bigint
nonce: number
positionInBlock: number
effectiveGasPrice: bigint
status: 'success' | 'reverted'
logs: number
hasTransferEvent: boolean
}
async function postMortemAnalysis() {
const client = createPublicClient({
chain: beraChain,
transport: http('https://rpc.berachain.com', { timeout: 30000 }),
})
console.log('🔍 COMPREHENSIVE POST-MORTEM ANALYSIS\n')
console.log('=' .repeat(80))
console.log('')
// Get current block and our transaction block
const currentBlock = await client.getBlockNumber()
const ourBlockNumber = 12958392n
const ourTxHash = '0xfe881782cb7863c5edd999091e7f59a22982446ce0a61ee646b97b8d9d40beb9' as const
console.log(`📊 Current block: ${currentBlock}`)
console.log(`📊 Our transaction block: ${ourBlockNumber}`)
console.log(`📊 Our transaction hash: ${ourTxHash}`)
console.log('')
// Analyze our transaction
console.log('=' .repeat(80))
console.log('OUR TRANSACTION ANALYSIS')
console.log('=' .repeat(80))
console.log('')
try {
const ourTx = await client.getTransaction({ hash: ourTxHash })
const ourReceipt = await client.getTransactionReceipt({ hash: ourTxHash })
const ourBlock = await client.getBlock({ blockNumber: ourBlockNumber, includeTransactions: true })
console.log(`Transaction: ${ourTxHash}`)
console.log(`Block: ${ourBlockNumber}`)
console.log(`Block timestamp: ${new Date(Number(ourBlock.timestamp) * 1000).toISOString()}`)
console.log(`Status: ${ourReceipt.status}`)
console.log(`From: ${ourTx.from}`)
console.log(`Nonce: ${ourTx.nonce}`)
console.log(`Gas limit: ${ourTx.gas.toString()}`)
console.log(`Gas used: ${ourReceipt.gasUsed.toString()}`)
console.log(`Effective gas price: ${ourReceipt.effectiveGasPrice?.toString() || 'N/A'} Gwei`)
console.log(`Max fee per gas: ${ourTx.maxFeePerGas?.toString() || 'N/A'} Gwei`)
console.log(`Max priority fee: ${ourTx.maxPriorityFeePerGas?.toString() || 'N/A'} Gwei`)
console.log(`Position in block: ${ourBlock.transactions.findIndex(t => typeof t === 'object' && t.hash === ourTxHash)}`)
console.log(`Total transactions in block: ${ourBlock.transactions.length}`)
console.log(`Logs: ${ourReceipt.logs.length}`)
console.log('')
// Check all transactions in the same block
const sacrificeTxs = ourBlock.transactions.filter(
(t): t is typeof t & { to: string } =>
typeof t === 'object' && t.to?.toLowerCase() === SACRIFICE_ADDR.toLowerCase()
)
console.log(`Transactions to sacrifice contract in block ${ourBlockNumber}: ${sacrificeTxs.length}`)
console.log('')
const allMints: MintAnalysis[] = []
for (const tx of sacrificeTxs) {
try {
const receipt = await client.getTransactionReceipt({ hash: tx.hash })
const position = ourBlock.transactions.findIndex(
t => typeof t === 'object' && 'hash' in t && t.hash === tx.hash
)
const hasTransferEvent = receipt.logs.some(log => {
try {
const decoded = decodeEventLog({
abi: TRANSFER_ABI,
data: log.data,
topics: log.topics,
})
return decoded.args.from?.toLowerCase() === '0x0000000000000000000000000000000000000000'
} catch {
return false
}
})
allMints.push({
txHash: tx.hash,
blockNumber: ourBlockNumber,
blockTimestamp: Number(ourBlock.timestamp) * 1000,
from: tx.from,
gasPrice: tx.gasPrice || 0n,
maxFeePerGas: tx.maxFeePerGas || null,
maxPriorityFeePerGas: tx.maxPriorityFeePerGas || null,
gasUsed: receipt.gasUsed,
gasLimit: tx.gas,
nonce: tx.nonce,
positionInBlock: position,
effectiveGasPrice: receipt.effectiveGasPrice || 0n,
status: receipt.status,
logs: receipt.logs.length,
hasTransferEvent,
})
} catch (e) {
console.log(`Error analyzing tx ${tx.hash}: ${e}`)
}
}
// Sort by position in block
allMints.sort((a, b) => a.positionInBlock - b.positionInBlock)
console.log('=' .repeat(80))
console.log('ALL TRANSACTIONS IN BLOCK (sorted by position)')
console.log('=' .repeat(80))
console.log('')
for (const mint of allMints) {
const isOurs = mint.from.toLowerCase() === OUR_WALLET.toLowerCase()
const marker = isOurs ? ' 👈 OUR TX' : ''
const successMarker = mint.status === 'success' && mint.hasTransferEvent ? ' ✅ SUCCESS' : ''
const revertedMarker = mint.status === 'reverted' ? ' ❌ REVERTED' : ''
console.log(`Position ${mint.positionInBlock}: ${mint.txHash.slice(0, 20)}...${marker}${successMarker}${revertedMarker}`)
console.log(` From: ${mint.from}`)
console.log(` Status: ${mint.status}`)
console.log(` Nonce: ${mint.nonce}`)
console.log(` Gas price: ${(Number(mint.effectiveGasPrice) / 1e9).toFixed(2)} Gwei`)
if (mint.maxFeePerGas) {
console.log(` Max fee: ${(Number(mint.maxFeePerGas) / 1e9).toFixed(2)} Gwei`)
}
if (mint.maxPriorityFeePerGas) {
console.log(` Max priority: ${(Number(mint.maxPriorityFeePerGas) / 1e9).toFixed(2)} Gwei`)
}
console.log(` Gas used: ${mint.gasUsed.toString()}`)
console.log(` Logs: ${mint.logs}${mint.hasTransferEvent ? ' (has Transfer event)' : ''}`)
console.log('')
}
// Find successful mints
const successfulMints = allMints.filter(m => m.status === 'success' && m.hasTransferEvent)
console.log('=' .repeat(80))
console.log('SUCCESSFUL MINTS SUMMARY')
console.log('=' .repeat(80))
console.log('')
if (successfulMints.length === 0) {
console.log('❌ NO SUCCESSFUL MINTS FOUND IN THIS BLOCK')
console.log('')
console.log('This suggests:')
console.log(' 1. The window was not open when this block was mined')
console.log(' 2. All transactions reverted because contract was paused')
console.log(' 3. The expected unlock time was incorrect')
} else {
console.log(`✅ Found ${successfulMints.length} successful mint(s):`)
console.log('')
for (const mint of successfulMints) {
console.log(` ${mint.txHash}`)
console.log(` Winner: ${mint.from}`)
console.log(` Position: ${mint.positionInBlock}`)
console.log(` Gas price: ${(Number(mint.effectiveGasPrice) / 1e9).toFixed(2)} Gwei`)
console.log(` Max fee: ${mint.maxFeePerGas ? (Number(mint.maxFeePerGas) / 1e9).toFixed(2) : 'N/A'} Gwei`)
console.log('')
}
}
// Search wider range for successful mints
console.log('=' .repeat(80))
console.log('SEARCHING WIDER BLOCK RANGE FOR SUCCESSFUL MINTS')
console.log('=' .repeat(80))
console.log('')
const searchStart = ourBlockNumber - 50n
const searchEnd = ourBlockNumber + 50n
console.log(`Searching blocks ${searchStart} to ${searchEnd}...`)
console.log('')
const widerMints: MintAnalysis[] = []
for (let blockNum = searchStart; blockNum <= searchEnd; blockNum++) {
try {
const block = await client.getBlock({ blockNumber: blockNum, includeTransactions: true })
const sacrificeTxs = block.transactions.filter(
(t): t is typeof t & { to: string } =>
typeof t === 'object' && t.to?.toLowerCase() === SACRIFICE_ADDR.toLowerCase()
)
for (const tx of sacrificeTxs) {
try {
const receipt = await client.getTransactionReceipt({ hash: tx.hash })
const position = block.transactions.findIndex(
t => typeof t === 'object' && 'hash' in t && t.hash === tx.hash
)
const hasTransferEvent = receipt.logs.some(log => {
try {
const decoded = decodeEventLog({
abi: TRANSFER_ABI,
data: log.data,
topics: log.topics,
})
return decoded.args.from?.toLowerCase() === '0x0000000000000000000000000000000000000000'
} catch {
return false
}
})
if (receipt.status === 'success' && hasTransferEvent) {
widerMints.push({
txHash: tx.hash,
blockNumber: blockNum,
blockTimestamp: Number(block.timestamp) * 1000,
from: tx.from,
gasPrice: tx.gasPrice || 0n,
maxFeePerGas: tx.maxFeePerGas || null,
maxPriorityFeePerGas: tx.maxPriorityFeePerGas || null,
gasUsed: receipt.gasUsed,
gasLimit: tx.gas,
nonce: tx.nonce,
positionInBlock: position,
effectiveGasPrice: receipt.effectiveGasPrice || 0n,
status: receipt.status,
logs: receipt.logs.length,
hasTransferEvent,
})
}
} catch (e) {
// Skip errors
}
}
if (blockNum % 10n === 0n) {
process.stdout.write(`\r Searched ${blockNum - searchStart + 1n} blocks...`)
}
} catch (e) {
// Skip block errors
}
}
console.log(`\n\n✅ Found ${widerMints.length} successful mint(s) in wider range`)
console.log('')
if (widerMints.length > 0) {
console.log('SUCCESSFUL MINTS:')
console.log('')
for (const mint of widerMints) {
const timeDiff = mint.blockTimestamp - (Number(ourBlock.timestamp) * 1000)
const timeDiffSec = (timeDiff / 1000).toFixed(1)
const timeMarker = timeDiff > 0 ? `+${timeDiffSec}s after our block` : `${Math.abs(Number(timeDiffSec))}s before our block`
console.log(` Block ${mint.blockNumber} (${timeMarker})`)
console.log(` TX: ${mint.txHash}`)
console.log(` Winner: ${mint.from}`)
console.log(` Position: ${mint.positionInBlock}`)
console.log(` Gas price: ${(Number(mint.effectiveGasPrice) / 1e9).toFixed(2)} Gwei`)
console.log(` Max fee: ${mint.maxFeePerGas ? (Number(mint.maxFeePerGas) / 1e9).toFixed(2) : 'N/A'} Gwei`)
console.log(` Timestamp: ${new Date(mint.blockTimestamp).toISOString()}`)
console.log('')
}
// Gas analysis
const gasPrices = widerMints.map(m => Number(m.effectiveGasPrice) / 1e9)
const maxFees = widerMints.map(m => m.maxFeePerGas ? Number(m.maxFeePerGas) / 1e9 : 0).filter(f => f > 0)
console.log('GAS ANALYSIS:')
console.log(` Min gas price: ${Math.min(...gasPrices).toFixed(2)} Gwei`)
console.log(` Max gas price: ${Math.max(...gasPrices).toFixed(2)} Gwei`)
console.log(` Avg gas price: ${(gasPrices.reduce((a, b) => a + b, 0) / gasPrices.length).toFixed(2)} Gwei`)
if (maxFees.length > 0) {
console.log(` Min max fee: ${Math.min(...maxFees).toFixed(2)} Gwei`)
console.log(` Max max fee: ${Math.max(...maxFees).toFixed(2)} Gwei`)
console.log(` Avg max fee: ${(maxFees.reduce((a, b) => a + b, 0) / maxFees.length).toFixed(2)} Gwei`)
}
console.log('')
}
// Our gas comparison
console.log('=' .repeat(80))
console.log('OUR BOT PERFORMANCE ANALYSIS')
console.log('=' .repeat(80))
console.log('')
const ourGasPrice = Number(ourReceipt.effectiveGasPrice || 0n) / 1e9
const ourMaxFee = ourTx.maxFeePerGas ? Number(ourTx.maxFeePerGas) / 1e9 : 0
console.log(`Our gas price: ${ourGasPrice.toFixed(2)} Gwei`)
console.log(`Our max fee: ${ourMaxFee.toFixed(2)} Gwei`)
console.log(`Our status: ${ourReceipt.status}`)
console.log(`Our position: ${ourBlock.transactions.findIndex(t => typeof t === 'object' && t.hash === ourTxHash)}`)
console.log('')
if (widerMints.length > 0) {
const gasPrices = widerMints.map(m => Number(m.effectiveGasPrice) / 1e9)
const maxFees = widerMints.map(m => m.maxFeePerGas ? Number(m.maxFeePerGas) / 1e9 : 0).filter(f => f > 0)
const winnerGasPrice = Math.min(...gasPrices)
const winnerMaxFee = maxFees.length > 0 ? Math.min(...maxFees) : 0
console.log('COMPARISON:')
console.log(` Winner gas price: ${winnerGasPrice.toFixed(2)} Gwei`)
console.log(` Our gas price: ${ourGasPrice.toFixed(2)} Gwei`)
console.log(` Difference: ${(ourGasPrice - winnerGasPrice).toFixed(2)} Gwei ${ourGasPrice > winnerGasPrice ? '(we paid MORE)' : '(we paid LESS)'}`)
if (winnerMaxFee > 0) {
console.log(` Winner max fee: ${winnerMaxFee.toFixed(2)} Gwei`)
console.log(` Our max fee: ${ourMaxFee.toFixed(2)} Gwei`)
console.log(` Difference: ${(ourMaxFee - winnerMaxFee).toFixed(2)} Gwei ${ourMaxFee > winnerMaxFee ? '(we paid MORE)' : '(we paid LESS)'}`)
}
console.log('')
}
// Timing analysis
console.log('=' .repeat(80))
console.log('TIMING ANALYSIS')
console.log('=' .repeat(80))
console.log('')
const ourBlockTime = Number(ourBlock.timestamp) * 1000
console.log(`Our block timestamp: ${new Date(ourBlockTime).toISOString()}`)
console.log(`Expected unlock time: 2025-11-12T07:11:11.000Z (from logs)`)
console.log('')
const expectedUnlockTime = new Date('2025-11-12T07:11:11.000Z').getTime()
const timeDiff = ourBlockTime - expectedUnlockTime
console.log(`Time difference: ${(timeDiff / 1000).toFixed(1)} seconds ${timeDiff > 0 ? 'AFTER' : 'BEFORE'} expected unlock`)
console.log('')
if (widerMints.length > 0) {
const firstMint = widerMints[0]
const firstMintTime = firstMint.blockTimestamp
const timeToFirstMint = firstMintTime - expectedUnlockTime
console.log(`First successful mint: ${new Date(firstMintTime).toISOString()}`)
console.log(`Time from expected unlock: ${(timeToFirstMint / 1000).toFixed(1)} seconds`)
console.log('')
}
// Final summary
console.log('=' .repeat(80))
console.log('FINAL SUMMARY')
console.log('=' .repeat(80))
console.log('')
console.log('WHAT HAPPENED:')
console.log(` 1. Our transaction was included in block ${ourBlockNumber}`)
console.log(` 2. Our transaction status: ${ourReceipt.status}`)
console.log(` 3. Successful mints found: ${widerMints.length}`)
console.log('')
if (ourReceipt.status === 'reverted') {
console.log('WHY WE FAILED:')
console.log(' ❌ Transaction reverted - window was not open')
console.log(' ❌ Contract was still paused when our transaction was included')
console.log('')
}
if (widerMints.length === 0) {
console.log('POSSIBLE REASONS:')
console.log(' 1. Window did not open at expected time')
console.log(' 2. Expected unlock time calculation was incorrect')
console.log(' 3. Window opened but no one successfully minted')
console.log(' 4. Window opened at a different time than calculated')
console.log('')
} else {
console.log('WHAT WE CAN LEARN:')
console.log(` ✅ ${widerMints.length} successful mint(s) occurred`)
if (widerMints.length > 0) {
const gasPrices = widerMints.map(m => Number(m.effectiveGasPrice) / 1e9)
console.log(` ✅ Winners used gas prices: ${gasPrices.map((g: number) => g.toFixed(2)).join(', ')} Gwei`)
if (ourGasPrice > 0) {
const wasHighEnough = ourGasPrice >= Math.min(...gasPrices)
console.log(` ${wasHighEnough ? '✅' : '❌'} Our gas was ${wasHighEnough ? 'HIGH ENOUGH' : 'TOO LOW'}`)
}
}
console.log('')
}
} catch (error: any) {
console.error('Error in analysis:', error.message)
console.error(error.stack)
}
}
postMortemAnalysis().catch(console.error)