1 unstable release
0.1.1 | Apr 5, 2024 |
---|---|
0.1.0 |
|
#4 in #uniswap
55 downloads per month
13KB
151 lines
Uniswap V2 library
Install
Run the following Cargo command in your project directory:
cargo add uniswap-v2-core
Or add the following line to your Cargo.toml:
uniswap-v2-core = "0.1.1"
Usage
Add the following lines to your Cargo.toml:
ethers = "2.0"
# Ethers' async features rely upon the Tokio async runtime.
tokio = { version = "1", features = ["full"] }
# Flexible concrete Error Reporting type built on std::error::Error with customizable Reports
eyre = "0.6"
Examples:
use ethers::types::Address;
use uniswap_v2_core::{uniswap_factory, uniswap_pair, uniswap_router};
const RPC_URL: &str = "https://eth.llamarpc.com";
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize a new instance of the Weth/Dai Uniswap V2 pair contract
let uniswap_v2_pair = uniswap_pair::new("0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11", RPC_URL)?;
// Use the get_reserves() function to fetch the pool reserves
let (reserve_0, reserve_1, _) = uniswap_v2_pair.get_reserves().call().await?;
println!("ETH / DAI Price: {}", reserve_0 / reserve_1);
// DAI: 0x6B175474E89094C44Da98b954EedeAC495271d0F
let token_0: Address = uniswap_v2_pair.token_0().call().await?;
// WETH: 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2
let token_1: Address = uniswap_v2_pair.token_1().call().await?;
println!("Token: {:?}, {:?}", token_0, token_1);
let uniswap_v2_factory =
uniswap_factory::new("0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f", RPC_URL)?;
let pair: Address = uniswap_v2_factory.get_pair(token_0, token_1).call().await?;
println!("Pair: {:?}", pair);
let uniswap_v2_router =
uniswap_router::new("0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D", RPC_URL)?;
let factory: Address = uniswap_v2_router.factory().call().await?;
println!("Factory: {:?}", factory);
Ok(())
}
Dependencies
~25–42MB
~794K SLoC