1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
pub use serai_abi::in_instructions::primitives;
use primitives::SignedBatch;

use crate::{
  primitives::{BlockHash, ExternalNetworkId},
  Transaction, SeraiError, Serai, TemporalSerai,
};

pub type InInstructionsEvent = serai_abi::in_instructions::Event;

const PALLET: &str = "InInstructions";

#[derive(Clone, Copy)]
pub struct SeraiInInstructions<'a>(pub(crate) &'a TemporalSerai<'a>);
impl<'a> SeraiInInstructions<'a> {
  pub async fn latest_block_for_network(
    &self,
    network: ExternalNetworkId,
  ) -> Result<Option<BlockHash>, SeraiError> {
    self.0.storage(PALLET, "LatestNetworkBlock", network).await
  }

  pub async fn last_batch_for_network(
    &self,
    network: ExternalNetworkId,
  ) -> Result<Option<u32>, SeraiError> {
    self.0.storage(PALLET, "LastBatch", network).await
  }

  pub async fn batch_events(&self) -> Result<Vec<InInstructionsEvent>, SeraiError> {
    self
      .0
      .events(|event| {
        if let serai_abi::Event::InInstructions(event) = event {
          if matches!(event, InInstructionsEvent::Batch { .. }) {
            Some(event.clone())
          } else {
            None
          }
        } else {
          None
        }
      })
      .await
  }

  pub fn execute_batch(batch: SignedBatch) -> Transaction {
    Serai::unsigned(serai_abi::Call::InInstructions(
      serai_abi::in_instructions::Call::execute_batch { batch },
    ))
  }
}