-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.js
More file actions
979 lines (849 loc) · 31.8 KB
/
Copy pathmonitor.js
File metadata and controls
979 lines (849 loc) · 31.8 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
require("dotenv").config();
const http = require("http");
const fcl = require("@onflow/fcl");
const fetch = require("node-fetch");
const t = require("@onflow/types");
const fs = require("fs");
const path = require("path");
const { TwitterApi } = require("twitter-api-v2");
const { subscribeToEvents } = require("fcl-subscribe");
const logger = require("./logger");
const { renderSaleCard } = require("./render-card");
const { computeHealthStatus } = require("./lib/helpers");
/* ── File Logging Setup ─────────────────────────────────── */
// Create log file with timestamp
const logFileName = `pinnacle-bot-${new Date().toISOString().split("T")[0]}.log`;
const logFilePath = path.join(__dirname, logFileName);
// Function to write to log file
function writeToLogFile(message) {
try {
fs.appendFileSync(logFilePath, message + "\n");
} catch (error) {
// Silently fail if we can't write to log file
}
}
/* ── Config ─────────────────────────────────────────────── */
const config = {
/* Flow */
FLOW_ACCESS_NODE: process.env.FLOW_ACCESS_NODE || "https://mainnet.onflow.org",
FLOW_REST_ENDPOINT: process.env.FLOW_REST_ENDPOINT || "https://rest-mainnet.onflow.org",
/* Pinnacle */
PINNACLE_NFT_TYPE: "A.edf9df96c92f4595.Pinnacle.NFT",
PINNACLE_PRICE_THRESHOLD: 150,
EVENT_TYPES: {
LISTING_COMPLETED: [
"A.4eb8a10cb9f87357.NFTStorefrontV2.ListingCompleted",
"A.3cdbb3d569211ff3.NFTStorefrontV2.ListingCompleted",
],
OFFER_COMPLETED: "A.b8ea91944fd51c43.OffersV2.OfferCompleted",
},
/* Command-line flag parsing */
ENABLE_TWEETS: process.argv.includes("--live-tweets"),
IS_BACKFILL: process.argv.includes("--backfill"),
};
/* ── Edition Type Map ───────────────────────────────────── */
const EDITION_TYPE_NAMES = {
1: "Genesis",
9: "Legendary",
3: "Limited",
7: "Ltd Event",
8: "Open Event",
4: "Open",
5: "Starter",
};
/* ── Image Cache ────────────────────────────────────────── */
// Removed caching to ensure images are always included in tweets
/* ── Logger ─────────────────────────────────────────────── */
function log(type, message, data = {}) {
const level = type === "fatal" ? "fatal" : type;
const hasData = Object.keys(data).length > 0;
// Structured logging via Pino
if (hasData) {
logger[level] ? logger[level](data, message) : logger.info(data, message);
} else {
logger[level] ? logger[level](message) : logger.info(message);
}
// File output (preserved for production log files)
const timestamp = new Date().toISOString();
const logMessage = `[${timestamp}] ${type.toUpperCase()}: ${message}`;
writeToLogFile(logMessage);
if (hasData) {
writeToLogFile(JSON.stringify(data, null, 2));
}
}
/* ── Twitter Client ─────────────────────────────────────── */
let twitterClient;
if (config.ENABLE_TWEETS) {
twitterClient = new TwitterApi({
appKey: process.env.PINNACLEPINBOT_API_KEY,
appSecret: process.env.PINNACLEPINBOT_API_SECRET,
accessToken: process.env.PINNACLEPINBOT_ACCESS_TOKEN,
accessSecret: process.env.PINNACLEPINBOT_ACCESS_SECRET,
});
}
/* ── Tweet Throttle (separate tweets, enforced 60s gap + jitter) ──
- Never batches tweets.
- Serializes posting so two tweets can’t fire in the same second.
- Gap = TWEET_GAP_SECONDS (default 60) + random(0..TWEET_JITTER_SECONDS) (default 20).
*/
const TWEET_GAP_SECONDS = Number(process.env.TWEET_GAP_SECONDS || 60);
const TWEET_JITTER_SECONDS = Number(process.env.TWEET_JITTER_SECONDS || 20);
const TWEET_GAP_MS = Math.max(0, TWEET_GAP_SECONDS) * 1000;
const TWEET_JITTER_MS = Math.max(0, TWEET_JITTER_SECONDS) * 1000;
let lastTweetAtMs = 0;
let tweetWorkerRunning = false;
const tweetQueue = []; // [{ job: async () => void, resolve: () => void }]
let tweetsSent = 0;
let lastTweetAt = null;
let lastSaleAttemptedAt = null;
let failedTweets = 0;
/* ── Discord Alerts ─────────────────────────────────────── */
async function sendDiscordAlert(title, description, color = 0xff0000) {
const webhookUrl = process.env.DISCORD_WEBHOOK_URL;
if (!webhookUrl) {
log("warn", "DISCORD_WEBHOOK_URL not set, skipping alert");
return;
}
try {
await fetch(webhookUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
embeds: [{
title: `🚨 PinnacleBot: ${title}`,
description,
color,
timestamp: new Date().toISOString(),
}],
}),
});
} catch (err) {
log("error", "Failed to send Discord alert", { error: err.message });
}
}
function nextDelayMs() {
const jitter = TWEET_JITTER_MS ? Math.floor(Math.random() * (TWEET_JITTER_MS + 1)) : 0;
return TWEET_GAP_MS + jitter;
}
function enqueueTweet(job) {
return new Promise((resolve) => {
tweetQueue.push({ job, resolve });
void processTweetQueue();
});
}
async function processTweetQueue() {
if (tweetWorkerRunning) return;
tweetWorkerRunning = true;
try {
while (tweetQueue.length > 0) {
const { job, resolve } = tweetQueue.shift();
// Enforce minimum spacing between tweet attempts
if (lastTweetAtMs) {
const required = nextDelayMs();
const waitMs = lastTweetAtMs + required - Date.now();
if (waitMs > 0) {
log("info", "Tweet throttle waiting", {
waitMs,
queueDepth: tweetQueue.length,
gapSeconds: TWEET_GAP_SECONDS,
jitterSeconds: TWEET_JITTER_SECONDS,
});
await new Promise((r) => setTimeout(r, waitMs));
}
}
try {
await job();
} catch (e) {
log("error", "Tweet job failed (caught by queue)", { error: e?.message || String(e) });
} finally {
// Update after each attempt so we never rapid-fire even on errors
lastTweetAtMs = Date.now();
resolve();
}
}
} finally {
tweetWorkerRunning = false;
}
}
/* ── Flow Node Pool ────────────────────────────────────── */
const NODE_POOL = [
{ fcl: "https://mainnet.onflow.org", rest: "https://rest-mainnet.onflow.org" },
{ fcl: "https://access-mainnet.onflow.org", rest: "https://rest-mainnet.onflow.org" },
];
// Per-node health tracking
const nodeHealth = NODE_POOL.map(() => ({
consecutiveFailures: 0,
lastFailure: 0,
lastSuccess: 0,
}));
const PRIMARY_RECOVERY_MS = 60_000; // Try primary again after 60s
let activeNodeIndex = 0;
function pickNode() {
const now = Date.now();
const primary = nodeHealth[0];
// If we're on a fallback and primary has cooled off, try primary again
if (
activeNodeIndex !== 0 &&
primary.consecutiveFailures > 0 &&
now - primary.lastFailure > PRIMARY_RECOVERY_MS
) {
log("info", "Primary node cooldown elapsed, rotating back to primary");
setActiveNode(0);
}
return activeNodeIndex;
}
function setActiveNode(index) {
if (index === activeNodeIndex) return;
activeNodeIndex = index;
fcl.config().put("accessNode.api", NODE_POOL[index].fcl);
log("warn", `Switched to node ${index}: ${NODE_POOL[index].fcl}`);
}
function markNodeSuccess(index) {
nodeHealth[index].consecutiveFailures = 0;
nodeHealth[index].lastSuccess = Date.now();
}
function markNodeFailure(index) {
nodeHealth[index].consecutiveFailures++;
nodeHealth[index].lastFailure = Date.now();
}
function getRestEndpoint() {
return NODE_POOL[activeNodeIndex].rest;
}
/* ── Flow Setup ─────────────────────────────────────────── */
fcl
.config()
.put("accessNode.api", NODE_POOL[0].fcl)
.put("fcl.eventPollRate", 500)
.put("fcl.requestLogging", false); // Suppress "Access node unavailable" console.warn spam
/* ── Cadence Scripts ────────────────────────────────────── */
let pinnacleScript, editionScript;
try {
pinnacleScript = fs.readFileSync(path.join(__dirname, "flow", "pinnacle.cdc"), "utf8");
editionScript = fs.readFileSync(path.join(__dirname, "flow", "get_edition.cdc"), "utf8");
} catch (error) {
log("error", "Failed to load Cadence scripts from /flow directory.", error);
process.exit(1);
}
/* ── Helper Functions ───────────────────────────────────── */
// Simple retry for REST calls — no node rotation, just backoff
async function retrySimple(op, n = 3, d = 1000) {
let lastErr;
for (let i = 0; i < n; i++) {
try {
return await op();
} catch (err) {
lastErr = err;
if (i < n - 1) await new Promise((r) => setTimeout(r, d));
}
}
throw lastErr;
}
// Node-aware retry for FCL calls — tries all nodes before giving up
async function retryWithFailover(op, n = 3, d = 1000) {
let lastErr;
const startNode = pickNode();
for (let nodeAttempt = 0; nodeAttempt < NODE_POOL.length; nodeAttempt++) {
const nodeIndex = (startNode + nodeAttempt) % NODE_POOL.length;
if (nodeAttempt > 0) setActiveNode(nodeIndex);
for (let i = 0; i < n; i++) {
try {
const result = await op();
markNodeSuccess(nodeIndex);
return result;
} catch (err) {
lastErr = err;
markNodeFailure(nodeIndex);
if (i < n - 1) await new Promise((r) => setTimeout(r, d));
}
}
if (nodeAttempt < NODE_POOL.length - 1) {
log("warn", `Node ${nodeIndex} (${NODE_POOL[nodeIndex].fcl}) exhausted ${n} retries, failing over`);
}
}
throw lastErr;
}
// REST calls — simple retry, no node rotation (REST endpoint is independent of FCL node)
async function get(url) {
return retrySimple(async () => {
const res = await fetch(url);
if (!res.ok) {
const errorBody = await res.text();
throw new Error(`Request Failed: ${res.status} ${res.statusText} - ${errorBody}`);
}
return res.json();
});
}
function extract(v) {
if (!v || typeof v !== "object") return v;
// Handle Type objects specifically
if (v.type === "Type" && v.value && v.value.staticType && v.value.staticType.typeID) {
return v.value.staticType.typeID;
}
// Handle regular value objects
if ("value" in v) {
return extract(v.value);
}
return v;
}
function decodeEvent(evt) {
if (!evt || !evt.payload) return evt;
if (evt.data) return evt; // Already decoded
try {
const j = JSON.parse(Buffer.from(evt.payload, "base64").toString());
const obj = {};
j.value.fields.forEach((f) => {
obj[f.name] = extract(f.value);
});
return { ...evt, data: obj };
} catch (e) {
log("warn", "Could not decode event payload", { payload: evt.payload });
return evt;
}
}
function decodeEventPayloadBase64(payloadBase64) {
try {
const buff = Buffer.from(payloadBase64, "base64");
return JSON.parse(buff.toString("utf-8"));
} catch {
return null;
}
}
function unwrapAddressField(fieldValue) {
// Case 1: plain string
if (typeof fieldValue === "string") return fieldValue;
// Case 2: { value:"0xabc", type:"Address" }
if (fieldValue && typeof fieldValue.value === "string") return fieldValue.value;
// Case 3: { value:{ value:"0xabc", type:"Address" }, type:"Optional" }
if (
fieldValue &&
typeof fieldValue.value === "object" &&
fieldValue.value.value &&
typeof fieldValue.value.value === "string"
) {
return fieldValue.value.value;
}
// Case 4: { value:{ value:"0xabc", type:"Address" }, type:"Optional" } - different structure
if (
fieldValue &&
typeof fieldValue.value === "object" &&
fieldValue.value.value &&
typeof fieldValue.value.value === "string" &&
fieldValue.value.type === "Address"
) {
return fieldValue.value.value;
}
// Case 5: Direct object with address property
if (fieldValue && typeof fieldValue === "object" && fieldValue.address) {
return fieldValue.address;
}
// Case 6: Nested object with address in different location
if (fieldValue && typeof fieldValue === "object") {
// Try to find any string that looks like an address
const findAddress = (obj) => {
for (const [key, value] of Object.entries(obj)) {
if (typeof value === "string" && value.startsWith("0x") && value.length === 18) {
return value;
}
if (typeof value === "object" && value !== null) {
const found = findAddress(value);
if (found) return found;
}
}
return null;
};
const found = findAddress(fieldValue);
if (found) return found;
}
// Debug logging for unknown formats
if (fieldValue !== null && fieldValue !== undefined) {
log("debug", "Unknown address field format", {
fieldValue: JSON.stringify(fieldValue),
type: typeof fieldValue,
});
}
return null;
}
function parseBuyerSellerFromNonFungibleToken(events, nftId) {
let seller = "UnknownSeller";
let buyer = "UnknownBuyer";
for (const evt of events) {
if (
evt.type === "A.1d7e57aa55817448.NonFungibleToken.Withdrawn" ||
evt.type === "A.1d7e57aa55817448.NonFungibleToken.Deposited"
) {
const decoded = evt.payload ? decodeEventPayloadBase64(evt.payload) : null;
if (!decoded) {
log("debug", "Failed to decode event payload", { eventType: evt.type });
continue;
}
const fields = decoded.value?.fields || [];
let eventIdString = "";
let fromAddr = null;
let toAddr = null;
for (const f of fields) {
if (f.name === "id") {
// Handle different ID formats
if (f.value && typeof f.value.value !== "undefined") {
eventIdString = String(f.value.value);
} else if (f.value && typeof f.value === "string") {
eventIdString = f.value;
} else {
eventIdString = String(f.value || "");
}
}
if (f.name === "from") fromAddr = unwrapAddressField(f.value);
if (f.name === "to") toAddr = unwrapAddressField(f.value);
}
if (String(eventIdString) === String(nftId)) {
if (evt.type.endsWith(".Withdrawn")) {
if (fromAddr) {
seller = fromAddr;
log("debug", "Found seller address", { seller, eventType: evt.type });
} else {
log("debug", "Failed to extract seller address", {
eventType: evt.type,
fromField: JSON.stringify(fields.find((f) => f.name === "from")?.value),
});
}
}
if (evt.type.endsWith(".Deposited")) {
if (toAddr) {
buyer = toAddr;
log("debug", "Found buyer address", { buyer, eventType: evt.type });
} else {
log("debug", "Failed to extract buyer address", {
eventType: evt.type,
toField: JSON.stringify(fields.find((f) => f.name === "to")?.value),
});
}
}
}
}
}
if (seller === "UnknownSeller" || buyer === "UnknownBuyer") {
log("warn", "Could not determine valid owner addresses", {
nftId,
seller,
buyer,
eventCount: events.length,
});
}
return { seller, buyer };
}
async function getUsernameFromAddress(address) {
try {
const url = `https://open.meetdapper.com/profile?address=${address}`;
const response = await fetch(url);
if (!response.ok) {
log("warn", `Failed to fetch username for address ${address}`, { status: response.status });
return null;
}
const data = await response.json();
return data.displayName || null;
} catch (error) {
log("warn", `Error fetching username for address ${address}`, { error: error.message });
return null;
}
}
function formatPrice(price) {
return Math.round(price).toLocaleString();
}
function composeTweet({ usd, chars, ed }) {
return `$${formatPrice(usd)} — ${chars} just sold on @DisneyPinnacle\nhttps://disneypinnacle.com/pin/${ed.id}`;
}
/* ── Flow Functions ─────────────────────────────────────── */
async function getTxResults(txId) {
try {
const txUrl = `${getRestEndpoint()}/v1/transactions/${txId}`;
const txData = await get(txUrl);
if (txData && txData.events) {
return txData;
}
} catch (e) {
log("warn", `Could not fetch from /transactions endpoint, falling back. Error: ${e.message}`);
}
log("info", "Falling back to /transaction_results endpoint.", { txId });
const resultsUrl = `${getRestEndpoint()}/v1/transaction_results/${txId}`;
return get(resultsUrl);
}
async function executePinnacleScript(address, nftId) {
return retryWithFailover(() =>
fcl
.send([
fcl.script(pinnacleScript),
fcl.args([fcl.arg(address, t.Address), fcl.arg(String(nftId), t.UInt64)]),
])
.then(fcl.decode)
);
}
async function executeGetEditionScript(editionId) {
return retryWithFailover(() =>
fcl
.send([fcl.script(editionScript), fcl.args([fcl.arg(String(editionId), t.Int)])])
.then(fcl.decode)
);
}
/* ── Event Processing ───────────────────────────────────── */
async function handleListing(evt) {
const decodedEvent = decodeEvent(evt);
if (!decodedEvent || !decodedEvent.data) {
log("warn", "Could not decode event or event data missing", { transactionId: evt.transaction_id });
return;
}
const { data, transactionId } = decodedEvent;
// Filter for Pinnacle NFT purchases only (using the same logic as old working code)
const nftType = data.nftType?.typeID || data.nftType;
if (nftType !== config.PINNACLE_NFT_TYPE || !data.purchased) {
return;
}
const usd = Number(data.salePrice);
// [MODIFIED] Added a log for below-threshold sales
if (usd < config.PINNACLE_PRICE_THRESHOLD) {
log("info", `Price ${usd} below threshold ${config.PINNACLE_PRICE_THRESHOLD}, skipping`);
return; // Stop processing this event
}
log("info", `Processing sale: ${data.nftID} for $${usd}`, { transactionId });
lastSaleAttemptedAt = new Date().toISOString();
// Retry transaction results with exponential backoff for timing issues
let txRes = null;
let retryCount = 0;
const maxRetries = 3;
while (retryCount < maxRetries) {
txRes = await getTxResults(transactionId);
if (txRes && txRes.events && txRes.events.length > 0) {
break; // Success - we got events
}
retryCount++;
if (retryCount < maxRetries) {
const delay = Math.pow(2, retryCount) * 1000; // 2s, 4s, 8s
log("warn", `Transaction events not ready, retrying in ${delay}ms (attempt ${retryCount}/${maxRetries})`, {
transactionId,
eventCount: txRes?.events?.length || 0,
});
await new Promise((resolve) => setTimeout(resolve, delay));
}
}
if (!txRes || !txRes.events || txRes.events.length === 0) {
log("error", "Could not retrieve transaction events after retries.", {
transactionId,
attempts: retryCount,
eventCount: txRes?.events?.length || 0,
});
return;
}
const { seller, buyer } = parseBuyerSellerFromNonFungibleToken(txRes.events, data.nftID);
const queryAddr = buyer !== "UnknownBuyer" ? buyer : seller !== "UnknownSeller" ? seller : null;
if (!queryAddr) {
log("warn", "Could not determine a valid owner address to query.", { transactionId, buyer, seller });
return;
}
const pin = await executePinnacleScript(queryAddr, data.nftID);
if (!pin) {
log("warn", "Pinnacle script returned null, possibly due to NFT data issue.", {
nftId: data.nftID,
ownerAddress: queryAddr,
});
return;
}
log("debug", "Pinnacle script executed successfully", {
nftId: data.nftID,
editionId: pin.editionID,
serialNumber: pin.serialNumber,
traitsCount: pin.traits?.length || 0,
});
const ed = await executeGetEditionScript(pin.editionID);
if (!ed) {
log("warn", "Edition script returned null", { editionId: pin.editionID });
return;
}
log("debug", "Edition script executed successfully", {
editionId: pin.editionID,
renderID: ed.renderID,
maxMintSize: ed.maxMintSize,
});
const traitsMap = new Map();
if (pin.traits && Array.isArray(pin.traits)) {
for (const trait of pin.traits) {
if (trait && trait.name) traitsMap.set(trait.name, trait.value);
}
}
const characterValues = traitsMap.get("Characters");
const chars = Array.isArray(characterValues) ? characterValues.join(", ") : "N/A";
const setName = traitsMap.get("SetName") || "Unknown Set";
const imgUrl = `https://assets.disneypinnacle.com/render/${ed.renderID}/front_cropped.png`;
// Fetch usernames for seller and buyer
let sellerDisplay = seller;
let buyerDisplay = buyer;
if (seller !== "UnknownSeller") {
const sellerUsername = await getUsernameFromAddress(seller);
sellerDisplay = sellerUsername || `0x${seller.replace(/^0x/, "")}`;
}
if (buyer !== "UnknownBuyer") {
const buyerUsername = await getUsernameFromAddress(buyer);
buyerDisplay = buyerUsername || `0x${buyer.replace(/^0x/, "")}`;
}
const tweetData = {
usd,
ed: { id: pin.editionID, name: setName, max: ed.maxMintSize },
chars,
serial: pin.serialNumber,
seller: sellerDisplay,
buyer: buyerDisplay,
img: imgUrl,
editionType: EDITION_TYPE_NAMES[ed.editionTypeID] ?? null,
isChaser: !!ed.isChaser,
variant: ed.variant || null,
};
const text = composeTweet(tweetData);
log("debug", "Tweet composed successfully", {
nftId: data.nftID,
editionId: pin.editionID,
tweetLength: text.length,
hasImage: !!imgUrl,
});
if (config.ENABLE_TWEETS) {
const sendPromise = enqueueTweet(async () => {
try {
let mediaId;
try {
const renderID = ed.renderID;
log("info", "Downloading image for tweet", { renderID });
await new Promise((r) => setTimeout(r, 300 + Math.random() * 300));
const imageResponse = await fetch(imgUrl, {
headers: {
"User-Agent": "Mozilla/5.0 (compatible; PinnacleBot/1.0)",
Referer: "https://disneypinnacle.com/",
},
});
const contentType = imageResponse.headers.get("content-type");
if (!contentType?.startsWith("image/")) throw new Error("Invalid content type");
const nftBuffer = await imageResponse.buffer();
const cardBuffer = await renderSaleCard({
usd,
character: chars,
setName,
serial: pin.serialNumber,
maxMint: ed.maxMintSize,
editionType: tweetData.editionType,
seller: sellerDisplay,
buyer: buyerDisplay,
nftBuffer,
isChaser: tweetData.isChaser,
variant: tweetData.variant,
});
mediaId = await twitterClient.v1.uploadMedia(cardBuffer, { mimeType: "image/png" });
log("info", "Card rendered and uploaded successfully", { renderID, mediaId });
} catch (imageError) {
log("warn", "Failed to process image for tweet.", { imageError: imageError.message });
}
log("info", "Attempting to tweet sale...", { nftId: data.nftID, queueDepth: tweetQueue.length });
await twitterClient.v2.tweet({
text,
...(mediaId && { media: { media_ids: [mediaId] } }),
});
log("info", `Tweeted Edition ${pin.editionID} for $${usd}`);
tweetsSent++;
lastTweetAt = new Date().toISOString();
} catch (error) {
log("error", "Failed to tweet", { error: error.message, tweetData });
failedTweets++;
await sendDiscordAlert(
"Tweet Failed",
`**Error:** ${error.message}\n**Sale:** $${tweetData.usd} — ${tweetData.chars}\n**Edition:** ${tweetData.ed.name} #${tweetData.serial}/${tweetData.ed.max}\n**Seller:** ${tweetData.seller} → **Buyer:** ${tweetData.buyer}\n\nFailed tweets this session: ${failedTweets}`
);
}
});
// In backfill mode, wait so we don't queue thousands instantly
if (config.IS_BACKFILL) await sendPromise;
} else {
// Check if image would be included
log("info", `DRY RUN: Tweet not sent. Use --live-tweets flag to enable. Card: WOULD RENDER (${imgUrl})`);
logger.info({ tweetText: text }, "Composed tweet (dry run)");
writeToLogFile(text);
}
}
/* ── App Modes ──────────────────────────────────────────── */
async function runBackfill() {
const fromBlockArg = process.argv.find((arg) => arg.startsWith("--from-block="));
const toBlockArg = process.argv.find((arg) => arg.startsWith("--to-block="));
if (!fromBlockArg || !toBlockArg) {
log("error", "Backfill mode requires --from-block=<number> and --to-block=<number> arguments.");
process.exit(1);
}
const fromBlock = parseInt(fromBlockArg.split("=")[1], 10);
const toBlock = parseInt(toBlockArg.split("=")[1], 10);
if (isNaN(fromBlock) || isNaN(toBlock) || fromBlock > toBlock) {
log("error", "Invalid block height range provided.");
process.exit(1);
}
log("info", `--- BACKFILL MODE ---`);
log("info", `Scanning for events from block ${fromBlock} to ${toBlock}.`);
try {
let foundEvents = [];
// Include all event types (like the live subscription)
const allEventTypes = [...config.EVENT_TYPES.LISTING_COMPLETED, config.EVENT_TYPES.OFFER_COMPLETED];
for (const eventType of allEventTypes) {
log("info", `Querying for event type: ${eventType}`);
try {
// Use REST API directly for more reliable event fetching
const url = `${getRestEndpoint()}/v1/events?type=${encodeURIComponent(
eventType
)}&start_height=${fromBlock}&end_height=${toBlock}`;
const response = await fetch(url);
if (!response.ok) throw new Error(`HTTP ${response.status}: ${response.statusText}`);
const data = await response.json();
// Extract individual events from blocks
if (data && data.length > 0) {
for (const block of data) {
if (block.events && Array.isArray(block.events)) {
for (const event of block.events) {
const enrichedEvent = {
...event,
blockHeight: parseInt(block.block_height),
blockId: block.block_id,
blockTimestamp: block.block_timestamp,
};
foundEvents.push(enrichedEvent);
}
}
}
}
} catch (error) {
log("error", `Failed to fetch events for ${eventType}`, { error: error.message });
}
}
log("info", `Found ${foundEvents.length} total sales events in range.`);
if (foundEvents.length === 0) return;
foundEvents.sort((a, b) => a.blockHeight - b.blockHeight);
for (const event of foundEvents) {
try {
await handleListing(event);
} catch (error) {
log("error", "Error processing backfill event", { error: error.message, event });
}
}
} catch (error) {
log("error", "An error occurred during backfill.", { message: error.message });
}
}
async function runLiveSubscription() {
try {
if (config.ENABLE_TWEETS) {
await twitterClient.v2.me();
log("info", "Twitter client initialized successfully for live tweeting.");
log("info", "Tweet throttle enabled", {
gapSeconds: TWEET_GAP_SECONDS,
jitterSeconds: TWEET_JITTER_SECONDS,
});
}
await sendDiscordAlert(
"Bot Started",
`Mode: ${config.ENABLE_TWEETS ? "LIVE" : "DRY RUN"}\nThreshold: $${config.PINNACLE_PRICE_THRESHOLD}+\nDiscord alerts: active`,
0x00ff00
);
log("info", "Starting live event subscription...");
// Subscribe to all relevant events (like the old working code)
const events = [...config.EVENT_TYPES.LISTING_COMPLETED, config.EVENT_TYPES.OFFER_COMPLETED];
const subscription = subscribeToEvents({
fcl,
events,
onEvent: async (event) => {
try {
// Single check for Pinnacle NFT purchases (like the old working code)
const nftType = event.data?.nftType?.typeID || event.data?.nftType;
if (nftType === config.PINNACLE_NFT_TYPE && event.data?.purchased) {
await handleListing(event);
}
} catch (error) {
log("error", "Error processing event", {
error: error.message,
stack: error.stack,
event: {
type: event.type,
transactionId: event.transactionId,
data: {
...event.data,
nftType: event.data?.nftType?.typeID || event.data?.nftType,
},
},
});
}
},
onError: (error) => log("error", "Subscription error", { error: error.message }),
// Override the default 60-second sleep time to make it real-time
sleepTime: 1000, // 1 second instead of 60 seconds
startBlock: "latest",
});
log("info", "Event subscription started successfully. Watching for sales...");
process.on("SIGINT", () => {
log("info", "Shutting down...");
if (subscription && typeof subscription.close === "function") subscription.close();
process.exit(0);
});
} catch (error) {
log("error", "Fatal error in live subscription setup", { message: error.message });
process.exit(1);
}
}
/* ── Health Check Server ────────────────────────────────── */
const HEALTH_PORT = Number(process.env.HEALTH_PORT || 8090);
const botStartTime = Date.now();
// Commit this process booted with, read ONCE at start. /health exposes it so
// the deploy verifier + ops audit can prove source==deployed==running and
// catch "deployed but never restarted".
const GIT_SHA = (() => {
try {
return require("child_process")
.execSync("git rev-parse HEAD", { cwd: __dirname, timeout: 2000 })
.toString()
.trim();
} catch {
return "unknown";
}
})();
http
.createServer((req, res) => {
if (req.url === "/health" && (req.method === "GET" || req.method === "HEAD")) {
const body = JSON.stringify({
status: computeHealthStatus({ lastSaleAttemptedAt, lastTweetAt }),
service: "pinnacle-pin-bot",
uptime: Math.floor((Date.now() - botStartTime) / 1000),
mode: config.IS_BACKFILL ? "backfill" : "live",
tweetsEnabled: config.ENABLE_TWEETS,
tweetQueueDepth: tweetQueue.length,
tweetsSent,
lastTweetAt,
lastSaleAttemptedAt,
sha: GIT_SHA,
timestamp: new Date().toISOString(),
});
res.writeHead(200, { "Content-Type": "application/json" });
res.end(body);
} else {
res.writeHead(404);
res.end("Not found");
}
})
.listen(HEALTH_PORT, '127.0.0.1', () => {
log("info", `Health check server listening on port ${HEALTH_PORT}`);
});
/* ── Main Entry Point ───────────────────────────────────── */
async function main() {
const mode = config.IS_BACKFILL
? config.ENABLE_TWEETS
? "--- BACKFILL LIVE TWEET MODE ---"
: "--- BACKFILL DRY RUN MODE ---"
: config.ENABLE_TWEETS
? "--- LIVE TWEET MODE ---"
: "--- LIVE DRY RUN MODE ---";
log("warn", mode);
if (config.IS_BACKFILL) {
await runBackfill();
} else {
await runLiveSubscription();
}
}
main().catch((e) => {
log("fatal", "Unhandled error in main execution.", e);
process.exit(1);
});