Skip to content

Commit f7ebf24

Browse files
authored
refactor(database): move convertMultiAssetToModels to models (#974)
Signed-off-by: Chris Gianelloni <wolf31o2@blinklabs.io>
1 parent 854ad5d commit f7ebf24

File tree

2 files changed

+52
-40
lines changed

2 files changed

+52
-40
lines changed

database/models/asset.go

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414

1515
package models
1616

17-
import "github.com/blinklabs-io/dingo/database/types"
17+
import (
18+
"encoding/hex"
19+
20+
"github.com/blinklabs-io/dingo/database/types"
21+
lcommon "github.com/blinklabs-io/gouroboros/ledger/common"
22+
)
1823

1924
type Asset struct {
2025
Name []byte `gorm:"index"`
@@ -29,3 +34,47 @@ type Asset struct {
2934
func (Asset) TableName() string {
3035
return "asset"
3136
}
37+
38+
// ConvertMultiAssetToModels converts a MultiAsset structure into a slice of Asset models.
39+
// Each asset is populated with its name, hex-encoded name, policy ID, fingerprint, and amount.
40+
// Returns an empty slice if multiAsset is nil or contains no assets.
41+
func ConvertMultiAssetToModels(
42+
multiAsset *lcommon.MultiAsset[lcommon.MultiAssetTypeOutput],
43+
) []Asset {
44+
if multiAsset == nil {
45+
return []Asset{}
46+
}
47+
numAssets := 0
48+
// Get all policy IDs
49+
policyIds := multiAsset.Policies()
50+
for _, policyId := range policyIds {
51+
numAssets += len(multiAsset.Assets(policyId))
52+
}
53+
assets := make([]Asset, 0, numAssets)
54+
for _, policyId := range policyIds {
55+
policyIdBytes := policyId.Bytes()
56+
57+
// Get asset names for this policy
58+
assetNames := multiAsset.Assets(policyId)
59+
for _, assetNameBytes := range assetNames {
60+
amount := multiAsset.Asset(policyId, assetNameBytes)
61+
62+
// Calculate fingerprint
63+
fingerprint := lcommon.NewAssetFingerprint(
64+
policyIdBytes,
65+
assetNameBytes,
66+
)
67+
68+
asset := Asset{
69+
Name: assetNameBytes,
70+
NameHex: []byte(hex.EncodeToString(assetNameBytes)),
71+
PolicyId: policyIdBytes,
72+
Fingerprint: []byte(fingerprint.String()),
73+
Amount: types.Uint64(amount),
74+
}
75+
assets = append(assets, asset)
76+
}
77+
}
78+
79+
return assets
80+
}

database/plugin/metadata/sqlite/utxo.go

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@
1515
package sqlite
1616

1717
import (
18-
"encoding/hex"
1918
"errors"
2019
"fmt"
2120

2221
"github.com/blinklabs-io/dingo/database/models"
23-
"github.com/blinklabs-io/dingo/database/types"
2422
"github.com/blinklabs-io/gouroboros/ledger"
2523
lcommon "github.com/blinklabs-io/gouroboros/ledger/common"
2624
"gorm.io/gorm"
@@ -216,7 +214,7 @@ func (d *MetadataStoreSqlite) SetUtxo(
216214

217215
// Handle assets if present
218216
if assets != nil {
219-
tmpUtxo.Assets = convertMultiAssetToModels(assets)
217+
tmpUtxo.Assets = models.ConvertMultiAssetToModels(assets)
220218
}
221219

222220
var result *gorm.DB
@@ -323,44 +321,9 @@ func utxoLedgerToModel(
323321
MultiAsset() *lcommon.MultiAsset[lcommon.MultiAssetTypeOutput]
324322
}); ok {
325323
if multiAsset := multiAssetOutput.MultiAsset(); multiAsset != nil {
326-
ret.Assets = convertMultiAssetToModels(multiAsset)
324+
ret.Assets = models.ConvertMultiAssetToModels(multiAsset)
327325
}
328326
}
329327

330328
return ret
331329
}
332-
333-
func convertMultiAssetToModels(
334-
multiAsset *lcommon.MultiAsset[lcommon.MultiAssetTypeOutput],
335-
) []models.Asset {
336-
var assets []models.Asset
337-
338-
// Get all policy IDs
339-
policyIds := multiAsset.Policies()
340-
for _, policyId := range policyIds {
341-
policyIdBytes := policyId.Bytes()
342-
343-
// Get asset names for this policy
344-
assetNames := multiAsset.Assets(policyId)
345-
for _, assetNameBytes := range assetNames {
346-
amount := multiAsset.Asset(policyId, assetNameBytes)
347-
348-
// Calculate fingerprint
349-
fingerprint := lcommon.NewAssetFingerprint(
350-
policyIdBytes,
351-
assetNameBytes,
352-
)
353-
354-
asset := models.Asset{
355-
Name: assetNameBytes,
356-
NameHex: []byte(hex.EncodeToString(assetNameBytes)),
357-
PolicyId: policyIdBytes,
358-
Fingerprint: []byte(fingerprint.String()),
359-
Amount: types.Uint64(amount),
360-
}
361-
assets = append(assets, asset)
362-
}
363-
}
364-
365-
return assets
366-
}

0 commit comments

Comments
 (0)