Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: v2 + v3 factory address tracked for better discovery debugging / analysis #215

Merged
merged 3 commits into from
Oct 12, 2024
Merged
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
1 change: 1 addition & 0 deletions benches/uniswapv2_simulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ fn criterion_benchmark(c: &mut Criterion) {

let mut pool = UniswapV2Pool {
address: address!("ddF8390cEd9fAD414b1ca1DD4Fe14F881C2Cfa70"),
factory_address: None,
token_a,
token_a_decimals: 18,
token_b: address!("fc0d6cf33e38bce7ca7d89c0e292274031b7157a"),
Expand Down
2 changes: 1 addition & 1 deletion examples/filter-value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async fn main() -> eyre::Result<()> {
let usdc = address!("3c499c542cEF5E3811e1192ce70d8cC03d5c3359");
let usd_weth_pair_address = address!("cd353F79d9FADe311fC3119B841e1f456b54e858");
let usd_weth_pool = AMM::UniswapV2Pool(
UniswapV2Pool::new_from_address(usd_weth_pair_address, 300, provider.clone()).await?,
UniswapV2Pool::new_from_address(usd_weth_pair_address, None, 300, provider.clone()).await?,
);
let weth_value_in_token_to_weth_pool_threshold = U256::from(100000000000000000_u128); // 10 weth

Expand Down
2 changes: 1 addition & 1 deletion examples/simulate-swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async fn main() -> eyre::Result<()> {

// Initialize the pool
let pool_address = address!("B4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc"); // WETH/USDC
let pool = UniswapV2Pool::new_from_address(pool_address, 300, provider.clone()).await?;
let pool = UniswapV2Pool::new_from_address(pool_address, None, 300, provider.clone()).await?;

// Simulate a swap
let token_in = address!("C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");
Expand Down
2 changes: 1 addition & 1 deletion examples/swap-calldata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async fn main() -> eyre::Result<()> {

// Initialize the pool
let pool_address = address!("B4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc");
let pool = UniswapV2Pool::new_from_address(pool_address, 300, provider.clone()).await?;
let pool = UniswapV2Pool::new_from_address(pool_address, None, 300, provider.clone()).await?;

// Generate the swap calldata
let to_address = address!("DecafC0ffee15BadDecafC0ffee15BadDecafC0f");
Expand Down
3 changes: 2 additions & 1 deletion src/amm/uniswap_v2/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl AutomatedMarketMakerFactory for UniswapV2Factory {
{
let pair_created_event = IUniswapV2Factory::PairCreated::decode_log(log.as_ref(), true)?;
Ok(AMM::UniswapV2Pool(
UniswapV2Pool::new_from_address(pair_created_event.pair, self.fee, provider).await?,
UniswapV2Pool::new_from_address(pair_created_event.pair, Some(log.address()), self.fee, provider).await?,
))
}

Expand All @@ -136,6 +136,7 @@ impl AutomatedMarketMakerFactory for UniswapV2Factory {

Ok(AMM::UniswapV2Pool(UniswapV2Pool {
address: pair_created_event.pair,
factory_address: Some(log.address()),
token_a: pair_created_event.token0,
token_b: pair_created_event.token1,
token_a_decimals: 0,
Expand Down
15 changes: 14 additions & 1 deletion src/amm/uniswap_v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ sol! {
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct UniswapV2Pool {
pub address: Address,
#[serde(skip_serializing_if = "Option::is_none")]
pub factory_address: Option<Address>,
pub token_a: Address,
pub token_a_decimals: u8,
pub token_b: Address,
Expand Down Expand Up @@ -187,6 +189,7 @@ impl UniswapV2Pool {
#[allow(clippy::too_many_arguments)]
pub fn new(
address: Address,
factory_address: Address,
token_a: Address,
token_a_decimals: u8,
token_b: Address,
Expand All @@ -197,6 +200,7 @@ impl UniswapV2Pool {
) -> UniswapV2Pool {
UniswapV2Pool {
address,
factory_address: Some(factory_address),
token_a,
token_a_decimals,
token_b,
Expand All @@ -210,6 +214,7 @@ impl UniswapV2Pool {
/// Creates a new instance of the pool from the pair address, and syncs the pool data.
pub async fn new_from_address<T, N, P>(
pair_address: Address,
factory_address: Option<Address>,
fee: u32,
provider: Arc<P>,
) -> Result<Self, AMMError>
Expand All @@ -220,6 +225,7 @@ impl UniswapV2Pool {
{
let mut pool = UniswapV2Pool {
address: pair_address,
factory_address,
token_a: Address::ZERO,
token_a_decimals: 0,
token_b: Address::ZERO,
Expand Down Expand Up @@ -256,7 +262,7 @@ impl UniswapV2Pool {
if event_signature == IUniswapV2Factory::PairCreated::SIGNATURE_HASH {
let pair_created_event =
factory::IUniswapV2Factory::PairCreated::decode_log(log.as_ref(), true)?;
UniswapV2Pool::new_from_address(pair_created_event.pair, fee, provider).await
UniswapV2Pool::new_from_address(pair_created_event.pair, Some(log.address()), fee, provider).await
} else {
Err(EventLogError::InvalidEventSignature)?
}
Expand All @@ -274,6 +280,7 @@ impl UniswapV2Pool {

Ok(UniswapV2Pool {
address: pair_created_event.pair,
factory_address: Some(log.address()),
token_a: pair_created_event.token0,
token_b: pair_created_event.token1,
token_a_decimals: 0,
Expand Down Expand Up @@ -583,6 +590,7 @@ mod tests {

let pool = UniswapV2Pool::new_from_address(
address!("B4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc"),
Some(address!("5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f")),
300,
provider.clone(),
)
Expand All @@ -593,6 +601,10 @@ mod tests {
pool.address,
address!("B4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc")
);
assert_eq!(
pool.factory_address,
Some(address!("5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f"))
);
assert_eq!(
pool.token_a,
address!("a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48")
Expand Down Expand Up @@ -640,6 +652,7 @@ mod tests {
let token_b = address!("8f18dc399594b451eda8c5da02d0563c0b2d0f16");
let x = UniswapV2Pool {
address: address!("652a7b75c229850714d4a11e856052aac3e9b065"),
factory_address: Some(address!("5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f")),
token_a,
token_a_decimals: 18,
token_b,
Expand Down
3 changes: 2 additions & 1 deletion src/amm/uniswap_v3/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl AutomatedMarketMakerFactory for UniswapV3Factory {
if let Some(block_number) = log.block_number {
let pool_created_filter = IUniswapV3Factory::PoolCreated::decode_log(&log.inner, true)?;
Ok(AMM::UniswapV3Pool(
UniswapV3Pool::new_from_address(pool_created_filter.pool, block_number, provider)
UniswapV3Pool::new_from_address(pool_created_filter.pool, Some(log.address()), block_number, provider)
.await?,
))
} else {
Expand Down Expand Up @@ -126,6 +126,7 @@ impl AutomatedMarketMakerFactory for UniswapV3Factory {

Ok(AMM::UniswapV3Pool(UniswapV3Pool {
address: pool_created_event.pool,
factory_address: Some(log.address()),
token_a: pool_created_event.token0,
token_b: pool_created_event.token1,
token_a_decimals: 0,
Expand Down
10 changes: 9 additions & 1 deletion src/amm/uniswap_v3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ sol! {
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct UniswapV3Pool {
pub address: Address,
#[serde(skip_serializing_if = "Option::is_none")]
pub factory_address: Option<Address>,
pub token_a: Address,
pub token_a_decimals: u8,
pub token_b: Address,
Expand Down Expand Up @@ -454,6 +456,7 @@ impl UniswapV3Pool {
#[allow(clippy::too_many_arguments)]
pub fn new(
address: Address,
factory_address: Option<Address>,
token_a: Address,
token_a_decimals: u8,
token_b: Address,
Expand All @@ -468,6 +471,7 @@ impl UniswapV3Pool {
) -> UniswapV3Pool {
UniswapV3Pool {
address,
factory_address,
token_a,
token_a_decimals,
token_b,
Expand All @@ -487,6 +491,7 @@ impl UniswapV3Pool {
/// This function will populate all pool data.
pub async fn new_from_address<T, N, P>(
pair_address: Address,
factory_address: Option<Address>,
creation_block: u64,
provider: Arc<P>,
) -> Result<Self, AMMError>
Expand All @@ -497,6 +502,7 @@ impl UniswapV3Pool {
{
let mut pool = UniswapV3Pool {
address: pair_address,
factory_address,
token_a: Address::ZERO,
token_a_decimals: 0,
token_b: Address::ZERO,
Expand Down Expand Up @@ -543,7 +549,7 @@ impl UniswapV3Pool {
let pool_created_event =
IUniswapV3Factory::PoolCreated::decode_log(&log.inner, true)?;

UniswapV3Pool::new_from_address(pool_created_event.pool, block_number, provider)
UniswapV3Pool::new_from_address(pool_created_event.pool, Some(log.address()), block_number, provider)
.await
} else {
Err(EventLogError::LogBlockNumberNotFound)?
Expand All @@ -564,6 +570,7 @@ impl UniswapV3Pool {

Ok(UniswapV3Pool {
address: pool_created_event.pool,
factory_address: Some(log.address()),
token_a: pool_created_event.token0,
token_b: pool_created_event.token1,
token_a_decimals: 0,
Expand Down Expand Up @@ -1859,6 +1866,7 @@ mod test {

let pool = UniswapV3Pool::new_from_address(
address!("88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640"),
Some(address!("1F98431c8aD98523631AE4a59f267346ea31F984")),
12369620,
provider.clone(),
)
Expand Down
Loading