use serde::{Deserialize, Serialize};
use std::fmt;
use std::str::FromStr;
#[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 {}
#[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 {
#[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,
]
}
#[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,
}
}
#[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,
}
}
#[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",
}
}
#[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())
}
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RouteRequest {
pub token_in: String,
pub token_out: String,
pub amount_in: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub to: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub save_gas: Option<bool>,
#[serde(rename = "includedSources", skip_serializing_if = "Option::is_none")]
pub include_dexs: Option<String>,
#[serde(rename = "excludedSources", skip_serializing_if = "Option::is_none")]
pub exclude_dexs: Option<String>,
#[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
}
#[must_use]
pub fn with_included_sources(mut self, sources: impl Into<String>) -> Self {
self.include_dexs = Some(sources.into());
self
}
#[must_use]
pub fn with_excluded_sources(mut self, sources: impl Into<String>) -> Self {
self.exclude_dexs = Some(sources.into());
self
}
#[must_use]
pub fn with_gas_include(mut self, gas_include: bool) -> Self {
self.gas_include = Some(gas_include);
self
}
#[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
}
pub fn validate(&self) -> Result<(), &'static str> {
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");
}
if let Some(ref to) = self.to {
if !is_valid_address(to) {
return Err("invalid recipient address");
}
}
if !is_valid_amount(&self.amount_in) {
return Err("invalid amount_in");
}
Ok(())
}
}
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())
}
fn is_valid_amount(s: &str) -> bool {
!s.is_empty() && s.chars().all(|c| c.is_ascii_digit())
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RoutesResponse {
pub code: i32,
pub message: String,
pub data: Option<RouteData>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RouteData {
pub route_summary: serde_json::Value,
#[serde(default)]
pub router_address: Option<String>,
}
impl RouteData {
pub fn summary(&self) -> Result<RouteSummary, serde_json::Error> {
RouteSummary::deserialize(&self.route_summary)
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RouteSummary {
pub token_in: String,
pub token_out: String,
pub amount_in: String,
pub amount_out: String,
#[serde(default)]
pub amount_in_usd: Option<String>,
#[serde(default)]
pub amount_out_usd: Option<String>,
#[serde(default)]
pub gas: Option<String>,
#[serde(default)]
pub gas_price: Option<String>,
#[serde(default)]
pub gas_usd: Option<String>,
#[serde(default)]
pub route: Vec<Vec<SwapStep>>,
}
impl RouteSummary {
#[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)
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SwapStep {
pub pool: String,
pub token_in: String,
pub token_out: String,
#[serde(default)]
pub swap_amount: Option<String>,
#[serde(default)]
pub amount_out: Option<String>,
#[serde(default)]
pub exchange: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BuildRouteRequest {
pub route_summary: serde_json::Value,
pub sender: String,
pub recipient: String,
#[serde(rename = "slippageTolerance", skip_serializing_if = "Option::is_none")]
pub slippage_tolerance_bps: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub deadline: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub enable_permit: Option<bool>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BuildRouteResponse {
pub code: i32,
pub message: String,
pub data: Option<BuildRouteData>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BuildRouteData {
pub router_address: String,
pub data: String,
#[serde(default, rename = "transactionValue", alias = "value")]
pub value: Option<String>,
#[serde(default)]
pub gas: Option<String>,
#[serde(default)]
pub amount_in: Option<String>,
#[serde(default)]
pub amount_out: Option<String>,
#[serde(default)]
pub gas_usd: Option<String>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct SwapRequest {
pub token_in: String,
pub token_out: String,
pub amount_in: String,
pub sender: Option<String>,
pub recipient: Option<String>,
pub origin: Option<String>,
pub slippage_tolerance_bps: Option<u32>,
pub deadline: Option<u64>,
pub included_sources: Option<String>,
pub excluded_sources: Option<String>,
pub exclude_rfq_sources: Option<bool>,
pub only_scalable_sources: Option<bool>,
pub only_direct_pools: Option<bool>,
pub only_single_path: Option<bool>,
pub gas_include: Option<bool>,
pub gas_price: Option<String>,
pub charge_fee_by: Option<String>,
pub is_in_bps: Option<bool>,
pub fee_amount: Option<String>,
pub fee_receiver: Option<String>,
pub permit: Option<String>,
pub ignore_capped_slippage: Option<bool>,
pub enable_gas_estimation: Option<bool>,
pub source: Option<String>,
pub referral: Option<String>,
}
impl SwapRequest {
#[must_use]
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(),
..Self::default()
}
}
#[must_use]
pub fn with_sender(mut self, sender: impl Into<String>) -> Self {
self.sender = Some(sender.into());
self
}
#[must_use]
pub fn with_recipient(mut self, recipient: impl Into<String>) -> Self {
self.recipient = Some(recipient.into());
self
}
#[must_use]
pub fn with_origin(mut self, origin: impl Into<String>) -> Self {
self.origin = Some(origin.into());
self
}
#[must_use]
pub fn with_slippage_bps(mut self, bps: u32) -> Self {
self.slippage_tolerance_bps = Some(bps);
self
}
#[must_use]
pub fn with_deadline(mut self, deadline: u64) -> Self {
self.deadline = Some(deadline);
self
}
#[must_use]
pub fn with_included_sources(mut self, sources: impl Into<String>) -> Self {
self.included_sources = Some(sources.into());
self
}
#[must_use]
pub fn with_excluded_sources(mut self, sources: impl Into<String>) -> Self {
self.excluded_sources = Some(sources.into());
self
}
#[must_use]
pub fn with_exclude_rfq_sources(mut self, exclude: bool) -> Self {
self.exclude_rfq_sources = Some(exclude);
self
}
#[must_use]
pub fn with_fee(
mut self,
charge_fee_by: impl Into<String>,
fee_amount: impl Into<String>,
is_in_bps: bool,
fee_receiver: impl Into<String>,
) -> Self {
self.charge_fee_by = Some(charge_fee_by.into());
self.fee_amount = Some(fee_amount.into());
self.is_in_bps = Some(is_in_bps);
self.fee_receiver = Some(fee_receiver.into());
self
}
#[must_use]
pub fn with_gas_estimation(mut self, enabled: bool) -> Self {
self.enable_gas_estimation = Some(enabled);
self
}
#[must_use]
pub fn with_source(mut self, source: impl Into<String>) -> Self {
self.source = Some(source.into());
self
}
#[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()),
];
let strings = [
("sender", &self.sender),
("recipient", &self.recipient),
("origin", &self.origin),
("includedSources", &self.included_sources),
("excludedSources", &self.excluded_sources),
("gasPrice", &self.gas_price),
("chargeFeeBy", &self.charge_fee_by),
("feeAmount", &self.fee_amount),
("feeReceiver", &self.fee_receiver),
("permit", &self.permit),
("source", &self.source),
("referral", &self.referral),
];
for (key, value) in strings {
if let Some(value) = value {
params.push((key, value.clone()));
}
}
let bools = [
("excludeRFQSources", self.exclude_rfq_sources),
("onlyScalableSources", self.only_scalable_sources),
("onlyDirectPools", self.only_direct_pools),
("onlySinglePath", self.only_single_path),
("gasInclude", self.gas_include),
("isInBps", self.is_in_bps),
("ignoreCappedSlippage", self.ignore_capped_slippage),
("enableGasEstimation", self.enable_gas_estimation),
];
for (key, value) in bools {
if let Some(value) = value {
params.push((key, value.to_string()));
}
}
if let Some(bps) = self.slippage_tolerance_bps {
params.push(("slippageTolerance", bps.to_string()));
}
if let Some(deadline) = self.deadline {
params.push(("deadline", deadline.to_string()));
}
params
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct SwapResponse {
pub code: i32,
#[serde(default)]
pub message: Option<String>,
#[serde(default)]
pub data: Option<SwapData>,
#[serde(default)]
pub request_id: Option<String>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct SwapData {
pub route_summary: RouteSummary,
pub router_address: String,
pub data: String,
#[serde(default)]
pub transaction_value: Option<String>,
#[serde(default)]
pub amount_in: Option<String>,
#[serde(default)]
pub amount_in_usd: Option<String>,
#[serde(default)]
pub amount_out: Option<String>,
#[serde(default)]
pub amount_out_usd: Option<String>,
#[serde(default)]
pub gas: Option<String>,
#[serde(default)]
pub gas_usd: Option<String>,
#[serde(default)]
pub additional_cost_usd: Option<String>,
#[serde(default)]
pub additional_cost_message: Option<String>,
}
#[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}");
}
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());
}
}