follower_event_watcher.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. use std::time::{SystemTime, UNIX_EPOCH};
  2. use crate::{
  3. darkfi::Error,
  4. ethereum::swap_creator::SwapCreator,
  5. protocol::{
  6. follower::Event,
  7. traits::{CounterpartyKeys, FollowerEventWatcher},
  8. },
  9. };
  10. use ethers::prelude::Middleware;
  11. use smol::{channel, stream::StreamExt as _};
  12. pub(crate) struct Watcher;
  13. #[darkfi_serial::async_trait]
  14. impl FollowerEventWatcher for Watcher {
  15. async fn run_counterparty_funds_locked_watcher<M: Middleware>(
  16. event_tx: channel::Sender<Event>,
  17. contract: SwapCreator<M>,
  18. claim_commitment: [u8; 32],
  19. refund_commitment: [u8; 32],
  20. from_block: u64,
  21. ) -> Result<(), crate::Error> {
  22. // watch for a `NewSwap` event with the correct swap parameters
  23. // note: we still need to check for correct asset, value, and timeout,
  24. // which is done in the event handler [`Follower`].
  25. let topic2: ethers::types::U256 = claim_commitment.into();
  26. let topic3: ethers::types::U256 = refund_commitment.into();
  27. let events = contract
  28. .ready_filter()
  29. .from_block(from_block)
  30. .address(contract.address().into())
  31. .topic2(topic2) // `newSwap` event sig is topic0 and `contract_swap_id` is topic1
  32. .topic3(topic3);
  33. let mut stream = events.stream().await.unwrap().with_meta();
  34. // we listen for the first event, as there can only be one event
  35. // that matches the filter (ie. has the same swap_id)
  36. let Some(Ok((event, _meta))) = stream.next().await else {
  37. return Err(Error::ReadyEventStreamFailed.into());
  38. };
  39. let contract_swap_id = event.swap_id;
  40. event_tx.send(Event::CounterpartyFundsLocked(contract_swap_id)).await.unwrap();
  41. Ok(())
  42. }
  43. async fn run_ready_to_claim_watcher<M: Middleware>(
  44. event_tx: channel::Sender<Event>,
  45. contract: SwapCreator<M>,
  46. contract_swap_id: &[u8; 32],
  47. from_block: u64,
  48. ) -> Result<(), crate::Error> {
  49. let topic1: ethers::types::U256 = contract_swap_id.into();
  50. let events = contract
  51. .ready_filter()
  52. .from_block(from_block)
  53. .address(contract.address().into())
  54. .topic1(topic1); // `ready` event sig is topic0
  55. let mut stream = events.stream().await.unwrap().with_meta();
  56. // we listen for the first event, as there can only be one event
  57. // that matches the filter (ie. has the same swap_id)
  58. let Some(Ok((_, _meta))) = stream.next().await else {
  59. return Err(Error::ReadyEventStreamFailed.into());
  60. };
  61. event_tx.send(Event::ReadyToClaim).await.unwrap();
  62. Ok(())
  63. }
  64. async fn run_counterparty_funds_refunded_watcher<M: Middleware>(
  65. event_tx: channel::Sender<Event>,
  66. contract: SwapCreator<M>,
  67. contract_swap_id: &[u8; 32],
  68. from_block: u64,
  69. ) -> Result<(), crate::Error> {
  70. let topic1: ethers::types::U256 = contract_swap_id.into();
  71. let events = contract
  72. .refunded_filter()
  73. .from_block(from_block)
  74. .address(contract.address().into())
  75. .topic1(topic1); // `refunded` event sig is topic0
  76. let mut stream = events.stream().await.unwrap().with_meta();
  77. // we listen for the first event, as there can only be one event
  78. // that matches the filter (ie. has the same swap_id)
  79. let Some(Ok((event, _meta))) = stream.next().await else {
  80. return Err(Error::RefundedEventStreamFailed.into());
  81. };
  82. let counterparty_secret = event.s;
  83. event_tx.send(Event::CounterpartyFundsRefunded(counterparty_secret)).await.unwrap();
  84. Ok(())
  85. }
  86. }