Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/driver/example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ max-order-age = "1m"
# [[liquidity.balancer-v2]] # Balancer V2 configuration
# preset = "balancer-v2"
# graph-url = "http://localhost:1234" # which subgraph url to fetch the data from
# api-key = "<goldsky_api_key>" # optional bearer token for private subgraph endpoints (e.g. Goldsky /api/private/...)
# pool-deny-list = [] # optional

# [[liquidity.balancer-v2]] # Custom Balancer V2 configuration
Expand All @@ -94,6 +95,7 @@ max-order-age = "1m"
# [[liquidity.uniswap-v3]] # Uniswap V3 configuration
# preset = "uniswap-v3"
# graph-url = "http://localhost:1234" # which subgraph url to fetch the data from
# api-key = "<goldsky_api_key>" # optional bearer token for private subgraph endpoints (e.g. Goldsky /api/private/...)
# max_pools_to_initialize = 100 # how many of the deepest pools to initialise on startup

# [[liquidity.uniswap-v3]] # Custom Uniswap V3 configuration
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 @@ -177,6 +177,7 @@ async fn init_liquidity(
boundary::liquidity::cache_config(),
block_stream.clone(),
boundary::liquidity::http_client(),
config.api_key.clone(),
web3.clone(),
&contracts,
config.pool_deny_list.to_vec(),
Expand Down
1 change: 1 addition & 0 deletions crates/driver/src/boundary/liquidity/uniswap/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ async fn init_liquidity(
&config.graph_url,
web3.clone(),
boundary::liquidity::http_client(),
config.api_key.clone(),
block_retriever,
config.max_pools_to_initialize,
config.max_pools_per_tick_query,
Expand Down
8 changes: 8 additions & 0 deletions crates/driver/src/infra/config/file/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,12 @@ pub async fn load(chain: Chain, path: &Path) -> infra::Config {
preset,
max_pools_to_initialize,
graph_url,
api_key,
reinit_interval,
max_pools_per_tick_query,
} => liquidity::config::UniswapV3 {
max_pools_to_initialize,
api_key,
reinit_interval,
..match preset {
file::UniswapV3Preset::UniswapV3 => {
Expand All @@ -244,12 +246,14 @@ pub async fn load(chain: Chain, path: &Path) -> infra::Config {
router,
max_pools_to_initialize,
graph_url,
api_key,
reinit_interval,
max_pools_per_tick_query,
} => liquidity::config::UniswapV3 {
router: router.into(),
max_pools_to_initialize,
graph_url,
api_key,
reinit_interval,
max_pools_per_tick_query,
},
Expand All @@ -265,9 +269,11 @@ pub async fn load(chain: Chain, path: &Path) -> infra::Config {
preset,
pool_deny_list,
graph_url,
api_key,
reinit_interval,
} => liquidity::config::BalancerV2 {
pool_deny_list: pool_deny_list.clone(),
api_key,
reinit_interval,
..match preset {
file::BalancerV2Preset::BalancerV2 => {
Expand All @@ -285,6 +291,7 @@ pub async fn load(chain: Chain, path: &Path) -> infra::Config {
composable_stable,
pool_deny_list,
graph_url,
api_key,
reinit_interval,
} => liquidity::config::BalancerV2 {
vault: vault.into(),
Expand All @@ -295,6 +302,7 @@ pub async fn load(chain: Chain, path: &Path) -> infra::Config {
composable_stable,
pool_deny_list: pool_deny_list.clone(),
graph_url,
api_key,
reinit_interval,
},
})
Expand Down
24 changes: 24 additions & 0 deletions crates/driver/src/infra/config/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,12 @@ enum UniswapV3Config {

graph_url: Url,

/// Optional bearer token used to access private subgraph endpoints
/// (e.g. Goldsky `/api/private/...`). When set, requests carry the
/// `Authorization: Bearer <api-key>` header.
#[serde(default)]
api_key: Option<String>,

/// How many pool IDs can be present in a where clause of a Tick query
/// at once. Some subgraphs are overloaded and throw errors when
/// there are too many.
Expand Down Expand Up @@ -561,6 +567,12 @@ enum UniswapV3Config {
/// The URL used to connect to uniswap v3 subgraph client.
graph_url: Url,

/// Optional bearer token used to access private subgraph endpoints
/// (e.g. Goldsky `/api/private/...`). When set, requests carry the
/// `Authorization: Bearer <api-key>` header.
#[serde(default)]
api_key: Option<String>,

/// How often the liquidity source should be reinitialized to get
/// access to new pools.
#[serde(with = "humantime_serde", default = "default_reinit_interval")]
Expand Down Expand Up @@ -598,6 +610,12 @@ enum BalancerV2Config {
/// The URL used to connect to balancer v2 subgraph client.
graph_url: Url,

/// Optional bearer token used to access private subgraph endpoints
/// (e.g. Goldsky `/api/private/...`). When set, requests carry the
/// `Authorization: Bearer <api-key>` header.
#[serde(default)]
api_key: Option<String>,

/// How often the liquidity source should be reinitialized to get
/// access to new pools.
#[serde(with = "humantime_serde", default = "default_reinit_interval")]
Expand Down Expand Up @@ -639,6 +657,12 @@ enum BalancerV2Config {
/// The URL used to connect to balancer v2 subgraph client.
graph_url: Url,

/// Optional bearer token used to access private subgraph endpoints
/// (e.g. Goldsky `/api/private/...`). When set, requests carry the
/// `Authorization: Bearer <api-key>` header.
#[serde(default)]
api_key: Option<String>,

/// How often the liquidity source should be reinitialized to get
/// access to new pools.
#[serde(with = "humantime_serde", default = "default_reinit_interval")]
Expand Down
14 changes: 14 additions & 0 deletions crates/driver/src/infra/liquidity/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ pub struct UniswapV3 {
/// The URL used to connect to uniswap v3 subgraph client.
pub graph_url: Url,

/// Optional bearer token sent as `Authorization: Bearer <api_key>` with
/// every subgraph request. Used to access private subgraph endpoints
/// (e.g. Goldsky `/api/private/...`).
#[debug(ignore)]
pub api_key: Option<String>,

/// How often the liquidity source should be reinitialized to
/// become aware of new pools.
pub reinit_interval: Option<Duration>,
Expand All @@ -192,6 +198,7 @@ impl UniswapV3 {
router: contracts::UniswapV3SwapRouterV2::deployment_address(&chain.id())?.into(),
max_pools_to_initialize: 100,
graph_url: graph_url.clone(),
api_key: None,
reinit_interval: None,
max_pools_per_tick_query,
})
Expand Down Expand Up @@ -229,6 +236,12 @@ pub struct BalancerV2 {
/// The base URL used to connect to balancer v2 subgraph client.
pub graph_url: Url,

/// Optional bearer token sent as `Authorization: Bearer <api_key>` with
/// every subgraph request. Used to access private subgraph endpoints
/// (e.g. Goldsky `/api/private/...`).
#[debug(ignore)]
pub api_key: Option<String>,

/// How often the liquidty source should be re-initialized to become
/// aware of new pools.
pub reinit_interval: Option<Duration>,
Expand Down Expand Up @@ -285,6 +298,7 @@ impl BalancerV2 {
),
pool_deny_list: Vec::new(),
graph_url: graph_url.clone(),
api_key: None,
reinit_interval: None,
})
}
Expand Down
7 changes: 6 additions & 1 deletion crates/liquidity-sources/src/balancer_v2/graph_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ pub struct BalancerSubgraphClient(SubgraphClient);

impl BalancerSubgraphClient {
/// Creates a new Balancer subgraph client with full subgraph URL.
pub fn from_subgraph_url(subgraph_url: &Url, client: Client) -> Result<Self> {
pub fn from_subgraph_url(
subgraph_url: &Url,
client: Client,
api_key: Option<String>,
) -> Result<Self> {
Ok(Self(SubgraphClient::try_new(
subgraph_url.clone(),
client,
api_key,
usize::MAX,
)?))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,13 @@ impl BalancerPoolFetcher {
config: CacheConfig,
block_stream: CurrentBlockWatcher,
client: Client,
api_key: Option<String>,
web3: Web3,
contracts: &BalancerContracts,
deny_listed_pool_ids: Vec<B256>,
) -> Result<Self> {
let pool_initializer = BalancerSubgraphClient::from_subgraph_url(subgraph_url, client)?;
let pool_initializer =
BalancerSubgraphClient::from_subgraph_url(subgraph_url, client, api_key)?;
let web3 = web3.labeled("balancerV2");
let fetcher = Arc::new(Cache::new(
create_aggregate_pool_fetcher(
Expand Down
19 changes: 16 additions & 3 deletions crates/liquidity-sources/src/subgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ const MAX_NUMBER_OF_ATTEMPTS_DEFAULT: usize = 10;
pub struct SubgraphClient {
client: Client,
subgraph_url: Url,
/// Optional bearer token used to authenticate requests against private
/// subgraph endpoints (e.g. Goldsky private endpoints under
/// `/api/private/...`). When set, it is sent as the `Authorization:
/// Bearer <token>` header on every request.
api_key: Option<String>,
max_pools_per_tick_query: usize,
max_number_of_attempts: usize,
}
Expand All @@ -32,14 +37,20 @@ pub struct Data<T> {

impl SubgraphClient {
/// Creates a new subgraph client from the specified organization and name.
///
/// If `api_key` is provided, every request sent by this client will carry
/// an `Authorization: Bearer <api_key>` header so that private subgraph
/// endpoints (e.g. Goldsky private endpoints) can be used.
pub fn try_new(
subgraph_url: Url,
client: Client,
api_key: Option<String>,
max_pools_per_tick_query: usize,
) -> Result<Self> {
Ok(Self {
client,
subgraph_url,
api_key,
max_pools_per_tick_query,
max_number_of_attempts: MAX_NUMBER_OF_ATTEMPTS_DEFAULT,
})
Expand Down Expand Up @@ -74,9 +85,11 @@ impl SubgraphClient {
where
T: DeserializeOwned,
{
match self
.client
.post(self.subgraph_url.clone())
let mut request = self.client.post(self.subgraph_url.clone());
if let Some(api_key) = &self.api_key {
request = request.bearer_auth(api_key);
}
match request
.json(&Query {
query,
variables: variables.clone(),
Expand Down
9 changes: 7 additions & 2 deletions crates/liquidity-sources/src/uniswap_v3/graph_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,15 @@ impl UniV3SubgraphClient {
pub async fn from_subgraph_url(
subgraph_url: &Url,
client: Client,
api_key: Option<String>,
max_pools_per_tick_query: usize,
) -> Result<Self> {
let subgraph_client =
SubgraphClient::try_new(subgraph_url.clone(), client, max_pools_per_tick_query)?;
let subgraph_client = SubgraphClient::try_new(
subgraph_url.clone(),
client,
api_key,
max_pools_per_tick_query,
)?;

Ok(Self {
client: subgraph_client,
Expand Down
13 changes: 10 additions & 3 deletions crates/liquidity-sources/src/uniswap_v3/pool_fetching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,17 @@ impl PoolsCheckpointHandler {
pub async fn new(
subgraph_url: &Url,
client: Client,
api_key: Option<String>,
max_pools_to_initialize_cache: usize,
max_pools_per_tick_query: usize,
) -> Result<Self> {
let graph_api =
UniV3SubgraphClient::from_subgraph_url(subgraph_url, client, max_pools_per_tick_query)
.await?;
let graph_api = UniV3SubgraphClient::from_subgraph_url(
subgraph_url,
client,
api_key,
max_pools_per_tick_query,
)
.await?;
let mut registered_pools = graph_api.get_registered_pools().await?;
tracing::debug!(
block = %registered_pools.fetched_block_number, pools = %registered_pools.pools.len(),
Expand Down Expand Up @@ -285,6 +290,7 @@ impl UniswapV3PoolFetcher {
subgraph_url: &Url,
web3: Web3,
client: Client,
api_key: Option<String>,
block_retriever: Arc<dyn BlockRetrieving>,
max_pools_to_initialize: usize,
max_pools_per_tick_query: usize,
Expand All @@ -293,6 +299,7 @@ impl UniswapV3PoolFetcher {
let checkpoint = PoolsCheckpointHandler::new(
subgraph_url,
client,
api_key,
max_pools_to_initialize,
max_pools_per_tick_query,
)
Expand Down
Loading