Skip to content

Commit

Permalink
Merge branch 'main' into event-based-pool-fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
sunce86 authored Aug 8, 2022
2 parents 4ca1b20 + 1e5aa9e commit f118b82
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
39 changes: 25 additions & 14 deletions crates/shared/src/sources/uniswap_v3/graph_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ const POOLS_WITH_TICKS_BY_IDS_QUERY: &str = r#"
}
) {
id
token0 {
symbol
id
decimals
}
token1 {
symbol
id
decimals
}
feeTier
liquidity
sqrtPrice
tick
Expand Down Expand Up @@ -167,9 +178,9 @@ pub struct RegisteredPools {
#[serde(rename_all = "camelCase")]
pub struct PoolData {
pub id: H160,
pub token0: Option<Token>,
pub token1: Option<Token>,
pub fee_tier: Option<U256>,
pub token0: Token,
pub token1: Token,
pub fee_tier: U256,
pub liquidity: U256,
pub sqrt_price: U256,
#[serde(with = "serde_with::rust::display_fromstr")]
Expand Down Expand Up @@ -201,7 +212,7 @@ impl ContainsId for TickData {
}
}

#[derive(Debug, Clone, Deserialize, PartialEq, Serialize)]
#[derive(Debug, Clone, Default, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Token {
pub id: H160,
Expand Down Expand Up @@ -289,39 +300,39 @@ mod tests {
inner: vec![
PoolData {
id: H160::from_str("0x0001fcbba8eb491c3ccfeddc5a5caba1a98c4c28").unwrap(),
token0: Some(Token {
token0: Token {
id: H160::from_str("0xbef81556ef066ec840a540595c8d12f516b6378f")
.unwrap(),
symbol: "BCZ".to_string(),
decimals: 18,
}),
token1: Some(Token {
},
token1: Token {
id: H160::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2")
.unwrap(),
symbol: "WETH".to_string(),
decimals: 18,
}),
fee_tier: Some(U256::from_str("10000").unwrap()),
},
fee_tier: U256::from_str("10000").unwrap(),
liquidity: U256::from_str("303015134493562686441").unwrap(),
sqrt_price: U256::from_str("792216481398733702759960397").unwrap(),
tick: BigInt::from(-92110),
ticks: None,
},
PoolData {
id: H160::from_str("0x0002e63328169d7feea121f1e32e4f620abf0352").unwrap(),
token0: Some(Token {
token0: Token {
id: H160::from_str("0x0d438f3b5175bebc262bf23753c1e53d03432bde")
.unwrap(),
symbol: "wNXM".to_string(),
decimals: 18,
}),
token1: Some(Token {
},
token1: Token {
id: H160::from_str("0x903bef1736cddf2a537176cf3c64579c3867a881")
.unwrap(),
symbol: "ICHI".to_string(),
decimals: 9,
}),
fee_tier: Some(U256::from_str("3000").unwrap()),
},
fee_tier: U256::from_str("3000").unwrap(),
liquidity: U256::from_str("3125586395511534995").unwrap(),
sqrt_price: U256::from_str("5986323062404391218190509").unwrap(),
tick: BigInt::from(-189822),
Expand Down
13 changes: 4 additions & 9 deletions crates/shared/src/sources/uniswap_v3/pool_fetching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ impl TryFrom<PoolData> for PoolInfo {
fn try_from(pool: PoolData) -> Result<Self> {
Ok(Self {
address: pool.id,
tokens: vec![
pool.token0.context("no token0")?,
pool.token1.context("no token1")?,
],
tokens: vec![pool.token0, pool.token1],
state: PoolState {
sqrt_price: pool.sqrt_price,
liquidity: pool.liquidity,
Expand All @@ -81,7 +78,7 @@ impl TryFrom<PoolData> for PoolInfo {
}
})
.collect(),
fee: Ratio::new(pool.fee_tier.context("no fee")?.as_u32(), 1_000_000u32),
fee: Ratio::new(pool.fee_tier.as_u32(), 1_000_000u32),
},
gas_stats: PoolStats {
mean_gas: U256::from(300_000), // todo: hardcoded for testing purposes
Expand Down Expand Up @@ -118,10 +115,8 @@ impl UniswapV3PoolFetcher {

let mut pools_by_token_pair: HashMap<TokenPair, HashSet<H160>> = HashMap::new();
for pool in registered_pools.pools {
let token0 = pool.token0.clone().context("token0 does not exist")?.id;
let token1 = pool.token1.clone().context("token1 does not exist")?.id;

let pair = TokenPair::new(token0, token1).context("cant create pair")?;
let pair =
TokenPair::new(pool.token0.id, pool.token1.id).context("cant create pair")?;
pools_by_token_pair.entry(pair).or_default().insert(pool.id);
}

Expand Down

0 comments on commit f118b82

Please sign in to comment.