agent_first_pay/store/
redb_store.rs1use crate::provider::PayError;
2use crate::store::db;
3use crate::store::transaction;
4use crate::store::wallet;
5use crate::store::{MigrationLog, PayStore};
6use crate::types::{HistoryRecord, Network};
7use std::collections::BTreeMap;
8use std::path::PathBuf;
9
10#[derive(Clone)]
13pub struct RedbStore {
14 data_dir: String,
15}
16
17impl RedbStore {
18 pub fn new(data_dir: &str) -> Self {
19 Self {
20 data_dir: data_dir.to_string(),
21 }
22 }
23
24 #[allow(dead_code)]
25 pub fn data_dir(&self) -> &str {
26 &self.data_dir
27 }
28}
29
30impl PayStore for RedbStore {
31 fn save_wallet_metadata(&self, meta: &wallet::WalletMetadata) -> Result<(), PayError> {
32 wallet::save_wallet_metadata(&self.data_dir, meta)
33 }
34
35 fn load_wallet_metadata(&self, wallet_id: &str) -> Result<wallet::WalletMetadata, PayError> {
36 wallet::load_wallet_metadata(&self.data_dir, wallet_id)
37 }
38
39 fn list_wallet_metadata(
40 &self,
41 network: Option<Network>,
42 ) -> Result<Vec<wallet::WalletMetadata>, PayError> {
43 wallet::list_wallet_metadata(&self.data_dir, network)
44 }
45
46 fn delete_wallet_metadata(&self, wallet_id: &str) -> Result<(), PayError> {
47 wallet::delete_wallet_metadata(&self.data_dir, wallet_id)
48 }
49
50 fn wallet_directory_path(&self, wallet_id: &str) -> Result<PathBuf, PayError> {
51 wallet::wallet_directory_path(&self.data_dir, wallet_id)
52 }
53
54 fn wallet_data_directory_path(&self, wallet_id: &str) -> Result<PathBuf, PayError> {
55 wallet::wallet_data_directory_path(&self.data_dir, wallet_id)
56 }
57
58 fn wallet_data_directory_path_for_meta(&self, meta: &wallet::WalletMetadata) -> PathBuf {
59 wallet::wallet_data_directory_path_for_wallet_metadata(&self.data_dir, meta)
60 }
61
62 fn resolve_wallet_id(&self, id_or_label: &str) -> Result<String, PayError> {
63 wallet::resolve_wallet_id(&self.data_dir, id_or_label)
64 }
65
66 fn append_transaction_record(&self, record: &HistoryRecord) -> Result<(), PayError> {
67 transaction::append_transaction_record(&self.data_dir, record)
68 }
69
70 fn load_wallet_transaction_records(
71 &self,
72 wallet_id: &str,
73 ) -> Result<Vec<HistoryRecord>, PayError> {
74 transaction::load_wallet_transaction_records(&self.data_dir, wallet_id)
75 }
76
77 fn find_transaction_record_by_id(
78 &self,
79 tx_id: &str,
80 ) -> Result<Option<HistoryRecord>, PayError> {
81 transaction::find_transaction_record_by_id(&self.data_dir, tx_id)
82 }
83
84 fn update_transaction_record_memo(
85 &self,
86 tx_id: &str,
87 memo: Option<&BTreeMap<String, String>>,
88 ) -> Result<(), PayError> {
89 transaction::update_transaction_record_memo(&self.data_dir, tx_id, memo)
90 }
91
92 fn update_transaction_record_fee(
93 &self,
94 tx_id: &str,
95 fee_value: u64,
96 fee_unit: &str,
97 ) -> Result<(), PayError> {
98 transaction::update_transaction_record_fee(&self.data_dir, tx_id, fee_value, fee_unit)
99 }
100
101 fn update_transaction_record_status(
102 &self,
103 tx_id: &str,
104 status: crate::types::TxStatus,
105 confirmed_at_epoch_s: Option<u64>,
106 ) -> Result<(), PayError> {
107 transaction::update_transaction_record_status(
108 &self.data_dir,
109 tx_id,
110 status,
111 confirmed_at_epoch_s,
112 )
113 }
114
115 fn drain_migration_log(&self) -> Vec<MigrationLog> {
116 db::drain_migration_log()
117 }
118}