Skip to content

Commit 7c3f034

Browse files
authored
feat: add support for searchUtxosByAddressWithAsset (#320)
Signed-off-by: Ales Verbic <[email protected]>
1 parent bb7ee6b commit 7c3f034

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

internal/utxorpc/query.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package utxorpc
1616

1717
import (
18+
"bytes"
1819
"context"
1920
"encoding/hex"
2021
"fmt"
@@ -192,8 +193,10 @@ func (s *queryServiceServer) SearchUtxos(
192193
return nil, fmt.Errorf("ERROR: empty predicate: %v", predicate)
193194
}
194195

195-
var addresses []common.Address
196196
addressPattern := predicate.GetMatch().GetCardano().GetAddress()
197+
assetPattern := predicate.GetMatch().GetCardano().GetAsset()
198+
199+
var addresses []common.Address
197200
if addressPattern != nil {
198201
// Handle Exact Address
199202
exactAddressBytes := addressPattern.GetExactAddress()
@@ -263,6 +266,7 @@ func (s *queryServiceServer) SearchUtxos(
263266
return nil, err
264267
}
265268

269+
// Proceed to include the UTxO in the response
266270
for utxoId, utxo := range utxos.Results {
267271
var aud query.AnyUtxoData
268272
var audc query.AnyUtxoData_Cardano
@@ -273,8 +277,32 @@ func (s *queryServiceServer) SearchUtxos(
273277
aud.NativeBytes = utxo.Cbor()
274278
audc.Cardano = utxo.Utxorpc()
275279
aud.ParsedState = &audc
280+
281+
// If AssetPattern is specified, filter based on it
282+
if assetPattern != nil {
283+
assetFound := false
284+
for _, multiasset := range audc.Cardano.Assets {
285+
if bytes.Equal(multiasset.PolicyId, assetPattern.PolicyId) {
286+
for _, asset := range multiasset.Assets {
287+
if bytes.Equal(asset.Name, assetPattern.AssetName) {
288+
assetFound = true
289+
break
290+
}
291+
}
292+
}
293+
if assetFound {
294+
break
295+
}
296+
}
297+
298+
// Asset not found; skip this UTxO
299+
if !assetFound {
300+
continue
301+
}
302+
}
276303
resp.Items = append(resp.Items, &aud)
277304
}
305+
278306
resp.LedgerTip = &query.ChainPoint{
279307
Slot: point.Slot,
280308
Hash: point.Hash,

0 commit comments

Comments
 (0)