Skip to content

Commit

Permalink
fix: add event index to transaction events
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed Sep 18, 2023
1 parent 1868a06 commit e8889f5
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 120 deletions.
58 changes: 30 additions & 28 deletions components/chainhook-sdk/src/chainhooks/stacks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::types::{
StacksContractDeploymentPredicate, StacksPredicate, StacksPrintEventBasedPredicate,
};
use chainhook_types::{
BlockIdentifier, StacksChainEvent, StacksTransactionData, StacksTransactionEvent,
BlockIdentifier, StacksChainEvent, StacksTransactionData, StacksTransactionEventData,
StacksTransactionKind, TransactionIdentifier,
};
use hiro_system_kit::slog;
Expand Down Expand Up @@ -377,17 +377,17 @@ 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, _, _) => {
(StacksTransactionEventData::FTMintEvent(ft_event), true, _, _) => {
return ft_event
.asset_class_identifier
.eq(&expected_event.asset_identifier)
}
(StacksTransactionEvent::FTTransferEvent(ft_event), _, true, _) => {
(StacksTransactionEventData::FTTransferEvent(ft_event), _, true, _) => {
return ft_event
.asset_class_identifier
.eq(&expected_event.asset_identifier)
}
(StacksTransactionEvent::FTBurnEvent(ft_event), _, _, true) => {
(StacksTransactionEventData::FTBurnEvent(ft_event), _, _, true) => {
return ft_event
.asset_class_identifier
.eq(&expected_event.asset_identifier)
Expand All @@ -403,17 +403,17 @@ pub fn evaluate_stacks_predicate_on_transaction<'a>(
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, _, _) => {
(StacksTransactionEventData::NFTMintEvent(nft_event), true, _, _) => {
return nft_event
.asset_class_identifier
.eq(&expected_event.asset_identifier)
}
(StacksTransactionEvent::NFTTransferEvent(nft_event), _, true, _) => {
(StacksTransactionEventData::NFTTransferEvent(nft_event), _, true, _) => {
return nft_event
.asset_class_identifier
.eq(&expected_event.asset_identifier)
}
(StacksTransactionEvent::NFTBurnEvent(nft_event), _, _, true) => {
(StacksTransactionEventData::NFTBurnEvent(nft_event), _, _, true) => {
return nft_event
.asset_class_identifier
.eq(&expected_event.asset_identifier)
Expand All @@ -437,10 +437,12 @@ pub fn evaluate_stacks_predicate_on_transaction<'a>(
expecting_lock,
expecting_burn,
) {
(StacksTransactionEvent::STXMintEvent(_), true, _, _, _) => return true,
(StacksTransactionEvent::STXTransferEvent(_), _, true, _, _) => return true,
(StacksTransactionEvent::STXLockEvent(_), _, _, true, _) => return true,
(StacksTransactionEvent::STXBurnEvent(_), _, _, _, true) => return true,
(StacksTransactionEventData::STXMintEvent(_), true, _, _, _) => return true,
(StacksTransactionEventData::STXTransferEvent(_), _, true, _, _) => {
return true
}
(StacksTransactionEventData::STXLockEvent(_), _, _, true, _) => return true,
(StacksTransactionEventData::STXBurnEvent(_), _, _, _, true) => return true,
_ => continue,
}
}
Expand All @@ -449,7 +451,7 @@ pub fn evaluate_stacks_predicate_on_transaction<'a>(
StacksPredicate::PrintEvent(expected_event) => {
for event in transaction.metadata.receipt.events.iter() {
match event {
StacksTransactionEvent::SmartContractEvent(actual) => {
StacksTransactionEventData::SmartContractEvent(actual) => {
if actual.topic == "print" {
match expected_event {
StacksPrintEventBasedPredicate::Contains {
Expand Down Expand Up @@ -539,35 +541,35 @@ fn encode_transaction_including_with_clarity_decoding(
}

pub fn serialized_event_with_decoded_clarity_value(
event: &StacksTransactionEvent,
event: &StacksTransactionEventData,
ctx: &Context,
) -> serde_json::Value {
match event {
StacksTransactionEvent::STXTransferEvent(payload) => {
StacksTransactionEventData::STXTransferEvent(payload) => {
json!({
"type": "STXTransferEvent",
"data": payload
})
}
StacksTransactionEvent::STXMintEvent(payload) => {
StacksTransactionEventData::STXMintEvent(payload) => {
json!({
"type": "STXMintEvent",
"data": payload
})
}
StacksTransactionEvent::STXLockEvent(payload) => {
StacksTransactionEventData::STXLockEvent(payload) => {
json!({
"type": "STXLockEvent",
"data": payload
})
}
StacksTransactionEvent::STXBurnEvent(payload) => {
StacksTransactionEventData::STXBurnEvent(payload) => {
json!({
"type": "STXBurnEvent",
"data": payload
})
}
StacksTransactionEvent::NFTTransferEvent(payload) => {
StacksTransactionEventData::NFTTransferEvent(payload) => {
json!({
"type": "NFTTransferEvent",
"data": {
Expand All @@ -578,7 +580,7 @@ pub fn serialized_event_with_decoded_clarity_value(
}
})
}
StacksTransactionEvent::NFTMintEvent(payload) => {
StacksTransactionEventData::NFTMintEvent(payload) => {
json!({
"type": "NFTMintEvent",
"data": {
Expand All @@ -588,7 +590,7 @@ pub fn serialized_event_with_decoded_clarity_value(
}
})
}
StacksTransactionEvent::NFTBurnEvent(payload) => {
StacksTransactionEventData::NFTBurnEvent(payload) => {
json!({
"type": "NFTBurnEvent",
"data": {
Expand All @@ -598,25 +600,25 @@ pub fn serialized_event_with_decoded_clarity_value(
}
})
}
StacksTransactionEvent::FTTransferEvent(payload) => {
StacksTransactionEventData::FTTransferEvent(payload) => {
json!({
"type": "FTTransferEvent",
"data": payload
})
}
StacksTransactionEvent::FTMintEvent(payload) => {
StacksTransactionEventData::FTMintEvent(payload) => {
json!({
"type": "FTMintEvent",
"data": payload
})
}
StacksTransactionEvent::FTBurnEvent(payload) => {
StacksTransactionEventData::FTBurnEvent(payload) => {
json!({
"type": "FTBurnEvent",
"data": payload
})
}
StacksTransactionEvent::DataVarSetEvent(payload) => {
StacksTransactionEventData::DataVarSetEvent(payload) => {
json!({
"type": "DataVarSetEvent",
"data": {
Expand All @@ -626,7 +628,7 @@ pub fn serialized_event_with_decoded_clarity_value(
}
})
}
StacksTransactionEvent::DataMapInsertEvent(payload) => {
StacksTransactionEventData::DataMapInsertEvent(payload) => {
json!({
"type": "DataMapInsertEvent",
"data": {
Expand All @@ -637,7 +639,7 @@ pub fn serialized_event_with_decoded_clarity_value(
}
})
}
StacksTransactionEvent::DataMapUpdateEvent(payload) => {
StacksTransactionEventData::DataMapUpdateEvent(payload) => {
json!({
"type": "DataMapUpdateEvent",
"data": {
Expand All @@ -648,7 +650,7 @@ pub fn serialized_event_with_decoded_clarity_value(
}
})
}
StacksTransactionEvent::DataMapDeleteEvent(payload) => {
StacksTransactionEventData::DataMapDeleteEvent(payload) => {
json!({
"type": "DataMapDeleteEvent",
"data": {
Expand All @@ -658,7 +660,7 @@ pub fn serialized_event_with_decoded_clarity_value(
}
})
}
StacksTransactionEvent::SmartContractEvent(payload) => {
StacksTransactionEventData::SmartContractEvent(payload) => {
json!({
"type": "SmartContractEvent",
"data": {
Expand Down
35 changes: 18 additions & 17 deletions components/chainhook-sdk/src/chainhooks/tests/fixtures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use chainhook_types::StacksBlockData;
use chainhook_types::{
FTBurnEventData, FTMintEventData, FTTransferEventData, NFTBurnEventData, NFTMintEventData,
NFTTransferEventData, STXBurnEventData, STXLockEventData, STXMintEventData,
STXTransferEventData, SmartContractEventData, StacksTransactionData, StacksTransactionEvent,
STXTransferEventData, SmartContractEventData, StacksTransactionData,
StacksTransactionEventData,
};
use std::collections::HashMap;

Expand All @@ -26,7 +27,7 @@ pub fn get_stacks_testnet_block(block_height: u64) -> &'static StacksBlockData {
}

pub fn build_stacks_testnet_block_from_smart_contract_event_data(
events: &Vec<StacksTransactionEvent>,
events: &Vec<StacksTransactionEventData>,
) -> StacksBlockData {
let mut base_block =
load_stacks_block_fixture(std::include_str!("stacks/testnet/base/block.json"));
Expand Down Expand Up @@ -71,7 +72,7 @@ pub fn get_expected_occurrence() -> String {
std::include_str!("stacks/testnet/occurrence.json").to_owned()
}

pub fn get_all_event_types() -> Vec<StacksTransactionEvent> {
pub fn get_all_event_types() -> Vec<StacksTransactionEventData> {
vec![
get_test_event_by_type("stx_transfer"),
get_test_event_by_type("stx_mint"),
Expand All @@ -88,54 +89,54 @@ pub fn get_all_event_types() -> Vec<StacksTransactionEvent> {
get_test_event_by_type("smart_contract_not_print_event"),
]
}
pub fn get_test_event_by_type(event_type: &str) -> StacksTransactionEvent {
pub fn get_test_event_by_type(event_type: &str) -> StacksTransactionEventData {
match event_type {
"stx_transfer" => StacksTransactionEvent::STXTransferEvent(STXTransferEventData {
"stx_transfer" => StacksTransactionEventData::STXTransferEvent(STXTransferEventData {
sender: "".to_string(),
recipient: "".to_string(),
amount: "".to_string(),
}),
"stx_mint" => StacksTransactionEvent::STXMintEvent(STXMintEventData {
"stx_mint" => StacksTransactionEventData::STXMintEvent(STXMintEventData {
recipient: "".to_string(),
amount: "".to_string(),
}),
"stx_lock" => StacksTransactionEvent::STXLockEvent(STXLockEventData {
"stx_lock" => StacksTransactionEventData::STXLockEvent(STXLockEventData {
locked_amount: "".to_string(),
unlock_height: "".to_string(),
locked_address: "".to_string(),
}),
"stx_burn" => StacksTransactionEvent::STXBurnEvent(STXBurnEventData {
"stx_burn" => StacksTransactionEventData::STXBurnEvent(STXBurnEventData {
sender: "".to_string(),
amount: "".to_string(),
}),
"nft_transfer" => StacksTransactionEvent::NFTTransferEvent(NFTTransferEventData {
"nft_transfer" => StacksTransactionEventData::NFTTransferEvent(NFTTransferEventData {
sender: "".to_string(),
asset_class_identifier: "asset-id".to_string(),
hex_asset_identifier: "asset-id".to_string(),
recipient: "".to_string(),
}),
"nft_mint" => StacksTransactionEvent::NFTMintEvent(NFTMintEventData {
"nft_mint" => StacksTransactionEventData::NFTMintEvent(NFTMintEventData {
asset_class_identifier: "asset-id".to_string(),
hex_asset_identifier: "asset-id".to_string(),
recipient: "".to_string(),
}),
"nft_burn" => StacksTransactionEvent::NFTBurnEvent(NFTBurnEventData {
"nft_burn" => StacksTransactionEventData::NFTBurnEvent(NFTBurnEventData {
asset_class_identifier: "asset-id".to_string(),
hex_asset_identifier: "asset-id".to_string(),
sender: "".to_string(),
}),
"ft_transfer" => StacksTransactionEvent::FTTransferEvent(FTTransferEventData {
"ft_transfer" => StacksTransactionEventData::FTTransferEvent(FTTransferEventData {
sender: "".to_string(),
asset_class_identifier: "asset-id".to_string(),
amount: "".to_string(),
recipient: "".to_string(),
}),
"ft_mint" => StacksTransactionEvent::FTMintEvent(FTMintEventData {
"ft_mint" => StacksTransactionEventData::FTMintEvent(FTMintEventData {
asset_class_identifier: "asset-id".to_string(),
recipient: "".to_string(),
amount: "".to_string(),
}),
"ft_burn" => StacksTransactionEvent::FTBurnEvent(FTBurnEventData {
"ft_burn" => StacksTransactionEventData::FTBurnEvent(FTBurnEventData {
asset_class_identifier: "asset-id".to_string(),
sender: "".to_string(),
amount: "".to_string(),
Expand All @@ -145,22 +146,22 @@ pub fn get_test_event_by_type(event_type: &str) -> StacksTransactionEvent {
"data_map_update" => todo!(),
"data_map_delete" => todo!(),
"smart_contract_print_event" => {
StacksTransactionEvent::SmartContractEvent(SmartContractEventData {
StacksTransactionEventData::SmartContractEvent(SmartContractEventData {
topic: "print".to_string(),
contract_identifier: "ST3AXH4EBHD63FCFPTZ8GR29TNTVWDYPGY0KDY5E5.loan-data"
.to_string(),
hex_value: PRINT_EVENT_HEX.to_string(),
})
}
"smart_contract_print_event_empty" => {
StacksTransactionEvent::SmartContractEvent(SmartContractEventData {
StacksTransactionEventData::SmartContractEvent(SmartContractEventData {
topic: "print".to_string(),
contract_identifier: "some-id".to_string(),
hex_value: EMPTY_EVENT_HEX.to_string(),
})
}
"smart_contract_not_print_event" => {
StacksTransactionEvent::SmartContractEvent(SmartContractEventData {
StacksTransactionEventData::SmartContractEvent(SmartContractEventData {
topic: "not-print".to_string(),
contract_identifier: "ST3AXH4EBHD63FCFPTZ8GR29TNTVWDYPGY0KDY5E5.loan-data"
.to_string(),
Expand Down
Loading

0 comments on commit e8889f5

Please sign in to comment.