@@ -2,6 +2,8 @@ package indexer
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
6
+ "fmt"
5
7
"strconv"
6
8
7
9
"github.com/AFK-AlignedFamKernel/afk_monorepo/backend/core"
@@ -52,6 +54,7 @@ func processPixelPlacedEvent(event IndexerEvent) {
52
54
return
53
55
}
54
56
57
+ fmt .Printf (address , position , dayIdx , color , "print" )
55
58
// Set pixel in postgres
56
59
_ , err = core .AFKBackend .Databases .Postgres .Exec (context .Background (), "INSERT INTO Pixels (address, position, day, color) VALUES ($1, $2, $3, $4)" , address , position , dayIdx , color )
57
60
if err != nil {
@@ -116,7 +119,6 @@ func revertPixelPlacedEvent(event IndexerEvent) {
116
119
func processBasicPixelPlacedEvent (event IndexerEvent ) {
117
120
address := event .Event .Keys [1 ][2 :] // Remove 0x prefix
118
121
timestampHex := event .Event .Data [0 ]
119
-
120
122
timestamp , err := strconv .ParseInt (timestampHex , 0 , 64 )
121
123
if err != nil {
122
124
PrintIndexerError ("processBasicPixelPlacedEvent" , "Error converting timestamp hex to int" , address , timestampHex )
@@ -234,3 +236,117 @@ func revertExtraPixelsPlacedEvent(event IndexerEvent) {
234
236
return
235
237
}
236
238
}
239
+
240
+ func processBasicPixelPlacedEventWithMetadata (event IndexerEvent ) {
241
+ address := event .Event .Keys [1 ][2 :] // Remove 0x prefix
242
+ timestampHex := event .Event .Data [0 ]
243
+ timestamp , err := strconv .ParseInt (timestampHex , 0 , 64 )
244
+ if err != nil {
245
+ PrintIndexerError ("processBasicPixelPlacedEventWithMetadata" , "Error converting timestamp hex to int" , address , timestampHex )
246
+ return
247
+ }
248
+
249
+ // Extract position and color from the event (position is Keys[2], color is in Data[1])
250
+ positionHex := event .Event .Keys [2 ]
251
+ position , err := strconv .Atoi (positionHex )
252
+ if err != nil {
253
+ PrintIndexerError ("processBasicPixelPlacedEventWithMetadata" , "Error converting position hex to int" , address , positionHex )
254
+ return
255
+ }
256
+
257
+ colorHex := event .Event .Data [1 ]
258
+ color , err := strconv .Atoi (colorHex )
259
+ if err != nil {
260
+ PrintIndexerError ("processBasicPixelPlacedEventWithMetadata" , "Error converting color hex to int" , address , colorHex )
261
+ return
262
+ }
263
+
264
+ // Extract metadata from the last index in Data (metadata is in Data[n])
265
+ metadata := event .Event .Data [len (event .Event .Data )- 1 ]
266
+
267
+ // Unmarshal metadata (if it exists)
268
+ var metadataMap map [string ]interface {}
269
+ if len (metadata ) > 0 {
270
+ err = json .Unmarshal ([]byte (metadata ), & metadataMap )
271
+ if err != nil {
272
+ PrintIndexerError ("processBasicPixelPlacedEventWithMetadata" , "Error parsing metadata" , address , string (metadata ))
273
+ return
274
+ }
275
+ }
276
+
277
+ // Prepare SQL statement for inserting pixel info and metadata together
278
+ metadataJson , err := json .Marshal (metadataMap )
279
+ if err != nil {
280
+ PrintIndexerError ("processBasicPixelPlacedEventWithMetadata" , "Error serializing metadata" , address , string (metadata ))
281
+ return
282
+ }
283
+
284
+ // Use a single query to insert the pixel information and metadata into the database
285
+ _ , err = core .AFKBackend .Databases .Postgres .Exec (context .Background (),
286
+ `INSERT INTO Pixels (address, position, color, time)
287
+ VALUES ($1, $2, $3, TO_TIMESTAMP($4))
288
+ ON CONFLICT (address, position)
289
+ DO UPDATE SET color = $3, time = TO_TIMESTAMP($4),
290
+ metadata = COALESCE(metadata, $5)` ,
291
+ address , position , color , timestamp , metadataJson )
292
+ if err != nil {
293
+ PrintIndexerError ("processBasicPixelPlacedEventWithMetadata" , "Error inserting/updating pixel and metadata" , address , string (metadataJson ))
294
+ return
295
+ }
296
+
297
+ // Insert or update the last placed time in the LastPlacedTime table
298
+ _ , err = core .AFKBackend .Databases .Postgres .Exec (context .Background (),
299
+ "INSERT INTO LastPlacedTime (address, time) VALUES ($1, TO_TIMESTAMP($2)) ON CONFLICT (address) DO UPDATE SET time = TO_TIMESTAMP($2)" ,
300
+ address , timestamp )
301
+ if err != nil {
302
+ PrintIndexerError ("processBasicPixelPlacedEventWithMetadata" , "Error inserting last placed time into postgres" , address , timestampHex )
303
+ return
304
+ }
305
+ }
306
+
307
+ func revertBasicPixelPlacedEventWithMetadata (event IndexerEvent ) {
308
+ address := event .Event .Keys [1 ][2 :] // Remove 0x prefix
309
+ posHex := event .Event .Keys [2 ]
310
+
311
+ // Convert hex to int for position
312
+ position , err := strconv .ParseInt (posHex , 0 , 64 )
313
+ if err != nil {
314
+ PrintIndexerError ("revertPixelPlacedEvent" , "Error converting position hex to int" , address , posHex )
315
+ return
316
+ }
317
+
318
+ // We can also retrieve the metadata from the event if needed
319
+ metadata := event .Event .Data [len (event .Event .Data )- 1 ]
320
+ var metadataMap map [string ]interface {}
321
+ if len (metadata ) > 0 {
322
+ err = json .Unmarshal ([]byte (metadata ), & metadataMap ) // Unmarshal from metadata (which is a string) to map
323
+ if err != nil {
324
+ PrintIndexerError ("revertPixelPlacedEvent" , "Error parsing metadata" , address , string (metadata ))
325
+ return
326
+ }
327
+ }
328
+
329
+ // Delete the pixel entry (including metadata) from the PostgreSQL database
330
+ _ , err = core .AFKBackend .Databases .Postgres .Exec (context .Background (), `
331
+ DELETE FROM Pixels
332
+ WHERE address = $1 AND position = $2
333
+ ORDER BY time LIMIT 1` , address , position )
334
+ if err != nil {
335
+ PrintIndexerError ("revertPixelPlacedEvent" , "Error deleting pixel from postgres" , address , posHex )
336
+ return
337
+ }
338
+
339
+ // Optionally, you can also delete the metadata from the database,
340
+ // but usually deleting the pixel entry will automatically take care of it since metadata is part of the same row.
341
+
342
+ // Delete the pixel's associated last placed time entry from the LastPlacedTime table
343
+ _ , err = core .AFKBackend .Databases .Postgres .Exec (context .Background (),
344
+ "DELETE FROM LastPlacedTime WHERE address = $1" , address )
345
+ if err != nil {
346
+ PrintIndexerError ("revertPixelPlacedEvent" , "Error deleting last placed time from postgres" , address , posHex )
347
+ return
348
+ }
349
+
350
+ // Optionally log the event if needed
351
+ fmt .Printf ("Pixel at position %d for address %s has been reverted.\n " , position , address )
352
+ }
0 commit comments