Skip to content

Commit

Permalink
add simple test and update openapi
Browse files Browse the repository at this point in the history
  • Loading branch information
brady.ouren committed Oct 30, 2024
1 parent d2fabfd commit a66b793
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::scan::stacks::{Record, RecordKind};
use crate::service::tests::helpers::mock_bitcoin_rpc::TipData;
use chainhook_sdk::indexer::bitcoin::NewBitcoinBlock;
use chainhook_sdk::indexer::stacks::{NewBlock, NewEvent, NewTransaction, RewardSet, RewardSetSigner};
use chainhook_sdk::indexer::stacks::{
NewBlock, NewEvent, NewTransaction, RewardSet, RewardSetSigner,
};
use chainhook_sdk::types::{
FTBurnEventData, FTMintEventData, FTTransferEventData, NFTBurnEventData, NFTMintEventData,
NFTTransferEventData, STXBurnEventData, STXLockEventData, STXMintEventData,
Expand Down Expand Up @@ -87,6 +89,13 @@ fn create_stacks_new_event(
} else {
None
};
let tenure_change_event = if let StacksTransactionEventPayload::TenureChangeEvent(data) = &event
{
event_type = "tenure_change_event".to_string();
Some(serde_json::to_value(data).unwrap())
} else {
None
};
let contract_event = if let StacksTransactionEventPayload::SmartContractEvent(data) = &event {
event_type = "smart_contract_print_event".to_string();
Some(serde_json::to_value(data).unwrap())
Expand All @@ -112,6 +121,7 @@ fn create_stacks_new_event(
data_map_insert_event: None,
data_map_update_event: None,
data_map_delete_event: None,
tenure_change_event,
contract_event,
}
}
Expand Down Expand Up @@ -336,12 +346,7 @@ pub async fn mine_stacks_block(
.map_err(|e| format!("failed to send new_block request: {}", e))?
.text()
.await
.map_err(|e| {
format!(
"failed to parse response for new_block request: {}",
e
)
})?;
.map_err(|e| format!("failed to parse response for new_block request: {}", e))?;
Ok(())
}

Expand Down Expand Up @@ -414,12 +419,7 @@ async fn call_new_burn_block(
.map_err(|e| format!("failed to send new_burn_block request: {}", e))?
.text()
.await
.map_err(|e| {
format!(
"failed to parse response for new_burn_block request: {}",
e
)
})?;
.map_err(|e| format!("failed to parse response for new_burn_block request: {}", e))?;
Ok(())
}

Expand Down
10 changes: 10 additions & 0 deletions components/chainhook-sdk/src/indexer/stacks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ pub struct NewEvent {
pub data_map_insert_event: Option<JsonValue>,
pub data_map_update_event: Option<JsonValue>,
pub data_map_delete_event: Option<JsonValue>,
pub tenure_change_event: Option<JsonValue>,
pub contract_event: Option<JsonValue>,
}

Expand Down Expand Up @@ -278,6 +279,15 @@ impl NewEvent {
index: self.event_index,
},
});
} else if let Some(ref event_data) = self.tenure_change_event {
let data: TenureChangeEventData =
serde_json::from_value(event_data.clone()).expect("Unable to decode event_data");
return Ok(StacksTransactionEvent {
event_payload: StacksTransactionEventPayload::TenureChangeEvent(data.clone()),
position: StacksTransactionEventPosition {
index: self.event_index,
},
});
} else if let Some(ref event_data) = self.contract_event {
let data: SmartContractEventData =
serde_json::from_value(event_data.clone()).expect("Unable to decode event_data");
Expand Down
13 changes: 9 additions & 4 deletions components/chainhook-sdk/src/indexer/stacks/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use chainhook_types::{
FTBurnEventData, FTMintEventData, FTTransferEventData, NFTBurnEventData, NFTMintEventData,
NFTTransferEventData, STXBurnEventData, STXLockEventData, STXMintEventData,
STXTransferEventData, SmartContractEventData, StacksTransactionEventPayload,
TenureChangeEventData,
};

use crate::indexer::tests::helpers::stacks_events::create_new_event_from_stacks_event;
Expand Down Expand Up @@ -213,10 +214,10 @@ fn test_stacks_vector_040() {
process_stacks_blocks_and_check_expectations((helpers::stacks_shapes::get_vector_040(), None));
}

// #[test]
// fn test_stacks_vector_041() {
// process_stacks_blocks_and_check_expectations((helpers::shapes::get_vector_041(), None));
// }
#[test]
fn test_stacks_vector_041() {
process_stacks_blocks_and_check_expectations((helpers::stacks_shapes::get_vector_041(), None));
}

#[test]
fn test_stacks_vector_042() {
Expand Down Expand Up @@ -363,6 +364,9 @@ fn test_stacks_vector_055() {
topic: "print".to_string(),
hex_value: String::new(),
}); "smart_contract_print_event")]
#[test_case(StacksTransactionEventPayload::TenureChangeEvent(TenureChangeEventData {
consensus_hash: String::new(),
}); "tenure_change_event")]
fn new_events_can_be_converted_into_chainhook_event(original_event: StacksTransactionEventPayload) {
let new_event = create_new_event_from_stacks_event(original_event.clone());
let event = new_event.into_chainhook_event().unwrap();
Expand Down Expand Up @@ -392,6 +396,7 @@ fn into_chainhook_event_rejects_invalid_missing_event() {
data_map_insert_event: None,
data_map_update_event: None,
data_map_delete_event: None,
tenure_change_event: None,
contract_event: None,
};
new_event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ pub fn create_new_event_from_stacks_event(event: StacksTransactionEventPayload)
} else {
None
};
let tenure_change_event = if let StacksTransactionEventPayload::TenureChangeEvent(data) = &event
{
event_type = "tenure_change".to_string();
Some(serde_json::to_value(data).unwrap())
} else {
None
};
let contract_event = if let StacksTransactionEventPayload::SmartContractEvent(data) = &event {
event_type = "smart_contract_print_event".to_string();
Some(serde_json::to_value(data).unwrap())
Expand All @@ -116,6 +123,7 @@ pub fn create_new_event_from_stacks_event(event: StacksTransactionEventPayload)
data_map_insert_event,
data_map_update_event,
data_map_delete_event,
tenure_change_event,
contract_event,
}
}
21 changes: 21 additions & 0 deletions docs/chainhook-openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,27 @@
}
}
},
{
"type": "object",
"required": [
"actions",
"scope"
],
"properties": {
"scope": {
"type": "string",
"enum": [
"tenure_change"
]
},
"actions": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
{
"type": "object",
"oneOf": [
Expand Down

0 comments on commit a66b793

Please sign in to comment.