use bitcoin::secp256k1::All;
use ibc_chain_registry::chain::ChainData;
use crate::{
sender::{Sender, SenderBuilder, SenderOptions},
DaemonAsyncBuilder,
};
use super::{super::error::DaemonError, core::Daemon};
#[derive(Clone, Default)]
pub struct DaemonBuilder {
pub(crate) chain: Option<ChainData>,
pub(crate) handle: Option<tokio::runtime::Handle>,
pub(crate) deployment_id: Option<String>,
pub(crate) sender: Option<SenderBuilder<All>>,
pub(crate) sender_options: SenderOptions,
}
impl DaemonBuilder {
pub fn chain(&mut self, chain: impl Into<ChainData>) -> &mut Self {
self.chain = Some(chain.into());
self
}
pub fn deployment_id(&mut self, deployment_id: impl Into<String>) -> &mut Self {
self.deployment_id = Some(deployment_id.into());
self
}
pub fn handle(&mut self, handle: &tokio::runtime::Handle) -> &mut Self {
self.handle = Some(handle.clone());
self
}
pub fn mnemonic(&mut self, mnemonic: impl ToString) -> &mut Self {
self.sender = Some(SenderBuilder::Mnemonic(mnemonic.to_string()));
self
}
pub fn sender(&mut self, wallet: Sender<All>) -> &mut Self {
self.sender = Some(SenderBuilder::Sender(wallet));
self
}
pub fn authz_granter(&mut self, granter: impl ToString) -> &mut Self {
self.sender_options.set_authz_granter(granter.to_string());
self
}
pub fn fee_granter(&mut self, granter: impl ToString) -> &mut Self {
self.sender_options.set_fee_granter(granter.to_string());
self
}
pub fn hd_index(&mut self, index: u32) -> &mut Self {
self.sender_options.hd_index = Some(index);
self
}
pub fn build(&self) -> Result<Daemon, DaemonError> {
let rt_handle = self
.handle
.clone()
.ok_or(DaemonError::BuilderMissing("runtime handle".into()))?;
let daemon = rt_handle.block_on(DaemonAsyncBuilder::from(self.clone()).build())?;
Ok(Daemon { rt_handle, daemon })
}
}