Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make l1_fee_scalar f64 #209

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 79 additions & 3 deletions crates/rpc-types/src/eth/transaction/optimism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct OptimismTransactionFields {
}

/// Additional fields for Optimism transaction receipts
#[derive(Clone, Copy, Default, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Copy, Default, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OptimismTransactionReceiptFields {
/// Deposit nonce for deposit transactions post-regolith
Expand All @@ -30,8 +30,8 @@ pub struct OptimismTransactionReceiptFields {
#[serde(skip_serializing_if = "Option::is_none")]
pub l1_fee: Option<U256>,
/// L1 fee scalar for the transaction
#[serde(skip_serializing_if = "Option::is_none")]
pub l1_fee_scalar: Option<U256>,
#[serde(default, skip_serializing_if = "Option::is_none", with = "l1_fee_scalar_serde")]
pub l1_fee_scalar: Option<f64>,
/// L1 gas price for the transaction
#[serde(skip_serializing_if = "Option::is_none")]
pub l1_gas_price: Option<U256>,
Expand All @@ -51,3 +51,79 @@ impl From<OptimismTransactionReceiptFields> for OtherFields {
serde_json::to_value(value).unwrap().try_into().unwrap()
}
}

/// Serialize/Deserialize l1FeeScalar to/from string
mod l1_fee_scalar_serde {
use serde::{de, Deserialize};

pub(super) fn serialize<S>(value: &Option<f64>, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
if let Some(v) = value {
return s.serialize_str(&v.to_string());
}
s.serialize_none()
}

pub(super) fn deserialize<'de, D>(deserializer: D) -> Result<Option<f64>, D::Error>
where
D: serde::Deserializer<'de>,
{
let s: Option<String> = Option::deserialize(deserializer)?;
if let Some(s) = s {
return Ok(Some(s.parse::<f64>().map_err(de::Error::custom)?));
}

Ok(None)
}
}

#[cfg(test)]
mod tests {
use serde_json::{json, Value};

use super::*;

#[test]
fn serialize_empty_optimism_transaction_receipt_fields_struct() {
let op_fields = OptimismTransactionReceiptFields::default();

let json = serde_json::to_value(op_fields).unwrap();
assert_eq!(json, json!({}));
}

#[test]
fn serialize_l1_fee_scalar() {
let op_fields = OptimismTransactionReceiptFields {
l1_fee_scalar: Some(0.678),
..OptimismTransactionReceiptFields::default()
};

let json = serde_json::to_value(op_fields).unwrap();

assert_eq!(json["l1FeeScalar"], serde_json::Value::String("0.678".to_string()));
}

#[test]
fn deserialize_l1_fee_scalar() {
let json = json!({
"l1FeeScalar": "0.678"
});

let op_fields: OptimismTransactionReceiptFields = serde_json::from_value(json).unwrap();
assert_eq!(op_fields.l1_fee_scalar, Some(0.678f64));

let json = json!({
"l1FeeScalar": Value::Null
});

let op_fields: OptimismTransactionReceiptFields = serde_json::from_value(json).unwrap();
assert_eq!(op_fields.l1_fee_scalar, None);

let json = json!({});

let op_fields: OptimismTransactionReceiptFields = serde_json::from_value(json).unwrap();
assert_eq!(op_fields.l1_fee_scalar, None);
}
}
Loading