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

feat: add nonce to RPC transaction #246

Merged
merged 2 commits into from
Nov 12, 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
18 changes: 17 additions & 1 deletion crates/rpc-types/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub struct Transaction {
#[deref_mut]
pub inner: alloy_rpc_types_eth::Transaction<OpTxEnvelope>,

/// Nonce for deposit transactions. Only present in RPC responses.
pub deposit_nonce: Option<u64>,

/// Deposit receipt version for deposit transactions post-canyon
pub deposit_receipt_version: Option<u64>,
}
Expand Down Expand Up @@ -177,6 +180,13 @@ mod tx_serde {
with = "alloy_serde::quantity::opt"
)]
effective_gas_price: Option<u128>,
#[serde(
default,
rename = "nonce",
skip_serializing_if = "Option::is_none",
with = "alloy_serde::quantity::opt"
)]
deposit_nonce: Option<u64>,
}

#[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -214,6 +224,7 @@ mod tx_serde {
from,
},
deposit_receipt_version,
deposit_nonce,
} = value;

// if inner transaction is a deposit, then don't serialize `from` directly
Expand All @@ -228,7 +239,7 @@ mod tx_serde {
block_number,
transaction_index,
deposit_receipt_version,
other: OptionalFields { from, effective_gas_price },
other: OptionalFields { from, effective_gas_price, deposit_nonce },
}
}
}
Expand Down Expand Up @@ -256,6 +267,9 @@ mod tx_serde {
return Err(serde_json::Error::custom("missing `from` field"));
};

// Only serialize deposit_nonce if inner transaction is deposit to avoid duplicated keys
let deposit_nonce = other.deposit_nonce.filter(|_| inner.is_deposit());

let effective_gas_price = other.effective_gas_price.or(inner.gas_price());

Ok(Self {
Expand All @@ -268,6 +282,7 @@ mod tx_serde {
effective_gas_price,
},
deposit_receipt_version,
deposit_nonce,
})
}
}
Expand All @@ -289,5 +304,6 @@ mod tests {
panic!("Expected deposit transaction");
};
assert_eq!(tx.from, inner.from);
assert_eq!(tx.deposit_nonce, Some(22211221));
}
}
Loading