1 个不稳定版本

0.1.1 2024年4月5日
0.1.0 2024年4月4日

#4#uniswap

Download history

每月 55 次下载

MIT 许可证

13KB
151

Uniswap V2 库

安装

在您的项目目录中运行以下 Cargo 命令

cargo add uniswap-v2-core

或者将以下行添加到您的 Cargo.toml 文件中

uniswap-v2-core = "0.1.1"

使用方法

将以下行添加到您的 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"

示例

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(())
}

依赖项

~25–42MB
~794K SLoC