Skip to content

Commit

Permalink
Make subgraph URL's configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
narcis authored and narcis96 committed Dec 6, 2023
1 parent 02bfbbf commit ff7876c
Show file tree
Hide file tree
Showing 19 changed files with 109 additions and 32 deletions.
2 changes: 2 additions & 0 deletions crates/autopilot/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ pub async fn run(args: Arguments) {
.unwrap_or_else(|| BalancerFactoryKind::for_chain(chain_id));
let contracts = BalancerContracts::new(&web3, factories).await.unwrap();
match BalancerPoolFetcher::new(
&args.shared.graph_api_base_url,
chain_id,
block_retriever.clone(),
token_info_fetcher.clone(),
Expand Down Expand Up @@ -338,6 +339,7 @@ pub async fn run(args: Arguments) {
};
let uniswap_v3_pool_fetcher = if baseline_sources.contains(&BaselineSource::UniswapV3) {
match UniswapV3PoolFetcher::new(
&args.shared.graph_api_base_url,
chain_id,
web3.clone(),
http_factory.create(),
Expand Down
1 change: 1 addition & 0 deletions crates/driver/src/boundary/liquidity/balancer/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ async fn init_liquidity(

let balancer_pool_fetcher = Arc::new(
BalancerPoolFetcher::new(
&config.graph_api_base_url,
eth.network().chain.into(),
block_retriever.clone(),
token_info_fetcher.clone(),
Expand Down
1 change: 1 addition & 0 deletions crates/driver/src/boundary/liquidity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use {
futures::future,
itertools::Itertools,
model::TokenPair,
reqwest::Url,
shared::{
baseline_solver::BaseTokens,
current_block,
Expand Down
2 changes: 2 additions & 0 deletions crates/driver/src/boundary/liquidity/uniswap/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use {
contracts::{GPv2Settlement, UniswapV3SwapRouter},
ethrpc::current_block::BlockRetrieving,
itertools::Itertools,
reqwest::Url,
shared::{
http_solver::model::TokenAmount,
interaction::Interaction,
Expand Down Expand Up @@ -135,6 +136,7 @@ async fn init_liquidity(

let pool_fetcher = Arc::new(
UniswapV3PoolFetcher::new(
&config.graph_api_base_url,
eth.network().chain.into(),
web3.clone(),
boundary::liquidity::http_client(),
Expand Down
4 changes: 4 additions & 0 deletions crates/driver/src/infra/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ pub struct Args {
#[clap(long, env)]
pub ethrpc: Url,

/// The base URL to connect to subgraph clients.
#[clap(long, env, default_value = "https://api.thegraph.com/subgraphs/name/")]
pub graph_api_base_url: Url,

/// Path to the driver configuration file. This file should be in TOML
/// format. For an example see
/// https://github.com/cowprotocol/services/blob/main/crates/driver/example.toml.
Expand Down
4 changes: 4 additions & 0 deletions crates/driver/src/infra/config/file/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,11 @@ pub async fn load(network: &blockchain::Network, path: &Path) -> infra::Config {
file::UniswapV3Config::Manual {
router,
max_pools_to_initialize,
graph_api_base_url,
} => liquidity::config::UniswapV3 {
router: router.into(),
max_pools_to_initialize,
graph_api_base_url: graph_api_base_url.clone(),
},
})
.collect(),
Expand Down Expand Up @@ -204,6 +206,7 @@ pub async fn load(network: &blockchain::Network, path: &Path) -> infra::Config {
liquidity_bootstrapping,
composable_stable,
pool_deny_list,
graph_api_base_url,
} => liquidity::config::BalancerV2 {
vault: vault.into(),
weighted: weighted
Expand All @@ -224,6 +227,7 @@ pub async fn load(network: &blockchain::Network, path: &Path) -> infra::Config {
.map(eth::ContractAddress::from)
.collect(),
pool_deny_list: pool_deny_list.clone(),
graph_api_base_url: graph_api_base_url.clone(),
},
})
.collect(),
Expand Down
4 changes: 4 additions & 0 deletions crates/driver/src/infra/config/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,8 @@ enum UniswapV3Config {
/// How many pools to initialize during start up.
#[serde(default = "uniswap_v3::default_max_pools_to_initialize")]
max_pools_to_initialize: usize,

graph_api_base_url: Url,
},
}

Expand Down Expand Up @@ -431,6 +433,8 @@ enum BalancerV2Config {
/// Deny listed Balancer V2 pools.
#[serde(default)]
pool_deny_list: Vec<eth::H256>,

graph_api_base_url: Url,
},
}

Expand Down
15 changes: 14 additions & 1 deletion crates/driver/src/infra/liquidity/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use {
crate::{domain::eth, infra::blockchain::contracts::deployment_address},
hex_literal::hex,
reqwest::Url,
std::{collections::HashSet, time::Duration},
};

const DEFAULT_GRAPH_API_BASE_URL: &str = "https://api.thegraph.com/subgraphs/name/";

/// Configuration options for liquidity fetching.
#[derive(Clone, Debug)]
pub struct Config {
Expand Down Expand Up @@ -121,13 +124,16 @@ impl Swapr {
}

/// Uniswap V3 liquidity fetching options.
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Debug)]
pub struct UniswapV3 {
/// The address of the Uniswap V3 compatible router contract.
pub router: eth::ContractAddress,

/// How many pools should be initialized during start up.
pub max_pools_to_initialize: usize,

/// The base URL to connect to subgraph clients.
pub graph_api_base_url: Url,
}

impl UniswapV3 {
Expand All @@ -137,6 +143,8 @@ impl UniswapV3 {
Some(Self {
router: deployment_address(contracts::UniswapV3SwapRouter::raw_contract(), network)?,
max_pools_to_initialize: 100,
graph_api_base_url: Url::parse(&DEFAULT_GRAPH_API_BASE_URL)
.expect("invalid default Graph API base URL"),
})
}
}
Expand Down Expand Up @@ -168,6 +176,9 @@ pub struct BalancerV2 {
/// pools to get "bricked". This configuration allows those pools to be
/// ignored.
pub pool_deny_list: Vec<eth::H256>,

/// The base URL to connect to subgraph clients.
pub graph_api_base_url: Url,
}

impl BalancerV2 {
Expand Down Expand Up @@ -205,6 +216,8 @@ impl BalancerV2 {
contracts::BalancerV2ComposableStablePoolFactoryV5::raw_contract(),
]),
pool_deny_list: Vec::new(),
graph_api_base_url: Url::parse(&DEFAULT_GRAPH_API_BASE_URL)
.expect("invalid default Graph API base URL"),
})
}
}
1 change: 1 addition & 0 deletions crates/driver/src/infra/liquidity/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use {
domain::liquidity,
infra::{self, blockchain::Ethereum, observe},
},
reqwest::Url,
std::{collections::HashSet, sync::Arc},
};

Expand Down
1 change: 1 addition & 0 deletions crates/driver/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use {
},
clap::Parser,
futures::future::join_all,
reqwest::Url,
std::{net::SocketAddr, sync::Arc, time::Duration},
tokio::sync::oneshot,
};
Expand Down
2 changes: 2 additions & 0 deletions crates/orderbook/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ pub async fn run(args: Arguments) {
.unwrap_or_else(|| BalancerFactoryKind::for_chain(chain_id));
let contracts = BalancerContracts::new(&web3, factories).await.unwrap();
match BalancerPoolFetcher::new(
&args.shared.graph_api_base_url,
chain_id,
block_retriever.clone(),
token_info_fetcher.clone(),
Expand Down Expand Up @@ -319,6 +320,7 @@ pub async fn run(args: Arguments) {
};
let uniswap_v3_pool_fetcher = if baseline_sources.contains(&BaselineSource::UniswapV3) {
match UniswapV3PoolFetcher::new(
&args.shared.graph_api_base_url,
chain_id,
web3.clone(),
http_factory.create(),
Expand Down
5 changes: 5 additions & 0 deletions crates/shared/src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ pub struct Arguments {
#[clap(long, env, default_value = "http://localhost:8545")]
pub node_url: Url,

/// The base URL to connect to subgraph clients.
#[clap(long, env, default_value = "https://api.thegraph.com/subgraphs/name/")]
pub graph_api_base_url: Url,

/// An Ethereum node URL that supports `eth_call`s with state overrides to
/// be used for simulations.
#[clap(long, env)]
Expand Down Expand Up @@ -415,6 +419,7 @@ impl Display for Arguments {
self.logging.log_stderr_threshold
)?;
writeln!(f, "node_url: {}", self.node_url)?;
writeln!(f, "graph_api_base_url: {}", self.graph_api_base_url)?;
display_option(f, "chain_id", &self.chain_id)?;
display_option(f, "simulation_node_url", &self.simulation_node_url)?;
writeln!(f, "gas_estimators: {:?}", self.gas_estimators)?;
Expand Down
3 changes: 3 additions & 0 deletions crates/shared/src/price_estimation/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,10 @@ mod tests {
current_block_stream(Arc::new(web3.clone()), Duration::from_secs(10))
.await
.unwrap();
let base_url = Url::parse("https://api.thegraph.com/subgraphs/name/").expect("invalid url");
let balancer_pool_fetcher = Arc::new(
BalancerPoolFetcher::new(
&base_url,
chain_id,
block_retriever.clone(),
token_info.clone(),
Expand All @@ -782,6 +784,7 @@ mod tests {
);
let uniswap_v3_pool_fetcher = Arc::new(
UniswapV3PoolFetcher::new(
&base_url,
chain_id,
web3.clone(),
client.clone(),
Expand Down
11 changes: 7 additions & 4 deletions crates/shared/src/sources/balancer_v2/graph_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use {
crate::{event_handling::MAX_REORG_BLOCK_COUNT, subgraph::SubgraphClient},
anyhow::{bail, Result},
ethcontract::{H160, H256},
reqwest::Client,
reqwest::{Client, Url},
serde::Deserialize,
serde_json::json,
serde_with::{serde_as, DisplayFromStr},
Expand All @@ -34,14 +34,15 @@ pub struct BalancerSubgraphClient(SubgraphClient);

impl BalancerSubgraphClient {
/// Creates a new Balancer subgraph client for the specified chain ID.
pub fn for_chain(chain_id: u64, client: Client) -> Result<Self> {
pub fn for_chain(base_url: &Url, chain_id: u64, client: Client) -> Result<Self> {
let subgraph_name = match chain_id {
1 => "balancer-v2",
5 => "balancer-goerli-v2",
100 => "balancer-gnosis-chain-v2",
_ => bail!("unsupported chain {}", chain_id),
};
Ok(Self(SubgraphClient::new(
base_url,
"balancer-labs",
subgraph_name,
client,
Expand Down Expand Up @@ -485,8 +486,10 @@ mod tests {
async fn balancer_subgraph_query() {
for (network_name, chain_id) in [("Mainnet", 1), ("Goerli", 5)] {
println!("### {network_name}");

let client = BalancerSubgraphClient::for_chain(chain_id, Client::new()).unwrap();
let base_url =
Url::parse("https://api.thegraph.com/subgraphs/name/").expect("invalid url");
let client =
BalancerSubgraphClient::for_chain(&base_url, chain_id, Client::new()).unwrap();
let result = client.get_registered_pools().await.unwrap();
println!(
"Retrieved {} total pools at block {}",
Expand Down
11 changes: 7 additions & 4 deletions crates/shared/src/sources/balancer_v2/pool_fetching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use {
ethcontract::{dyns::DynInstance, BlockId, Instance, H160, H256},
ethrpc::current_block::{BlockRetrieving, CurrentBlockStream},
model::TokenPair,
reqwest::Client,
reqwest::{Client, Url},
std::{
collections::{BTreeMap, HashMap, HashSet},
sync::Arc,
Expand Down Expand Up @@ -279,6 +279,7 @@ impl BalancerContracts {
impl BalancerPoolFetcher {
#[allow(clippy::too_many_arguments)]
pub async fn new(
base_url: &Url,
chain_id: u64,
block_retriever: Arc<dyn BlockRetrieving>,
token_infos: Arc<dyn TokenInfoFetching>,
Expand All @@ -289,7 +290,7 @@ impl BalancerPoolFetcher {
contracts: &BalancerContracts,
deny_listed_pool_ids: Vec<H256>,
) -> Result<Self> {
let pool_initializer = BalancerSubgraphClient::for_chain(chain_id, client)?;
let pool_initializer = BalancerSubgraphClient::for_chain(base_url, chain_id, client)?;
let fetcher = Arc::new(Cache::new(
create_aggregate_pool_fetcher(
web3,
Expand Down Expand Up @@ -532,7 +533,9 @@ mod tests {
"072f14b85add63488ddad88f855fda4a99d6ac9b000200000000000000000027"
))];
// let deny_list = vec![];
let base_url = Url::parse("https://api.thegraph.com/subgraphs/name/").expect("invalid url");
let pool_fetcher = BalancerPoolFetcher::new(
&base_url,
chain_id,
Arc::new(web3.clone()),
token_info_fetcher,
Expand Down Expand Up @@ -590,9 +593,9 @@ mod tests {
),
pool_id_deny_list: Default::default(),
};

let base_url = Url::parse("https://api.thegraph.com/subgraphs/name/").expect("invalid url");
// see what the subgraph says.
let client = BalancerSubgraphClient::for_chain(chain_id, Client::new()).unwrap();
let client = BalancerSubgraphClient::for_chain(&base_url, chain_id, Client::new()).unwrap();
let subgraph_pools = client.get_registered_pools().await.unwrap();
let subgraph_token_pairs = subgraph_pools_token_pairs(&subgraph_pools.pools).collect();

Expand Down
23 changes: 16 additions & 7 deletions crates/shared/src/sources/uniswap_v3/graph_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use {
ethcontract::{H160, U256},
num::BigInt,
number::serialization::HexOrDecimalU256,
reqwest::Client,
reqwest::{Client, Url},
serde::{Deserialize, Serialize},
serde_json::{json, Map, Value},
serde_with::{serde_as, DisplayFromStr},
Expand Down Expand Up @@ -107,12 +107,17 @@ pub struct UniV3SubgraphClient(SubgraphClient);

impl UniV3SubgraphClient {
/// Creates a new Uniswap V3 subgraph client for the specified chain ID.
pub fn for_chain(chain_id: u64, client: Client) -> Result<Self> {
pub fn for_chain(base_url: &Url, chain_id: u64, client: Client) -> Result<Self> {
let subgraph_name = match chain_id {
1 => "uniswap-v3",
_ => bail!("unsupported chain {}", chain_id),
};
Ok(Self(SubgraphClient::new("uniswap", subgraph_name, client)?))
Ok(Self(SubgraphClient::new(
base_url,
"uniswap",
subgraph_name,
client,
)?))
}

async fn get_pools(&self, query: &str, variables: Map<String, Value>) -> Result<Vec<PoolData>> {
Expand Down Expand Up @@ -472,7 +477,8 @@ mod tests {
#[tokio::test]
#[ignore]
async fn get_registered_pools_test() {
let client = UniV3SubgraphClient::for_chain(1, Client::new()).unwrap();
let base_url = Url::parse("https://api.thegraph.com/subgraphs/name/").expect("invalid url");
let client = UniV3SubgraphClient::for_chain(&base_url, 1, Client::new()).unwrap();
let result = client.get_registered_pools().await.unwrap();
println!(
"Retrieved {} total pools at block {}",
Expand All @@ -484,7 +490,8 @@ mod tests {
#[tokio::test]
#[ignore]
async fn get_pools_by_pool_ids_test() {
let client = UniV3SubgraphClient::for_chain(1, Client::new()).unwrap();
let base_url = Url::parse("https://api.thegraph.com/subgraphs/name/").expect("invalid url");
let client = UniV3SubgraphClient::for_chain(&base_url, 1, Client::new()).unwrap();
let registered_pools = client.get_registered_pools().await.unwrap();
let pool_ids = registered_pools
.pools
Expand All @@ -505,7 +512,8 @@ mod tests {
#[tokio::test]
#[ignore]
async fn get_ticks_by_pools_ids_test() {
let client = UniV3SubgraphClient::for_chain(1, Client::new()).unwrap();
let base_url = Url::parse("https://api.thegraph.com/subgraphs/name/").expect("invalid url");
let client = UniV3SubgraphClient::for_chain(&base_url, 1, Client::new()).unwrap();
let block_number = client.get_safe_block().await.unwrap();
let pool_ids = vec![
H160::from_str("0x9db9e0e53058c89e5b94e29621a205198648425b").unwrap(),
Expand All @@ -521,7 +529,8 @@ mod tests {
#[tokio::test]
#[ignore]
async fn get_pools_with_ticks_by_ids_test() {
let client = UniV3SubgraphClient::for_chain(1, Client::new()).unwrap();
let base_url = Url::parse("https://api.thegraph.com/subgraphs/name/").expect("invalid url");
let client = UniV3SubgraphClient::for_chain(&base_url, 1, Client::new()).unwrap();
let block_number = client.get_safe_block().await.unwrap();
let pool_ids = vec![
H160::from_str("0x9db9e0e53058c89e5b94e29621a205198648425b").unwrap(),
Expand Down
Loading

0 comments on commit ff7876c

Please sign in to comment.