Skip to content

Commit

Permalink
fix: take the solver x and y from config parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
AmanRaj1608 committed Jun 27, 2024
1 parent 69da0df commit e769433
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
8 changes: 6 additions & 2 deletions bin/reflux/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,12 @@ async fn run_solver(config: Config) {
let redis_client = RedisClient::build(&config.infra.redis_url)
.await
.expect("Failed to instantiate redis client");
let routing_engine =
Arc::new(RoutingEngine::new(account_service.clone(), buckets, redis_client.clone()));
let routing_engine = Arc::new(RoutingEngine::new(
account_service.clone(),
buckets,
redis_client.clone(),
config.solver_config,
));

// Subscribe to cache update messages
let cache_update_topic = config.indexer_config.indexer_update_topic.clone();
Expand Down
4 changes: 0 additions & 4 deletions crates/account-aggregation/src/service.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use log::debug;
use std::sync::Arc;
use thiserror::Error;

Expand Down Expand Up @@ -230,7 +229,6 @@ impl AccountAggregationService {

let mut balances = Vec::new();
let networks = self.networks.clone();
debug!("Networks: {:?}", networks);

// todo: parallelize this
for user in accounts.iter() {
Expand All @@ -239,14 +237,12 @@ impl AccountAggregationService {
"{}/v1/{}/address/{}/balances_v2/?key={}",
self.covalent_base_url, network, user, self.covalent_api_key
);
debug!("Requesting: {}", url);
let response = self.client.get(&url).send().await?;
let api_response: ApiResponse = response.json().await?;
let user_balances = extract_balance_data(api_response)?;
balances.extend(user_balances);
}
}
println!("{:?}", balances);

Ok(balances)
}
Expand Down
14 changes: 13 additions & 1 deletion crates/config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::ops::Deref;

use derive_more::{Display, From, Into};
use serde::Deserialize;
use serde_valid::{UniqueItemsError, Validate, ValidateUniqueItems};
use serde_valid::yaml::FromYamlStr;
use serde_valid::{UniqueItemsError, Validate, ValidateUniqueItems};

// Config Type
#[derive(Debug)]
Expand All @@ -29,6 +29,8 @@ pub struct Config {
pub server: ServerConfig,
// Configuration for the indexer
pub indexer_config: IndexerConfig,
// Configuration for the solver
pub solver_config: SolverConfig,
}

impl Config {
Expand Down Expand Up @@ -126,6 +128,7 @@ impl Config {
infra: raw_config.infra,
server: raw_config.server,
indexer_config: raw_config.indexer_config,
solver_config: raw_config.solver_config,
})
}
}
Expand Down Expand Up @@ -228,6 +231,7 @@ pub struct RawConfig {
pub infra: InfraConfig,
pub server: ServerConfig,
pub indexer_config: IndexerConfig,
pub solver_config: SolverConfig,
}

#[derive(Debug, Deserialize, Validate, PartialOrd, Clone)]
Expand Down Expand Up @@ -429,6 +433,14 @@ pub struct IndexerConfig {
pub points_per_bucket: u64,
}

#[derive(Debug, Deserialize, Validate)]
pub struct SolverConfig {
#[validate(minimum = 1.0)]
pub x_value: f64,
#[validate(minimum = 1.0)]
pub y_value: f64,
}

pub fn get_sample_config() -> Config {
Config::from_file("../../config.yaml.example").unwrap()
}
Expand Down
24 changes: 18 additions & 6 deletions crates/routing-engine/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tokio::sync::RwLock;

use account_aggregation::service::AccountAggregationService;
use account_aggregation::types::Balance;
use config::config::BucketConfig;
use config::{config::BucketConfig, SolverConfig};
use storage::{RedisClient, RedisClientError};

use crate::estimator::{Estimator, LinearRegressionEstimator};
Expand Down Expand Up @@ -51,23 +51,25 @@ pub enum RoutingEngineError {

/// Routing Engine
/// This struct is responsible for calculating the best cost path for a user
#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct RoutingEngine {
buckets: Vec<BucketConfig>,
aas_client: AccountAggregationService,
cache: Arc<RwLock<HashMap<String, String>>>, // (hash(bucket), hash(estimator_value)
redis_client: RedisClient,
estimates: SolverConfig,
}

impl RoutingEngine {
pub fn new(
aas_client: AccountAggregationService,
buckets: Vec<BucketConfig>,
redis_client: RedisClient,
solver_config: SolverConfig,
) -> Self {
let cache = Arc::new(RwLock::new(HashMap::new()));

Self { aas_client, cache, buckets, redis_client }
Self { aas_client, cache, buckets, redis_client, estimates: solver_config }
}

pub async fn refresh_cache(&self) {
Expand Down Expand Up @@ -110,8 +112,8 @@ impl RoutingEngine {
debug!("Direct assets: {:?}", direct_assets);

// Sort direct assets by A^x / C^y, here x=2 and y=1
let x = 2.0;
let y = 1.0;
let x = self.estimates.x_value;
let y = self.estimates.y_value;
let mut sorted_assets: Vec<(&Balance, f64)> = stream::iter(direct_assets.into_iter())
.then(|balance| async move {
let fee_cost = self
Expand Down Expand Up @@ -283,7 +285,7 @@ mod tests {
use tokio::sync::RwLock;

use account_aggregation::service::AccountAggregationService;
use config::BucketConfig;
use config::{BucketConfig, SolverConfig};
use storage::mongodb_client::MongoDBClient;

use crate::engine::PathQuery;
Expand Down Expand Up @@ -350,11 +352,16 @@ mod tests {
);
let redis_client =
storage::RedisClient::build(&"redis://localhost:6379".to_string()).await.unwrap();
let estimates = SolverConfig {
x_value: 2.0,
y_value: 1.0,
};
let routing_engine = RoutingEngine {
aas_client,
buckets,
cache: Arc::new(RwLock::new(cache)),
redis_client,
estimates,
};

// Define the target amount and path query
Expand Down Expand Up @@ -429,11 +436,16 @@ mod tests {

let redis_client =
storage::RedisClient::build(&"redis://localhost:6379".to_string()).await.unwrap();
let estimates = SolverConfig {
x_value: 2.0,
y_value: 1.0,
};
let routing_engine = RoutingEngine {
aas_client,
buckets,
cache: Arc::new(RwLock::new(cache)),
redis_client,
estimates,
};

// should have USDT in bsc-mainnet > $0.5
Expand Down

0 comments on commit e769433

Please sign in to comment.