Browse Source

impl run_counterparty_funds_locked_watcher for Follower

elizabeth 2 years ago
parent
commit
47485accd5
2 changed files with 27 additions and 0 deletions
  1. 24 0
      src/darkfi/follower_event_watcher.rs
  2. 3 0
      src/protocol/traits.rs

+ 24 - 0
src/darkfi/follower_event_watcher.rs

@@ -18,8 +18,32 @@ impl FollowerEventWatcher for Watcher {
     async fn run_counterparty_funds_locked_watcher<M: Middleware>(
         event_tx: channel::Sender<Event>,
         contract: SwapCreator<M>,
+        claim_commitment: [u8; 32],
+        refund_commitment: [u8; 32],
+        from_block: u64,
     ) -> Result<(), crate::Error> {
         // watch for a `NewSwap` event with the correct swap parameters
+        // note: we still need to check for correct asset, value, and timeout,
+        // which is done in the event handler [`Follower`].
+        let topic2: ethers::types::U256 = claim_commitment.into();
+        let topic3: ethers::types::U256 = refund_commitment.into();
+        let events = contract
+            .ready_filter()
+            .from_block(from_block)
+            .address(contract.address().into())
+            .topic2(topic2) // `newSwap` event sig is topic0 and `contract_swap_id` is topic1
+            .topic3(topic3);
+
+        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::ReadyEventStreamFailed.into());
+        };
+
+        let contract_swap_id = event.swap_id;
+        event_tx.send(Event::CounterpartyFundsLocked(contract_swap_id)).await.unwrap();
         Ok(())
     }
 

+ 3 - 0
src/protocol/traits.rs

@@ -162,6 +162,9 @@ pub(crate) trait FollowerEventWatcher {
     async fn run_counterparty_funds_locked_watcher<M: Middleware>(
         event_tx: channel::Sender<follower::Event>,
         contract: SwapCreator<M>,
+        claim_commitment: [u8; 32],
+        refund_commitment: [u8; 32],
+        from_block: u64,
     ) -> Result<(), Error>;
 
     async fn run_ready_to_claim_watcher<M: Middleware>(