Skip to content

Commit

Permalink
use implement trait enum
Browse files Browse the repository at this point in the history
  • Loading branch information
MicaiahReid committed Aug 2, 2023
1 parent f379552 commit 623e725
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
3 changes: 3 additions & 0 deletions components/chainhook-cli/src/service/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@ async fn it_handles_stacks_predicates_with_network(network: &str) {
#[test_case(json!({"scope":"block_height", "between": [100,102]}); "with scope block_height between match")]
#[test_case(json!({"scope":"contract_deployment", "deployer": "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM"}); "with scope contract_deployment type deployer")]
#[test_case(json!({"scope":"contract_deployment", "deployer": "*"}); "with scope contract_deployment type deployer wildcard")]
#[test_case(json!({"scope":"contract_deployment", "implement_trait": "sip09"}); "with scope contract_deployment type implement_trait sip09")]
#[test_case(json!({"scope":"contract_deployment", "implement_trait": "sip10"}); "with scope contract_deployment type implement_trait sip10")]
#[test_case(json!({"scope":"contract_deployment", "implement_trait": "*"}); "with scope contract_deployment type implement_trait and wildcard trait")]
#[test_case(json!({"scope":"contract_call","contract_identifier": "SP000000000000000000002Q6VF78.pox","method": "stack-stx"}); "with scope contract_call")]
#[test_case(json!({"scope":"print_event","contract_identifier": "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.monkey-sip09","contains": "vault"}); "with scope print_event both fields")]
#[test_case(json!({"scope":"print_event","contract_identifier": "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.monkey-sip09", "contains": "*"}); "with scope print_event wildcard conatins")]
Expand Down
32 changes: 10 additions & 22 deletions components/chainhook-sdk/src/chainhooks/stacks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,10 @@ pub fn evaluate_stacks_predicate_on_transaction<'a>(
}
_ => false,
},
StacksPredicate::ContractDeployment(StacksContractDeploymentPredicate::ImplementSip09) => {
match &transaction.metadata.kind {
StacksPredicate::ContractDeployment(StacksContractDeploymentPredicate::ImplementTrait(
stacks_trait,
)) => match stacks_trait {
_ => match &transaction.metadata.kind {
StacksTransactionKind::ContractDeployment(_actual_deployment) => {
ctx.try_log(|logger| {
slog::warn!(
Expand All @@ -310,22 +312,8 @@ pub fn evaluate_stacks_predicate_on_transaction<'a>(
false
}
_ => false,
}
}
StacksPredicate::ContractDeployment(StacksContractDeploymentPredicate::ImplementSip10) => {
match &transaction.metadata.kind {
StacksTransactionKind::ContractDeployment(_actual_deployment) => {
ctx.try_log(|logger| {
slog::warn!(
logger,
"StacksContractDeploymentPredicate::Trait uninmplemented"
)
});
false
}
_ => false,
}
}
},
},
StacksPredicate::ContractCall(expected_contract_call) => match &transaction.metadata.kind {
StacksTransactionKind::ContractCall(actual_contract_call) => {
actual_contract_call
Expand Down Expand Up @@ -391,13 +379,13 @@ pub fn evaluate_stacks_predicate_on_transaction<'a>(
|| expected_event.contract_identifier == "*"
{
if expected_event.contains == "*" {
return true;
}
return true;
}
let value =
format!("{}", expect_decoded_clarity_value(&actual.hex_value));
if value.contains(&expected_event.contains) {
return true;
}
return true;
}
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions components/chainhook-sdk/src/chainhooks/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use self::fixtures::get_all_event_types;

use super::{
stacks::{evaluate_stacks_chainhooks_on_chain_event, StacksTriggerChainhook, handle_stacks_hook_action, StacksChainhookOccurrence},
types::{StacksChainhookSpecification, StacksPrintEventBasedPredicate, StacksNftEventBasedPredicate, StacksFtEventBasedPredicate,StacksContractCallBasedPredicate,StacksContractDeploymentPredicate, ExactMatchingRule, FileHook},
types::{StacksChainhookSpecification, StacksPrintEventBasedPredicate, StacksNftEventBasedPredicate, StacksFtEventBasedPredicate,StacksContractCallBasedPredicate,StacksContractDeploymentPredicate, ExactMatchingRule, FileHook, StacksTrait},
};
use crate::{chainhooks::{types::{HookAction, StacksPredicate, StacksStxEventBasedPredicate,}, tests::fixtures::{get_expected_occurrence, get_test_event_by_type}}, utils::AbstractStacksBlock};
use crate::utils::Context;
Expand Down Expand Up @@ -309,14 +309,19 @@ fn test_stacks_predicates(blocks_with_events: Vec<Vec<StacksTransactionEvent>>,
"Deployer predicate does not match non-matching deployer"
)]
#[test_case(
StacksPredicate::ContractDeployment(StacksContractDeploymentPredicate::ImplementSip09),
StacksPredicate::ContractDeployment(StacksContractDeploymentPredicate::ImplementTrait(StacksTrait::Sip09)),
0;
"ImplementSip09 predicate returns no values"
"ImplementSip predicate returns no values for Sip09"
)]
#[test_case(
StacksPredicate::ContractDeployment(StacksContractDeploymentPredicate::ImplementSip10),
StacksPredicate::ContractDeployment(StacksContractDeploymentPredicate::ImplementTrait(StacksTrait::Sip10)),
0;
"ImplementSip10 predicate returns no values"
"ImplementSip predicate returns no values for Sip10"
)]
#[test_case(
StacksPredicate::ContractDeployment(StacksContractDeploymentPredicate::ImplementTrait(StacksTrait::Any)),
0;
"ImplementSip predicate returns no values for Any"
)]
fn test_stacks_predicate_contract_deploy(predicate: StacksPredicate, expected_applies: u64) {
// Prepare block
Expand Down
9 changes: 9 additions & 0 deletions components/chainhook-sdk/src/chainhooks/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,15 @@ pub struct StacksContractCallBasedPredicate {
// #[serde(tag = "type", content = "rule")]
pub enum StacksContractDeploymentPredicate {
Deployer(String),
ImplementTrait(StacksTrait),
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum StacksTrait {
Sip09,
Sip10,
#[serde(rename = "*")]
Any,
}

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

0 comments on commit 623e725

Please sign in to comment.