Skip to content

Instantly share code, notes, and snippets.

@jmcph4
Created April 12, 2024 05:43
Show Gist options
  • Save jmcph4/92d248a0ac6628d6f574b6c937a4a20c to your computer and use it in GitHub Desktop.
Save jmcph4/92d248a0ac6628d6f574b6c937a4a20c to your computer and use it in GitHub Desktop.
[package]
name = "uniswap_v2_prices"
edition = "2021"
[dependencies]
alloy = { git = "https://github.com/alloy-rs/alloy", features = [
"contract",
"network",
"providers",
"rpc",
"rpc-types",
"rpc-types-eth",
"transports",
"provider-http",
"provider-ws",
"rpc-client",
"pubsub"
] }
tokio = { version = "1.37.0", features = ["full"] }
$ cargo run
4462901919135589517858 / 2991078847897721207214 = 1
use alloy::network::Ethereum;
use alloy::providers::ReqwestProvider;
use alloy::{primitives::U256, sol};
sol!(
#[allow(missing_docs)]
#[sol(rpc)]
IUniswapV2Pair,
"UniswapV2Pair.json"
);
const RPC_URL: &str = "https://eth.merkle.io";
const POOL_ADDRESS: &str = "0x9C4Fe5FFD9A9fC5678cFBd93Aa2D4FD684b67C4C";
#[tokio::main]
async fn main() {
let rpc_url = RPC_URL.parse().unwrap();
let provider = ReqwestProvider::<Ethereum>::new_http(rpc_url);
let pool_address = POOL_ADDRESS.parse().unwrap();
let IUniswapV2Pair::getReservesReturn {
_reserve0,
_reserve1,
_blockTimestampLast,
} = IUniswapV2Pair::new(pool_address, provider)
.getReserves()
.call()
.await
.expect("failed to call getReserves");
let reserves: (U256, U256) =
(_reserve0.try_into().unwrap(), _reserve1.try_into().unwrap());
println!(
"{} / {} = {}",
reserves.0,
reserves.1,
reserves.0 / reserves.1
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment