Skip to content

Commit

Permalink
Merge pull request #228 from 0xDmtri/main
Browse files Browse the repository at this point in the history
chore(provider) + bugfix(UniV3)
  • Loading branch information
0xOsiris authored Nov 3, 2024
2 parents adf8589 + 7fd3e96 commit 791fac3
Show file tree
Hide file tree
Showing 26 changed files with 203 additions and 271 deletions.
4 changes: 1 addition & 3 deletions examples/discover-erc-4626-vaults.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Arc;

use alloy::providers::ProviderBuilder;

use amms::discovery;
Expand All @@ -10,7 +8,7 @@ async fn main() -> eyre::Result<()> {

// Add rpc endpoint here:
let rpc_endpoint = std::env::var("ETHEREUM_RPC_ENDPOINT")?;
let provider = Arc::new(ProviderBuilder::new().on_http(rpc_endpoint.parse()?));
let provider = ProviderBuilder::new().on_http(rpc_endpoint.parse()?);

// discover vaults
let vaults = discovery::erc_4626::discover_erc_4626_vaults(provider, 30000).await?;
Expand Down
4 changes: 1 addition & 3 deletions examples/discover-factories.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Arc;

use alloy::providers::ProviderBuilder;

use amms::discovery::factory::{discover_factories, DiscoverableFactory};
Expand All @@ -9,7 +7,7 @@ async fn main() -> eyre::Result<()> {
tracing_subscriber::fmt::init();

let rpc_endpoint = std::env::var("ETHEREUM_RPC_ENDPOINT")?;
let provider = Arc::new(ProviderBuilder::new().on_http(rpc_endpoint.parse()?));
let provider = ProviderBuilder::new().on_http(rpc_endpoint.parse()?);

// Find all UniswapV2 and UniswapV3 compatible factories and filter out matches with less than 1000 AMMs
let number_of_amms_threshold = 1000;
Expand Down
4 changes: 1 addition & 3 deletions examples/filter-value.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Arc;

use alloy::{
primitives::{address, U256},
providers::ProviderBuilder,
Expand All @@ -19,7 +17,7 @@ async fn main() -> eyre::Result<()> {
tracing_subscriber::fmt::init();

let rpc_endpoint = std::env::var("ETHEREUM_RPC_ENDPOINT")?;
let provider = Arc::new(ProviderBuilder::new().on_http(rpc_endpoint.parse()?));
let provider = ProviderBuilder::new().on_http(rpc_endpoint.parse()?);

// Initialize factories
let factories = vec![
Expand Down
4 changes: 1 addition & 3 deletions examples/simulate-swap.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Arc;

use alloy::{
primitives::{address, Address, U256},
providers::ProviderBuilder,
Expand All @@ -12,7 +10,7 @@ async fn main() -> eyre::Result<()> {
tracing_subscriber::fmt::init();

let rpc_endpoint = std::env::var("ETHEREUM_RPC_ENDPOINT")?;
let provider = Arc::new(ProviderBuilder::new().on_http(rpc_endpoint.parse()?));
let provider = ProviderBuilder::new().on_http(rpc_endpoint.parse()?);

// Initialize the pool
let pool_address = address!("B4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc"); // WETH/USDC
Expand Down
4 changes: 1 addition & 3 deletions examples/state-space.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Arc;

use alloy::{primitives::address, providers::ProviderBuilder, rpc::client::WsConnect};

use amms::{
Expand All @@ -17,7 +15,7 @@ async fn main() -> eyre::Result<()> {

// Initialize WS provider
let ws = WsConnect::new(ws_endpoint);
let provider = Arc::new(ProviderBuilder::new().on_ws(ws).await?);
let provider = ProviderBuilder::new().on_ws(ws).await?;

// Initialize factories
let factories = vec![
Expand Down
4 changes: 1 addition & 3 deletions examples/swap-calldata.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Arc;

use alloy::{
primitives::{address, U256},
providers::ProviderBuilder,
Expand All @@ -12,7 +10,7 @@ async fn main() -> eyre::Result<()> {
tracing_subscriber::fmt::init();

let rpc_endpoint = std::env::var("ETHEREUM_RPC_ENDPOINT")?;
let provider = Arc::new(ProviderBuilder::new().on_http(rpc_endpoint.parse()?));
let provider = ProviderBuilder::new().on_http(rpc_endpoint.parse()?);

// Initialize the pool
let pool_address = address!("B4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc");
Expand Down
4 changes: 1 addition & 3 deletions examples/sync-amms.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Arc;

use alloy::{primitives::address, providers::ProviderBuilder};

use amms::{
Expand All @@ -16,7 +14,7 @@ async fn main() -> eyre::Result<()> {

// Add rpc endpoint here:
let rpc_endpoint = std::env::var("ETHEREUM_RPC_ENDPOINT")?;
let provider = Arc::new(ProviderBuilder::new().on_http(rpc_endpoint.parse()?));
let provider = ProviderBuilder::new().on_http(rpc_endpoint.parse()?);

let factories = vec![
// Add UniswapV2
Expand Down
10 changes: 4 additions & 6 deletions src/amm/balancer_v2/batch_request/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Arc;

use alloy::{
network::Network,
primitives::{Address, U256},
Expand All @@ -26,12 +24,12 @@ sol! {
pub async fn get_balancer_v2_pool_data_batch_request<T, N, P>(
pool: &mut BalancerV2Pool,
block_number: Option<u64>,
provider: Arc<P>,
provider: P,
) -> Result<(), AMMError>
where
T: Transport + Clone,
N: Network,
P: Provider<T, N>,
P: Provider<T, N> + Clone,
{
let deployer = IGetBalancerV2PoolDataBatchRequest::deploy_builder(provider, vec![pool.address]);
let res = if let Some(block_number) = block_number {
Expand Down Expand Up @@ -63,12 +61,12 @@ where

pub async fn get_amm_data_batch_request<T, N, P>(
amms: &mut [AMM],
provider: Arc<P>,
provider: P,
) -> Result<(), AMMError>
where
T: Transport + Clone,
N: Network,
P: Provider<T, N>,
P: Provider<T, N> + Clone,
{
let deployer = IGetBalancerV2PoolDataBatchRequest::deploy_builder(
provider,
Expand Down
18 changes: 8 additions & 10 deletions src/amm/balancer_v2/factory.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Arc;

use alloy::{
network::Network,
primitives::{Address, B256},
Expand Down Expand Up @@ -50,13 +48,13 @@ impl AutomatedMarketMakerFactory for BalancerV2Factory {
async fn get_all_amms<T, N, P>(
&self,
to_block: Option<u64>,
provider: Arc<P>,
provider: P,
step: u64,
) -> Result<Vec<AMM>, AMMError>
where
T: Transport + Clone,
N: Network,
P: Provider<T, N>,
P: Provider<T, N> + Clone,
{
let to = to_block.unwrap_or(provider.get_block_number().await?);
Ok(self.get_all_pools_from_logs(to, step, provider).await?)
Expand All @@ -67,12 +65,12 @@ impl AutomatedMarketMakerFactory for BalancerV2Factory {
&self,
amms: &mut [AMM],
_block_number: Option<u64>,
provider: Arc<P>,
provider: P,
) -> Result<(), AMMError>
where
T: Transport + Clone,
N: Network,
P: Provider<T, N>,
P: Provider<T, N> + Clone,
{
// Max batch size for call
let step = 127;
Expand All @@ -95,11 +93,11 @@ impl AutomatedMarketMakerFactory for BalancerV2Factory {
/// Creates a new AMM from a log factory creation event.
///
/// Returns a AMM with data populated.
async fn new_amm_from_log<T, N, P>(&self, log: Log, provider: Arc<P>) -> Result<AMM, AMMError>
async fn new_amm_from_log<T, N, P>(&self, log: Log, provider: P) -> Result<AMM, AMMError>
where
T: Transport + Clone,
N: Network,
P: Provider<T, N>,
P: Provider<T, N> + Clone,
{
let mut pool = self.new_empty_amm_from_log(log)?;
pool.populate_data(None, provider).await?;
Expand All @@ -123,12 +121,12 @@ impl BalancerV2Factory {
self,
to_block: u64,
step: u64,
provider: Arc<P>,
provider: P,
) -> Result<Vec<AMM>, AMMError>
where
T: Transport + Clone,
N: Network,
P: Provider<T, N>,
P: Provider<T, N> + Clone,
{
// Unwrap can be used here because the creation block was verified within `Dex::new()`
let mut from_block = self.creation_block;
Expand Down
12 changes: 5 additions & 7 deletions src/amm/balancer_v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ pub mod bmath;
pub mod error;
pub mod factory;

use std::sync::Arc;

use alloy::{
network::Network,
primitives::{Address, B256, U256},
Expand Down Expand Up @@ -78,11 +76,11 @@ impl AutomatedMarketMaker for BalancerV2Pool {

/// Syncs the AMM data on chain via batched static calls.
#[instrument(skip(self, provider), level = "debug")]
async fn sync<T, N, P>(&mut self, provider: Arc<P>) -> Result<(), AMMError>
async fn sync<T, N, P>(&mut self, provider: P) -> Result<(), AMMError>
where
T: Transport + Clone,
N: Network,
P: Provider<T, N>,
P: Provider<T, N> + Clone,
{
// Tokens can change, so we are saving a request here.
self.populate_data(None, provider).await
Expand Down Expand Up @@ -168,15 +166,15 @@ impl AutomatedMarketMaker for BalancerV2Pool {
async fn populate_data<T, N, P>(
&mut self,
block_number: Option<u64>,
middleware: Arc<P>,
provider: P,
) -> Result<(), AMMError>
where
T: Transport + Clone,
N: Network,
P: Provider<T, N>,
P: Provider<T, N> + Clone,
{
Ok(
batch_request::get_balancer_v2_pool_data_batch_request(self, block_number, middleware)
batch_request::get_balancer_v2_pool_data_batch_request(self, block_number, provider)
.await?,
)
}
Expand Down
6 changes: 2 additions & 4 deletions src/amm/erc_4626/batch_request/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Arc;

use crate::{amm::AutomatedMarketMaker, errors::AMMError};

use alloy::{
Expand All @@ -22,12 +20,12 @@ sol! {

pub async fn get_4626_vault_data_batch_request<T, N, P>(
vault: &mut ERC4626Vault,
provider: Arc<P>,
provider: P,
) -> Result<(), AMMError>
where
T: Transport + Clone,
N: Network,
P: Provider<T, N>,
P: Provider<T, N> + Clone,
{
let deployer =
IGetERC4626VaultDataBatchRequest::deploy_builder(provider, vec![vault.vault_token]);
Expand Down
18 changes: 9 additions & 9 deletions src/amm/erc_4626/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod batch_request;

use std::{cmp::Ordering, sync::Arc};
use std::cmp::Ordering;

use alloy::{
network::Network,
Expand Down Expand Up @@ -68,11 +68,11 @@ impl AutomatedMarketMaker for ERC4626Vault {
}

#[instrument(skip(self, provider), level = "debug")]
async fn sync<T, N, P>(&mut self, provider: Arc<P>) -> Result<(), AMMError>
async fn sync<T, N, P>(&mut self, provider: P) -> Result<(), AMMError>
where
T: Transport + Clone,
N: Network,
P: Provider<T, N>,
P: Provider<T, N> + Clone,
{
let (vault_reserve, asset_reserve) = self.get_reserves(provider).await?;
tracing::debug!(vault_reserve = ?vault_reserve, asset_reserve = ?asset_reserve, address = ?self.vault_token, "ER4626 sync");
Expand Down Expand Up @@ -108,12 +108,12 @@ impl AutomatedMarketMaker for ERC4626Vault {
async fn populate_data<T, N, P>(
&mut self,
_block_number: Option<u64>,
provider: Arc<P>,
provider: P,
) -> Result<(), AMMError>
where
T: Transport + Clone,
N: Network,
P: Provider<T, N>,
P: Provider<T, N> + Clone,
{
batch_request::get_4626_vault_data_batch_request(self, provider.clone()).await?;

Expand Down Expand Up @@ -183,12 +183,12 @@ impl ERC4626Vault {

pub async fn new_from_address<T, N, P>(
vault_token: Address,
provider: Arc<P>,
provider: P,
) -> Result<Self, AMMError>
where
T: Transport + Clone,
N: Network,
P: Provider<T, N>,
P: Provider<T, N> + Clone,
{
let mut vault = ERC4626Vault {
vault_token,
Expand Down Expand Up @@ -217,11 +217,11 @@ impl ERC4626Vault {
|| self.asset_reserve.is_zero())
}

pub async fn get_reserves<T, N, P>(&self, provider: Arc<P>) -> Result<(U256, U256), AMMError>
pub async fn get_reserves<T, N, P>(&self, provider: P) -> Result<(U256, U256), AMMError>
where
T: Transport + Clone,
N: Network,
P: Provider<T, N>,
P: Provider<T, N> + Clone,
{
// Initialize a new instance of the vault
let vault = IERC4626Vault::new(self.vault_token, provider);
Expand Down
Loading

0 comments on commit 791fac3

Please sign in to comment.