Filter public positions by title, event and current holding
This commit is contained in:
@@ -425,3 +425,5 @@ sequenceDiagram
|
||||
```
|
||||
|
||||
GET /v2/positions 查询钱包或单一市场的当前持仓、待赎回份额、互补仓位及已退出记录。用 pagination.next_cursor 续读时沿用原钱包或市场与筛选条件。
|
||||
|
||||
持仓页支持按标题、事件或最低份额与金额筛选。使用下一页游标时重发这三个筛选参数。
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -4210,6 +4210,30 @@ fn public_positions_walk_a_mergeable_pair_and_held_settlement() {
|
||||
.unwrap();
|
||||
assert_eq!(winning_dust["current_size"], 0.0088);
|
||||
assert_eq!(winning_dust["redeemable"], true);
|
||||
assert_eq!(
|
||||
read(
|
||||
&e,
|
||||
json!({"user":"alice","title":"%e%","filter_type":"tokens","filter_amount":10})
|
||||
)
|
||||
.unwrap()["data"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.len(),
|
||||
2
|
||||
);
|
||||
assert_eq!(
|
||||
read(&e, json!({"user":"alice","filter_amount":10.001})).unwrap()["data"],
|
||||
json!([])
|
||||
);
|
||||
assert_eq!(
|
||||
read(&e, json!({"user":"alice","title":"unmatched title"})).unwrap()["data"],
|
||||
json!([])
|
||||
);
|
||||
assert_eq!(
|
||||
read(&e, json!({"user":"alice","event_id":"unmatched"})).unwrap()["data"],
|
||||
json!([])
|
||||
);
|
||||
assert!(read(&e, json!({"user":"alice","event_id":"1","condition":"e"})).is_err());
|
||||
let first = read(&e, json!({"user":"alice","status":"MERGEABLE","limit":1})).unwrap();
|
||||
assert_eq!(first["data"][0]["mergeable"], true);
|
||||
let cursor = first["pagination"]["next_cursor"].as_str().unwrap();
|
||||
@@ -4230,6 +4254,13 @@ fn public_positions_walk_a_mergeable_pair_and_held_settlement() {
|
||||
assert_eq!(held["data"][0]["status"], "REDEEMABLE");
|
||||
assert_eq!(held["data"][0]["current_value"], 10);
|
||||
assert_eq!(held["data"][0]["mergeable"], false);
|
||||
let cash = read(
|
||||
&e,
|
||||
json!({"user":"alice","filterType":"cash","filterAmount":10}),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(cash["data"].as_array().unwrap().len(), 1);
|
||||
assert_eq!(cash["data"][0]["token_id"], "eb:YES:e");
|
||||
let lost = read(&e, json!({"user":"alice","status":"REDEEMABLE_LOST"})).unwrap();
|
||||
assert_eq!(lost["data"].as_array().unwrap().len(), 1);
|
||||
assert_eq!(lost["data"][0]["token_id"], "eb:NO:e");
|
||||
|
||||
Reference in New Issue
Block a user