Browse Source

start implementing follower event watcher for darkfi

elizabeth 2 years ago
parent
commit
5ecf5178da

+ 4 - 0
src/darkfi/error.rs

@@ -26,4 +26,8 @@ pub enum Error {
     PutKeypair(#[source] drk::WalletDbError),
     #[error("failed convert counterparty secret bytes to secret key")]
     CounterpartySecretKeyConversionFailed(#[source] darkfi_sdk::ContractError),
+    #[error("listening to Ready event stream failed")]
+    ReadyEventStreamFailed,
+    #[error("listening to Refunded event stream failed")]
+    RefundedEventStreamFailed,
 }

+ 76 - 0
src/darkfi/follower_event_watcher.rs

@@ -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(())
+    }
+}

+ 1 - 0
src/darkfi/mod.rs

@@ -1,5 +1,6 @@
 mod error;
 mod follower;
+mod follower_event_watcher;
 pub(crate) mod wallet;
 
 pub(crate) use error::Error;

+ 2 - 2
src/ethereum/initiator_event_watcher.rs

@@ -42,10 +42,10 @@ impl InitiatorEventWatcher for Watcher {
     ) -> Result<(), crate::Error> {
         let topic1: ethers::types::U256 = contract_swap_id.into();
         let events = contract
-            .claimed_filter() // claimed event sig is topic0
+            .claimed_filter()
             .from_block(from_block)
             .address(contract.address().into())
-            .topic1(topic1);
+            .topic1(topic1); // `claimed` event sig is topic0
 
         let mut stream = events.stream().await.unwrap().with_meta();
 

+ 1 - 1
src/protocol/mod.rs

@@ -1,6 +1,6 @@
 //! This module contains the protocol traits and logic for DRK-ETH atomic swaps.
 mod error;
-mod follower;
+pub(crate) mod follower;
 pub(crate) mod initiator;
 pub(crate) mod traits;
 

+ 2 - 1
src/protocol/traits.rs

@@ -159,8 +159,9 @@ pub(crate) trait Follower {
 
 #[async_trait]
 pub(crate) trait FollowerEventWatcher {
-    async fn run_counterparty_funds_locked_watcher(
+    async fn run_counterparty_funds_locked_watcher<M: Middleware>(
         event_tx: channel::Sender<follower::Event>,
+        contract: SwapCreator<M>,
     ) -> Result<(), Error>;
 
     async fn run_ready_to_claim_watcher<M: Middleware>(