-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from mrgnlabs/beta/crossbar-patch
Feat: Merge swb pull oracle patch
- Loading branch information
Showing
20 changed files
with
1,112 additions
and
267 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use solana_sdk::pubkey::Pubkey; | ||
use std::{ | ||
collections::HashMap, | ||
sync::{Arc, Mutex}, | ||
}; | ||
use switchboard_on_demand_client::CrossbarClient; | ||
|
||
/// CrossbarMaintainer will maintain the feeds prices | ||
/// with simulated prices from the crossbar service | ||
pub(crate) struct CrossbarMaintainer { | ||
crossbar_client: CrossbarClient, | ||
} | ||
|
||
impl CrossbarMaintainer { | ||
/// Creates a new CrossbarMaintainer empty instance | ||
pub fn new() -> Self { | ||
let crossbar_client = CrossbarClient::default(None); | ||
Self { crossbar_client } | ||
} | ||
|
||
pub async fn simulate(&self, feeds: Vec<(Pubkey, String)>) -> Vec<(Pubkey, f64)> { | ||
if feeds.is_empty() { | ||
return Vec::new(); | ||
} | ||
|
||
// Create a fast lookup map from feed hash to oracle hash | ||
let feed_hash_to_oracle_hash_map: HashMap<String, Pubkey> = feeds | ||
.iter() | ||
.map(|(address, feed_hash)| (feed_hash.clone(), address.clone())) | ||
.collect(); | ||
|
||
let feed_hashes: Vec<&str> = feeds | ||
.iter() | ||
.map(|(_, feed_hash)| feed_hash.as_str()) | ||
.collect(); | ||
|
||
let simulated_prices = self | ||
.crossbar_client | ||
.simulate_feeds(&feed_hashes) | ||
.await | ||
.unwrap(); | ||
let mut prices = Vec::new(); | ||
for simulated_response in simulated_prices { | ||
if let Some(price) = calculate_price(simulated_response.results) { | ||
prices.push(( | ||
feed_hash_to_oracle_hash_map | ||
.get(&simulated_response.feedHash) | ||
.unwrap() | ||
.clone(), | ||
price, | ||
)); | ||
} | ||
} | ||
prices | ||
} | ||
} | ||
fn calculate_price(mut numbers: Vec<f64>) -> Option<f64> { | ||
if numbers.is_empty() { | ||
return None; | ||
} | ||
|
||
numbers.sort_by(|a, b| a.partial_cmp(b).unwrap()); | ||
let mid = numbers.len() / 2; | ||
|
||
if numbers.len() % 2 == 0 { | ||
Some((numbers[mid - 1] + numbers[mid]) / 2.0) | ||
} else { | ||
Some(numbers[mid]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.