Skip to content

Commit c4fad12

Browse files
committed
fmt
1 parent b0b9134 commit c4fad12

File tree

3 files changed

+124
-77
lines changed

3 files changed

+124
-77
lines changed

src/agent/services.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
pub mod exporter;
22
pub mod keypairs;
3+
pub mod lazer_exporter;
34
pub mod notifier;
45
pub mod oracle;
5-
pub mod lazer_exporter;
66

77
pub use {
88
exporter::exporter,
9-
lazer_exporter::lazer_exporter,
109
keypairs::keypairs,
10+
lazer_exporter::lazer_exporter,
1111
notifier::notifier,
1212
oracle::oracle,
1313
};

src/agent/services/lazer_exporter.rs

Lines changed: 109 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,51 @@
1-
use std::sync::Arc;
2-
use std::time::Duration;
3-
use anyhow::Result;
4-
use futures_util::SinkExt;
5-
use futures_util::stream::{SplitSink, SplitStream, StreamExt};
6-
use http::HeaderValue;
7-
use pyth_lazer_protocol::publisher::PriceFeedDataV1;
8-
use reqwest::Client;
9-
use serde::Deserialize;
10-
use tokio::net::TcpStream;
11-
use tokio::task::JoinHandle;
12-
use tracing::instrument;
13-
use tracing;
14-
use tokio_tungstenite::WebSocketStream;
15-
use tokio_tungstenite::{
16-
connect_async_with_config,
17-
tungstenite::{client::IntoClientRequest, Message},
18-
MaybeTlsStream,
1+
use {
2+
crate::agent::state,
3+
anyhow::Result,
4+
futures_util::{
5+
stream::{
6+
SplitSink,
7+
SplitStream,
8+
StreamExt,
9+
},
10+
SinkExt,
11+
},
12+
http::HeaderValue,
13+
pyth_lazer_protocol::publisher::PriceFeedDataV1,
14+
reqwest::Client,
15+
serde::Deserialize,
16+
std::{
17+
sync::Arc,
18+
time::Duration,
19+
},
20+
tokio::{
21+
net::TcpStream,
22+
task::JoinHandle,
23+
},
24+
tokio_tungstenite::{
25+
connect_async_with_config,
26+
tungstenite::{
27+
client::IntoClientRequest,
28+
Message,
29+
},
30+
MaybeTlsStream,
31+
WebSocketStream,
32+
},
33+
tokio_util::bytes::{
34+
BufMut,
35+
BytesMut,
36+
},
37+
tracing::{
38+
self,
39+
instrument,
40+
},
41+
url::Url,
1942
};
20-
use tokio_util::bytes::{BufMut, BytesMut};
21-
use url::Url;
22-
use crate::agent::state;
2343

2444
#[derive(Clone, Debug, Deserialize)]
2545
pub struct Config {
26-
pub history_url: Url,
27-
pub relayer_urls: Vec<Url>,
28-
pub authorization_token: String,
46+
pub history_url: Url,
47+
pub relayer_urls: Vec<Url>,
48+
pub authorization_token: String,
2949
#[serde(with = "humantime_serde")]
3050
pub publish_interval_duration: Duration,
3151
}
@@ -84,61 +104,77 @@ async fn connect_to_relayers(
84104
relayer_senders.push(relayer_sender);
85105
relayer_receivers.push(relayer_receiver);
86106
}
87-
let sender = RelayerSender { ws_senders: relayer_senders };
107+
let sender = RelayerSender {
108+
ws_senders: relayer_senders,
109+
};
88110
tracing::info!("connected to relayers: {:?}", config.relayer_urls);
89111
Ok((sender, relayer_receivers))
90112
}
91113

92114
#[derive(Deserialize)]
93115
struct SymbolResponse {
94-
pub pyth_lazer_id: u32,
95-
pub name: String,
96-
pub symbol: String,
97-
pub description: String,
98-
pub asset_type: String,
99-
pub exponent: i32,
100-
pub cmc_id: Option<u32>,
101-
pub interval: Option<String>,
116+
pub pyth_lazer_id: u32,
117+
pub name: String,
118+
pub symbol: String,
119+
pub description: String,
120+
pub asset_type: String,
121+
pub exponent: i32,
122+
pub cmc_id: Option<u32>,
123+
pub interval: Option<String>,
102124
pub min_publishers: u16,
103-
pub min_channel: String,
104-
pub state: String,
105-
pub hermes_id: Option<String>,
125+
pub min_channel: String,
126+
pub state: String,
127+
pub hermes_id: Option<String>,
106128
}
107129

108130
async fn fetch_symbols(history_url: &Url) -> Result<Vec<SymbolResponse>> {
109131
let mut url = history_url.clone();
110132
url.set_scheme("http").unwrap();
111133
url.set_path("/history/v1/symbols");
112134
let client = Client::new();
113-
let response = client
114-
.get(url)
115-
.send()
116-
.await?
117-
.text()
118-
.await?;
135+
let response = client.get(url).send().await?.text().await?;
119136
Ok(serde_json::from_str(&response)?)
120137
}
121138

122139
#[instrument(skip(config, state))]
123-
pub fn lazer_exporter(config: Config, state: Arc<state::State>) -> Vec<JoinHandle<()>>
124-
{
140+
pub fn lazer_exporter(config: Config, state: Arc<state::State>) -> Vec<JoinHandle<()>> {
125141
// TODO: add loop to handle relayer failure/retry
126142
let mut handles = Vec::new();
127-
handles.push(tokio::spawn(lazer_exporter::lazer_exporter(config.clone(), state)));
143+
handles.push(tokio::spawn(lazer_exporter::lazer_exporter(
144+
config.clone(),
145+
state,
146+
)));
128147
handles
129148
}
130149

131150
mod lazer_exporter {
132-
use std::collections::HashMap;
133-
use std::num::NonZeroI64;
134-
use std::sync::Arc;
135-
use std::time::Duration;
136-
use futures_util::StreamExt;
137-
use pyth_lazer_protocol::publisher::PriceFeedDataV1;
138-
use pyth_lazer_protocol::router::{Price, PriceFeedId, TimestampUs};
139-
use tokio_stream::StreamMap;
140-
use crate::agent::services::lazer_exporter::{Config, connect_to_relayers, fetch_symbols, SymbolResponse};
141-
use crate::agent::state::local::LocalStore;
151+
use {
152+
crate::agent::{
153+
services::lazer_exporter::{
154+
connect_to_relayers,
155+
fetch_symbols,
156+
Config,
157+
SymbolResponse,
158+
},
159+
state::local::LocalStore,
160+
},
161+
futures_util::StreamExt,
162+
pyth_lazer_protocol::{
163+
publisher::PriceFeedDataV1,
164+
router::{
165+
Price,
166+
PriceFeedId,
167+
TimestampUs,
168+
},
169+
},
170+
std::{
171+
collections::HashMap,
172+
num::NonZeroI64,
173+
sync::Arc,
174+
time::Duration,
175+
},
176+
tokio_stream::StreamMap,
177+
};
142178

143179
pub async fn lazer_exporter<S>(config: Config, state: Arc<S>)
144180
where
@@ -152,7 +188,11 @@ mod lazer_exporter {
152188
run(&config, state.clone()).await;
153189

154190
failure_count += 1;
155-
tracing::error!("Lazer exporter failed {} times; retrying in {:?}", failure_count, retry_duration);
191+
tracing::error!(
192+
"Lazer exporter failed {} times; retrying in {:?}",
193+
failure_count,
194+
retry_duration
195+
);
156196
tokio::time::sleep(retry_duration).await;
157197

158198
// TODO: Back off or crash altogether on persistent failure
@@ -165,20 +205,23 @@ mod lazer_exporter {
165205
S: Send + Sync + 'static,
166206
{
167207
// TODO: Re-fetch on an interval?
168-
let lazer_symbols: HashMap<String, SymbolResponse> = match fetch_symbols(&config.history_url).await {
169-
Ok(symbols) => symbols.into_iter().filter_map(|symbol| {
170-
symbol.hermes_id.clone().map(|id| (id, symbol))
171-
}).collect(),
172-
Err(e) => {
173-
tracing::error!("Failed to fetch Lazer symbols: {e:?}");
174-
return;
175-
}
176-
};
208+
let lazer_symbols: HashMap<String, SymbolResponse> =
209+
match fetch_symbols(&config.history_url).await {
210+
Ok(symbols) => symbols
211+
.into_iter()
212+
.filter_map(|symbol| symbol.hermes_id.clone().map(|id| (id, symbol)))
213+
.collect(),
214+
Err(e) => {
215+
tracing::error!("Failed to fetch Lazer symbols: {e:?}");
216+
return;
217+
}
218+
};
177219

178220
// Establish relayer connections
179221
// Relayer will drop the connection if no data received in 5s
180222
let (mut relayer_sender, relayer_receivers) = connect_to_relayers(&config)
181-
.await.expect("failed to connect to relayers");
223+
.await
224+
.expect("failed to connect to relayers");
182225
let mut stream_map = StreamMap::new();
183226
for (i, receiver) in relayer_receivers.into_iter().enumerate() {
184227
stream_map.insert(config.relayer_urls[i].clone(), receiver);

src/agent/state/exporter.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ fn create_instruction_without_accumulator(
800800
is_writable: false,
801801
},
802802
],
803-
data: bincode::serde::encode_to_vec(
803+
data: bincode::serde::encode_to_vec(
804804
&(UpdPriceCmd {
805805
version: PYTH_ORACLE_VERSION,
806806
cmd: UPDATE_PRICE_NO_FAIL_ON_ERROR,
@@ -810,7 +810,9 @@ fn create_instruction_without_accumulator(
810810
conf: price_info.conf,
811811
pub_slot: current_slot,
812812
}),
813-
bincode::config::legacy().with_little_endian().with_fixed_int_encoding()
813+
bincode::config::legacy()
814+
.with_little_endian()
815+
.with_fixed_int_encoding(),
814816
)?,
815817
})
816818
}
@@ -945,15 +947,17 @@ fn create_instruction_with_accumulator(
945947
],
946948
data: bincode::serde::encode_to_vec(
947949
&(UpdPriceCmd {
948-
version: PYTH_ORACLE_VERSION,
949-
cmd: UPDATE_PRICE_NO_FAIL_ON_ERROR,
950-
status: price_info.status,
951-
unused_: 0,
952-
price: price_info.price,
953-
conf: price_info.conf,
950+
version: PYTH_ORACLE_VERSION,
951+
cmd: UPDATE_PRICE_NO_FAIL_ON_ERROR,
952+
status: price_info.status,
953+
unused_: 0,
954+
price: price_info.price,
955+
conf: price_info.conf,
954956
pub_slot: current_slot,
955957
}),
956-
bincode::config::legacy().with_little_endian().with_fixed_int_encoding()
958+
bincode::config::legacy()
959+
.with_little_endian()
960+
.with_fixed_int_encoding(),
957961
)?,
958962
})
959963
}

0 commit comments

Comments
 (0)