Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
Add get_and_map_to to Database (#993)
Browse files Browse the repository at this point in the history
* Add get_and_map_to to Database

* update
  • Loading branch information
tcoratger authored Apr 21, 2024
1 parent 998ec71 commit 1817700
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
17 changes: 17 additions & 0 deletions src/eth_provider/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::eth_provider::database::types::{
transaction::{StoredPendingTransaction, StoredTransaction, StoredTransactionHash},
};
use futures::TryStreamExt;
use itertools::Itertools;
use mongodb::{
bson::{doc, Document},
options::{FindOneOptions, FindOptions, UpdateModifications, UpdateOptions},
Expand Down Expand Up @@ -57,6 +58,22 @@ impl Database {
Ok(self.collection::<T>().find(filter, find_options).await?.try_collect().await?)
}

/// Retrieves documents from a collection and converts them into another type.
///
/// Returns a vector of documents of type `D` if successful, or an error.
pub async fn get_and_map_to<D, T>(
&self,
filter: impl Into<Option<Document>>,
project: impl Into<Option<Document>>,
) -> DatabaseResult<Vec<D>>
where
T: DeserializeOwned + CollectionName,
D: From<T>,
{
let stored_data: Vec<T> = self.get(filter, project).await?;
Ok(stored_data.into_iter().map_into().collect())
}

/// Get a single document from a collection
pub async fn get_one<T>(
&self,
Expand Down
16 changes: 9 additions & 7 deletions src/eth_provider/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ use super::starknet::kakarot_core::{
KAKAROT_ADDRESS,
};
use super::starknet::{ERC20Reader, STARKNET_NATIVE_TOKEN};
use super::utils::{
contract_not_found, entrypoint_not_found, into_filter, iter_into, split_u256, try_from_u8_iterator,
};
use super::utils::{contract_not_found, entrypoint_not_found, into_filter, split_u256, try_from_u8_iterator};
use crate::eth_provider::utils::format_hex;
use crate::models::block::{EthBlockId, EthBlockNumberOrTag};
use crate::models::felt::Felt252Wrapper;
Expand Down Expand Up @@ -805,11 +803,15 @@ where
};

let block_transactions = if full {
BlockTransactions::Full(iter_into(self.database.get::<StoredTransaction>(transactions_filter, None).await?))
BlockTransactions::Full(
self.database.get_and_map_to::<_, StoredTransaction>(transactions_filter, None).await?,
)
} else {
BlockTransactions::Hashes(iter_into(
self.database.get::<StoredTransactionHash>(transactions_filter, doc! {"tx.hash": 1}).await?,
))
BlockTransactions::Hashes(
self.database
.get_and_map_to::<_, StoredTransactionHash>(transactions_filter, doc! {"tx.hash": 1})
.await?,
)
};

Ok(block_transactions)
Expand Down
7 changes: 0 additions & 7 deletions src/eth_provider/utils.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
use std::fmt::LowerHex;

use cainome::cairo_serde::Error;
use itertools::Itertools;
use mongodb::bson::{doc, Document};
use reth_primitives::{U128, U256};
use starknet::{
core::types::{ContractErrorData, StarknetError},
providers::ProviderError,
};

/// Converts an iterator of `Into<D>` into a `Vec<D>`.
#[inline]
pub(crate) fn iter_into<D, S: Into<D>>(iter: impl IntoIterator<Item = S>) -> Vec<D> {
iter.into_iter().map_into().collect()
}

/// Converts an iterator of `TryInto<u8>` into a `FromIterator<u8>`.
#[inline]
pub(crate) fn try_from_u8_iterator<I: TryInto<u8>, T: FromIterator<u8>>(it: impl IntoIterator<Item = I>) -> T {
Expand Down

0 comments on commit 1817700

Please sign in to comment.