Filter public positions by title, event and current holding
This commit is contained in:
@@ -19,6 +19,13 @@ pub struct PositionQuery {
|
||||
#[serde(alias = "condition_id", alias = "conditionId")]
|
||||
condition: Option<String>,
|
||||
status: Option<String>,
|
||||
#[serde(alias = "eventId")]
|
||||
event_id: Option<String>,
|
||||
title: Option<String>,
|
||||
#[serde(alias = "filterType")]
|
||||
filter_type: Option<String>,
|
||||
#[serde(alias = "filterAmount")]
|
||||
filter_amount: Option<f64>,
|
||||
#[serde(alias = "sortBy")]
|
||||
sort_by: Option<String>,
|
||||
#[serde(alias = "sortDirection")]
|
||||
@@ -119,6 +126,50 @@ impl Exchange {
|
||||
}
|
||||
}
|
||||
}
|
||||
if query.event_id.is_some() && (query.condition.is_some() || user.is_none()) {
|
||||
return Err(invalid(if query.condition.is_some() {
|
||||
"must provide either eventId or condition, not both"
|
||||
} else {
|
||||
"event_id requires a user"
|
||||
}));
|
||||
}
|
||||
let events = query
|
||||
.event_id
|
||||
.as_deref()
|
||||
.map(|raw| {
|
||||
let ids: std::collections::BTreeSet<_> = raw
|
||||
.split(',')
|
||||
.map(str::trim)
|
||||
.filter(|s| !s.is_empty())
|
||||
.collect();
|
||||
if ids.len() > 20 {
|
||||
return Err(invalid("expected at most 20 event ids"));
|
||||
}
|
||||
let numeric: Vec<i64> = ids.into_iter().filter_map(|id| id.parse().ok()).collect();
|
||||
serde_json::to_string(&numeric).map_err(|_| MarketDataError::Unavailable)
|
||||
})
|
||||
.transpose()?;
|
||||
let title = query
|
||||
.title
|
||||
.as_deref()
|
||||
.map(str::trim)
|
||||
.filter(|s| !s.is_empty());
|
||||
if title.is_some_and(|s| s.chars().count() > 200) {
|
||||
return Err(invalid("title exceeds 200 characters"));
|
||||
}
|
||||
let title = title.map(|s| format!("%{s}%"));
|
||||
let cash = match query.filter_type.as_deref() {
|
||||
None => false,
|
||||
Some(name) if name.eq_ignore_ascii_case("TOKENS") => false,
|
||||
Some(name) if name.eq_ignore_ascii_case("CASH") => true,
|
||||
Some(_) => return Err(invalid("filterType must be CASH or TOKENS")),
|
||||
};
|
||||
let floor = query
|
||||
.filter_amount
|
||||
.filter(|v| v.is_finite() && *v >= 0.0)
|
||||
.map_or(0, |v| (v * UNIT as f64).ceil() as i64);
|
||||
let shares_floor = if cash { 100_000 } else { floor.max(100_000) };
|
||||
|
||||
let conditions = condition_filter(query.condition.as_deref(), &tx)?;
|
||||
if user.is_none() {
|
||||
let count = conditions
|
||||
@@ -200,7 +251,10 @@ impl Exchange {
|
||||
wallet,
|
||||
selection.conditions,
|
||||
selection.status,
|
||||
selection.archived
|
||||
selection.archived,
|
||||
events,
|
||||
title,
|
||||
shares_floor
|
||||
])?;
|
||||
while let Some(row) = rows.next()? {
|
||||
let account: String = row.get(0)?;
|
||||
@@ -213,6 +267,9 @@ impl Exchange {
|
||||
.map_err(|_| MarketDataError::Unavailable)?
|
||||
.map(|(price, _)| price);
|
||||
let value = super::holding_value(size, mark);
|
||||
if cash && floor > 0 && value.is_none_or(|v| v < i128::from(floor)) {
|
||||
continue;
|
||||
}
|
||||
let rank = match selection.sort.as_str() {
|
||||
"TOKENS" => size,
|
||||
"PRICE" => mark.unwrap_or(0),
|
||||
|
||||
@@ -4,6 +4,10 @@ WITH held AS (
|
||||
FROM position_units p LEFT JOIN market_visibility v USING(market)
|
||||
WHERE (?1 IS NULL OR CASE WHEN ?2 THEN p.account COLLATE NOCASE=?1 ELSE p.account=?1 END)
|
||||
AND (?3 IS NULL OR p.market IN (SELECT value FROM json_each(?3)))
|
||||
AND (?6 IS NULL OR p.market IN (
|
||||
SELECT l.market FROM listed_markets l JOIN event_directory_ids e ON e.event=l.event
|
||||
WHERE e.id IN (SELECT value FROM json_each(?6))
|
||||
))
|
||||
)
|
||||
SELECT p.account,p.market,p.outcome,p.size,p.phase,p.payout_per_share,p.archived,
|
||||
p.phase<>'resolved' AND p.size>=100000 AND COALESCE(other.size,0)>=100000 AS mergeable,
|
||||
@@ -12,9 +16,9 @@ FROM held p
|
||||
JOIN market_wire_metadata w ON w.market=p.market
|
||||
LEFT JOIN held other ON other.account=p.account AND other.market=p.market AND other.outcome<>p.outcome
|
||||
LEFT JOIN neg_risk_members n ON n.market=p.market
|
||||
WHERE CASE ?4
|
||||
WHEN 'CLOSED' THEN p.size<100000
|
||||
ELSE p.active AND (?5 OR NOT p.archived) AND p.size>=100000 AND CASE ?4
|
||||
WHERE (?7 IS NULL OR w.question LIKE ?7) AND CASE ?4
|
||||
WHEN 'CLOSED' THEN p.size<CASE WHEN ?1 IS NULL THEN ?8 ELSE 100000 END
|
||||
ELSE p.active AND (?5 OR NOT p.archived) AND p.size>=?8 AND CASE ?4
|
||||
WHEN 'OPEN' THEN 1
|
||||
WHEN 'REDEEMABLE' THEN p.phase='resolved'
|
||||
WHEN 'REDEEMABLE_LOST' THEN p.phase='resolved' AND p.payout_per_share=0
|
||||
|
||||
Reference in New Issue
Block a user