@@ -89,7 +89,9 @@ func (d *MetadataStoreSqlite) SetTransaction(
8989 if result .Error != nil {
9090 return fmt .Errorf ("create transaction: %w" , result .Error )
9191 }
92- // If ID is still zero (conflict path with SQLite), fetch it by hash
92+ // SQLite's ON CONFLICT clause doesn't return the ID of an existing row when
93+ // the conflict path is taken (no insert occurs). We need to fetch the ID
94+ // explicitly so we can associate witness records with the correct transaction.
9395 if tmpTx .ID == 0 {
9496 existingTx , err := d .GetTransactionByHash (txHash , txn )
9597 if err != nil {
@@ -304,14 +306,14 @@ func (d *MetadataStoreSqlite) SetTransaction(
304306 return fmt .Errorf ("delete existing plutus data: %w" , result .Error )
305307 }
306308 }
307- if tx .Witnesses () != nil {
308- ws := tx . Witnesses ()
309+ ws := tx .Witnesses ()
310+ if ws != nil {
309311
310312 // Add Vkey Witnesses
311313 for _ , vkey := range ws .Vkey () {
312314 keyWitness := models.KeyWitness {
313315 TransactionID : tmpTx .ID ,
314- Type : 0 , // VkeyWitness
316+ Type : models . KeyWitnessTypeVkey ,
315317 Vkey : vkey .Vkey ,
316318 Signature : vkey .Signature ,
317319 }
@@ -324,7 +326,7 @@ func (d *MetadataStoreSqlite) SetTransaction(
324326 for _ , bootstrap := range ws .Bootstrap () {
325327 keyWitness := models.KeyWitness {
326328 TransactionID : tmpTx .ID ,
327- Type : 1 , // BootstrapWitness
329+ Type : models . KeyWitnessTypeBootstrap ,
328330 PublicKey : bootstrap .PublicKey ,
329331 Signature : bootstrap .Signature ,
330332 ChainCode : bootstrap .ChainCode ,
@@ -337,50 +339,106 @@ func (d *MetadataStoreSqlite) SetTransaction(
337339
338340 // Add Native Scripts
339341 for _ , script := range ws .NativeScripts () {
342+ scriptHash := script .Hash ()
340343 scriptRecord := models.Script {
341344 TransactionID : tmpTx .ID ,
342345 Type : uint8 (lcommon .ScriptRefTypeNativeScript ),
343- ScriptData : script . Cbor (),
346+ ScriptHash : scriptHash . Bytes (),
344347 }
345348 if result := txn .Create (& scriptRecord ); result .Error != nil {
346349 return fmt .Errorf ("create native script: %w" , result .Error )
347350 }
351+ // Also store the script content separately to avoid duplicates
352+ scriptContent := models.ScriptContent {
353+ Hash : scriptHash .Bytes (),
354+ Type : uint8 (lcommon .ScriptRefTypeNativeScript ),
355+ Content : script .Cbor (),
356+ CreatedSlot : point .Slot ,
357+ }
358+ if result := txn .Clauses (clause.OnConflict {
359+ Columns : []clause.Column {{Name : "hash" }},
360+ DoNothing : true ,
361+ }).Create (& scriptContent ); result .Error != nil {
362+ return fmt .Errorf ("create native script content: %w" , result .Error )
363+ }
348364 }
349365
350366 // Add PlutusV1 Scripts
351367 for _ , script := range ws .PlutusV1Scripts () {
368+ scriptHash := script .Hash ()
352369 scriptRecord := models.Script {
353370 TransactionID : tmpTx .ID ,
354371 Type : uint8 (lcommon .ScriptRefTypePlutusV1 ),
355- ScriptData : script ,
372+ ScriptHash : scriptHash . Bytes () ,
356373 }
357374 if result := txn .Create (& scriptRecord ); result .Error != nil {
358375 return fmt .Errorf ("create plutus v1 script: %w" , result .Error )
359376 }
377+ // Also store the script content separately to avoid duplicates
378+ scriptContent := models.ScriptContent {
379+ Hash : scriptHash .Bytes (),
380+ Type : uint8 (lcommon .ScriptRefTypePlutusV1 ),
381+ Content : script .RawScriptBytes (),
382+ CreatedSlot : point .Slot ,
383+ }
384+ if result := txn .Clauses (clause.OnConflict {
385+ Columns : []clause.Column {{Name : "hash" }},
386+ DoNothing : true ,
387+ }).Create (& scriptContent ); result .Error != nil {
388+ return fmt .Errorf ("create plutus v1 script content: %w" , result .Error )
389+ }
360390 }
361391
362392 // Add PlutusV2 Scripts
363393 for _ , script := range ws .PlutusV2Scripts () {
394+ scriptHash := script .Hash ()
364395 scriptRecord := models.Script {
365396 TransactionID : tmpTx .ID ,
366397 Type : uint8 (lcommon .ScriptRefTypePlutusV2 ),
367- ScriptData : script ,
398+ ScriptHash : scriptHash . Bytes () ,
368399 }
369400 if result := txn .Create (& scriptRecord ); result .Error != nil {
370401 return fmt .Errorf ("create plutus v2 script: %w" , result .Error )
371402 }
403+ // Also store the script content separately to avoid duplicates
404+ scriptContent := models.ScriptContent {
405+ Hash : scriptHash .Bytes (),
406+ Type : uint8 (lcommon .ScriptRefTypePlutusV2 ),
407+ Content : script .RawScriptBytes (),
408+ CreatedSlot : point .Slot ,
409+ }
410+ if result := txn .Clauses (clause.OnConflict {
411+ Columns : []clause.Column {{Name : "hash" }},
412+ DoNothing : true ,
413+ }).Create (& scriptContent ); result .Error != nil {
414+ return fmt .Errorf ("create plutus v2 script content: %w" , result .Error )
415+ }
372416 }
373417
374418 // Add PlutusV3 Scripts
375419 for _ , script := range ws .PlutusV3Scripts () {
420+ scriptHash := script .Hash ()
376421 scriptRecord := models.Script {
377422 TransactionID : tmpTx .ID ,
378423 Type : uint8 (lcommon .ScriptRefTypePlutusV3 ),
379- ScriptData : script ,
424+ ScriptHash : scriptHash . Bytes () ,
380425 }
381426 if result := txn .Create (& scriptRecord ); result .Error != nil {
382427 return fmt .Errorf ("create plutus v3 script: %w" , result .Error )
383428 }
429+ // Also store the script content separately to avoid duplicates
430+ scriptContent := models.ScriptContent {
431+ Hash : scriptHash .Bytes (),
432+ Type : uint8 (lcommon .ScriptRefTypePlutusV3 ),
433+ Content : script .RawScriptBytes (),
434+ CreatedSlot : point .Slot ,
435+ }
436+ if result := txn .Clauses (clause.OnConflict {
437+ Columns : []clause.Column {{Name : "hash" }},
438+ DoNothing : true ,
439+ }).Create (& scriptContent ); result .Error != nil {
440+ return fmt .Errorf ("create plutus v3 script content: %w" , result .Error )
441+ }
384442 }
385443
386444 // Add PlutusData (Datums)
0 commit comments