Skip to content

Commit

Permalink
several relatively minor but important fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Axel-Jacobsen committed Dec 28, 2023
1 parent 071acfe commit 0dee261
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ async fn main() {
return;
}

let active_positions = market_handler::MarketHandler::get_active_positions(all_my_bets).await;
let mut active_positions =
market_handler::MarketHandler::get_active_positions(all_my_bets).await;
active_positions.sort_unstable_by_key(|position| {
(position.contract_id.clone(), position.answer_id.clone())
});

for position in active_positions {
println!("{position}");
Expand Down
17 changes: 10 additions & 7 deletions src/manifold_types.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
use std::hash::Hash;
use std::fmt::Display;
use std::hash::Hash;

#[derive(Serialize, Deserialize, Debug, Clone, Hash, Eq, PartialEq)]
enum TimePeriod {
Expand Down Expand Up @@ -507,15 +507,18 @@ pub struct Position {

impl Display for Position {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let buysell = if self.amount < 0.0 { "SELL" } else { "BUY" };
let answer_id_str = if let Some(answer_id) = &self.answer_id {
format!("| answer id: {}", answer_id)
} else {
"".to_string()
};

write!(
f,
"contract id: {} | answer id: {} | bet: {:.2} {} {}",
self.contract_id,
self.answer_id.clone().unwrap_or_default(),
"bet: {:.4} {} | contract id: {} {answer_id_str}",
self.amount.abs(),
buysell,
self.outcome
self.outcome,
self.contract_id,
)
}
}
Expand Down
30 changes: 19 additions & 11 deletions src/market_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ impl MarketHandler {
let params = [
("userId".to_string(), me.id.clone()),
("before".to_string(), bet_before_id),
("limit".to_string(), "1000".to_string()),
];

let bets_response = rate_limited_get_endpoint(
Expand All @@ -276,18 +277,21 @@ impl MarketHandler {
}
};

if bets.is_empty() {
all_bets.extend(bets.clone());

if bets.len() < 1000 {
break;
} else {
debug!("found {} sold bets", bets.len());
debug!("found {} bets", bets.len());
bet_before_id = bets.last().unwrap().id.clone();
all_bets.extend(bets.clone());
}
}
Ok(all_bets)
}

pub async fn get_active_positions(all_bets: Vec<manifold_types::Bet>) -> Vec<manifold_types::Position> {
pub async fn get_active_positions(
all_bets: Vec<manifold_types::Bet>,
) -> Vec<manifold_types::Position> {
#[derive(Hash, Eq, PartialEq)]
struct PositionKey {
outcome: String,
Expand All @@ -308,7 +312,7 @@ impl MarketHandler {

all_positions
.into_iter()
.filter(|(_, total_amount_sum)| *total_amount_sum != 0.0)
.filter(|(_, total_amount_sum)| *total_amount_sum > 1e-10)
.map(|(position, total_amount_sum)| manifold_types::Position {
outcome: position.outcome,
contract_id: position.contract_id,
Expand All @@ -320,11 +324,12 @@ impl MarketHandler {

pub async fn liquidate_all_positions(&self) -> Result<(), errors::ReqwestResponseParsing> {
let all_bets = self.get_all_my_positions().await?;
let open_positions = Self::get_active_positions(all_bets.clone()).await;

for bet in all_bets {
for pos in open_positions {
let data = Some(serde_json::json!({
"contractId": bet.contract_id,
"answerId": bet.answer_id,
"contractId": pos.contract_id,
"answerId": pos.answer_id,
}));

let sell_response = rate_limited_post_endpoint(
Expand All @@ -338,10 +343,13 @@ impl MarketHandler {
match sell_response {
Ok(resp) => {
info!(
"successfully sold shares for bet {} contract id {} answer id {:?}",
bet.id, bet.contract_id, bet.answer_id
"successfully sold {} {} shares for contract id {} answer id {:?}",
pos.amount, pos.outcome, pos.contract_id, pos.answer_id
);
debug!(
"full response {:?} for contract {} answer {:?}",
resp, pos.contract_id, pos.answer_id
);
debug!("full response {:?} for bet id {}", resp, bet.id);
}
Err(e) => error!("couldn't sell shares: {e}"),
};
Expand Down

0 comments on commit 0dee261

Please sign in to comment.