Skip to content

Commit

Permalink
fix: remove early return for event evaluation (#484)
Browse files Browse the repository at this point in the history
### Description

When evaluatin a stacks predicate of type `ft_event` or `nft_event`, we
were returning from a loop of all events after the first loop. We should
only return early if there is a match, and should continue searching all
events if there isn't. This PR implements that fix.

Fixes #469

---

### Checklist

- [x] All tests pass
- [x] Tests added in this PR (if applicable)
  • Loading branch information
MicaiahReid committed Feb 8, 2024
1 parent 32f4d4e commit 98f9e86
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
31 changes: 25 additions & 6 deletions components/chainhook-sdk/src/chainhooks/stacks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,19 +378,28 @@ pub fn evaluate_stacks_predicate_on_transaction<'a>(
for event in transaction.metadata.receipt.events.iter() {
match (event, expecting_mint, expecting_transfer, expecting_burn) {
(StacksTransactionEvent::FTMintEvent(ft_event), true, _, _) => {
return ft_event
if ft_event
.asset_class_identifier
.eq(&expected_event.asset_identifier)
{
return true;
}
}
(StacksTransactionEvent::FTTransferEvent(ft_event), _, true, _) => {
return ft_event
if ft_event
.asset_class_identifier
.eq(&expected_event.asset_identifier)
{
return true;
}
}
(StacksTransactionEvent::FTBurnEvent(ft_event), _, _, true) => {
return ft_event
if ft_event
.asset_class_identifier
.eq(&expected_event.asset_identifier)
{
return true;
}
}
_ => continue,
}
Expand All @@ -401,22 +410,32 @@ pub fn evaluate_stacks_predicate_on_transaction<'a>(
let expecting_mint = expected_event.actions.contains(&"mint".to_string());
let expecting_transfer = expected_event.actions.contains(&"transfer".to_string());
let expecting_burn = expected_event.actions.contains(&"burn".to_string());

for event in transaction.metadata.receipt.events.iter() {
match (event, expecting_mint, expecting_transfer, expecting_burn) {
(StacksTransactionEvent::NFTMintEvent(nft_event), true, _, _) => {
return nft_event
if nft_event
.asset_class_identifier
.eq(&expected_event.asset_identifier)
{
return true;
}
}
(StacksTransactionEvent::NFTTransferEvent(nft_event), _, true, _) => {
return nft_event
if nft_event
.asset_class_identifier
.eq(&expected_event.asset_identifier)
{
return true;
}
}
(StacksTransactionEvent::NFTBurnEvent(nft_event), _, _, true) => {
return nft_event
if nft_event
.asset_class_identifier
.eq(&expected_event.asset_identifier)
{
return true;
}
}
_ => continue,
}
Expand Down
38 changes: 38 additions & 0 deletions components/chainhook-sdk/src/chainhooks/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ pub mod fixtures;
1;
"FtEvent predicates match transfer event"
)]
#[test_case(
vec![vec![StacksTransactionEvent::FTTransferEvent(chainhook_types::FTTransferEventData {
sender: "".to_string(),
asset_class_identifier: "different-id".to_string(),
amount: "".to_string(),
recipient: "".to_string(),
}), StacksTransactionEvent::FTTransferEvent(chainhook_types::FTTransferEventData {
sender: "".to_string(),
asset_class_identifier: "asset-id".to_string(),
amount: "".to_string(),
recipient: "".to_string(),
})]],
StacksPredicate::FtEvent(StacksFtEventBasedPredicate {
asset_identifier: "asset-id".to_string(),
actions: vec!["transfer".to_string()]
}),
1;
"FtEvent predicates match transfer event if matching event is not first in transaction"
)]
#[test_case(
vec![vec![get_test_event_by_type("ft_burn")]],
StacksPredicate::FtEvent(StacksFtEventBasedPredicate {
Expand Down Expand Up @@ -121,6 +140,25 @@ pub mod fixtures;
1;
"NftEvent predicates match transfer event"
)]
#[test_case(
vec![vec![StacksTransactionEvent::NFTTransferEvent(chainhook_types::NFTTransferEventData {
sender: "".to_string(),
asset_class_identifier: "different-id".to_string(),
hex_asset_identifier: "different-id".to_string(),
recipient: "".to_string(),
}), StacksTransactionEvent::NFTTransferEvent(chainhook_types::NFTTransferEventData {
sender: "".to_string(),
asset_class_identifier: "asset-id".to_string(),
hex_asset_identifier: "asset-id".to_string(),
recipient: "".to_string(),
})]],
StacksPredicate::NftEvent(StacksNftEventBasedPredicate {
asset_identifier: "asset-id".to_string(),
actions: vec!["transfer".to_string()]
}),
1;
"NftEvent predicates match transfer event if matching event is not first in transaction"
)]
#[test_case(
vec![vec![get_test_event_by_type("nft_burn")]],
StacksPredicate::NftEvent(StacksNftEventBasedPredicate {
Expand Down

0 comments on commit 98f9e86

Please sign in to comment.