Skip to main content

skyblock_rs/objects/
bazaar.rs

1/// The ID of the product in question
2#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
3pub struct Product {
4	/// The ID of the product retrieved
5	pub product_id: String,
6	/// The weekly historic data of the product.
7	/// Note: This field is associated with deprecated endpoints
8	#[deprecated]
9	#[serde(skip_serializing_if = "Option::is_none")]
10	pub week_historic: Option<Vec<Historic>>,
11	/// The current, live data of the bazaar product
12	#[serde(flatten)]
13	pub live_data: LiveProductData,
14}
15
16/// Current, live data of the auction
17#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
18pub struct LiveProductData {
19	/// List of the top (up to 30) buy orders
20	pub buy_summary: Vec<Order>,
21	/// List of the top (up to 30) sell offers
22	pub sell_summary: Vec<Order>,
23	/// The current quick stats of a bazaar item
24	pub quick_status: QuickStatus,
25}
26
27impl LiveProductData {
28	pub fn top_buy(&self) -> f64 {
29		let mut top = f64::NEG_INFINITY;
30
31		for order in &self.buy_summary {
32			if top < order.price_per_unit {
33				top = order.price_per_unit
34			}
35		}
36
37		return top;
38	}
39
40	pub fn top_sell(&self) -> f64 {
41		let mut top = f64::INFINITY;
42
43		for order in &self.sell_summary {
44			if top > order.price_per_unit {
45				top = order.price_per_unit
46			}
47		}
48
49		return top;
50	}
51}
52
53#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
54pub struct QuickStatus {
55	#[serde(rename = "productId")]
56	pub product_id: String,
57	/// Weighted average of the top 2% b/v
58	#[serde(rename = "buyPrice")]
59	pub buy_price: f32,
60	/// Sum of item amounts in all orders
61	#[serde(rename = "buyVolume")]
62	pub buy_volume: f32,
63	/// Historic transacted volume in last week + live states
64	#[serde(rename = "buyMovingWeek")]
65	pub buy_moving_week: f32,
66	/// Number of active orders
67	#[serde(rename = "buyOrders")]
68	pub buy_orders: f32,
69	/// Weighted average of the top 2% b/v
70	#[serde(rename = "sellPrice")]
71	pub sell_price: f32,
72	/// Sum of item amounts in all orders
73	#[serde(rename = "sellVolume")]
74	pub sell_volume: f32,
75	/// Historic transacted volume in last week + live states
76	#[serde(rename = "sellMovingWeek")]
77	pub sell_moving_week: f32,
78	/// Number of active orders
79	#[serde(rename = "sellOrders")]
80	pub sell_orders: f32,
81}
82
83#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
84pub struct Historic {
85	#[serde(rename = "productId")]
86	pub product_id: String,
87	pub timestamp: i64,
88	/// Number of active items
89	#[serde(rename = "nowBuyVolume")]
90	pub market_demand: f32,
91	/// Number of active items
92	#[serde(rename = "nowSellVolume")]
93	pub market_supply: f32,
94	/// Volume of coins transacted via buy orders
95	#[serde(rename = "buyCoins")]
96	pub buy_coins: f32,
97	/// Volume of items transacted via buy orders
98	#[serde(rename = "buyVolume")]
99	pub buy_volume: f32,
100	/// Volume of items via instant buys
101	#[serde(rename = "buys")]
102	pub instant_buys: f32,
103	/// Volume of coins transacted via sell orders
104	#[serde(rename = "sellCoins")]
105	pub sell_coins: f32,
106	/// Volume of items transacted via sell orders
107	#[serde(rename = "sellVolume")]
108	pub sell_volume: f32,
109	/// Volume of items via instant sells
110	#[serde(rename = "sells")]
111	pub instant_sells: f32,
112}
113
114impl Historic {
115	/// Calculate the instant buy price by recent sales
116	pub fn recent_instant_buy_price(&self) -> f32 {
117		self.sell_coins / self.sell_volume
118	}
119
120	/// Calculate the instant sell price by recent purchases
121	pub fn recent_instant_sell_price(&self) -> f32 {
122		self.buy_coins / self.buy_volume
123	}
124}
125
126#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
127pub struct Order {
128	/// Total amount of items in the orders involved
129	pub amount: i32,
130	/// The price of the transaction for each item in the order
131	#[serde(rename = "pricePerUnit")]
132	pub price_per_unit: f64,
133	/// The number of orders associated with this price
134	pub orders: i32,
135}