Skip to content

Commit 125995a

Browse files
committed
feat: process response, deserialize bcs events
1 parent ace5246 commit 125995a

File tree

4 files changed

+50
-23
lines changed

4 files changed

+50
-23
lines changed

protocol-units/bridge/chains/movement/src/event_monitoring.rs

+37-15
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ use std::{pin::Pin, task::Poll};
22

33
use crate::MovementClient;
44
use crate::{event_types::MovementChainEvent, types::MovementHash, utils::MovementAddress};
5+
use anyhow::Result;
6+
use aptos_sdk::rest_client::Response;
7+
use aptos_types::contract_event::{ContractEvent, ContractEventV1, EventWithVersion};
58
use async_stream::try_stream;
6-
use bridge_shared::{
7-
bridge_monitoring::{BridgeContractCounterpartyEvent, BridgeContractCounterpartyMonitoring},
8-
counterparty_contract::SmartContractCounterpartyEvent,
9+
use bridge_shared::bridge_monitoring::{
10+
BridgeContractCounterpartyEvent, BridgeContractCounterpartyMonitoring,
911
};
12+
use bridge_shared::types::CounterpartyCompletedDetails;
1013
use futures::{FutureExt, Stream, StreamExt};
1114
use tokio::sync::mpsc::UnboundedReceiver;
1215

@@ -44,24 +47,43 @@ impl Stream for MovementCounterpartyMonitoring<MovementAddress, MovementHash> {
4447
let this = self.get_mut();
4548
let stream = try_stream! {
4649
loop {
47-
let struct_tag = format!("0x{}::atomic_bridge_counterpary::BridgeTransferAssetsLockedEvent", client.counterparty_address.to_hex_literal());
48-
rest_client.get_account_events_bcs(client.counterparty_address, struct_tag.as_str(), field_name, start, limit);
49-
let response = reqwest::get("https://httpbin.org/ip").await.map_err(|e| {
50-
tracing::error!("Failed to get response: {:?}", e);
51-
Error::msg("Failed to get response")
52-
})?;
53-
54-
// Create an event from the response (replace with actual logic)
55-
let event = BridgeContractCounterpartyEvent {
56-
// Populate event fields based on the response
57-
};
50+
let struct_tag = format!(
51+
"0x{}::atomic_bridge_counterpary::BridgeCounterpartyEvents",
52+
client.counterparty_address.to_hex_literal()
53+
);
54+
let response = rest_client
55+
.get_account_events_bcs(
56+
client.counterparty_address,
57+
struct_tag.as_str(),
58+
"bridge_transfer_assets_locked",
59+
Some(1),
60+
None
61+
).await?;
62+
let events = process_response(response);
63+
let bridge_transfer_details = bcs::from_bytes::<CounterpartyCompletedDetails<MovementAddress, MovementHash>>(
64+
&response.event_data
65+
);
5866

5967
// Yield the event
60-
yield Ok(event);
68+
yield Ok(events);
6169
}
6270
};
6371

6472
let mut stream = Box::pin(stream);
6573
Pin::new(&mut stream).poll_next(cx)
6674
}
6775
}
76+
77+
fn process_response(
78+
res: Response<Vec<EventWithVersion>>,
79+
) -> Result<Vec<CounterpartyCompletedDetails<MovementAddress, MovementHash>>, bcs::Error> {
80+
res.into_inner()
81+
.into_iter()
82+
.map(|e| {
83+
let event_data = e.event.event_data(); // Use the method from the trait
84+
bcs::from_bytes::<CounterpartyCompletedDetails<MovementAddress, MovementHash>>(
85+
event_data,
86+
)
87+
})
88+
.collect() // Collect the results, handling potential errors
89+
}

protocol-units/bridge/move-modules/sources/atomic_bridge_counterparty.move

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ module atomic_bridge::atomic_bridge_counterparty {
3737
/// An event triggered upon locking assets for a bridge transfer
3838
struct BridgeTransferAssetsLockedEvent has store, drop {
3939
bridge_transfer_id: vector<u8>,
40+
initiator: vector<u8>,
4041
recipient: address,
4142
amount: u64,
4243
hash_lock: vector<u8>,
@@ -103,6 +104,7 @@ module atomic_bridge::atomic_bridge_counterparty {
103104
event::emit(
104105
BridgeTransferAssetsLockedEvent {
105106
bridge_transfer_id,
107+
initiator,
106108
recipient,
107109
amount,
108110
hash_lock,

protocol-units/bridge/shared/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ rust-version.workspace = true
1313

1414
[dependencies]
1515
async-trait = "0.1.80"
16+
bcs = { workspace = true }
1617
delegate = "0.12.0"
1718
derive_more = { workspace = true, features = ["deref", "deref_mut"] }
1819
futures.workspace = true
@@ -22,6 +23,7 @@ thiserror.workspace = true
2223
tracing.workspace = true
2324
rand.workspace = true
2425
rand_chacha = "0.2.2"
26+
serde = { workspace = true, features = ["derive"] }
2527
futures-time = "3.0.0"
2628
alloy = { workspace = true, features = [
2729
"full",

protocol-units/bridge/shared/src/types.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ use alloy::primitives::Uint;
22
use derive_more::{Deref, DerefMut};
33
use hex::{self, FromHexError};
44
use rand::{Rng, RngCore};
5+
use serde::{Deserialize, Serialize};
56
use std::{fmt::Debug, hash::Hash};
67
use thiserror::Error;
78

89
use crate::bridge_contracts::{BridgeContractCounterpartyError, BridgeContractInitiatorError};
910

10-
#[derive(Deref, Debug, Clone, PartialEq, Eq, Hash)]
11+
#[derive(Deref, Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
1112
pub struct BridgeTransferId<H>(pub H);
1213

1314
impl<H> BridgeTransferId<H> {
@@ -55,7 +56,7 @@ where
5556
}
5657
}
5758

58-
#[derive(Deref, Debug, Clone, PartialEq, Eq, Hash)]
59+
#[derive(Deref, Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
5960
pub struct InitiatorAddress<A>(pub A);
6061

6162
impl From<&str> for InitiatorAddress<Vec<u8>> {
@@ -70,7 +71,7 @@ impl From<String> for InitiatorAddress<Vec<u8>> {
7071
}
7172
}
7273

73-
#[derive(Deref, Debug, Clone, PartialEq, Eq, Hash)]
74+
#[derive(Deref, Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
7475
pub struct RecipientAddress<A>(pub A);
7576

7677
impl From<&str> for RecipientAddress<Vec<u8>> {
@@ -85,7 +86,7 @@ pub struct RecipientAddressCounterparty<A>(pub A);
8586
#[derive(Deref, Debug, Clone, PartialEq, Eq, Hash)]
8687
pub struct InitiatorAddressCounterParty(pub Vec<u8>);
8788

88-
#[derive(Deref, Debug, Clone, PartialEq, Eq, Hash)]
89+
#[derive(Deref, Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
8990
pub struct HashLock<H>(pub H);
9091

9192
impl<H> HashLock<H> {
@@ -107,7 +108,7 @@ pub fn convert_hash_lock<H: From<O>, O>(other: HashLock<O>) -> HashLock<H> {
107108
HashLock(From::from(other.0))
108109
}
109110

110-
#[derive(Deref, Debug, Clone, PartialEq, Eq)]
111+
#[derive(Deref, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
111112
pub struct HashLockPreImage(pub Vec<u8>);
112113

113114
impl AsRef<[u8]> for HashLockPreImage {
@@ -126,7 +127,7 @@ impl HashLockPreImage {
126127
}
127128
}
128129

129-
#[derive(Deref, Debug, Clone, PartialEq, Eq)]
130+
#[derive(Deref, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
130131
pub struct TimeLock(pub u64);
131132

132133
impl From<Uint<256, 4>> for TimeLock {
@@ -137,7 +138,7 @@ impl From<Uint<256, 4>> for TimeLock {
137138
}
138139
}
139140

140-
#[derive(Deref, DerefMut, Debug, Clone, Copy, PartialEq, Eq)]
141+
#[derive(Deref, DerefMut, Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
141142
pub struct Amount(pub u64);
142143

143144
impl From<Uint<256, 4>> for Amount {
@@ -174,7 +175,7 @@ pub struct LockDetails<A, H> {
174175
pub amount: Amount,
175176
}
176177

177-
#[derive(Debug, PartialEq, Eq, Clone)]
178+
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
178179
pub struct CounterpartyCompletedDetails<A, H> {
179180
pub bridge_transfer_id: BridgeTransferId<H>,
180181
pub initiator_address: InitiatorAddress<Vec<u8>>,

0 commit comments

Comments
 (0)