anya_core/bitcoin/adapters/
mod.rs1use std::error::Error;
2use std::sync::Arc;
5use crate::bitcoin::config::BitcoinConfig;
10use crate::bitcoin::interface::{
11 Address,
12 AddressType,
13 BitcoinImplementationType,
14 BitcoinInterface,
16 BitcoinResult,
17 Block,
18 BlockHeader,
19 Transaction,
20};
21
22#[allow(dead_code)]
24pub struct BitcoinAdapter {
25 config: Arc<BitcoinConfig>,
27
28 implementation: Arc<dyn BitcoinInterface>,
30}
31
32impl BitcoinAdapter {
33 pub async fn new(config: BitcoinConfig) -> Result<Self, Box<dyn Error>> {
35 let implementation = Arc::new(crate::bitcoin::rust::RustBitcoinImplementation::new(
36 &config,
37 )?) as Arc<dyn BitcoinInterface>;
38
39 Ok(Self {
40 config: Arc::new(config),
41 implementation,
42 })
43 }
44
45 pub fn get_implementation(&self) -> Arc<dyn BitcoinInterface> {
47 self.implementation.clone()
48 }
49}
50
51#[async_trait::async_trait]
54impl BitcoinInterface for BitcoinAdapter {
55 async fn get_transaction(&self, txid: &str) -> BitcoinResult<Transaction> {
57 self.implementation.get_transaction(txid).await
58 }
59
60 async fn get_block(&self, hash: &str) -> BitcoinResult<Block> {
62 self.implementation.get_block(hash).await
63 }
64
65 async fn get_block_height(&self) -> BitcoinResult<u32> {
67 self.implementation.get_block_height().await
68 }
69
70 async fn generate_address(&self, address_type: AddressType) -> BitcoinResult<Address> {
72 self.implementation.generate_address(address_type).await
73 }
74
75 async fn create_transaction(
77 &self,
78 outputs: Vec<(String, u64)>,
79 fee_rate: u64,
80 ) -> BitcoinResult<Transaction> {
81 self.implementation
82 .create_transaction(outputs, fee_rate)
83 .await
84 }
85
86 async fn broadcast_transaction(&self, transaction: &Transaction) -> BitcoinResult<String> {
88 self.implementation.broadcast_transaction(transaction).await
89 }
90
91 async fn get_balance(&self, address: &Address) -> BitcoinResult<u64> {
93 self.implementation.get_balance(address).await
94 }
95
96 async fn estimate_fee(&self, target_blocks: u8) -> BitcoinResult<u64> {
98 self.implementation.estimate_fee(target_blocks).await
99 }
100
101 async fn get_block_header(&self, hash: &str) -> BitcoinResult<BlockHeader> {
103 self.implementation.get_block_header(hash).await
104 }
105
106 async fn verify_merkle_proof(
108 &self,
109 tx_hash: &str,
110 block_header: &BlockHeader,
111 ) -> BitcoinResult<bool> {
112 self.implementation
113 .verify_merkle_proof(tx_hash, block_header)
114 .await
115 }
116
117 async fn send_transaction(&self, tx: &Transaction) -> BitcoinResult<String> {
119 self.implementation.send_transaction(tx).await
120 }
121
122 fn implementation_type(&self) -> BitcoinImplementationType {
123 self.implementation.implementation_type()
124 }
125}
126
127#[cfg(test)]
128mod tests {
129 use super::*;
130
131 #[tokio::test]
132 async fn test_adapter_initialization() -> Result<(), Box<dyn Error>> {
133 let config = BitcoinConfig::default();
134 let adapter = BitcoinAdapter::new(config).await?;
135
136 let implementation = adapter.get_implementation();
138 assert_eq!(
139 implementation.implementation_type(),
140 BitcoinImplementationType::Rust
141 );
142
143 assert_eq!(
145 adapter.implementation_type(),
146 BitcoinImplementationType::Rust
147 );
148
149 Ok(())
151 }
152}