use super::mpp::detect_realm;
pub use crate::protocol::methods::tempo::session_method::{
InMemoryChannelStore as SessionChannelStore, SessionMethod as TempoSessionMethod,
SessionMethodConfig,
};
pub use crate::protocol::methods::tempo::ChargeMethod as TempoChargeMethod;
pub use crate::protocol::methods::tempo::{
TempoChargeExt, TempoMethodDetails, CHAIN_ID, METHOD_NAME,
};
pub struct TempoConfig<'a> {
pub recipient: &'a str,
}
pub struct TempoBuilder {
pub(crate) currency: String,
pub(crate) currency_explicit: bool,
pub(crate) recipient: String,
pub(crate) rpc_url: String,
pub(crate) realm: String,
pub(crate) secret_key: Option<String>,
pub(crate) decimals: u32,
pub(crate) fee_payer: bool,
pub(crate) chain_id: Option<u64>,
pub(crate) fee_payer_signer: Option<Box<dyn alloy::signers::Signer + Send + Sync>>,
}
impl TempoBuilder {
pub fn rpc_url(mut self, url: &str) -> Self {
self.rpc_url = url.to_string();
if self.chain_id.is_none() {
self.chain_id = Some(chain_id_from_rpc_url(url));
}
self
}
pub fn chain_id(mut self, id: u64) -> Self {
self.chain_id = Some(id);
self
}
pub fn currency(mut self, addr: &str) -> Self {
self.currency = addr.to_string();
self.currency_explicit = true;
self
}
pub fn realm(mut self, realm: &str) -> Self {
self.realm = realm.to_string();
self
}
pub fn secret_key(mut self, key: &str) -> Self {
self.secret_key = Some(key.to_string());
self
}
pub fn decimals(mut self, d: u32) -> Self {
self.decimals = d;
self
}
pub fn fee_payer(mut self, enabled: bool) -> Self {
self.fee_payer = enabled;
self
}
pub fn fee_payer_signer(
mut self,
signer: impl alloy::signers::Signer + Send + Sync + 'static,
) -> Self {
self.fee_payer_signer = Some(Box::new(signer));
self
}
}
pub fn tempo(config: TempoConfig<'_>) -> TempoBuilder {
TempoBuilder {
currency: crate::protocol::methods::tempo::DEFAULT_CURRENCY_MAINNET.to_string(),
currency_explicit: false,
recipient: config.recipient.to_string(),
rpc_url: crate::protocol::methods::tempo::DEFAULT_RPC_URL.to_string(),
realm: detect_realm(),
secret_key: None,
decimals: 6,
fee_payer: false,
chain_id: None,
fee_payer_signer: None,
}
}
fn chain_id_from_rpc_url(url: &str) -> u64 {
if url.contains("moderato") {
crate::protocol::methods::tempo::MODERATO_CHAIN_ID
} else {
crate::protocol::methods::tempo::CHAIN_ID
}
}
pub fn tempo_provider(rpc_url: &str) -> crate::error::Result<TempoProvider> {
use alloy::providers::ProviderBuilder;
use tempo_alloy::TempoNetwork;
let url = rpc_url
.parse()
.map_err(|e| crate::error::MppError::InvalidConfig(format!("invalid RPC URL: {}", e)))?;
Ok(ProviderBuilder::new_with_network::<TempoNetwork>().connect_http(url))
}
pub type TempoProvider = alloy::providers::fillers::FillProvider<
alloy::providers::fillers::JoinFill<
alloy::providers::Identity,
alloy::providers::fillers::JoinFill<
alloy::providers::fillers::NonceFiller,
alloy::providers::fillers::JoinFill<
alloy::providers::fillers::GasFiller,
alloy::providers::fillers::ChainIdFiller,
>,
>,
>,
alloy::providers::RootProvider<tempo_alloy::TempoNetwork>,
tempo_alloy::TempoNetwork,
>;