Skip to content

Commit

Permalink
Make publish_url configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
SupernaviX committed Oct 2, 2024
1 parent c59978b commit c14d815
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
1 change: 1 addition & 0 deletions config.base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ network_timeout_ms: 30000 # How long to allow TCP requests to run before timing
round_duration_ms: 10000 # How long does it take to run a round?
gema_periods: 10 # Factor to use for GEMA price smoothing
price_precision: 1024 # How many bits of precision are allowed in prices in the final output?
publish_url: https://infra-integration.silver-train-1la.pages.dev/api/updatePrices
consensus: true
logs:
json: true
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct RawOracleConfig {
pub round_duration_ms: u64,
pub gema_periods: usize,
pub price_precision: u64,
pub publish_url: Option<String>,
pub logs: RawLogConfig,
pub frost_address: Option<String>,
pub keygen: KeygenConfig,
Expand Down Expand Up @@ -54,6 +55,7 @@ pub struct OracleConfig {
pub round_duration: Duration,
pub gema_periods: usize,
pub price_precision: u64,
pub publish_url: Option<String>,
pub logs: LogConfig,
pub frost_address: Option<String>,
pub keygen: KeygenConfig,
Expand Down Expand Up @@ -154,6 +156,7 @@ impl TryFrom<RawOracleConfig> for OracleConfig {
round_duration: Duration::from_millis(raw.round_duration_ms),
gema_periods: raw.gema_periods,
price_precision: raw.price_precision,
publish_url: raw.publish_url,
logs,
frost_address: raw.frost_address,
keygen: raw.keygen,
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl Node {

let api_server = APIServer::new(&config, payload_source.clone(), price_audit_rx);

let publisher = Publisher::new(&network.id, payload_source)?;
let publisher = Publisher::new(&config, payload_source)?;

Ok(Node {
api_server,
Expand Down
24 changes: 12 additions & 12 deletions src/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,26 @@ use reqwest::Client;
use tokio::sync::watch;
use tracing::{info, trace, warn};

use crate::{network::NodeId, signature_aggregator::Payload};

const URL: &str = "https://infra-integration.silver-train-1la.pages.dev/api/updatePrices";
use crate::{config::OracleConfig, network::NodeId, signature_aggregator::Payload};

pub struct Publisher {
id: NodeId,
url: Option<String>,
source: watch::Receiver<Payload>,
client: Client,
}

impl Publisher {
pub fn new(id: &NodeId, source: watch::Receiver<Payload>) -> Result<Self> {
pub fn new(config: &OracleConfig, source: watch::Receiver<Payload>) -> Result<Self> {
Ok(Self {
id: id.clone(),
id: config.id.clone(),
url: config.publish_url.clone(),
source,
client: Client::builder().build()?,
})
}

pub async fn run(self) {
const DEBUG: bool = false;

let mut source = self.source;
let client = self.client;
while source.changed().await.is_ok() {
Expand All @@ -45,21 +43,23 @@ impl Publisher {
}
payload
};
info!(payload, "publishing payload");
if let Some(url) = &self.url {
info!(payload, "publishing payload");

if !DEBUG {
match make_request(&client, payload).await {
match make_request(url, &client, payload).await {
Ok(res) => trace!("Payload published! {}", res),
Err(err) => warn!("Could not publish payload: {}", err),
}
} else {
info!(payload, "final payload (not publishing)");
}
}
}
}

async fn make_request(client: &Client, payload: String) -> Result<String> {
async fn make_request(url: &str, client: &Client, payload: String) -> Result<String> {
let response = client
.post(URL)
.post(url)
.header("Content-Type", "application/json")
.body(payload)
.timeout(Duration::from_secs(5))
Expand Down

0 comments on commit c14d815

Please sign in to comment.