Skip to content

Commit 34cb073

Browse files
feat: balance & history provider analytics (#954)
* feat: balance & history provider analytics * fix: fixing a bug where only Zerion provider will be used for EVM --------- Co-authored-by: Max Kalashnikoff <geekmaks@gmail.com>
1 parent d6362ff commit 34cb073

File tree

9 files changed

+87
-46
lines changed

9 files changed

+87
-46
lines changed

src/analytics/balance_lookup_info.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use {parquet_derive::ParquetRecordWriter, serde::Serialize, std::sync::Arc};
1+
use {
2+
crate::providers::ProviderKind, parquet_derive::ParquetRecordWriter, serde::Serialize,
3+
std::sync::Arc,
4+
};
25

36
#[derive(Debug, Clone, Serialize, ParquetRecordWriter)]
47
#[serde(rename_all = "camelCase")]
@@ -15,6 +18,8 @@ pub struct BalanceLookupInfo {
1518
pub address: String,
1619
pub project_id: String,
1720

21+
pub provider: String,
22+
1823
pub origin: Option<String>,
1924
pub region: Option<String>,
2025
pub country: Option<Arc<str>>,
@@ -36,6 +41,7 @@ impl BalanceLookupInfo {
3641
currency: String,
3742
address: String,
3843
project_id: String,
44+
provider: &ProviderKind,
3945
origin: Option<String>,
4046
region: Option<Vec<String>>,
4147
country: Option<Arc<str>>,
@@ -53,6 +59,7 @@ impl BalanceLookupInfo {
5359
currency,
5460
address,
5561
project_id,
62+
provider: provider.to_string(),
5663
origin,
5764
region: region.map(|r| r.join(", ")),
5865
country,

src/analytics/history_lookup_info.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use {
2+
crate::providers::ProviderKind,
23
parquet_derive::ParquetRecordWriter,
34
serde::Serialize,
45
std::{sync::Arc, time::Duration},
@@ -19,6 +20,8 @@ pub struct HistoryLookupInfo {
1920
pub fungibles_count: usize,
2021
pub nft_count: usize,
2122

23+
pub provider: String,
24+
2225
pub origin: Option<String>,
2326
pub region: Option<String>,
2427
pub country: Option<Arc<str>>,
@@ -39,6 +42,7 @@ impl HistoryLookupInfo {
3942
transfers_count: usize,
4043
fungibles_count: usize,
4144
nft_count: usize,
45+
provider: &ProviderKind,
4246
origin: Option<String>,
4347
region: Option<Vec<String>>,
4448
country: Option<Arc<str>>,
@@ -55,6 +59,7 @@ impl HistoryLookupInfo {
5559
transfers_count,
5660
fungibles_count,
5761
nft_count,
62+
provider: provider.to_string(),
5863
origin,
5964
region: region.map(|r| r.join(", ")),
6065
country,

src/handlers/balance.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -228,17 +228,17 @@ async fn handler_internal(
228228

229229
match provider_response {
230230
Ok(response) => {
231-
balance_response = Some(response);
231+
balance_response = Some((response, provider.provider_kind()));
232232
break;
233233
}
234234
e => {
235235
debug!("Balance provider returned an error {e:?}, trying the next provider");
236236
}
237237
};
238238
}
239-
let mut response = balance_response.ok_or(RpcError::BalanceTemporarilyUnavailable(
240-
namespace.to_string(),
241-
))?;
239+
let (mut response, provider_kind) = balance_response.ok_or(
240+
RpcError::BalanceTemporarilyUnavailable(namespace.to_string()),
241+
)?;
242242

243243
{
244244
let origin = headers
@@ -262,6 +262,7 @@ async fn handler_internal(
262262
query.currency.to_string(),
263263
address.clone(),
264264
project_id.clone(),
265+
&provider_kind,
265266
origin.clone(),
266267
region.clone(),
267268
country.clone(),

src/handlers/history.rs

+41-41
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,12 @@ async fn handler_internal(
178178
}
179179
} else {
180180
state.validate_project_access_and_quota(&project_id).await?;
181-
history_provider_kind = ProviderKind::Zerion;
182181
let provider = state
183182
.providers
184183
.history_providers
185184
.get(&namespace)
186185
.ok_or_else(|| RpcError::UnsupportedNamespace(namespace))?;
186+
history_provider_kind = provider.provider_kind();
187187
provider
188188
.get_transactions(address.clone(), query.0.clone(), state.metrics.clone())
189189
.await
@@ -207,9 +207,46 @@ async fn handler_internal(
207207
.map(|geo| (geo.country, geo.continent, geo.region))
208208
.unwrap_or((None, None, None));
209209

210-
// Different analytics for different history providers
210+
// Analytics schema exception for Coinbase Onramp
211211
match history_provider_kind {
212-
ProviderKind::Zerion => {
212+
ProviderKind::Coinbase => {
213+
for transaction in response.clone().data {
214+
state
215+
.analytics
216+
.onramp_history_lookup(OnrampHistoryLookupInfo::new(
217+
transaction.id,
218+
latency_tracker,
219+
address.clone(),
220+
project_id.clone(),
221+
origin.clone(),
222+
region.clone(),
223+
country.clone(),
224+
continent.clone(),
225+
transaction.metadata.status,
226+
transaction
227+
.transfers
228+
.as_ref()
229+
.map(|v| {
230+
v.first()
231+
.and_then(|item| {
232+
item.fungible_info.as_ref().map(|info| info.name.clone())
233+
})
234+
.unwrap_or_default()
235+
})
236+
.unwrap_or(None)
237+
.unwrap_or_default(),
238+
transaction.metadata.chain.clone().unwrap_or_default(),
239+
transaction
240+
.transfers
241+
.as_ref()
242+
.map(|v| v[0].quantity.numeric.clone())
243+
.unwrap_or_default(),
244+
query.sdk_info.sv.clone(),
245+
query.sdk_info.st.clone(),
246+
));
247+
}
248+
}
249+
_ => {
213250
state.analytics.history_lookup(HistoryLookupInfo::new(
214251
address,
215252
project_id,
@@ -250,6 +287,7 @@ async fn handler_internal(
250287
.unwrap_or(0)
251288
})
252289
.sum(),
290+
&history_provider_kind,
253291
origin,
254292
region,
255293
country,
@@ -258,44 +296,6 @@ async fn handler_internal(
258296
query.sdk_info.st.clone(),
259297
));
260298
}
261-
ProviderKind::Coinbase => {
262-
for transaction in response.clone().data {
263-
state
264-
.analytics
265-
.onramp_history_lookup(OnrampHistoryLookupInfo::new(
266-
transaction.id,
267-
latency_tracker,
268-
address.clone(),
269-
project_id.clone(),
270-
origin.clone(),
271-
region.clone(),
272-
country.clone(),
273-
continent.clone(),
274-
transaction.metadata.status,
275-
transaction
276-
.transfers
277-
.as_ref()
278-
.map(|v| {
279-
v.first()
280-
.and_then(|item| {
281-
item.fungible_info.as_ref().map(|info| info.name.clone())
282-
})
283-
.unwrap_or_default()
284-
})
285-
.unwrap_or(None)
286-
.unwrap_or_default(),
287-
transaction.metadata.chain.clone().unwrap_or_default(),
288-
transaction
289-
.transfers
290-
.as_ref()
291-
.map(|v| v[0].quantity.numeric.clone())
292-
.unwrap_or_default(),
293-
query.sdk_info.sv.clone(),
294-
query.sdk_info.st.clone(),
295-
));
296-
}
297-
}
298-
_ => {}
299299
}
300300

301301
let latency_tracker = latency_tracker_start

src/providers/coinbase.rs

+4
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ impl HistoryProvider for CoinbaseProvider {
170170
next: body.next_page_key,
171171
})
172172
}
173+
174+
fn provider_kind(&self) -> ProviderKind {
175+
self.provider_kind
176+
}
173177
}
174178

175179
#[async_trait]

src/providers/dune.rs

+4
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,10 @@ impl BalanceProvider for DuneProvider {
312312
balances: balances_vec,
313313
})
314314
}
315+
316+
fn provider_kind(&self) -> ProviderKind {
317+
self.provider_kind
318+
}
315319
}
316320

317321
impl BalanceProviderFactory<DuneConfig> for DuneProvider {

src/providers/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,8 @@ pub trait HistoryProvider: Send + Sync {
863863
params: HistoryQueryParams,
864864
metrics: Arc<Metrics>,
865865
) -> RpcResult<HistoryResponseBody>;
866+
867+
fn provider_kind(&self) -> ProviderKind;
866868
}
867869

868870
#[async_trait]
@@ -899,6 +901,8 @@ pub trait BalanceProvider: Send + Sync {
899901
metadata_cache: &Option<Arc<dyn KeyValueStorage<TokenMetadataCacheItem>>>,
900902
metrics: Arc<Metrics>,
901903
) -> RpcResult<BalanceResponseBody>;
904+
905+
fn provider_kind(&self) -> ProviderKind;
902906
}
903907

904908
pub trait BalanceProviderFactory<T: BalanceProviderConfig>: BalanceProvider {

src/providers/solscan.rs

+8
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,10 @@ impl BalanceProvider for SolScanProvider {
482482

483483
Ok(response)
484484
}
485+
486+
fn provider_kind(&self) -> ProviderKind {
487+
self.provider_kind
488+
}
485489
}
486490

487491
impl BalanceProviderFactory<SolScanConfig> for SolScanProvider {
@@ -597,6 +601,10 @@ impl HistoryProvider for SolScanProvider {
597601
next,
598602
})
599603
}
604+
605+
fn provider_kind(&self) -> ProviderKind {
606+
self.provider_kind
607+
}
600608
}
601609

602610
#[async_trait]

src/providers/zerion.rs

+8
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,10 @@ impl HistoryProvider for ZerionProvider {
383383
next,
384384
})
385385
}
386+
387+
fn provider_kind(&self) -> ProviderKind {
388+
self.provider_kind
389+
}
386390
}
387391

388392
#[async_trait]
@@ -567,6 +571,10 @@ impl BalanceProvider for ZerionProvider {
567571
balances: balances_vec,
568572
})
569573
}
574+
575+
fn provider_kind(&self) -> ProviderKind {
576+
self.provider_kind
577+
}
570578
}
571579

572580
impl BalanceProviderFactory<ZerionConfig> for ZerionProvider {

0 commit comments

Comments
 (0)