Skip to content

Commit

Permalink
initial types piped through
Browse files Browse the repository at this point in the history
  • Loading branch information
brady.ouren committed Oct 30, 2024
1 parent 99e5a61 commit d2fabfd
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 37 deletions.
52 changes: 40 additions & 12 deletions components/chainhook-sdk/src/chainhooks/stacks/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::observer::EventObserverConfig;
use crate::utils::{AbstractStacksBlock, Context, MAX_BLOCK_HEIGHTS_ENTRIES};

use super::types::validate_txid;
use super::types::{
append_error_context, BlockIdentifierIndexRule, ChainhookInstance, ExactMatchingRule,
HookAction,
};
use super::types::validate_txid;
use chainhook_types::{
BlockIdentifier, StacksChainEvent, StacksNetwork, StacksTransactionData,
StacksTransactionEvent, StacksTransactionEventPayload, StacksTransactionKind,
Expand Down Expand Up @@ -258,6 +258,7 @@ pub enum StacksPredicate {
FtEvent(StacksFtEventBasedPredicate),
NftEvent(StacksNftEventBasedPredicate),
StxEvent(StacksStxEventBasedPredicate),
TenureChange(TenureChangeBasedPredicate),
Txid(ExactMatchingRule),
}

Expand Down Expand Up @@ -299,6 +300,7 @@ impl StacksPredicate {
StacksPredicate::FtEvent(_) => {}
StacksPredicate::NftEvent(_) => {}
StacksPredicate::StxEvent(_) => {}
StacksPredicate::TenureChange(_) => {}
StacksPredicate::Txid(ExactMatchingRule::Equals(txid)) => {
if let Err(e) = validate_txid(txid) {
return Err(append_error_context(
Expand Down Expand Up @@ -452,6 +454,12 @@ pub struct StacksStxEventBasedPredicate {
pub actions: Vec<String>,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct TenureChangeBasedPredicate {
pub actions: Vec<String>,
}

#[derive(Clone)]
pub struct StacksTriggerChainhook<'a> {
pub chainhook: &'a StacksChainhookInstance,
Expand Down Expand Up @@ -484,17 +492,13 @@ pub struct StacksChainhookOccurrencePayload {
}

impl StacksChainhookOccurrencePayload {
pub fn from_trigger(
trigger: StacksTriggerChainhook<'_>,
) -> StacksChainhookOccurrencePayload {
pub fn from_trigger(trigger: StacksTriggerChainhook<'_>) -> StacksChainhookOccurrencePayload {
StacksChainhookOccurrencePayload {
apply: trigger
.apply
.into_iter()
.map(|(transactions, block)| {
let transactions = transactions
.into_iter().cloned()
.collect::<Vec<_>>();
let transactions = transactions.into_iter().cloned().collect::<Vec<_>>();
StacksApplyTransactionPayload {
block_identifier: block.get_identifier().clone(),
transactions,
Expand All @@ -505,9 +509,7 @@ impl StacksChainhookOccurrencePayload {
.rollback
.into_iter()
.map(|(transactions, block)| {
let transactions = transactions
.into_iter().cloned()
.collect::<Vec<_>>();
let transactions = transactions.into_iter().cloned().collect::<Vec<_>>();
StacksRollbackTransactionPayload {
block_identifier: block.get_identifier().clone(),
transactions,
Expand Down Expand Up @@ -790,6 +792,7 @@ pub fn evaluate_stacks_predicate_on_block<'a>(
| StacksPredicate::FtEvent(_)
| StacksPredicate::NftEvent(_)
| StacksPredicate::StxEvent(_)
| StacksPredicate::TenureChange(_)
| StacksPredicate::PrintEvent(_)
| StacksPredicate::Txid(_) => unreachable!(),
}
Expand Down Expand Up @@ -922,6 +925,18 @@ pub fn evaluate_stacks_predicate_on_transaction<'a>(
}
false
}
StacksPredicate::TenureChange(_expected_event) => {
for event in transaction.metadata.receipt.events.iter() {
if matches!(
&event.event_payload,
StacksTransactionEventPayload::TenureChangeEvent(_)
) {
return true;
}
}
false
}

StacksPredicate::StxEvent(expected_event) => {
let expecting_mint = expected_event.actions.contains(&"mint".to_string());
let expecting_transfer = expected_event.actions.contains(&"transfer".to_string());
Expand Down Expand Up @@ -949,7 +964,9 @@ pub fn evaluate_stacks_predicate_on_transaction<'a>(
}
StacksPredicate::PrintEvent(expected_event) => {
for event in transaction.metadata.receipt.events.iter() {
if let StacksTransactionEventPayload::SmartContractEvent(actual) = &event.event_payload {
if let StacksTransactionEventPayload::SmartContractEvent(actual) =
&event.event_payload
{
if actual.topic == "print" {
match expected_event {
StacksPrintEventBasedPredicate::Contains {
Expand Down Expand Up @@ -1212,6 +1229,17 @@ pub fn serialized_event_with_decoded_clarity_value(
"position": event.position
})
}
StacksTransactionEventPayload::TenureChangeEvent(_payload) => {
json!({
"type": "TenureChangeEvent",
// "data": {
// "contract_identifier": payload.contract_identifier,
// "topic": payload.topic,
// "value": serialized_decoded_clarity_value(&payload.tenure_change, ctx),
// },
"position": event.position
})
}
}
}

Expand All @@ -1235,7 +1263,7 @@ pub fn serialized_decoded_clarity_value(hex_value: &str, ctx: &Context) -> serde
Ok(bytes) => bytes,
_ => return json!(hex_value.to_string()),
};

match ClarityValue::consensus_deserialize(&mut Cursor::new(&value_bytes)) {
Ok(value) => serialize_to_json(&value),
Err(e) => {
Expand Down
38 changes: 13 additions & 25 deletions components/chainhook-sdk/src/indexer/stacks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ pub struct NewBlock {

#[serde(skip_serializing_if = "Option::is_none")]
pub block_time: Option<u64>,

#[serde(skip_serializing_if = "Option::is_none")]
pub signer_bitvec: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub signer_signature: Option<Vec<String>>,

Expand Down Expand Up @@ -387,8 +387,7 @@ pub fn standardize_stacks_block(
}
return Err(format!(
"unable to standardize block #{} ({})",
block.block_height,
e
block.block_height, e
));
}
};
Expand Down Expand Up @@ -629,7 +628,6 @@ pub fn get_value_description(raw_value: &str, ctx: &Context) -> String {
_ => return raw_value.to_string(),
};


match ClarityValue::consensus_deserialize(&mut Cursor::new(&value_bytes)) {
Ok(value) => format!("{}", value),
Err(e) => {
Expand Down Expand Up @@ -703,12 +701,12 @@ pub fn get_tx_description(
if let ClarityValue::Tuple(outter) = *data.data {
if let Some(ClarityValue::Tuple(inner)) = outter.data_map.get("data") {
if let (
Some(ClarityValue::Principal(stacking_address)),
Some(ClarityValue::UInt(amount_ustx)),
Some(ClarityValue::Principal(delegate)),
Some(ClarityValue::Optional(pox_addr)),
Some(ClarityValue::Optional(unlock_burn_height)),
) = (
Some(ClarityValue::Principal(stacking_address)),
Some(ClarityValue::UInt(amount_ustx)),
Some(ClarityValue::Principal(delegate)),
Some(ClarityValue::Optional(pox_addr)),
Some(ClarityValue::Optional(unlock_burn_height)),
) = (
&outter.data_map.get("stacker"),
&inner.data_map.get("amount-ustx"),
&inner.data_map.get("delegate-to"),
Expand All @@ -728,17 +726,13 @@ pub fn get_tx_description(
Some(value) => match &**value {
ClarityValue::Tuple(address_comps) => {
match (
&address_comps
.data_map
.get("version"),
&address_comps.data_map.get("version"),
&address_comps
.data_map
.get("hashbytes"),
) {
(
Some(ClarityValue::UInt(
_version,
)),
Some(ClarityValue::UInt(_version)),
Some(ClarityValue::Sequence(
SequenceData::Buffer(
_hashbytes,
Expand All @@ -763,14 +757,7 @@ pub fn get_tx_description(
},
}),
);
return Ok((
description,
tx_type,
0,
0,
"".to_string(),
None,
));
return Ok((description, tx_type, 0, 0, "".to_string(), None));
}
}
}
Expand Down Expand Up @@ -1324,6 +1311,7 @@ pub fn get_standardized_stacks_receipt(
StacksTransactionEventPayload::DataMapInsertEvent(_data) => {}
StacksTransactionEventPayload::DataMapUpdateEvent(_data) => {}
StacksTransactionEventPayload::DataMapDeleteEvent(_data) => {}
StacksTransactionEventPayload::TenureChangeEvent(_data) => {}
StacksTransactionEventPayload::SmartContractEvent(data) => {
mutated_contracts_radius.insert(data.contract_identifier.clone());
}
Expand Down
6 changes: 6 additions & 0 deletions components/chainhook-types-rs/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ pub struct SmartContractEventData {
pub hex_value: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct TenureChangeEventData {
pub consensus_hash: String,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(tag = "type", content = "data")]
pub enum StacksTransactionEventPayload {
Expand All @@ -139,6 +144,7 @@ pub enum StacksTransactionEventPayload {
DataMapUpdateEvent(DataMapUpdateEventData),
DataMapDeleteEvent(DataMapDeleteEventData),
SmartContractEvent(SmartContractEventData),
TenureChangeEvent(TenureChangeEventData),
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
Expand Down

0 comments on commit d2fabfd

Please sign in to comment.