This commit is contained in:
2026-04-24 08:53:40 +02:00
parent a7030d7d0f
commit 54778373b8
31 changed files with 1706 additions and 164 deletions

84
kb_lib/src/db/dtos/dex.rs Normal file
View File

@@ -0,0 +1,84 @@
// file: kb_lib/src/db/dtos/dex.rs
//! DEX DTO.
/// Application-facing normalized DEX DTO.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct KbDexDto {
/// Optional numeric primary key.
pub id: std::option::Option<i64>,
/// Stable short code.
pub code: std::string::String,
/// Display name.
pub name: std::string::String,
/// Optional primary program id.
pub program_id: std::option::Option<std::string::String>,
/// Optional router program id.
pub router_program_id: std::option::Option<std::string::String>,
/// Whether this DEX is enabled.
pub is_enabled: bool,
/// Creation timestamp.
pub created_at: chrono::DateTime<chrono::Utc>,
/// Update timestamp.
pub updated_at: chrono::DateTime<chrono::Utc>,
}
impl KbDexDto {
/// Creates a new DEX DTO.
pub fn new(
code: std::string::String,
name: std::string::String,
program_id: std::option::Option<std::string::String>,
router_program_id: std::option::Option<std::string::String>,
is_enabled: bool,
) -> Self {
let now = chrono::Utc::now();
Self {
id: None,
code,
name,
program_id,
router_program_id,
is_enabled,
created_at: now,
updated_at: now,
}
}
}
impl TryFrom<crate::KbDexEntity> for KbDexDto {
type Error = crate::KbError;
fn try_from(entity: crate::KbDexEntity) -> Result<Self, Self::Error> {
let created_at_result = chrono::DateTime::parse_from_rfc3339(&entity.created_at);
let created_at = match created_at_result {
Ok(created_at) => created_at.with_timezone(&chrono::Utc),
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot parse dex created_at '{}': {}",
entity.created_at, error
)));
}
};
let updated_at_result = chrono::DateTime::parse_from_rfc3339(&entity.updated_at);
let updated_at = match updated_at_result {
Ok(updated_at) => updated_at.with_timezone(&chrono::Utc),
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot parse dex updated_at '{}': {}",
entity.updated_at, error
)));
}
};
Ok(Self {
id: Some(entity.id),
code: entity.code,
name: entity.name,
program_id: entity.program_id,
router_program_id: entity.router_program_id,
is_enabled: entity.is_enabled != 0,
created_at,
updated_at,
})
}
}

View File

@@ -0,0 +1,84 @@
// file: kb_lib/src/db/dtos/pair.rs
//! Normalized pair DTO.
/// Application-facing normalized pair DTO.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct KbPairDto {
/// Optional numeric primary key.
pub id: std::option::Option<i64>,
/// Related DEX id.
pub dex_id: i64,
/// Related pool id.
pub pool_id: i64,
/// Base token id.
pub base_token_id: i64,
/// Quote token id.
pub quote_token_id: i64,
/// Optional display symbol.
pub symbol: std::option::Option<std::string::String>,
/// First seen timestamp.
pub first_seen_at: chrono::DateTime<chrono::Utc>,
/// Update timestamp.
pub updated_at: chrono::DateTime<chrono::Utc>,
}
impl KbPairDto {
/// Creates a new pair DTO.
pub fn new(
dex_id: i64,
pool_id: i64,
base_token_id: i64,
quote_token_id: i64,
symbol: std::option::Option<std::string::String>,
) -> Self {
let now = chrono::Utc::now();
Self {
id: None,
dex_id,
pool_id,
base_token_id,
quote_token_id,
symbol,
first_seen_at: now,
updated_at: now,
}
}
}
impl TryFrom<crate::KbPairEntity> for KbPairDto {
type Error = crate::KbError;
fn try_from(entity: crate::KbPairEntity) -> Result<Self, Self::Error> {
let first_seen_at_result = chrono::DateTime::parse_from_rfc3339(&entity.first_seen_at);
let first_seen_at = match first_seen_at_result {
Ok(first_seen_at) => first_seen_at.with_timezone(&chrono::Utc),
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot parse pair first_seen_at '{}': {}",
entity.first_seen_at, error
)));
}
};
let updated_at_result = chrono::DateTime::parse_from_rfc3339(&entity.updated_at);
let updated_at = match updated_at_result {
Ok(updated_at) => updated_at.with_timezone(&chrono::Utc),
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot parse pair updated_at '{}': {}",
entity.updated_at, error
)));
}
};
Ok(Self {
id: Some(entity.id),
dex_id: entity.dex_id,
pool_id: entity.pool_id,
base_token_id: entity.base_token_id,
quote_token_id: entity.quote_token_id,
symbol: entity.symbol,
first_seen_at,
updated_at,
})
}
}

View File

@@ -0,0 +1,89 @@
// file: kb_lib/src/db/dtos/pool.rs
//! Normalized pool DTO.
/// Application-facing normalized pool DTO.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct KbPoolDto {
/// Optional numeric primary key.
pub id: std::option::Option<i64>,
/// Related DEX id.
pub dex_id: i64,
/// Pool address.
pub address: std::string::String,
/// Pool kind.
pub pool_kind: crate::KbPoolKind,
/// Pool status.
pub status: crate::KbPoolStatus,
/// First seen timestamp.
pub first_seen_at: chrono::DateTime<chrono::Utc>,
/// Update timestamp.
pub updated_at: chrono::DateTime<chrono::Utc>,
}
impl KbPoolDto {
/// Creates a new pool DTO.
pub fn new(
dex_id: i64,
address: std::string::String,
pool_kind: crate::KbPoolKind,
status: crate::KbPoolStatus,
) -> Self {
let now = chrono::Utc::now();
Self {
id: None,
dex_id,
address,
pool_kind,
status,
first_seen_at: now,
updated_at: now,
}
}
}
impl TryFrom<crate::KbPoolEntity> for KbPoolDto {
type Error = crate::KbError;
fn try_from(entity: crate::KbPoolEntity) -> Result<Self, Self::Error> {
let pool_kind_result = crate::KbPoolKind::from_i16(entity.pool_kind);
let pool_kind = match pool_kind_result {
Ok(pool_kind) => pool_kind,
Err(error) => return Err(error),
};
let status_result = crate::KbPoolStatus::from_i16(entity.status);
let status = match status_result {
Ok(status) => status,
Err(error) => return Err(error),
};
let first_seen_at_result = chrono::DateTime::parse_from_rfc3339(&entity.first_seen_at);
let first_seen_at = match first_seen_at_result {
Ok(first_seen_at) => first_seen_at.with_timezone(&chrono::Utc),
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot parse pool first_seen_at '{}': {}",
entity.first_seen_at, error
)));
}
};
let updated_at_result = chrono::DateTime::parse_from_rfc3339(&entity.updated_at);
let updated_at = match updated_at_result {
Ok(updated_at) => updated_at.with_timezone(&chrono::Utc),
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot parse pool updated_at '{}': {}",
entity.updated_at, error
)));
}
};
Ok(Self {
id: Some(entity.id),
dex_id: entity.dex_id,
address: entity.address,
pool_kind,
status,
first_seen_at,
updated_at,
})
}
}

View File

@@ -0,0 +1,104 @@
// file: kb_lib/src/db/dtos/pool_listing.rs
//! Pool listing DTO.
/// Application-facing normalized pool listing DTO.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct KbPoolListingDto {
/// Optional numeric primary key.
pub id: std::option::Option<i64>,
/// Related DEX id.
pub dex_id: i64,
/// Related pool id.
pub pool_id: i64,
/// Optional related pair id.
pub pair_id: std::option::Option<i64>,
/// Discovery source family.
pub source_kind: crate::KbObservationSourceKind,
/// Optional source endpoint logical name.
pub source_endpoint_name: std::option::Option<std::string::String>,
/// Detection timestamp.
pub detected_at: chrono::DateTime<chrono::Utc>,
/// Optional initial base reserve estimate.
pub initial_base_reserve: std::option::Option<f64>,
/// Optional initial quote reserve estimate.
pub initial_quote_reserve: std::option::Option<f64>,
/// Optional initial price estimate in quote units.
pub initial_price_quote: std::option::Option<f64>,
/// Update timestamp.
pub updated_at: chrono::DateTime<chrono::Utc>,
}
impl KbPoolListingDto {
/// Creates a new pool listing DTO.
pub fn new(
dex_id: i64,
pool_id: i64,
pair_id: std::option::Option<i64>,
source_kind: crate::KbObservationSourceKind,
source_endpoint_name: std::option::Option<std::string::String>,
initial_base_reserve: std::option::Option<f64>,
initial_quote_reserve: std::option::Option<f64>,
initial_price_quote: std::option::Option<f64>,
) -> Self {
let now = chrono::Utc::now();
Self {
id: None,
dex_id,
pool_id,
pair_id,
source_kind,
source_endpoint_name,
detected_at: now,
initial_base_reserve,
initial_quote_reserve,
initial_price_quote,
updated_at: now,
}
}
}
impl TryFrom<crate::KbPoolListingEntity> for KbPoolListingDto {
type Error = crate::KbError;
fn try_from(entity: crate::KbPoolListingEntity) -> Result<Self, Self::Error> {
let source_kind_result = crate::KbObservationSourceKind::from_i16(entity.source_kind);
let source_kind = match source_kind_result {
Ok(source_kind) => source_kind,
Err(error) => return Err(error),
};
let detected_at_result = chrono::DateTime::parse_from_rfc3339(&entity.detected_at);
let detected_at = match detected_at_result {
Ok(detected_at) => detected_at.with_timezone(&chrono::Utc),
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot parse pool_listing detected_at '{}': {}",
entity.detected_at, error
)));
}
};
let updated_at_result = chrono::DateTime::parse_from_rfc3339(&entity.updated_at);
let updated_at = match updated_at_result {
Ok(updated_at) => updated_at.with_timezone(&chrono::Utc),
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot parse pool_listing updated_at '{}': {}",
entity.updated_at, error
)));
}
};
Ok(Self {
id: Some(entity.id),
dex_id: entity.dex_id,
pool_id: entity.pool_id,
pair_id: entity.pair_id,
source_kind,
source_endpoint_name: entity.source_endpoint_name,
detected_at,
initial_base_reserve: entity.initial_base_reserve,
initial_quote_reserve: entity.initial_quote_reserve,
initial_price_quote: entity.initial_price_quote,
updated_at,
})
}
}

View File

@@ -0,0 +1,89 @@
// file: kb_lib/src/db/dtos/pool_token.rs
//! Pool token DTO.
/// Application-facing normalized pool token DTO.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct KbPoolTokenDto {
/// Optional numeric primary key.
pub id: std::option::Option<i64>,
/// Related pool id.
pub pool_id: i64,
/// Related token id.
pub token_id: i64,
/// Token role.
pub role: crate::KbPoolTokenRole,
/// Optional vault address.
pub vault_address: std::option::Option<std::string::String>,
/// Optional token order inside the pool.
pub token_order: std::option::Option<i64>,
/// Creation timestamp.
pub created_at: chrono::DateTime<chrono::Utc>,
/// Update timestamp.
pub updated_at: chrono::DateTime<chrono::Utc>,
}
impl KbPoolTokenDto {
/// Creates a new pool token DTO.
pub fn new(
pool_id: i64,
token_id: i64,
role: crate::KbPoolTokenRole,
vault_address: std::option::Option<std::string::String>,
token_order: std::option::Option<i64>,
) -> Self {
let now = chrono::Utc::now();
Self {
id: None,
pool_id,
token_id,
role,
vault_address,
token_order,
created_at: now,
updated_at: now,
}
}
}
impl TryFrom<crate::KbPoolTokenEntity> for KbPoolTokenDto {
type Error = crate::KbError;
fn try_from(entity: crate::KbPoolTokenEntity) -> Result<Self, Self::Error> {
let role_result = crate::KbPoolTokenRole::from_i16(entity.role);
let role = match role_result {
Ok(role) => role,
Err(error) => return Err(error),
};
let created_at_result = chrono::DateTime::parse_from_rfc3339(&entity.created_at);
let created_at = match created_at_result {
Ok(created_at) => created_at.with_timezone(&chrono::Utc),
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot parse pool_token created_at '{}': {}",
entity.created_at, error
)));
}
};
let updated_at_result = chrono::DateTime::parse_from_rfc3339(&entity.updated_at);
let updated_at = match updated_at_result {
Ok(updated_at) => updated_at.with_timezone(&chrono::Utc),
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot parse pool_token updated_at '{}': {}",
entity.updated_at, error
)));
}
};
Ok(Self {
id: Some(entity.id),
pool_id: entity.pool_id,
token_id: entity.token_id,
role,
vault_address: entity.vault_address,
token_order: entity.token_order,
created_at,
updated_at,
})
}
}

104
kb_lib/src/db/dtos/token.rs Normal file
View File

@@ -0,0 +1,104 @@
// file: kb_lib/src/db/dtos/token.rs
//! Normalized token DTO.
/// Application-facing normalized token DTO.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct KbTokenDto {
/// Optional numeric primary key.
pub id: std::option::Option<i64>,
/// Mint address.
pub mint: std::string::String,
/// Optional token symbol.
pub symbol: std::option::Option<std::string::String>,
/// Optional token display name.
pub name: std::option::Option<std::string::String>,
/// Optional decimals value.
pub decimals: std::option::Option<u8>,
/// Token program id.
pub token_program: std::string::String,
/// Whether this token is typically used as quote token.
pub is_quote_token: bool,
/// First seen timestamp.
pub first_seen_at: chrono::DateTime<chrono::Utc>,
/// Update timestamp.
pub updated_at: chrono::DateTime<chrono::Utc>,
}
impl KbTokenDto {
/// Creates a new token DTO.
pub fn new(
mint: std::string::String,
symbol: std::option::Option<std::string::String>,
name: std::option::Option<std::string::String>,
decimals: std::option::Option<u8>,
token_program: std::string::String,
is_quote_token: bool,
) -> Self {
let now = chrono::Utc::now();
Self {
id: None,
mint,
symbol,
name,
decimals,
token_program,
is_quote_token,
first_seen_at: now,
updated_at: now,
}
}
}
impl TryFrom<crate::KbTokenEntity> for KbTokenDto {
type Error = crate::KbError;
fn try_from(entity: crate::KbTokenEntity) -> Result<Self, Self::Error> {
let first_seen_at_result = chrono::DateTime::parse_from_rfc3339(&entity.first_seen_at);
let first_seen_at = match first_seen_at_result {
Ok(first_seen_at) => first_seen_at.with_timezone(&chrono::Utc),
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot parse token first_seen_at '{}': {}",
entity.first_seen_at, error
)));
}
};
let updated_at_result = chrono::DateTime::parse_from_rfc3339(&entity.updated_at);
let updated_at = match updated_at_result {
Ok(updated_at) => updated_at.with_timezone(&chrono::Utc),
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot parse token updated_at '{}': {}",
entity.updated_at, error
)));
}
};
let decimals = match entity.decimals {
Some(decimals) => {
let decimals_result = u8::try_from(decimals);
match decimals_result {
Ok(decimals) => Some(decimals),
Err(error) => {
return Err(crate::KbError::Db(format!(
"cannot convert token decimals '{}' to u8: {}",
decimals, error
)));
}
}
}
None => None,
};
Ok(Self {
id: Some(entity.id),
mint: entity.mint,
symbol: entity.symbol,
name: entity.name,
decimals,
token_program: entity.token_program,
is_quote_token: entity.is_quote_token != 0,
first_seen_at,
updated_at,
})
}
}