use std::{collections::BTreeMap, fmt};
use serde::{Deserialize, Serialize};
use crate::{
redaction::RedactedUrlMap,
types::{Address, ChainId},
};
use super::chains::SupportedChainId;
const PROD_BASE_URL: &str = "https://api.cow.fi";
const STAGING_BASE_URL: &str = "https://barn.api.cow.fi";
const PARTNER_PROD_BASE_URL: &str = "https://partners.cow.fi";
const PARTNER_STAGING_BASE_URL: &str = "https://partners.barn.cow.fi";
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(
all(target_arch = "wasm32", target_os = "unknown", feature = "ts-bindings"),
derive(tsify::Tsify)
)]
#[cfg_attr(
all(target_arch = "wasm32", target_os = "unknown", feature = "ts-bindings"),
tsify(into_wasm_abi, from_wasm_abi)
)]
#[serde(rename_all = "lowercase")]
pub enum CowEnv {
Prod,
Staging,
}
impl CowEnv {
pub const ALL: [Self; 2] = [Self::Prod, Self::Staging];
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Prod => "prod",
Self::Staging => "staging",
}
}
}
impl fmt::Display for CowEnv {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
pub type ApiBaseUrls = RedactedUrlMap<ChainId>;
pub type AddressPerChain = BTreeMap<ChainId, Address>;
#[must_use]
pub fn default_api_base_urls(env: CowEnv, partner_api: bool) -> ApiBaseUrls {
SupportedChainId::ALL
.into_iter()
.map(|chain_id| {
let base = match (env, partner_api) {
(CowEnv::Prod, false) => PROD_BASE_URL,
(CowEnv::Staging, false) => STAGING_BASE_URL,
(CowEnv::Prod, true) => PARTNER_PROD_BASE_URL,
(CowEnv::Staging, true) => PARTNER_STAGING_BASE_URL,
};
(chain_id.into(), format!("{base}/{}", chain_id.api_path()))
})
.collect()
}