use celestia_types::state::AccAddress;
use serde::{
Deserialize, Serialize,
ser::{SerializeStruct, Serializer},
};
use serde_repr::{Deserialize_repr, Serialize_repr};
#[derive(
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Debug,
Default,
Serialize_repr,
Deserialize_repr,
)]
#[repr(u8)]
pub enum TxPriority {
Low = 1,
#[default]
Medium = 2,
High = 3,
}
#[derive(Debug, Default, Deserialize)]
pub struct TxConfig {
pub signer_address: Option<AccAddress>,
pub key_name: Option<String>,
pub gas_price: Option<f64>,
pub max_gas_price: Option<f64>,
pub gas: Option<u64>,
pub priority: Option<TxPriority>,
pub fee_granter_address: Option<AccAddress>,
}
impl TxConfig {
pub fn with_gas_price(mut self, gas_price: f64) -> Self {
self.gas_price = Some(gas_price);
self
}
pub fn with_max_gas_price(mut self, max_gas_price: f64) -> Self {
self.max_gas_price = Some(max_gas_price);
self
}
pub fn with_gas(mut self, gas: u64) -> Self {
self.gas = Some(gas);
self
}
pub fn with_priority(mut self, priority: TxPriority) -> Self {
self.priority = Some(priority);
self
}
pub fn with_fee_granter_address(mut self, fee_granter_address: AccAddress) -> Self {
self.fee_granter_address = Some(fee_granter_address);
self
}
pub fn with_signer_address(mut self, signer_address: AccAddress) -> Self {
self.signer_address = Some(signer_address);
self
}
pub fn with_key_name(mut self, key_name: impl Into<String>) -> Self {
self.key_name = Some(key_name.into());
self
}
}
impl Serialize for TxConfig {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state = serializer.serialize_struct("TxConfig", 6)?;
if let Some(signer_address) = &self.signer_address {
state.serialize_field("signer_address", signer_address)?;
}
if let Some(key_name) = &self.key_name {
state.serialize_field("key_name", key_name)?;
}
if let Some(gas_price) = &self.gas_price {
state.serialize_field("gas_price", gas_price)?;
state.serialize_field("is_gas_price_set", &true)?;
}
if let Some(max_gas_price) = &self.max_gas_price {
state.serialize_field("max_gas_price", max_gas_price)?;
}
if let Some(gas) = &self.gas {
state.serialize_field("gas", gas)?;
}
if let Some(priority) = &self.priority {
state.serialize_field("priority", priority)?;
}
if let Some(fee_granter_address) = &self.fee_granter_address {
state.serialize_field("fee_granter_address", fee_granter_address)?;
}
state.end()
}
}