1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// use redgold_schema::structs::{Address, ExternalTransactionId, SupportedCurrency};
// use rocket::serde::{Deserialize, Serialize};
// use crate::party::order_fulfillment::OrderFulfillment;
//
// use crate::party::price_volume::PriceVolume;
// pub const DUST_LIMIT: u64 = 2500;
// #[derive(Serialize, Deserialize, Clone)]
// pub struct BidAsk{
// pub bids: Vec<PriceVolume>,
// pub asks: Vec<PriceVolume>,
// pub center_price: f64
// }
//
// impl BidAsk {
//
// pub fn asking_price(&self) -> f64 {
// self.asks.get(0).map(|v| v.price).unwrap_or(0.)
// }
//
// pub fn sum_bid_volume(&self) -> u64 {
// self.bids.iter().map(|v| v.volume).sum::<u64>()
// }
//
// pub fn sum_ask_volume(&self) -> u64 {
// self.asks.iter().map(|v| v.volume).sum::<u64>()
// }
//
// pub fn volume_empty(&self) -> bool {
// self.bids.iter().find(|v| v.volume == 0).is_some() ||
// self.asks.iter().find(|v| v.volume == 0).is_some()
// }
//
// pub fn regenerate(&self, price: f64, min_ask: f64) -> BidAsk {
// BidAsk::generate_default(
// self.sum_ask_volume() as i64,
// self.sum_bid_volume(),
// price,
// min_ask
// )
// }
//
// pub fn generate_default(
// available_balance: i64,
// pair_balance: u64,
// last_exchange_price: f64,
// min_ask: f64,
// ) -> BidAsk {
// BidAsk::generate(
// available_balance,
// pair_balance,
// last_exchange_price,
// 40,
// 20.0f64,
// min_ask
// )
// }
//
// pub fn generate(
// available_balance_rdg: i64,
// pair_balance_btc: u64,
// last_exchange_price: f64, // this is for available type / pair type
// divisions: i32,
// scale: f64,
// // BTC / RDG
// min_ask: f64
// ) -> BidAsk {
//
// // A bid is an offer to buy RDG with BTC
// // The volume should be denominated in BTC because this is how much is staked natively
// let bids = if pair_balance_btc > 0 {
// PriceVolume::generate(
// pair_balance_btc,
// last_exchange_price, // Price here is RDG/BTC
// divisions,
// last_exchange_price*0.9,
// scale / 2.0
// )
// } else {
// vec![]
// };
//
//
// // An ask price in the inverse of a bid price, since we want to denominate in RDG
// // since the volume is in RDG.
// // Here it is now BTC / RDG
// let ask_price_expected = 1.0 / last_exchange_price;
//
// // Apply a max to ask price.
// let ask_price = f64::max(ask_price_expected, min_ask);
//
// // An ask is how much BTC is being asked for each RDG
// // Volume is denominated in RDG because this is what the contract is holding for resale
// let asks = if available_balance_rdg > 0 {
// PriceVolume::generate(
// available_balance_rdg as u64,
// ask_price,
// divisions,
// ask_price*3.0,
// scale
// )
// } else {
// vec![]
// };
// BidAsk {
// bids,
// asks,
// center_price: last_exchange_price,
// }
// }
// }
//
// //
// // impl BidAsk {
// //
// // pub fn remove_empty(&mut self) {
// // self.bids.retain(|v| v.volume > 0);
// // self.asks.retain(|v| v.volume > 0);
// // }
// // pub fn fulfill_taker_order(
// // &self,
// // order_amount: u64,
// // is_ask: bool,
// // event_time: i64,
// // tx_id: Option<String>,
// // destination: &Address
// // ) -> Option<OrderFulfillment> {
// // let mut remaining_order_amount = order_amount.clone();
// // let mut fulfilled_amount: u64 = 0;
// // let mut updated_curve = if is_ask {
// // // Asks are ordered in increasing amount(USD), denominated in BTC/RDG
// // self.asks.clone()
// // } else {
// // // Bids are ordered in decreasing amount(USD), denominated in RDG/BTC
// // self.bids.clone()
// // };
// //
// //
// // for pv in updated_curve.iter_mut() {
// //
// // let other_amount_requested = if is_ask {
// // // Comments left here for clarity even if code is the same
// // let price = pv.price; // BTC / RDG
// // // BTC / (BTC / RDG) = RDG
// // remaining_order_amount as f64 / price
// // } else {
// // // RDG / RDG/BTC = BTC
// // remaining_order_amount as f64 / pv.price
// // } as u64;
// //
// // let this_vol = pv.volume;
// // if other_amount_requested >= this_vol {
// // // We have more Other than this ask can fulfill, so we take it all and move on.
// // fulfilled_amount += this_vol;
// // remaining_order_amount -= (this_vol as f64 * pv.price) as u64;
// // pv.volume = 0;
// // } else {
// // // We have less Other than this ask can fulfill, so we take it and stop
// // pv.volume -= other_amount_requested;
// // remaining_order_amount = 0;
// // fulfilled_amount += other_amount_requested;
// // break
// // }
// // };
// //
// // updated_curve.retain(|v| v.volume > 0);
// //
// // if fulfilled_amount < DUST_LIMIT {
// // None
// // } else {
// // Some(OrderFulfillment {
// // order_amount,
// // fulfilled_amount,
// // updated_curve,
// // is_ask_fulfillment_from_external_deposit: is_ask,
// // event_time,
// // tx_id_ref: tx_id.map(|id| ExternalTransactionId{ identifier: id, currency: SupportedCurrency::Bitcoin as i32 }),
// // destination: destination.clone(),
// // })
// // }
// // }
// // }