Skip to content

Commit

Permalink
feat: add support for bitcoin op DelegatedStacking
Browse files Browse the repository at this point in the history
  • Loading branch information
lgalabru committed Apr 4, 2023
1 parent c6c1c0e commit 6516155
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 4 deletions.
107 changes: 104 additions & 3 deletions components/chainhook-event-observer/src/indexer/stacks/mod.rs
Expand Up @@ -2,13 +2,14 @@ mod blocks_pool;

pub use blocks_pool::StacksBlockPool;

use crate::chainhooks::stacks::try_decode_clarity_value;
use crate::indexer::AssetClassCache;
use crate::indexer::{IndexerConfig, StacksChainContext};
use crate::utils::Context;
use chainhook_types::*;
use clarity_repl::clarity::codec::StacksMessageCodec;
use clarity_repl::clarity::util::hash::hex_bytes;
use clarity_repl::clarity::vm::types::Value as ClarityValue;
use clarity_repl::clarity::vm::types::{SequenceData, Value as ClarityValue};
use clarity_repl::codec::{StacksTransaction, TransactionAuth, TransactionPayload};
use hiro_system_kit::slog;
use rocket::serde::json::Value as JsonValue;
Expand Down Expand Up @@ -539,8 +540,108 @@ pub fn get_tx_description(
"stacked: {} µSTX by {} through Bitcoin transaction",
data.locked_amount, data.locked_address,
);
let tx_type = StacksTransactionKind::Other;
let tx_type =
StacksTransactionKind::BitcoinOp(BitcoinOpData::StackSTX(StackSTXData {
locked_amount: data.locked_amount,
unlock_height: data.unlock_height,
stacking_address: data.locked_address.clone(),
}));
return Ok((description, tx_type, 0, 0, data.locked_address, None));
} else if let Some(ref event_data) = event.contract_event {
let data: SmartContractEventData = serde_json::from_value(event_data.clone())
.map_err(|e| format!("unable to decode event_data {}", e.to_string()))?;
if let Some(ClarityValue::Response(data)) =
try_decode_clarity_value(&data.hex_value)
{
if data.committed {
if let ClarityValue::Tuple(outter) = *data.data {
if let Some(ClarityValue::Tuple(inner)) = outter.data_map.get("data") {
match (
&outter.data_map.get("stacker"),
&inner.data_map.get("amount-ustx"),
&inner.data_map.get("delegate-to"),
&inner.data_map.get("pox-addr"),
&inner.data_map.get("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)),
) => {
let description = format!(
"stacked: {} µSTX delegated to {} through Bitcoin transaction",
amount_ustx, delegate.to_string(),
);
let tx_type = StacksTransactionKind::BitcoinOp(
BitcoinOpData::DelegateStackSTX(DelegateStackSTXData {
stacking_address: stacking_address.to_string(),
amount: amount_ustx.to_string(),
delegate: delegate.to_string(),
pox_address: match &pox_addr.data {
Some(value) => match &**value {
ClarityValue::Tuple(address_comps) => {
match (
&address_comps
.data_map
.get("version"),
&address_comps
.data_map
.get("hashbytes"),
) {
(
Some(ClarityValue::UInt(
_version,
)),
Some(ClarityValue::Sequence(
SequenceData::Buffer(
_hashbytes,
),
)),
) => None,
_ => None,
}
}
_ => None,
},
_ => None,
},
unlock_height: match &*(&unlock_burn_height.data) {
Some(value) => match &**value {
ClarityValue::UInt(value) => {
Some(value.to_string())
}
_ => None,
},
_ => None,
},
}),
);
return Ok((
description,
tx_type,
0,
0,
"".to_string(),
None,
));
}
_ => {}
}
}
}
}
}
} else {
return Ok((
"unsupported transaction".into(),
StacksTransactionKind::Unsupported,
0,
0,
"".to_string(),
None,
));
}
}
return Err(format!(
Expand Down Expand Up @@ -627,7 +728,7 @@ pub fn get_tx_description(
TransactionPayload::Coinbase(_, _) => {
(format!("coinbase"), StacksTransactionKind::Coinbase)
}
_ => (format!("other"), StacksTransactionKind::Other),
_ => (format!("other"), StacksTransactionKind::Unsupported),
};
Ok((description, tx_type, fee, nonce, sender, sponsor))
}
Expand Down
26 changes: 25 additions & 1 deletion components/chainhook-types-rs/src/rosetta.rs
Expand Up @@ -149,7 +149,31 @@ pub enum StacksTransactionKind {
ContractDeployment(StacksContractDeploymentData),
NativeTokenTransfer,
Coinbase,
Other,
BitcoinOp(BitcoinOpData),
Unsupported,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(tag = "type", content = "data")]
pub enum BitcoinOpData {
StackSTX(StackSTXData),
DelegateStackSTX(DelegateStackSTXData),
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct StackSTXData {
pub locked_amount: String,
pub unlock_height: String,
pub stacking_address: String,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct DelegateStackSTXData {
pub stacking_address: String,
pub amount: String,
pub delegate: String,
pub pox_address: Option<String>,
pub unlock_height: Option<String>,
}

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

0 comments on commit 6516155

Please sign in to comment.