|
@@ -0,0 +1,76 @@
|
|
|
|
|
+use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
|
+
|
|
|
|
|
+use crate::{
|
|
|
|
|
+ darkfi::Error,
|
|
|
|
|
+ ethereum::swap_creator::SwapCreator,
|
|
|
|
|
+ protocol::{
|
|
|
|
|
+ follower::Event,
|
|
|
|
|
+ traits::{CounterpartyKeys, FollowerEventWatcher},
|
|
|
|
|
+ },
|
|
|
|
|
+};
|
|
|
|
|
+use ethers::prelude::Middleware;
|
|
|
|
|
+use smol::{channel, stream::StreamExt as _};
|
|
|
|
|
+
|
|
|
|
|
+pub(crate) struct Watcher;
|
|
|
|
|
+
|
|
|
|
|
+#[darkfi_serial::async_trait]
|
|
|
|
|
+impl FollowerEventWatcher for Watcher {
|
|
|
|
|
+ async fn run_counterparty_funds_locked_watcher<M: Middleware>(
|
|
|
|
|
+ event_tx: channel::Sender<Event>,
|
|
|
|
|
+ contract: SwapCreator<M>,
|
|
|
|
|
+ ) -> Result<(), crate::Error> {
|
|
|
|
|
+ // watch for a `NewSwap` event with the correct swap parameters
|
|
|
|
|
+ Ok(())
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async fn run_ready_to_claim_watcher<M: Middleware>(
|
|
|
|
|
+ event_tx: channel::Sender<Event>,
|
|
|
|
|
+ contract: SwapCreator<M>,
|
|
|
|
|
+ contract_swap_id: &[u8; 32],
|
|
|
|
|
+ from_block: u64,
|
|
|
|
|
+ ) -> Result<(), crate::Error> {
|
|
|
|
|
+ let topic1: ethers::types::U256 = contract_swap_id.into();
|
|
|
|
|
+ let events = contract
|
|
|
|
|
+ .ready_filter()
|
|
|
|
|
+ .from_block(from_block)
|
|
|
|
|
+ .address(contract.address().into())
|
|
|
|
|
+ .topic1(topic1); // `ready` event sig is topic0
|
|
|
|
|
+
|
|
|
|
|
+ let mut stream = events.stream().await.unwrap().with_meta();
|
|
|
|
|
+
|
|
|
|
|
+ // we listen for the first event, as there can only be one event
|
|
|
|
|
+ // that matches the filter (ie. has the same swap_id)
|
|
|
|
|
+ let Some(Ok((_, _meta))) = stream.next().await else {
|
|
|
|
|
+ return Err(Error::ReadyEventStreamFailed.into());
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ event_tx.send(Event::ReadyToClaim).await.unwrap();
|
|
|
|
|
+ Ok(())
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async fn run_counterparty_funds_refunded_watcher<M: Middleware>(
|
|
|
|
|
+ event_tx: channel::Sender<Event>,
|
|
|
|
|
+ contract: SwapCreator<M>,
|
|
|
|
|
+ contract_swap_id: &[u8; 32],
|
|
|
|
|
+ from_block: u64,
|
|
|
|
|
+ ) -> Result<(), crate::Error> {
|
|
|
|
|
+ let topic1: ethers::types::U256 = contract_swap_id.into();
|
|
|
|
|
+ let events = contract
|
|
|
|
|
+ .refunded_filter()
|
|
|
|
|
+ .from_block(from_block)
|
|
|
|
|
+ .address(contract.address().into())
|
|
|
|
|
+ .topic1(topic1); // `refunded` event sig is topic0
|
|
|
|
|
+
|
|
|
|
|
+ let mut stream = events.stream().await.unwrap().with_meta();
|
|
|
|
|
+
|
|
|
|
|
+ // we listen for the first event, as there can only be one event
|
|
|
|
|
+ // that matches the filter (ie. has the same swap_id)
|
|
|
|
|
+ let Some(Ok((event, _meta))) = stream.next().await else {
|
|
|
|
|
+ return Err(Error::RefundedEventStreamFailed.into());
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ let counterparty_secret = event.s;
|
|
|
|
|
+ event_tx.send(Event::CounterpartyFundsRefunded(counterparty_secret)).await.unwrap();
|
|
|
|
|
+ Ok(())
|
|
|
|
|
+ }
|
|
|
|
|
+}
|