Skip to content

Commit

Permalink
Merge branch 'feat/settlement-engine' of https://github.com/bcnmy/reflux
Browse files Browse the repository at this point in the history
 into feat/settlement-engine
  • Loading branch information
AmanRaj1608 committed Jul 4, 2024
2 parents 806ba18 + 5202174 commit 554c196
Show file tree
Hide file tree
Showing 17 changed files with 280 additions and 245 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## reflux

Backend of solver which helps in seamless cross-chain asset consolidation. It aggregates user balances, automates routing, and suggests optimal transactions.
Backend of solver which helps in seamless cross-chain asset consolidation. It aggregates user balances, automates
routing, and suggests optimal transactions.

#### Installation

Expand All @@ -18,4 +19,4 @@ Once build is copleted, just run the server and test with the endpoints

### Dependencies graph

![image](./graph.png)
![image](./assets/dependency-graph.png)
File renamed without changes
12 changes: 6 additions & 6 deletions bin/reflux/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use tower_http::cors::{Any, CorsLayer};
use account_aggregation::service::AccountAggregationService;
use api::service_controller::ServiceController;
use config::Config;
use routing_engine::engine::RoutingEngine;
use routing_engine::estimator::LinearRegressionEstimator;
use routing_engine::{BungeeClient, CoingeckoClient, Indexer};
use storage::mongodb_client::MongoDBClient;
use routing_engine::estimator::LinearRegressionEstimator;
use routing_engine::routing_engine::RoutingEngine;
use storage::{ControlFlow, MessageQueue, RedisClient};
use storage::mongodb_client::MongoDBClient;

#[derive(Parser, Debug)]
struct Args {
Expand Down Expand Up @@ -184,9 +184,9 @@ async fn run_indexer(config: Config) {
.expect("Failed to Instantiate Bungee Client");

let token_price_provider = CoingeckoClient::new(
&config.coingecko.base_url,
&config.coingecko.api_key,
&redis_provider,
config.coingecko.base_url.clone(),
config.coingecko.api_key.clone(),
redis_provider.clone(),
Duration::from_secs(config.coingecko.expiry_sec),
);

Expand Down
13 changes: 6 additions & 7 deletions crates/account-aggregation/src/service.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use futures::future::join_all;
use log::debug;
use std::sync::Arc;
use thiserror::Error;

use derive_more::Display;
use mongodb::bson;
use reqwest::Client as ReqwestClient;
use thiserror::Error;
use uuid::Uuid;

use storage::mongodb_client::{DBError, MongoDBClient};
use storage::DBProvider;
use storage::mongodb_client::{DBError, MongoDBClient};

use crate::types::{
Account, AddAccountPayload, CovalentApiResponse, ExtractedBalance, RegisterAccountPayload,
Account, AddAccountPayload, CovalentApiResponse, TokenWithBalance, RegisterAccountPayload,
User, UserAccountMapping, UserAccountMappingQuery, UserQuery,
};

Expand Down Expand Up @@ -246,7 +246,7 @@ impl AccountAggregationService {
pub async fn get_user_accounts_balance(
&self,
account: &String,
) -> Result<Vec<ExtractedBalance>, AccountAggregationError> {
) -> Result<Vec<TokenWithBalance>, AccountAggregationError> {
let mut accounts: Vec<String> = Vec::new();
let user_id = self.get_user_id(account).await.unwrap_or(None);
if let Some(user_id) = user_id {
Expand All @@ -268,7 +268,6 @@ impl AccountAggregationService {
"{}/v1/{}/address/{}/balances_v2/?key={}",
self.covalent_base_url, network, user, self.covalent_api_key
);
debug!("Fetching balance from: {}", url);
let client = self.client.clone();
async move {
let response = client.get(&url).send().await;
Expand Down Expand Up @@ -306,7 +305,7 @@ impl AccountAggregationService {
/// Extract balance data from the API response
fn extract_balance_data(
api_response: CovalentApiResponse,
) -> Result<Vec<ExtractedBalance>, AccountAggregationError> {
) -> Result<Vec<TokenWithBalance>, AccountAggregationError> {
let chain_id = api_response.data.chain_id.to_string();
let results = api_response
.data
Expand All @@ -328,7 +327,7 @@ fn extract_balance_data(
} else {
let balance = balance_raw / 10f64.powf(item.contract_decimals.unwrap() as f64);

Some(ExtractedBalance {
Some(TokenWithBalance {
token: token.clone(),
token_address: item.contract_ticker_symbol.clone().unwrap(),
chain_id: chain_id.clone().parse::<u32>().unwrap(),
Expand Down
4 changes: 2 additions & 2 deletions crates/account-aggregation/src/service_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use async_trait::async_trait;

use storage::mongodb_client::MongoDBClient;

use crate::types::{Account, AddAccountPayload, ExtractedBalance, RegisterAccountPayload};
use crate::types::{Account, AddAccountPayload, RegisterAccountPayload, TokenWithBalance};

#[async_trait]
pub trait AccountAggregationServiceTrait {
Expand All @@ -21,5 +21,5 @@ pub trait AccountAggregationServiceTrait {
account_payload: RegisterAccountPayload,
) -> Result<(), Box<dyn Error>>;
fn add_account(&self, account_payload: AddAccountPayload) -> Result<(), Box<dyn Error>>;
fn get_user_accounts_balance(&self, account: &String) -> Vec<ExtractedBalance>;
fn get_user_accounts_balance(&self, account: &String) -> Vec<TokenWithBalance>;
}
2 changes: 1 addition & 1 deletion crates/account-aggregation/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct CovalentTokenData {
}

#[derive(Deserialize, Serialize, Debug)]
pub struct ExtractedBalance {
pub struct TokenWithBalance {
pub token: String,
pub token_address: String,
pub chain_id: u32,
Expand Down
13 changes: 8 additions & 5 deletions crates/api/src/service_controller.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use account_aggregation::{service::AccountAggregationService, types};
use axum::{extract::Query, http::StatusCode, response::IntoResponse, routing::get, Json, Router};
use routing_engine::engine::RoutingEngine;
use std::collections::HashMap;
use std::sync::Arc;

use axum::{extract::Query, http::StatusCode, Json, response::IntoResponse, Router, routing::get};
use serde_json::json;
use std::{collections::HashMap, sync::Arc};

use account_aggregation::{service::AccountAggregationService, types};
use routing_engine::routing_engine::RoutingEngine;

pub struct ServiceController {
account_service: Arc<AccountAggregationService>,
Expand Down Expand Up @@ -224,7 +227,7 @@ impl ServiceController {
}

match routing_engine
.get_best_cost_path(&query.account, query.to_chain, &query.to_token, query.to_value)
.get_best_cost_paths(&query.account, query.to_chain, &query.to_token, query.to_value)
.await
{
Ok(routes) => {
Expand Down
15 changes: 12 additions & 3 deletions crates/routing-engine/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ pub enum BuildEstimatorError<'config, 'est_de, Estimator: estimator::Estimator<'

#[cfg(test)]
mod tests {
use std::collections::HashMap;
use std::env;
use std::fmt::Error;
use std::time::Duration;
Expand All @@ -312,7 +313,7 @@ mod tests {
use thiserror::Error;

use config::{Config, get_sample_config};
use storage::{ControlFlow, KeyValueStore, MessageQueue, Msg};
use storage::{ControlFlow, KeyValueStore, MessageQueue, Msg, RedisClientError};

use crate::{BungeeClient, CostType};
use crate::estimator::{Estimator, LinearRegressionEstimator};
Expand All @@ -336,11 +337,19 @@ mod tests {
}

async fn set(&self, _: &String, _: &String, _: Duration) -> Result<(), Self::Error> {
Ok(())
todo!()
}

async fn set_multiple(&self, _: &Vec<(String, String)>) -> Result<(), Self::Error> {
Ok(())
todo!()
}

async fn get_all_keys(&self) -> Result<Vec<String>, RedisClientError> {
todo!()
}

async fn get_all_key_values(&self) -> Result<HashMap<String, String>, RedisClientError> {
todo!()
}
}

Expand Down
24 changes: 15 additions & 9 deletions crates/routing-engine/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use derive_more::Display;
use serde::{ser::SerializeStruct, Serialize};
use thiserror::Error;

use config::config::{BucketConfig, ChainConfig, Config, TokenConfig};
pub use indexer::Indexer;
pub use source::bungee::BungeeClient;
pub use token_price::CoingeckoClient;

pub mod engine;
pub mod routing_engine;
pub mod token_price;

pub mod estimator;
pub mod indexer;
mod settlement_engine;
mod source;

#[derive(Debug, Error, Display)]
Expand All @@ -21,12 +21,11 @@ pub enum CostType {
}

#[derive(Debug)]
pub struct Route<'a> {
from_chain: &'a ChainConfig,
to_chain: &'a ChainConfig,
from_token: &'a TokenConfig,
to_token: &'a TokenConfig,
amount_in_usd: f64,
pub struct Route<'config> {
from_chain: &'config ChainConfig,
to_chain: &'config ChainConfig,
from_token: &'config TokenConfig,
to_token: &'config TokenConfig,
is_smart_contract_deposit: bool,
}

Expand Down Expand Up @@ -57,7 +56,6 @@ impl<'a> Route<'a> {
to_chain: to_chain.unwrap(),
from_token: from_token.unwrap(),
to_token: to_token.unwrap(),
amount_in_usd: bucket.token_amount_from_usd,
is_smart_contract_deposit: bucket.is_smart_contract_deposit_supported,
})
}
Expand All @@ -71,3 +69,11 @@ pub enum RouteError {
#[error("Token not found while building route: {}", _0)]
TokenNotFoundError(String),
}

#[derive(Debug)]
pub struct BridgeResult<'config> {
route: Route<'config>,
source_amount_in_usd: f64,
from_address: String,
to_address: String,
}
Loading

0 comments on commit 554c196

Please sign in to comment.