storage.rs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. use crate::{amount::AmountCents, asset::AssetId, AccountId, Payment, PaymentId};
  2. use futures::TryStreamExt;
  3. #[derive(thiserror::Error, Debug)]
  4. pub enum Error {
  5. #[error("Storage error: {0}")]
  6. Storage(String),
  7. #[error("No storage update when expecting")]
  8. NoUpdate,
  9. }
  10. #[async_trait::async_trait]
  11. pub trait Batch {
  12. async fn spend_payment(
  13. &mut self,
  14. payment_id: PaymentId,
  15. transaction_id: AccountId,
  16. ) -> Result<(), Error>;
  17. async fn rollback(self) -> Result<(), Error>;
  18. async fn commit(self) -> Result<(), Error>;
  19. async fn store_new_payments(&mut self, outputs: &[Payment]) -> Result<(), Error>;
  20. async fn store_transaction(&mut self) -> Result<(), Error>;
  21. }
  22. #[async_trait::async_trait]
  23. pub trait Storage<B, I>
  24. where
  25. B: Batch,
  26. I: TryStreamExt<Ok = (PaymentId, AmountCents), Error = Error> + Unpin,
  27. {
  28. async fn get_payment(&self, id: PaymentId) -> Result<Payment, Error>;
  29. async fn begin(&self) -> Result<B, Error>;
  30. async fn get_balance(&self, account: AccountId) -> Result<Vec<(AssetId, AmountCents)>, Error>;
  31. async fn get_unspend_payments(&self, account: AccountId, asset: AssetId) -> Result<I, Error>;
  32. }