use r402_protocol::network::ChainId;
use r402_protocol::payment::PriceTag;
#[cfg(not(feature = "stellar"))]
use super::feature_missing;
#[cfg(feature = "stellar")]
use super::{
accept_custom_decimals, amount_i128, custom_asset_address, invalid_asset_address,
invalid_network, invalid_pay_to, named_ticker, no_deployment, reject_evm_fields, require_exact,
};
use crate::config::{AcceptConfig, ConfigError};
#[cfg(not(feature = "stellar"))]
pub(super) fn build(
_accept: &AcceptConfig,
_network: &ChainId,
_amount: u128,
_pay_to: &str,
) -> Result<PriceTag, ConfigError> {
Err(feature_missing("stellar", "stellar"))
}
#[cfg(not(feature = "stellar"))]
pub(super) fn decimals(_accept: &AcceptConfig, _network: &ChainId) -> Result<u8, ConfigError> {
Err(feature_missing("stellar", "stellar"))
}
#[cfg(feature = "stellar")]
pub(super) fn decimals(accept: &AcceptConfig, network: &ChainId) -> Result<u8, ConfigError> {
Ok(token(accept, network)?.decimals)
}
#[cfg(feature = "stellar")]
pub(super) fn build(
accept: &AcceptConfig,
network: &ChainId,
amount: u128,
pay_to: &str,
) -> Result<PriceTag, ConfigError> {
use std::str::FromStr as _;
use r402_stellar::StellarExact;
use r402_stellar::chain::StellarAddress;
require_exact(accept)?;
reject_evm_fields(accept, "stellar")?;
let amount = amount_i128(amount)?;
let pay_to = StellarAddress::from_str(pay_to)
.map_err(|error| invalid_pay_to("Stellar", pay_to, error))?;
let token = token(accept, network)?;
Ok(StellarExact::price_tag(pay_to, token.amount(amount)))
}
#[cfg(feature = "stellar")]
fn token(
accept: &AcceptConfig,
network: &ChainId,
) -> Result<r402_stellar::chain::StellarTokenDeployment, ConfigError> {
use std::str::FromStr as _;
use r402_stellar::USDC;
use r402_stellar::chain::{StellarAddress, StellarChainReference, StellarTokenDeployment};
let chain = StellarChainReference::try_from(network.clone())
.map_err(|error| invalid_network("stellar", &accept.network, error))?;
if named_ticker(accept, &["usdc"])?.is_some() {
return USDC::on(chain)
.cloned()
.ok_or_else(|| no_deployment("USDC", &accept.network));
}
let address = custom_asset_address(accept)?;
let address =
StellarAddress::from_str(address).map_err(|error| invalid_asset_address(address, error))?;
Ok(StellarTokenDeployment::new(
chain,
address,
accept_custom_decimals(accept)?,
))
}