Skip to content

Commit 1a89a7d

Browse files
committed
feat(hubble): support bond/unbond - bond/unbond events
1 parent 1c6ae79 commit 1a89a7d

19 files changed

+714
-6
lines changed

hubble/.sqlx/query-070e308d1ff5bbe001276ede0f5acdd81c60321c63772011448b2bb49e0d6ad8.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hubble/.sqlx/query-933fed658c09cbddd786b478a9cea0685c70180f28f7d1742bd9f6526b97e5b7.json

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hubble/.sqlx/query-b76aced4d9e0e1e097b2254f0e1e5a26bf94e28d62bd4e4b75ee05ecc08becab.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hubble/.sqlx/query-fe7a205435d15425e739fefe1933316052f3824ef30b264b5fd119fcdfc4dd43.json

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hubble/src/indexer/consumer.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ fn should_trigger_enrich_reset(kind: RecordKind) -> bool {
309309
CreateWrappedToken => false,
310310
CreateWrappedTokenRelation => false,
311311
CreateProxyAccount => false,
312+
Bond => false,
313+
Unbond => false,
312314
// ignore enriched records
313315
PacketSendDecoded => false,
314316
PacketSendTransfers => false,
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
use crate::indexer::event::{
4+
header::Header,
5+
types::{BondInAmount, BondMintAmount, BondMintToAddress, BondSenderAddress},
6+
};
7+
8+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
9+
pub struct BondEvent {
10+
#[serde(flatten)]
11+
pub header: Header,
12+
pub in_amount: BondInAmount,
13+
pub mint_amount: BondMintAmount,
14+
pub mint_to_address: BondMintToAddress,
15+
pub sender: BondSenderAddress,
16+
}
17+
18+
#[cfg(test)]
19+
mod tests {
20+
use super::*;
21+
use crate::indexer::event::test_utils::test_helpers::{
22+
create_bond_test_values, create_test_header, test_json_format, test_roundtrip_serialization,
23+
};
24+
25+
/// Creates a test BondEvent with predictable values
26+
fn create_test_event(suffix: u32) -> BondEvent {
27+
let header = create_test_header(suffix);
28+
let (in_amount, mint_amount, mint_to_address, sender) = create_bond_test_values(42);
29+
30+
BondEvent {
31+
header,
32+
in_amount,
33+
mint_amount,
34+
mint_to_address,
35+
sender,
36+
}
37+
}
38+
39+
#[test]
40+
fn test_json_serialization() {
41+
let event = create_test_event(42);
42+
test_roundtrip_serialization(&event);
43+
}
44+
45+
#[test]
46+
fn test_json_format_stability() {
47+
let event = create_test_event(42);
48+
49+
let expected_json = r#"{
50+
"block_hash": "0x424c4f434b5f484153485f3432",
51+
"event_index": "42",
52+
"height": "10042",
53+
"in_amount": "542",
54+
"message_index": "542",
55+
"mint_amount": "1042",
56+
"mint_to_address": "0x626f6e642d6d696e742d746f2d3432",
57+
"sender": "0x626f6e642d73656e6465722d3432",
58+
"timestamp": "2020-09-13T12:27:22Z",
59+
"transaction_event_index": "242",
60+
"transaction_hash": "0x54585f484153485f3432",
61+
"transaction_index": "142",
62+
"universal_chain_id": "test-chain-42"
63+
}"#;
64+
65+
test_json_format(&event, expected_json);
66+
}
67+
}

hubble/src/indexer/event/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub(crate) mod bond_event;
12
pub(crate) mod channel_open_ack_event;
23
pub(crate) mod channel_open_confirm_event;
34
pub(crate) mod channel_open_init_event;
@@ -21,6 +22,7 @@ pub(crate) mod supported;
2122
pub(crate) mod test_utils;
2223
pub(crate) mod token_bucket_update_event;
2324
pub(crate) mod types;
25+
pub(crate) mod unbond_event;
2426
pub(crate) mod update_client_event;
2527
pub(crate) mod wallet_mutation_entry_event;
2628
pub(crate) mod write_ack_event;

hubble/src/indexer/event/supported.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use serde_json::Value;
33
use time::OffsetDateTime;
44

55
use crate::indexer::event::{
6-
channel_open_ack_event::ChannelOpenAckEvent,
6+
bond_event::BondEvent, channel_open_ack_event::ChannelOpenAckEvent,
77
channel_open_confirm_event::ChannelOpenConfirmEvent,
88
channel_open_init_event::ChannelOpenInitEvent, channel_open_try_event::ChannelOpenTryEvent,
99
connection_open_ack_event::ConnectionOpenAckEvent,
@@ -15,7 +15,7 @@ use crate::indexer::event::{
1515
create_wrapped_token::CreateWrappedTokenEvent, packet_ack_event::PacketAckEvent,
1616
packet_recv_event::PacketRecvEvent, packet_send_event::PacketSendEvent,
1717
packet_timeout_event::PacketTimeoutEvent, token_bucket_update_event::TokenBucketUpdateEvent,
18-
types::BlockHeight, update_client_event::UpdateClientEvent,
18+
types::BlockHeight, unbond_event::UnbondEvent, update_client_event::UpdateClientEvent,
1919
wallet_mutation_entry_event::WalletMutationEntryEvent, write_ack_event::WriteAckEvent,
2020
};
2121

@@ -180,6 +180,16 @@ pub enum SupportedBlockEvent {
180180
#[serde(flatten)]
181181
inner: CreateProxyAccountEvent,
182182
},
183+
#[serde(rename = "bond")]
184+
Bond {
185+
#[serde(flatten)]
186+
inner: BondEvent,
187+
},
188+
#[serde(rename = "unbond")]
189+
Unbond {
190+
#[serde(flatten)]
191+
inner: UnbondEvent,
192+
},
183193
}
184194

185195
impl SupportedBlockEvent {
@@ -210,6 +220,8 @@ impl SupportedBlockEvent {
210220
SupportedBlockEvent::WalletMutationEntry { inner, .. } => inner.header.height,
211221
SupportedBlockEvent::CreateWrappedToken { inner, .. } => inner.header.height,
212222
SupportedBlockEvent::CreateProxyAccount { inner, .. } => inner.header.height,
223+
SupportedBlockEvent::Bond { inner, .. } => inner.header.height,
224+
SupportedBlockEvent::Unbond { inner, .. } => inner.header.height,
213225
}
214226
}
215227
}

hubble/src/indexer/event/test_utils.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ pub mod test_helpers {
77
use crate::indexer::event::{
88
header::Header,
99
types::{
10-
Acknowledgement, BlockHash, BlockHeight, BlockTimestamp, CanonicalChainId, Capacity,
10+
Acknowledgement, Batch, BlockHash, BlockHeight, BlockTimestamp, BondInAmount,
11+
BondMintAmount, BondMintToAddress, BondSenderAddress, CanonicalChainId, Capacity,
1112
ChannelId, ClientId, ClientType, ConnectionId, ContractAddress, Denom, EventIndex,
1213
Maker, MakerMsg, MessageIndex, MutationAmount, MutationDirection, PacketData,
1314
PacketHash, PortId, RefillRate, TimeoutTimestamp, TransactionEventIndex,
14-
TransactionHash, TransactionIndex, UniversalChainId, WalletAddress,
15+
TransactionHash, TransactionIndex, UnbondAmount, UnbondIsNewRequest,
16+
UnbondStakerAddress, UniversalChainId, WalletAddress,
1517
},
1618
};
1719

@@ -236,4 +238,33 @@ pub mod test_helpers {
236238
MutationDirection::In,
237239
)
238240
}
241+
242+
/// Creates test values for bond events
243+
pub fn create_bond_test_values(
244+
suffix: u32,
245+
) -> (
246+
BondInAmount,
247+
BondMintAmount,
248+
BondMintToAddress,
249+
BondSenderAddress,
250+
) {
251+
(
252+
BondInAmount(suffix as u128 + 500),
253+
BondMintAmount(suffix as u128 + 1000),
254+
BondMintToAddress(Bytes::from(format!("bond-mint-to-{suffix}"))),
255+
BondSenderAddress(Bytes::from(format!("bond-sender-{suffix}"))),
256+
)
257+
}
258+
259+
/// Creates test values for unbond events
260+
pub fn create_unbond_test_values(
261+
suffix: u32,
262+
) -> (UnbondAmount, Batch, UnbondIsNewRequest, UnbondStakerAddress) {
263+
(
264+
UnbondAmount(suffix as u128 + 500),
265+
Batch(suffix as u64 + 1000),
266+
UnbondIsNewRequest(true),
267+
UnbondStakerAddress(Bytes::from(format!("unbond-staker-{suffix}"))),
268+
)
269+
}
239270
}

hubble/src/indexer/event/types.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,24 @@ impl From<u64> for MessageIndex {
496496
}
497497
}
498498

499+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
500+
pub struct Batch(#[serde(with = "flexible_u64")] pub u64);
501+
502+
impl From<u64> for Batch {
503+
fn from(value: u64) -> Self {
504+
Self(value)
505+
}
506+
}
507+
508+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
509+
pub struct UnbondIsNewRequest(#[serde(with = "flexible_bool")] pub bool);
510+
511+
impl From<bool> for UnbondIsNewRequest {
512+
fn from(value: bool) -> Self {
513+
Self(value)
514+
}
515+
}
516+
499517
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
500518
pub struct TransactionEventIndex(#[serde(with = "flexible_u64")] pub u64);
501519

@@ -591,6 +609,33 @@ impl From<bytes::Bytes> for WalletAddress {
591609
}
592610
}
593611

612+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
613+
pub struct BondMintToAddress(#[serde(with = "bytes_as_hex")] pub bytes::Bytes);
614+
615+
impl From<bytes::Bytes> for BondMintToAddress {
616+
fn from(value: bytes::Bytes) -> Self {
617+
Self(value)
618+
}
619+
}
620+
621+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
622+
pub struct UnbondStakerAddress(#[serde(with = "bytes_as_hex")] pub bytes::Bytes);
623+
624+
impl From<bytes::Bytes> for UnbondStakerAddress {
625+
fn from(value: bytes::Bytes) -> Self {
626+
Self(value)
627+
}
628+
}
629+
630+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
631+
pub struct BondSenderAddress(#[serde(with = "bytes_as_hex")] pub bytes::Bytes);
632+
633+
impl From<bytes::Bytes> for BondSenderAddress {
634+
fn from(value: bytes::Bytes) -> Self {
635+
Self(value)
636+
}
637+
}
638+
594639
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
595640
pub struct MutationAmount(#[serde(with = "flexible_u128")] pub u128);
596641

@@ -608,6 +653,33 @@ pub enum MutationDirection {
608653
Out,
609654
}
610655

656+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
657+
pub struct BondInAmount(#[serde(with = "flexible_u128")] pub u128);
658+
659+
impl From<u128> for BondInAmount {
660+
fn from(value: u128) -> Self {
661+
Self(value)
662+
}
663+
}
664+
665+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
666+
pub struct BondMintAmount(#[serde(with = "flexible_u128")] pub u128);
667+
668+
impl From<u128> for BondMintAmount {
669+
fn from(value: u128) -> Self {
670+
Self(value)
671+
}
672+
}
673+
674+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
675+
pub struct UnbondAmount(#[serde(with = "flexible_u128")] pub u128);
676+
677+
impl From<u128> for UnbondAmount {
678+
fn from(value: u128) -> Self {
679+
Self(value)
680+
}
681+
}
682+
611683
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
612684
pub struct Path(pub U256);
613685

@@ -617,6 +689,29 @@ impl From<U256> for Path {
617689
}
618690
}
619691

692+
mod flexible_bool {
693+
use super::*;
694+
695+
pub fn serialize<S>(value: &bool, serializer: S) -> Result<S::Ok, S::Error>
696+
where
697+
S: Serializer,
698+
{
699+
serializer.serialize_bool(*value)
700+
}
701+
702+
pub fn deserialize<'de, D>(deserializer: D) -> Result<bool, D::Error>
703+
where
704+
D: Deserializer<'de>,
705+
{
706+
let value = Value::deserialize(deserializer)?;
707+
match value {
708+
Value::Bool(b) => Ok(b),
709+
Value::String(s) => s.parse().map_err(serde::de::Error::custom),
710+
_ => Err(serde::de::Error::custom("expected bool or string")),
711+
}
712+
}
713+
}
714+
620715
mod flexible_u64 {
621716
use super::*;
622717

0 commit comments

Comments
 (0)