Skip to content

Commit df3b848

Browse files
committed
fix(tap-agent): fix v2 unfinalized txs query
1 parent 435aa74 commit df3b848

File tree

1 file changed

+76
-14
lines changed

1 file changed

+76
-14
lines changed

crates/tap-agent/src/agent/sender_account.rs

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -631,14 +631,6 @@ impl State {
631631
sender_balance = self.sender_balance.to_u128(),
632632
"Denying sender."
633633
);
634-
// Check if this is horizon like sender and if it is actually enable,
635-
// otherwise just ignore.
636-
// FIXME: This should be removed once full horizon support
637-
// is implemented!
638-
if matches!(self.sender_type, SenderType::Horizon) && !self.config.horizon_enabled {
639-
return;
640-
}
641-
642634
SenderAccount::deny_sender(self.sender_type, &self.pgpool, self.sender).await;
643635
self.denied = true;
644636
SENDER_DENIED
@@ -865,15 +857,85 @@ impl Actor for SenderAccount {
865857
_ => vec![],
866858
}
867859
}
868-
// TODO Implement query for unfinalized v2 transactions
869-
// Depends on Escrow Subgraph Schema
870860
SenderType::Horizon => {
871861
if config.horizon_enabled {
872-
// TODO: Implement query for unfinalized v2 transactions, It depends on Escrow Subgraph Schema
873-
todo!("Implement query for unfinalized v2 transactions, It depends on Escrow Subgraph Schema")
862+
// V2 doesn't have transaction tracking like V1, but we can check if the RAVs
863+
// we're about to redeem are still the latest ones by querying LatestRavs.
864+
// If the subgraph has newer RAVs, it means ours were already redeemed.
865+
use indexer_query::latest_ravs_v2::{self, LatestRavs};
866+
867+
let collection_ids: Vec<String> = last_non_final_ravs
868+
.iter()
869+
.map(|(collection_id, _)| collection_id.clone())
870+
.collect();
871+
872+
if !collection_ids.is_empty() {
873+
// For V2, use the indexer address as the data service since the indexer
874+
// is providing the data service for the queries
875+
let data_service = config.indexer_address;
876+
877+
match escrow_subgraph
878+
.query::<LatestRavs, _>(latest_ravs_v2::Variables {
879+
payer: format!("{:x?}", sender_id),
880+
data_service: format!("{:x?}", data_service),
881+
service_provider: format!("{:x?}", config.indexer_address),
882+
collection_ids: collection_ids.clone(),
883+
})
884+
.await
885+
{
886+
Ok(Ok(response)) => {
887+
// Create a map of our current RAVs for easy lookup
888+
let our_ravs: HashMap<String, u128> = last_non_final_ravs
889+
.iter()
890+
.map(|(collection_id, value)| {
891+
let value_u128 = value
892+
.to_bigint()
893+
.and_then(|v| v.to_u128())
894+
.unwrap_or(0);
895+
(collection_id.clone(), value_u128)
896+
})
897+
.collect();
898+
899+
// Check which RAVs have been updated (indicating redemption)
900+
let mut finalized_allocation_ids = vec![];
901+
for rav in response.latest_ravs {
902+
if let Some(&our_value) = our_ravs.get(&rav.id) {
903+
// If the subgraph RAV has higher value, our RAV was redeemed
904+
if let Ok(subgraph_value) =
905+
rav.value_aggregate.parse::<u128>()
906+
{
907+
if subgraph_value > our_value {
908+
// Return collection ID string for filtering
909+
finalized_allocation_ids.push(rav.id);
910+
}
911+
}
912+
}
913+
}
914+
finalized_allocation_ids
915+
}
916+
Ok(Err(e)) => {
917+
tracing::warn!(
918+
error = %e,
919+
sender = %sender_id,
920+
"Failed to query V2 latest RAVs, assuming none are finalized"
921+
);
922+
vec![]
923+
}
924+
Err(e) => {
925+
tracing::warn!(
926+
error = %e,
927+
sender = %sender_id,
928+
"Failed to execute V2 latest RAVs query, assuming none are finalized"
929+
);
930+
vec![]
931+
}
932+
}
933+
} else {
934+
vec![]
935+
}
936+
} else {
937+
vec![]
874938
}
875-
// if we have any problems, we don't want to filter out
876-
vec![]
877939
}
878940
};
879941

0 commit comments

Comments
 (0)