kybr 0.2.0

Unofficial Rust client for the KyberSwap API (kybr)
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
616
617
618
619
//! Types for the `KyberSwap` API responses

use serde::{Deserialize, Serialize};
use std::fmt;
use std::str::FromStr;

/// Error returned when parsing a chain from a string fails
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseChainError {
    input: String,
}

impl fmt::Display for ParseChainError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "unknown chain: {}", self.input)
    }
}

impl std::error::Error for ParseChainError {}

/// Chains served by the `KyberSwap` aggregator API
///
/// Matches the active chains in `KyberSwap`'s supported-chains list
/// (`common-service.kyberswap.com/api/v1/aggregator/supported-chains`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Chain {
    Ethereum,
    Bsc,
    Polygon,
    Arbitrum,
    Optimism,
    Avalanche,
    Base,
    Linea,
    Sonic,
    Berachain,
    Unichain,
    Ronin,
    HyperEvm,
    Plasma,
    Monad,
    Etherlink,
    MegaEth,
    Robinhood,
    Arc,
}

impl Chain {
    /// All supported chains
    #[must_use]
    pub const fn all() -> &'static [Chain] {
        &[
            Chain::Ethereum,
            Chain::Bsc,
            Chain::Polygon,
            Chain::Arbitrum,
            Chain::Optimism,
            Chain::Avalanche,
            Chain::Base,
            Chain::Linea,
            Chain::Sonic,
            Chain::Berachain,
            Chain::Unichain,
            Chain::Ronin,
            Chain::HyperEvm,
            Chain::Plasma,
            Chain::Monad,
            Chain::Etherlink,
            Chain::MegaEth,
            Chain::Robinhood,
            Chain::Arc,
        ]
    }

    /// Convert from EVM chain ID
    #[must_use]
    pub fn from_chain_id(chain_id: u64) -> Option<Self> {
        match chain_id {
            1 => Some(Chain::Ethereum),
            56 => Some(Chain::Bsc),
            137 => Some(Chain::Polygon),
            42161 => Some(Chain::Arbitrum),
            10 => Some(Chain::Optimism),
            43114 => Some(Chain::Avalanche),
            8453 => Some(Chain::Base),
            59144 => Some(Chain::Linea),
            146 => Some(Chain::Sonic),
            80094 => Some(Chain::Berachain),
            130 => Some(Chain::Unichain),
            2020 => Some(Chain::Ronin),
            999 => Some(Chain::HyperEvm),
            9745 => Some(Chain::Plasma),
            143 => Some(Chain::Monad),
            42793 => Some(Chain::Etherlink),
            4326 => Some(Chain::MegaEth),
            4663 => Some(Chain::Robinhood),
            5042 => Some(Chain::Arc),
            _ => None,
        }
    }

    /// EVM chain ID
    #[must_use]
    pub const fn chain_id(&self) -> u64 {
        match self {
            Chain::Ethereum => 1,
            Chain::Bsc => 56,
            Chain::Polygon => 137,
            Chain::Arbitrum => 42161,
            Chain::Optimism => 10,
            Chain::Avalanche => 43114,
            Chain::Base => 8453,
            Chain::Linea => 59144,
            Chain::Sonic => 146,
            Chain::Berachain => 80094,
            Chain::Unichain => 130,
            Chain::Ronin => 2020,
            Chain::HyperEvm => 999,
            Chain::Plasma => 9745,
            Chain::Monad => 143,
            Chain::Etherlink => 42793,
            Chain::MegaEth => 4326,
            Chain::Robinhood => 4663,
            Chain::Arc => 5042,
        }
    }

    /// Chain name used in API paths
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Chain::Ethereum => "ethereum",
            Chain::Bsc => "bsc",
            Chain::Polygon => "polygon",
            Chain::Arbitrum => "arbitrum",
            Chain::Optimism => "optimism",
            Chain::Avalanche => "avalanche",
            Chain::Base => "base",
            Chain::Linea => "linea",
            Chain::Sonic => "sonic",
            Chain::Berachain => "berachain",
            Chain::Unichain => "unichain",
            Chain::Ronin => "ronin",
            Chain::HyperEvm => "hyperevm",
            Chain::Plasma => "plasma",
            Chain::Monad => "monad",
            Chain::Etherlink => "etherlink",
            Chain::MegaEth => "megaeth",
            Chain::Robinhood => "robinhood",
            Chain::Arc => "arc",
        }
    }

    /// Parse chain from string (returns Option for backward compatibility)
    #[must_use]
    pub fn try_from_str(s: &str) -> Option<Self> {
        s.parse().ok()
    }
}

impl FromStr for Chain {
    type Err = ParseChainError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "ethereum" | "eth" | "mainnet" => Ok(Chain::Ethereum),
            "bsc" | "bnb" | "binance" => Ok(Chain::Bsc),
            "polygon" | "matic" => Ok(Chain::Polygon),
            "arbitrum" | "arb" => Ok(Chain::Arbitrum),
            "optimism" | "op" => Ok(Chain::Optimism),
            "avalanche" | "avax" => Ok(Chain::Avalanche),
            "base" => Ok(Chain::Base),
            "linea" => Ok(Chain::Linea),
            "sonic" => Ok(Chain::Sonic),
            "berachain" | "bera" => Ok(Chain::Berachain),
            "unichain" => Ok(Chain::Unichain),
            "ronin" => Ok(Chain::Ronin),
            "hyperevm" | "hyperliquid" => Ok(Chain::HyperEvm),
            "plasma" => Ok(Chain::Plasma),
            "monad" => Ok(Chain::Monad),
            "etherlink" => Ok(Chain::Etherlink),
            "megaeth" => Ok(Chain::MegaEth),
            "robinhood" => Ok(Chain::Robinhood),
            "arc" => Ok(Chain::Arc),
            _ => Err(ParseChainError {
                input: s.to_string(),
            }),
        }
    }
}

impl std::fmt::Display for Chain {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl TryFrom<yldfi_common::Chain> for Chain {
    type Error = &'static str;

    fn try_from(chain: yldfi_common::Chain) -> Result<Self, Self::Error> {
        Self::from_chain_id(chain.id()).ok_or("Chain not supported by KyberSwap")
    }
}

impl From<Chain> for yldfi_common::Chain {
    fn from(chain: Chain) -> Self {
        Self::from_id(chain.chain_id())
    }
}

/// Route request parameters
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RouteRequest {
    /// Input token address
    pub token_in: String,
    /// Output token address
    pub token_out: String,
    /// Input amount with decimals
    pub amount_in: String,
    /// User address (optional for quote)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub to: Option<String>,
    /// Save gas mode
    #[serde(skip_serializing_if = "Option::is_none")]
    pub save_gas: Option<bool>,
    /// Only route through these sources (comma-separated, `includedSources`)
    #[serde(rename = "includedSources", skip_serializing_if = "Option::is_none")]
    pub include_dexs: Option<String>,
    /// Never route through these sources (comma-separated, `excludedSources`)
    #[serde(rename = "excludedSources", skip_serializing_if = "Option::is_none")]
    pub exclude_dexs: Option<String>,
    /// Account for gas cost when picking the route (`gasInclude`)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub gas_include: Option<bool>,
}

impl RouteRequest {
    pub fn new(
        token_in: impl Into<String>,
        token_out: impl Into<String>,
        amount_in: impl Into<String>,
    ) -> Self {
        Self {
            token_in: token_in.into(),
            token_out: token_out.into(),
            amount_in: amount_in.into(),
            to: None,
            save_gas: None,
            include_dexs: None,
            exclude_dexs: None,
            gas_include: None,
        }
    }

    #[must_use]
    pub fn with_recipient(mut self, to: impl Into<String>) -> Self {
        self.to = Some(to.into());
        self
    }

    #[must_use]
    pub fn with_save_gas(mut self, save_gas: bool) -> Self {
        self.save_gas = Some(save_gas);
        self
    }

    /// Only route through these sources (comma-separated, `includedSources`)
    #[must_use]
    pub fn with_included_sources(mut self, sources: impl Into<String>) -> Self {
        self.include_dexs = Some(sources.into());
        self
    }

    /// Never route through these sources (comma-separated, `excludedSources`)
    #[must_use]
    pub fn with_excluded_sources(mut self, sources: impl Into<String>) -> Self {
        self.exclude_dexs = Some(sources.into());
        self
    }

    /// Account for gas cost when picking the route (`gasInclude`)
    #[must_use]
    pub fn with_gas_include(mut self, gas_include: bool) -> Self {
        self.gas_include = Some(gas_include);
        self
    }

    /// Query parameters for `GET /{chain}/api/v1/routes`
    #[must_use]
    pub fn to_query_params(&self) -> Vec<(&'static str, String)> {
        let mut params = vec![
            ("tokenIn", self.token_in.clone()),
            ("tokenOut", self.token_out.clone()),
            ("amountIn", self.amount_in.clone()),
        ];
        if let Some(ref to) = self.to {
            params.push(("to", to.clone()));
        }
        if let Some(save_gas) = self.save_gas {
            params.push(("saveGas", save_gas.to_string()));
        }
        if let Some(ref sources) = self.include_dexs {
            params.push(("includedSources", sources.clone()));
        }
        if let Some(ref sources) = self.exclude_dexs {
            params.push(("excludedSources", sources.clone()));
        }
        if let Some(gas_include) = self.gas_include {
            params.push(("gasInclude", gas_include.to_string()));
        }
        params
    }

    /// Validate the route request parameters
    ///
    /// Checks that addresses and amounts are properly formatted.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Token addresses are not valid Ethereum addresses (0x + 40 hex chars)
    /// - Amounts contain non-numeric characters
    pub fn validate(&self) -> Result<(), &'static str> {
        // Validate token addresses
        if !is_valid_address(&self.token_in) {
            return Err("invalid token_in address");
        }
        if !is_valid_address(&self.token_out) {
            return Err("invalid token_out address");
        }

        // Validate recipient address if provided
        if let Some(ref to) = self.to {
            if !is_valid_address(to) {
                return Err("invalid recipient address");
            }
        }

        // Validate amount (should be numeric string)
        if !is_valid_amount(&self.amount_in) {
            return Err("invalid amount_in");
        }

        Ok(())
    }
}

/// Check if a string is a valid Ethereum address (0x + 40 hex chars)
fn is_valid_address(s: &str) -> bool {
    if !s.starts_with("0x") || s.len() != 42 {
        return false;
    }
    s[2..].chars().all(|c| c.is_ascii_hexdigit())
}

/// Check if a string is a valid amount (numeric string, no decimals)
fn is_valid_amount(s: &str) -> bool {
    !s.is_empty() && s.chars().all(|c| c.is_ascii_digit())
}

/// Routes response from `KyberSwap` API
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RoutesResponse {
    /// Response code (0 = success)
    pub code: i32,
    /// Response message
    pub message: String,
    /// Route data
    pub data: Option<RouteData>,
}

/// Route data
///
/// `route_summary` is kept as the raw JSON returned by the API: `/route/build`
/// validates it (including `routeID`, `checksum` and `timestamp`), so it must
/// be sent back unmodified. Use [`RouteData::summary`] for a typed view.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RouteData {
    /// Route summary exactly as returned by the API
    pub route_summary: serde_json::Value,
    /// Detailed router address
    #[serde(default)]
    pub router_address: Option<String>,
}

impl RouteData {
    /// Typed view of the route summary
    ///
    /// # Errors
    ///
    /// Returns an error if the summary does not match the expected shape.
    pub fn summary(&self) -> Result<RouteSummary, serde_json::Error> {
        RouteSummary::deserialize(&self.route_summary)
    }
}

/// Route summary
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RouteSummary {
    /// Input token address
    pub token_in: String,
    /// Output token address
    pub token_out: String,
    /// Input amount
    pub amount_in: String,
    /// Output amount
    pub amount_out: String,
    /// Input amount in USD
    #[serde(default)]
    pub amount_in_usd: Option<String>,
    /// Output amount in USD
    #[serde(default)]
    pub amount_out_usd: Option<String>,
    /// Estimated gas
    #[serde(default)]
    pub gas: Option<String>,
    /// Gas price used for estimation
    #[serde(default)]
    pub gas_price: Option<String>,
    /// Gas in USD
    #[serde(default)]
    pub gas_usd: Option<String>,
    /// Swap route
    #[serde(default)]
    pub route: Vec<Vec<SwapStep>>,
}

impl RouteSummary {
    /// Price impact in percent, derived from `amountInUsd` and
    /// `amountOutUsd` (v1 routes have no price-impact field)
    ///
    /// Positive means the output is worth less than the input.
    #[must_use]
    pub fn price_impact_percent(&self) -> Option<f64> {
        let amount_in: f64 = self.amount_in_usd.as_deref()?.parse().ok()?;
        let amount_out: f64 = self.amount_out_usd.as_deref()?.parse().ok()?;
        (amount_in > 0.0).then(|| (amount_in - amount_out) / amount_in * 100.0)
    }
}

/// Swap step in route
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SwapStep {
    /// Pool address
    pub pool: String,
    /// Token in address
    pub token_in: String,
    /// Token out address
    pub token_out: String,
    /// Swap amount
    #[serde(default)]
    pub swap_amount: Option<String>,
    /// Amount out
    #[serde(default)]
    pub amount_out: Option<String>,
    /// Pool type/exchange name
    #[serde(default)]
    pub exchange: Option<String>,
}

/// Build route request (to get transaction data)
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BuildRouteRequest {
    /// Route summary from [`RouteData::route_summary`], passed through
    /// unmodified (the API verifies its checksum)
    pub route_summary: serde_json::Value,
    /// Sender address
    pub sender: String,
    /// Recipient address
    pub recipient: String,
    /// Slippage tolerance in bips (sent as `slippageTolerance`, 10 = 0.1%)
    #[serde(rename = "slippageTolerance", skip_serializing_if = "Option::is_none")]
    pub slippage_tolerance_bps: Option<u32>,
    /// Deadline timestamp (unix seconds)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub deadline: Option<u64>,
    /// Enable permit
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enable_permit: Option<bool>,
}

/// Build route response with transaction data
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BuildRouteResponse {
    /// Response code
    pub code: i32,
    /// Response message
    pub message: String,
    /// Transaction data
    pub data: Option<BuildRouteData>,
}

/// Build route data
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BuildRouteData {
    /// Router contract address
    pub router_address: String,
    /// Encoded call data
    pub data: String,
    /// ETH value to send (API field `transactionValue`)
    #[serde(default, rename = "transactionValue", alias = "value")]
    pub value: Option<String>,
    /// Gas limit
    #[serde(default)]
    pub gas: Option<String>,
    /// Input amount
    #[serde(default)]
    pub amount_in: Option<String>,
    /// Expected output amount
    #[serde(default)]
    pub amount_out: Option<String>,
    /// Gas cost in USD
    #[serde(default)]
    pub gas_usd: Option<String>,
}

/// Token info
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct TokenInfo {
    pub address: String,
    pub symbol: String,
    pub name: String,
    pub decimals: u8,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn chain_aliases_parse() {
        for (name, chain) in [
            ("ETH", Chain::Ethereum),
            ("binance", Chain::Bsc),
            ("bnb", Chain::Bsc),
            ("sonic", Chain::Sonic),
            ("bera", Chain::Berachain),
            ("hyperevm", Chain::HyperEvm),
        ] {
            assert_eq!(name.parse::<Chain>().unwrap(), chain, "{name}");
        }
        // Chains KyberSwap has deactivated
        for name in [
            "fantom",
            "scroll",
            "zksync",
            "blast",
            "mantle",
            "polygon-zkevm",
            "gnosis",
        ] {
            assert!(name.parse::<Chain>().is_err(), "{name}");
        }
    }

    #[test]
    fn chain_mappings_round_trip() {
        for chain in Chain::all() {
            assert_eq!(chain.as_str().parse::<Chain>().unwrap(), *chain);
            assert_eq!(Chain::from_chain_id(chain.chain_id()), Some(*chain));
            let common = yldfi_common::Chain::from(*chain);
            assert!(
                !matches!(common, yldfi_common::Chain::Other(_)),
                "{chain:?}"
            );
            assert_eq!(Chain::try_from(common), Ok(*chain));
        }
    }

    const LIVE_ROUTE_DATA: &str = r#"{"routeSummary":{"tokenIn":"0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
        "amountIn":"1000000000000000000","amountInUsd":"2691.83","tokenOut":"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
        "amountOut":"2694223548","amountOutUsd":"2688.29","gas":"697210","gasPrice":"105254963","gasUsd":"0.19",
        "l1FeeUsd":"0","extraFee":{"feeAmount":"","chargeFeeBy":"","isInBps":false,"feeReceiver":""},
        "route":[[{"pool":"0xe6ff","tokenIn":"0xc02a","tokenOut":"0x2260","swapAmount":"1","amountOut":"2",
        "exchange":"uniswapv3","poolType":"uniswapv3","poolExtra":{"swapFee":100},"extra":{"_cs":"1"}}]],
        "routerAddress":"0x6131B5fae19EA4f9D964eAc0408E4408b66337b5","routeID":"e30c6d4ctNFIE6V9",
        "checksum":"4239051851851966504","timestamp":1790368014},
        "routerAddress":"0x6131B5fae19EA4f9D964eAc0408E4408b66337b5"}"#;

    #[test]
    fn build_data_reads_transaction_value() {
        let d: BuildRouteData = serde_json::from_str(
            r#"{"routerAddress":"0x6131","data":"0x","transactionValue":"1000000000000000000","gas":"330498"}"#,
        )
        .unwrap();
        assert_eq!(d.value.as_deref(), Some("1000000000000000000"));
    }

    #[test]
    fn route_summary_is_preserved_for_build() {
        let original: serde_json::Value = serde_json::from_str(LIVE_ROUTE_DATA).unwrap();
        let data: RouteData = serde_json::from_str(LIVE_ROUTE_DATA).unwrap();
        assert_eq!(data.summary().unwrap().amount_out, "2694223548");

        let build = BuildRouteRequest {
            route_summary: data.route_summary.clone(),
            sender: "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045".into(),
            recipient: "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045".into(),
            slippage_tolerance_bps: Some(50),
            deadline: None,
            enable_permit: None,
        };
        let body = serde_json::to_value(&build).unwrap();
        assert_eq!(body["routeSummary"], original["routeSummary"]);
        assert_eq!(body["routeSummary"]["checksum"], "4239051851851966504");
        assert_eq!(body["slippageTolerance"], 50);
        assert!(body.get("slippageToleranceBps").is_none());
    }
}