shuhuiluo / uniswap-v3-sdk-rs

Opinionated Rust implementation of the Uniswap V3 SDK with a focus on readability and performance
MIT License
88 stars 18 forks source link

Slippage Tolerance doesn't seem to work #53

Closed lamhochit closed 6 months ago

lamhochit commented 6 months ago

I have tried to create a Pool obeject with Tick Data Provider via the below:

let pool = get_pool(1,
                        H160::from(*UNISWAP_V3_FACTORY).to_alloy(),
                        token0.to_alloy(),
                        token1.to_alloy(),
                        fee_type,
                        provider.clone(),
                        None).await?;

    Pool::new_with_tick_data_provider(
        pool.token0,
        pool.token1,
        pool.fee,
        pool.sqrt_ratio_x96,
        pool.liquidity,
        TickListDataProvider::new(
            vec![
                Tick::new(
                    nearest_usable_tick(MIN_TICK, fee_type.tick_spacing()),
                    pool.liquidity,
                    pool.liquidity as i128,
                ),
                Tick::new(
                    nearest_usable_tick(MAX_TICK, fee_type.tick_spacing()),
                    pool.liquidity,
                    -(pool.liquidity as i128),
                )
            ],
            fee_type.tick_spacing()
        ),

And I created a swap router function to create the callData

pub async fn swap_router(token0: H160, token1: H160, recipient: H160, amount: u64, pool: Pool<TickListDataProvider>) -> Result<()> {
    let from_token: Token;
    let to_token: Token;

    if pool.token0.address == token0.to_alloy() {
        from_token = pool.token0.clone();
        to_token = pool.token1.clone();
    }
    else {
        from_token = pool.token1.clone();
        to_token = pool.token0.clone();
    }

    let trade = Trade::from_route(
        Route::new(vec![pool.clone()], from_token.clone(), to_token.clone()),
        CurrencyAmount::from_raw_amount(from_token.clone(), amount).unwrap(),
        TradeType::ExactInput,
    ).unwrap();

    let swap_options = SwapOptions {
        slippage_tolerance: Percent::new(1, 100),
        recipient: recipient.to_alloy(),
        deadline: uint!(123_U256),
        input_token_permit: None,
        sqrt_price_limit_x96: None,
        fee: None,
    };

    let MethodParameters { calldata, value } =
        swap_call_parameters(&mut [trade], swap_options.clone()).unwrap();

    info!("{:?}", calldata);
    Ok(())
}

Calling the function as below:

let token0 = Address::from_str("0x626E8036dEB333b408Be468F951bdB42433cBF18")?; // AIOZ
            let token1 = Address::from_str("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2")?; // WETH
            let recipient = Address::from_str("0x2edcb3a67a2b1e92370941bed38650fc8da244eb")?;
            let pool_details = get_uniswap_v3_pool(provider.clone(), token0, token1, U256::from(10000)).await?;
            info!("Retrieved Pool: {:?}", pool_details);
            let _ = swap_router(token0, token1, recipient, 10, pool_details).await;

This is the call data that I get:

[22:16:13.624][INFO] Retrieved Pool: Pool { token0: CurrencyLike { chain_id: 1, decimals: 18, symbol: Some("AIOZ"), name: Some("AIOZ Network"), meta: TokenMeta { address: 0x626e8036deb333b408be468f951bdb42
433cbf18, buy_fee_bps: None, sell_fee_bps: None } }, token1: CurrencyLike { chain_id: 1, decimals: 18, symbol: Some("WETH"), name: Some("Wrapped Ether"), meta: TokenMeta { address: 0xc02aaa39b223fe8d0a0e5c
4f27ead9083c756cc2, buy_fee_bps: None, sell_fee_bps: None } }, fee: HIGH, sqrt_ratio_x96: 0x0000000000000000000000000000000000000000043efa05b48d0ab5cdef5f22_U256, liquidity: 112128553911277104354316, tick_current: -81989 }
[22:16:13.625][INFO] 0x414bf389000000000000000000000000626e8036deb333b408be468f951bdb42433cbf18000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000
0000000000000027100000000000000000000000002edcb3a67a2b1e92370941bed38650fc8da244eb000000000000000000000000000000000000000000000000000000000000007b000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Would you mind taking a look? Appreciated your help and love your work!

lamhochit commented 6 months ago

Or I suspect the TickDataProvider is not configured correctly from my end?

shuhuiluo commented 6 months ago

What do you mean by slippage not working?

lamhochit commented 6 months ago

I tried putting in a slippage %, but when I try to decode my call data, the minAmountOut is 0

lamhochit commented 6 months ago

Am I initialising my Pool correctly? Thank you.