hypersdk 0.2.8

Rust SDK for Hyperliquid
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
//! Uniswap V3 DEX integration.
//!
//! This module provides a client for interacting with Uniswap V3 on HyperEVM,
//! including querying pools, managing positions, and calculating prices.
//!
//! # Overview
//!
//! Uniswap V3 is a decentralized exchange protocol that enables:
//! - Automated market making with concentrated liquidity
//! - Price discovery for token pairs
//! - Liquidity provision with custom price ranges
//! - Flash swaps and complex trading strategies
//!
//! # Examples
//!
//! ## Query Pool Price
//!
//! ```no_run
//! use hypersdk::hyperevm::uniswap::{self, Contracts};
//! use hypersdk::Address;
//!
//! # async fn example() -> anyhow::Result<()> {
//! let contracts = Contracts {
//!     factory: "0x...".parse()?,
//!     quoter: "0x...".parse()?,
//!     swap_router: "0x...".parse()?,
//!     non_fungible_position_manager: "0x...".parse()?,
//! };
//!
//! let client = uniswap::Client::mainnet(contracts).await?;
//!
//! let token0: Address = "0x...".parse()?;
//! let token1: Address = "0x...".parse()?;
//! let fee = 3000; // 0.3%
//!
//! let price = client.get_pool_price(token0, token1, fee).await?;
//! println!("Pool price: {}", price);
//! # Ok(())
//! # }
//! ```
//!
//! ## Query User Positions
//!
//! ```no_run
//! use hypersdk::hyperevm::uniswap::{self, Contracts};
//! use hypersdk::Address;
//!
//! # async fn example() -> anyhow::Result<()> {
//! # let contracts = Contracts {
//! #     factory: "0x...".parse()?,
//! #     quoter: "0x...".parse()?,
//! #     swap_router: "0x...".parse()?,
//! #     non_fungible_position_manager: "0x...".parse()?,
//! # };
//! let client = uniswap::Client::mainnet(contracts).await?;
//!
//! let user: Address = "0x...".parse()?;
//! let positions = client.positions(user).await?;
//!
//! for pos in positions {
//!     println!("Position {}: {} / {}",
//!         pos.token_id, pos.token0, pos.token1);
//!     println!("  Provided: {} / {}", pos.token0_provided, pos.token1_provided);
//!     println!("  Fees: {} / {}", pos.token0_fees, pos.token1_fees);
//! }
//! # Ok(())
//! # }
//! ```

pub mod contracts;
pub mod prjx;

use std::{
    collections::{HashMap, hash_map::Entry},
    hash::{DefaultHasher, Hash},
};

use alloy::{
    primitives::{U160, U256, aliases::U24},
    transports::TransportError,
};
use anyhow::Result;
use rust_decimal::{Decimal, MathematicalOps, dec, prelude::ToPrimitive};

use crate::hyperevm::{
    Address, DynProvider, ERC20, Provider,
    uniswap::contracts::{
        INonfungiblePositionManager::{self, CollectParams, INonfungiblePositionManagerInstance},
        IQuoterV2::{self, IQuoterV2Instance},
        ISwapRouter::{self, ISwapRouterInstance},
        IUniswapV3Factory::{self, IUniswapV3FactoryInstance},
        IUniswapV3Pool::{self, IUniswapV3PoolInstance},
    },
};

/// Standard Uniswap V3 fee tiers (in basis points).
///
/// Uniswap V3 supports multiple fee tiers for different volatility levels:
/// - `100`: 0.01% (for stable pairs like USDC/USDT)
/// - `500`: 0.05% (for less volatile pairs)
/// - `3_000`: 0.3% (most common, for standard pairs)
/// - `10_000`: 1% (for exotic or highly volatile pairs)
pub const FEES: [u32; 4] = [
    100,    // 0.01%
    500,    // 0.05%
    3_000,  // 0.3%
    10_000, // 1%
];

#[inline(always)]
fn tick_to_sqrt_price(tick: i64) -> Decimal {
    let price = dec!(1.0001).powi(tick);
    price.sqrt().unwrap()
}

// https://github.com/Uniswap/v3-core/blob/d8b1c635c275d2a9450bd6a78f3fa2484fef73eb/contracts/libraries/TickMath.sol
fn get_amounts_from_liquidity(
    liquidity: u128,
    tick_lower: i64,
    tick_upper: i64,
    tick_current: i64,
) -> (Decimal, Decimal) {
    let liquidity_f64 = Decimal::from(liquidity);

    let sqrt_lower = tick_to_sqrt_price(tick_lower);
    let sqrt_upper = tick_to_sqrt_price(tick_upper);
    let sqrt_price = tick_to_sqrt_price(tick_current);

    if tick_current <= tick_lower {
        let amount0 = liquidity_f64 * (sqrt_upper - sqrt_lower) / (sqrt_upper * sqrt_lower);
        return (amount0, Decimal::ZERO);
    }

    if tick_current >= tick_upper {
        let amount1 = liquidity_f64 * (sqrt_upper - sqrt_lower);
        return (Decimal::ZERO, amount1);
    }

    let amount0 = liquidity_f64 * (sqrt_upper - sqrt_price) / (sqrt_upper * sqrt_price);
    let amount1 = liquidity_f64 * (sqrt_price - sqrt_lower);

    (amount0, amount1)
}

/// Converts a price to Uniswap's sqrtPriceLimitX96 format.
///
/// This is an approximation since [`Decimal`] can't store the full Q64.96 precision.
///
/// # Parameters
///
/// - `price`: The price to convert
/// - `scale`: Decimal scaling factor
///
/// # Example
///
/// Convert a price to sqrtPriceLimitX96 format: `sqrt_price_limit_x96(price, scale)`
#[must_use]
pub fn sqrt_price_limit_x96(price: Decimal, scale: u32) -> U160 {
    let q96 = U160::from(2).pow(U160::from(96));
    let price = U160::from((price * Decimal::TEN.powi(scale as i64)).to_i128().unwrap());
    let sqrt = price.root(2);
    // sqrt * q96 / 18 digits (evm default max digits)
    sqrt * q96 / U160::from(10).pow(U160::from(18))
}

/// Converts Uniswap's sqrtPriceX96 format to a decimal price.
///
/// This is an approximation since [`Decimal`] can't store the full Q64.96 precision.
///
/// # Parameters
///
/// - `sqrt_price_x96`: The sqrt price in Q64.96 format
/// - `decimals0`: Decimals of token0
/// - `decimals1`: Decimals of token1
///
/// # Returns
///
/// The price of token0 in terms of token1.
///
/// # Example
///
/// ```
/// use hypersdk::hyperevm::uniswap::sqrt_x96_to_price;
/// use hypersdk::U160;
///
/// # let sqrt_price_x96 = U160::from(1u64);
/// let price = sqrt_x96_to_price(sqrt_price_x96, 18, 6);
/// println!("Price: {}", price);
/// ```
#[must_use]
pub fn sqrt_x96_to_price(sqrt_price_x96: U160, decimals0: u32, decimals1: u32) -> Decimal {
    let q96 = U160::from(2).pow(U160::from(96));

    // Scale sqrt_price to avoid precision loss
    let sqrt_price_scaled = sqrt_price_x96 * U160::from(10).pow(U160::from(decimals0));

    let price = (sqrt_price_scaled / q96).pow(U160::from(2));
    Decimal::from_i128_with_scale(price.to::<i128>(), decimals0 + decimals1)
}

/// Uniswap V3 contract addresses.
///
/// Contains the addresses of the core Uniswap V3 contracts needed for interaction.
///
/// # Example
///
/// ```
/// use hypersdk::hyperevm::uniswap::Contracts;
/// use hypersdk::Address;
///
/// # fn example() -> anyhow::Result<()> {
/// let contracts = Contracts {
///     factory: "0x...".parse()?,
///     quoter: "0x...".parse()?,
///     swap_router: "0x...".parse()?,
///     non_fungible_position_manager: "0x...".parse()?,
/// };
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Copy)]
pub struct Contracts {
    /// UniswapV3Factory contract address
    pub factory: Address,
    /// QuoterV2 contract address (for price quotes)
    pub quoter: Address,
    /// SwapRouter contract address (for executing swaps)
    pub swap_router: Address,
    /// NonfungiblePositionManager contract address (for LP positions)
    pub non_fungible_position_manager: Address,
}

/// A Uniswap V3 liquidity position.
///
/// Represents an NFT liquidity position with token amounts and accumulated fees.
///
/// # Example
///
/// Query user positions: `client.positions(user_address).await?`
/// Check if position is active with `pos.in_range` field.
#[derive(Debug, Clone)]
pub struct Position {
    /// NFT token ID for this position
    pub token_id: U256,
    /// First token in the pair
    pub token0: Address,
    /// Second token in the pair
    pub token1: Address,
    /// Amount of token0 provided as liquidity
    pub token0_provided: Decimal,
    /// Amount of token1 provided as liquidity
    pub token1_provided: Decimal,
    /// Accumulated fees in token0
    pub token0_fees: Decimal,
    /// Accumulated fees in token1
    pub token1_fees: Decimal,
    /// Whether the position is in range (actively earning fees)
    pub in_range: bool,
}

impl Position {
    /// Returns whether the position is actively earning fees.
    ///
    /// A position is "in range" when the current pool price is within
    /// the position's price range.
    #[must_use]
    #[inline]
    pub fn is_active(&self) -> bool {
        self.in_range
    }

    /// Returns the total value provided to the position in token0 equivalent.
    ///
    /// This converts token1 to token0 using the given price.
    ///
    /// # Parameters
    ///
    /// - `token1_price_in_token0`: The price of token1 in terms of token0
    #[must_use]
    pub fn total_value_in_token0(&self, token1_price_in_token0: Decimal) -> Decimal {
        self.token0_provided + (self.token1_provided * token1_price_in_token0)
    }

    /// Returns the total fees earned in token0 equivalent.
    ///
    /// # Parameters
    ///
    /// - `token1_price_in_token0`: The price of token1 in terms of token0
    #[must_use]
    pub fn total_fees_in_token0(&self, token1_price_in_token0: Decimal) -> Decimal {
        self.token0_fees + (self.token1_fees * token1_price_in_token0)
    }
}

/// Client for Uniswap V3 DEX.
///
/// Provides methods for querying pools, positions, and executing swaps.
///
/// # Example
///
/// ```no_run
/// use hypersdk::hyperevm::uniswap::{self, Contracts};
///
/// # async fn example() -> anyhow::Result<()> {
/// let contracts = Contracts {
///     factory: "0x...".parse()?,
///     quoter: "0x...".parse()?,
///     swap_router: "0x...".parse()?,
///     non_fungible_position_manager: "0x...".parse()?,
/// };
///
/// let client = uniswap::Client::mainnet(contracts).await?;
/// # Ok(())
/// # }
/// ```
pub struct Client<P>
where
    P: Provider,
{
    provider: P,
    contracts: Contracts,
}

impl Client<DynProvider> {
    /// Creates a client for mainnet.
    pub async fn mainnet(contracts: Contracts) -> Result<Self, TransportError> {
        let provider = DynProvider::new(super::mainnet().await?);
        Ok(Self::new(provider, contracts))
    }

    /// Creates a client for mainnet.
    pub async fn mainnet_with_url(url: &str, contracts: Contracts) -> Result<Self, TransportError> {
        let provider = DynProvider::new(super::mainnet_with_url(url).await?);
        Ok(Self::new(provider, contracts))
    }
}

impl<P> Client<P>
where
    P: Provider,
{
    /// Create a uniswap client.
    pub fn new(provider: P, contracts: Contracts) -> Self {
        Self {
            provider,
            contracts,
        }
    }

    /// Returns the root provider.
    pub fn provider(&self) -> &P {
        &self.provider
    }

    /// Returns the uniswap factory.
    pub fn factory(&self) -> IUniswapV3FactoryInstance<P> {
        IUniswapV3Factory::new(self.contracts.factory, self.provider().clone())
    }

    /// Returns the uniswap pool.
    pub fn pool(&self, address: Address) -> IUniswapV3PoolInstance<P> {
        IUniswapV3Pool::new(address, self.provider().clone())
    }

    /// Returns the uniswap quoter.
    pub fn quoter(&self) -> IQuoterV2Instance<P> {
        IQuoterV2::new(self.contracts.quoter, self.provider().clone())
    }

    /// Returns the uniswap swap router.
    pub fn swap_router(&self) -> ISwapRouterInstance<P> {
        ISwapRouter::new(self.contracts.swap_router, self.provider().clone())
    }

    /// Returns the uniswap non-fungible positions manager.
    pub fn non_fungible_position_manager(&self) -> INonfungiblePositionManagerInstance<P> {
        INonfungiblePositionManager::new(
            self.contracts.non_fungible_position_manager,
            self.provider().clone(),
        )
    }

    /// Load the current positions from a user.
    ///
    /// TODO: make it composable so a user could query a specific block, ...
    pub async fn positions(&self, target_address: Address) -> Result<Vec<Position>> {
        let npm = self.non_fungible_position_manager();
        let factory = self.factory();

        let position_count: U256 = npm.balanceOf(target_address).call().await?;
        let count = position_count.to::<usize>();

        let mut positions = vec![];

        struct PositionData {
            decimals0: u8,
            decimals1: u8,
            pool_address: Address,
        }

        let mut pools: HashMap<u64, PositionData> = HashMap::default();

        for i in 0..count {
            let token_id: U256 = npm
                .tokenOfOwnerByIndex(target_address, U256::from(i))
                .call()
                .await?;

            let pos = npm.positions(token_id).call().await?;
            if pos.liquidity == 0 {
                continue;
            }

            use std::hash::Hasher;
            let mut hasher = DefaultHasher::default();
            pos.token0.hash(&mut hasher);
            pos.token1.hash(&mut hasher);
            pos.fee.hash(&mut hasher);

            let prehash = hasher.finish();
            let entry = pools.entry(prehash);
            if let Entry::Vacant(entry) = entry {
                let token0_client = ERC20::new(pos.token0, self.provider.clone());
                let token1_client = ERC20::new(pos.token1, self.provider.clone());

                let (decimals0, decimals1, pool_address) = self
                    .provider
                    .multicall()
                    .add(token0_client.decimals())
                    .add(token1_client.decimals())
                    .add(factory.getPool(pos.token0, pos.token1, pos.fee))
                    .aggregate()
                    .await?;
                entry.insert(PositionData {
                    decimals0,
                    decimals1,
                    pool_address,
                });

                if pool_address.is_zero() {
                    continue;
                }
            }

            let pools = &pools[&prehash];
            let (decimals0, decimals1, pool_address) =
                (pools.decimals0, pools.decimals1, pools.pool_address);

            let max_u128: u128 = u128::MAX;
            let params = CollectParams {
                tokenId: token_id,
                recipient: target_address,
                amount0Max: max_u128,
                amount1Max: max_u128,
            };

            let collect_call = npm.collect(params);
            let res = collect_call.from(target_address).call().await?;

            use std::convert::TryFrom;
            let fees_in_0 = Decimal::from(u128::try_from(res.amount0)?);
            let fees_in_1 = Decimal::from(u128::try_from(res.amount1)?);

            let token0_fees = fees_in_0 / Decimal::TEN.powi(decimals0 as i64);
            let token1_fees = fees_in_1 / Decimal::TEN.powi(decimals1 as i64);

            let pool = self.pool(pool_address);
            let slot0 = pool.slot0().call().await?;

            let in_range = slot0.tick <= pos.tickUpper && slot0.tick >= pos.tickLower;

            let (amount0_raw, amount1_raw) = get_amounts_from_liquidity(
                pos.liquidity as u128,
                pos.tickLower.try_into()?,
                pos.tickUpper.try_into()?,
                slot0.tick.try_into()?,
            );

            let amount0_in_token = amount0_raw / Decimal::TEN.powi(decimals0 as i64);
            let amount1_in_token = amount1_raw / Decimal::TEN.powi(decimals1 as i64);
            positions.push(Position {
                token_id,
                token0: pos.token0,
                token1: pos.token1,
                token0_provided: amount0_in_token,
                token1_provided: amount1_in_token,
                token0_fees,
                token1_fees,
                in_range,
            });
        }

        Ok(positions)
    }

    /// Get the pool address.
    pub async fn get_pool_addres(
        &self,
        token0: Address,
        token1: Address,
        fee: u32,
    ) -> Result<Address> {
        let factory = self.factory();
        let token0_erc = ERC20::new(token0, self.provider.clone());
        let token1_erc = ERC20::new(token1, self.provider.clone());
        let (_, _, address) = self
            .provider
            .multicall()
            .add(token0_erc.symbol())
            .add(token1_erc.symbol())
            .add(factory.getPool(token0, token1, U24::from(fee)))
            .aggregate()
            .await?;
        Ok(address)
    }

    /// Get the price from a pool.
    pub async fn pool_price_sqrt_x96(
        &self,
        token0: Address,
        token1: Address,
        fee: u32,
    ) -> Result<U160> {
        let factory = self.factory();
        let pool_address = factory
            .getPool(token0, token1, U24::from(fee))
            .call()
            .await?;
        let pool = self.pool(pool_address);
        let slot0 = pool.slot0().call().await?;
        Ok(slot0.sqrtPriceX96)
    }

    /// Returns the pool's slot0.
    pub async fn slot0(
        &self,
        token0: Address,
        token1: Address,
        fee: u32,
    ) -> Result<IUniswapV3Pool::slot0Return> {
        let factory = self.factory();
        let pool_address = factory
            .getPool(token0, token1, U24::from(fee))
            .call()
            .await?;
        let pool = self.pool(pool_address);
        let ret = pool.slot0().call().await?;
        Ok(ret)
    }

    /// Get the pool's price in a Decimal approximation.
    pub async fn get_pool_price(
        &self,
        token0: Address,
        token1: Address,
        fee: u32,
    ) -> Result<Decimal> {
        let factory = self.factory();

        let token0_client = ERC20::new(token0, self.provider.clone());
        let token1_client = ERC20::new(token1, self.provider.clone());

        // get the pool address and the decimals of each token
        let (decimals0, decimals1, pool_address) = self
            .provider
            .multicall()
            .add(token0_client.decimals())
            .add(token1_client.decimals())
            .add(factory.getPool(token0, token1, U24::from(fee)))
            .aggregate()
            .await?;

        let pool = self.pool(pool_address);
        let slot0 = pool.slot0().call().await?;

        Ok(sqrt_x96_to_price(
            slot0.sqrtPriceX96,
            decimals0 as u32,
            decimals1 as u32,
        ))
    }

    /// Get the pool's price in a Decimal approximation.
    pub async fn pool_price_from(&self, pool_address: Address) -> Result<Decimal> {
        let pool = self.pool(pool_address);

        let (token0, token1) = self
            .provider
            .multicall()
            .add(pool.token0())
            .add(pool.token1())
            .aggregate()
            .await?;

        let token0_client = ERC20::new(token0, self.provider.clone());
        let token1_client = ERC20::new(token1, self.provider.clone());

        let (decimals0, decimals1) = self
            .provider
            .multicall()
            .add(token0_client.decimals())
            .add(token1_client.decimals())
            .aggregate()
            .await?;

        let pool = self.pool(pool_address);
        let slot0 = pool.slot0().call().await?;

        Ok(sqrt_x96_to_price(
            slot0.sqrtPriceX96,
            decimals0 as u32,
            decimals1 as u32,
        ))
    }
}