@@ -321,7 +321,18 @@ JOIN genesis_info_view
321321-- around that needs to be used with this query until a sqlc bug is fixed.
322322LEFT JOIN key_group_info_view
323323 ON assets .genesis_id = key_group_info_view .gen_asset_id
324- WHERE spent = FALSE
324+ JOIN managed_utxos utxos
325+ ON assets .anchor_utxo_id = utxos .utxo_id AND
326+ CASE
327+ WHEN sqlc .narg (' leased' ) = true THEN
328+ (utxos .lease_owner IS NOT NULL AND utxos .lease_expiry > @now)
329+ WHEN sqlc .narg (' leased' ) = false THEN
330+ (utxos .lease_owner IS NULL OR
331+ utxos .lease_expiry IS NULL OR
332+ utxos .lease_expiry <= @now)
333+ ELSE TRUE
334+ END
335+ WHERE spent = FALSE
325336GROUP BY assets .genesis_id , genesis_info_view .asset_id ,
326337 genesis_info_view .asset_tag , genesis_info_view .meta_hash ,
327338 genesis_info_view .asset_type , genesis_info_view .output_index ,
@@ -335,7 +346,18 @@ JOIN key_group_info_view
335346 ON assets .genesis_id = key_group_info_view .gen_asset_id AND
336347 (key_group_info_view .tweaked_group_key = sqlc .narg (' key_group_filter' ) OR
337348 sqlc .narg (' key_group_filter' ) IS NULL )
338- WHERE spent = FALSE
349+ JOIN managed_utxos utxos
350+ ON assets .anchor_utxo_id = utxos .utxo_id AND
351+ CASE
352+ WHEN sqlc .narg (' leased' ) = true THEN
353+ (utxos .lease_owner IS NOT NULL AND utxos .lease_expiry > @now)
354+ WHEN sqlc .narg (' leased' ) = false THEN
355+ (utxos .lease_owner IS NULL OR
356+ utxos .lease_expiry IS NULL OR
357+ utxos .lease_expiry <= @now)
358+ ELSE TRUE
359+ END
360+ WHERE spent = FALSE
339361GROUP BY key_group_info_view .tweaked_group_key ;
340362
341363-- name: FetchGroupedAssets :many
0 commit comments