Skip to content

Commit

Permalink
tapdb: Update AssetQueryFilters to handle additional filter options
Browse files Browse the repository at this point in the history
  • Loading branch information
itsrachelfish committed Dec 20, 2024
1 parent 4205556 commit cf67d1f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
47 changes: 43 additions & 4 deletions tapdb/assets_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ func (a *AssetStore) dbAssetsToChainAssets(dbAssets []ConfirmedAsset,
// constraintsToDbFilter maps application level constraints to the set of
// filters we use in the SQL queries.
func (a *AssetStore) constraintsToDbFilter(
query *AssetQueryFilters) QueryAssetFilters {
query *AssetQueryFilters) (QueryAssetFilters, error) {

assetFilter := QueryAssetFilters{
Now: sql.NullTime{
Expand All @@ -883,12 +883,41 @@ func (a *AssetStore) constraintsToDbFilter(
Valid: true,
}
}

if query.MaxAmt != 0 {
assetFilter.MaxAmt = sql.NullInt64{
Int64: int64(query.MaxAmt),
Valid: true,
}
} else {
// If MaxAmt is unspecified, use valid: false to ignore
// the amount option in the SQL query
assetFilter.MaxAmt = sql.NullInt64{
Valid: false,
}
}

if query.MinAnchorHeight != 0 {
assetFilter.MinAnchorHeight = sqlInt32(
query.MinAnchorHeight,
)
}

if query.ScriptKey != nil {
key := query.ScriptKey.PubKey
assetFilter.TweakedScriptKey = key.SerializeCompressed()
}

if query.AnchorPoint != nil {
anchorPointBytes, err := encodeOutpoint(*query.AnchorPoint)
if err != nil {
return QueryAssetFilters{}, fmt.Errorf("unable to encode "+
"outpoint: %w", err)
}

assetFilter.AnchorPoint = anchorPointBytes
}

// Add asset ID bytes and group key bytes to the filter. These
// byte arrays are empty if the asset ID or group key is not
// specified in the query.
Expand All @@ -908,7 +937,7 @@ func (a *AssetStore) constraintsToDbFilter(
}
}

return assetFilter
return assetFilter, nil
}

// specificAssetFilter maps the given asset parameters to the set of filters
Expand Down Expand Up @@ -980,6 +1009,10 @@ type AssetQueryFilters struct {
// MinAnchorHeight is the minimum block height the asset's anchor tx
// must have been confirmed at.
MinAnchorHeight int32

ScriptKey *asset.ScriptKey

AnchorPoint *wire.OutPoint
}

// QueryBalancesByAsset queries the balances for assets or alternatively
Expand Down Expand Up @@ -1199,7 +1232,10 @@ func (a *AssetStore) FetchAllAssets(ctx context.Context, includeSpent,

// We'll now map the application level filtering to the type of
// filtering our database query understands.
assetFilter := a.constraintsToDbFilter(query)
assetFilter, err := a.constraintsToDbFilter(query)
if err != nil {
return nil, err
}

// By default, the spent boolean is null, which means we'll fetch all
// assets. Only if we should exclude spent assets, we'll set the spent
Expand Down Expand Up @@ -1991,9 +2027,12 @@ func (a *AssetStore) ListEligibleCoins(ctx context.Context,

// First, we'll map the commitment constraints to our database query
// filters.
assetFilter := a.constraintsToDbFilter(&AssetQueryFilters{
assetFilter, err := a.constraintsToDbFilter(&AssetQueryFilters{
CommitmentConstraints: constraints,
})
if err != nil {
return nil, err
}

// We only want to select unspent and non-leased commitments.
assetFilter.Spent = sqlBool(false)
Expand Down
17 changes: 10 additions & 7 deletions tapdb/sqlc/assets.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tapdb/sqlc/queries/assets.sql
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ JOIN chain_txns txns
-- specified.
WHERE (
assets.amount >= COALESCE(sqlc.narg('min_amt'), assets.amount) AND
assets.amount <= COALESCE(sqlc.narg('max_amt'), assets.amount) AND
assets.spent = COALESCE(sqlc.narg('spent'), assets.spent) AND
(key_group_info_view.tweaked_group_key = sqlc.narg('key_group_filter') OR
sqlc.narg('key_group_filter') IS NULL) AND
Expand Down
3 changes: 3 additions & 0 deletions tapfreighter/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ type CommitmentConstraints struct {
// to satisfy the constraints.
MinAmt uint64

// MaxAmt specifies the maximum amount of minted assets to be selected.
MaxAmt uint64

// CoinSelectType is the type of coins that should be selected.
CoinSelectType tapsend.CoinSelectType
}
Expand Down

0 comments on commit cf67d1f

Please sign in to comment.