nautilus_blockchain/cache/rows.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3// https://poseitrader.io
4//
5// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6// You may not use this file except in compliance with the License.
7// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use alloy::primitives::Address;
17use nautilus_core::UnixNanos;
18use sqlx::{FromRow, Row, postgres::PgRow};
19
20use crate::validation::validate_address;
21
22/// A data transfer object that maps database rows to token data.
23///
24/// Implements `FromRow` trait to automatically convert PostgreSQL results into `TokenRow`
25/// objects that can be transformed into domain entity `Token` objects.
26#[derive(Debug)]
27pub struct TokenRow {
28 pub address: Address,
29 pub name: String,
30 pub symbol: String,
31 pub decimals: i32,
32}
33
34impl<'r> FromRow<'r, PgRow> for TokenRow {
35 fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
36 let address = validate_address(row.try_get::<String, _>("address")?.as_str()).unwrap();
37 let name = row.try_get::<String, _>("name")?;
38 let symbol = row.try_get::<String, _>("symbol")?;
39 let decimals = row.try_get::<i32, _>("decimals")?;
40
41 let token = Self {
42 address,
43 name,
44 symbol,
45 decimals,
46 };
47 Ok(token)
48 }
49}
50
51/// A data transfer object that maps database rows to block timestamp data.
52#[derive(Debug)]
53pub struct BlockTimestampRow {
54 /// The block number.
55 pub number: u64,
56 /// The block timestamp.
57 pub timestamp: UnixNanos,
58}
59
60impl FromRow<'_, PgRow> for BlockTimestampRow {
61 fn from_row(row: &PgRow) -> Result<Self, sqlx::Error> {
62 let number = row.try_get::<i64, _>("number")? as u64;
63 let timestamp = row.try_get::<String, _>("timestamp")?;
64 Ok(Self {
65 number,
66 timestamp: UnixNanos::from(timestamp),
67 })
68 }
69}