use crate::bitcoin::layer2::rgb::{contract::Contract, schema::Schema, wallet::RGBWallet};
use std::sync::{Arc, Mutex};
#[derive(Debug, Clone)]
pub struct ClientConfig {
pub endpoint: String,
pub network: bitcoin::Network,
pub retry_attempts: u8,
}
impl Default for ClientConfig {
fn default() -> Self {
Self {
endpoint: "http://localhost:3000".to_string(),
network: bitcoin::Network::Testnet,
retry_attempts: 3,
}
}
}
#[derive(Debug, Default)]
pub struct RGBClientBuilder {
config: Option<ClientConfig>,
wallet: Option<Arc<Mutex<RGBWallet>>>,
}
#[derive(Debug)]
pub struct RGBClient {
#[allow(dead_code)]
config: ClientConfig,
wallet: Arc<Mutex<RGBWallet>>,
}
impl RGBClientBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn with_config(mut self, config: ClientConfig) -> Self {
self.config = Some(config);
self
}
pub fn with_wallet(mut self, wallet: RGBWallet) -> Self {
self.wallet = Some(Arc::new(Mutex::new(wallet)));
self
}
pub fn build(self) -> Result<RGBClient, &'static str> {
let config = self.config.unwrap_or_default();
let wallet = self.wallet.ok_or("Wallet is required")?;
Ok(RGBClient { config, wallet })
}
}
impl RGBClient {
pub fn new(wallet: RGBWallet) -> Self {
Self {
config: ClientConfig::default(),
wallet: Arc::new(Mutex::new(wallet)),
}
}
pub fn issue_asset(&self, _schema: &Schema, amount: u64) -> Result<Contract, &'static str> {
if let Ok(mut wallet) = self.wallet.lock() {
wallet.add_asset("asset_id", amount);
}
Ok(Contract::new(
"rgb:asset",
crate::bitcoin::layer2::rgb::contract::ContractType::Asset,
"script",
))
}
pub fn transfer_asset(
&self,
contract_id: &str,
_recipient: &str,
amount: u64,
) -> Result<String, &'static str> {
if let Ok(mut wallet) = self.wallet.lock() {
wallet.transfer_asset(contract_id, amount)?;
}
Ok(format!("transfer:{contract_id}"))
}
pub fn get_balance(&self, contract_id: &str) -> Result<u64, &'static str> {
if let Ok(wallet) = self.wallet.lock() {
return wallet.get_balance(contract_id);
}
Err("Failed to get wallet lock")
}
}