Struct ethers_flashbots::Relay
source · pub struct Relay<S> { /* private fields */ }Expand description
A Flashbots relay client.
The client automatically signs every request and sets the Flashbots authorization header appropriately with the given signer.
Note: You probably do not want to use this directly, unless
you want to interact directly with the Relay. Most users should use
FlashbotsMiddleware instead.
Implementations§
source§impl<S: Signer> Relay<S>
impl<S: Signer> Relay<S>
sourcepub fn new(url: impl Into<Url>, signer: Option<S>) -> Self
pub fn new(url: impl Into<Url>, signer: Option<S>) -> Self
Initializes a new relay client.
Examples found in repository?
src/middleware.rs (line 117)
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
pub fn new(inner: M, relay_url: impl Into<Url>, relay_signer: S) -> Self {
Self {
inner,
relay: Relay::new(relay_url, Some(relay_signer)),
simulation_relay: None,
}
}
/// Get the relay client used by the middleware.
pub fn relay(&self) -> &Relay<S> {
&self.relay
}
/// Get the relay client used by the middleware to simulate
/// bundles if set.
pub fn simulation_relay(&self) -> Option<&Relay<S>> {
self.simulation_relay.as_ref()
}
/// Set a separate relay to use for simulating bundles.
///
/// This can either be a full Flashbots relay or a node that implements
/// the `eth_callBundle` remote procedure call.
pub fn set_simulation_relay(&mut self, relay_url: impl Into<Url>) {
self.simulation_relay = Some(Relay::new(relay_url, None));
}sourcepub async fn request<T: Serialize + Send + Sync, R: DeserializeOwned>(
&self,
method: &str,
params: T
) -> Result<R, RelayError<S>>
pub async fn request<T: Serialize + Send + Sync, R: DeserializeOwned>(
&self,
method: &str,
params: T
) -> Result<R, RelayError<S>>
Sends a request with the provided method to the relay, with the parameters serialized as JSON.
Examples found in repository?
src/middleware.rs (line 159)
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
pub async fn simulate_bundle(
&self,
bundle: &BundleRequest,
) -> Result<SimulatedBundle, FlashbotsMiddlewareError<M, S>> {
bundle
.block()
.and(bundle.simulation_block())
.and(bundle.simulation_timestamp())
.ok_or(FlashbotsMiddlewareError::MissingParameters)?;
self.simulation_relay
.as_ref()
.unwrap_or(&self.relay)
.request("eth_callBundle", [bundle])
.await
.map_err(FlashbotsMiddlewareError::RelayError)
}
/// Send a bundle to the relayer.
///
/// See [`eth_sendBundle`][fb_sendBundle] for more information.
///
/// [fb_sendBundle]: https://docs.flashbots.net/flashbots-auction/searchers/advanced/rpc-endpoint#eth_sendbundle
pub async fn send_bundle(
&self,
bundle: &BundleRequest,
) -> Result<PendingBundle<'_, <Self as Middleware>::Provider>, FlashbotsMiddlewareError<M, S>>
{
// The target block must be set
bundle
.block()
.ok_or(FlashbotsMiddlewareError::MissingParameters)?;
// `min_timestamp` and `max_timestamp` must both either be unset or set.
if bundle.min_timestamp().xor(bundle.max_timestamp()).is_some() {
return Err(FlashbotsMiddlewareError::MissingParameters);
}
let response: SendBundleResponse = self
.relay
.request("eth_sendBundle", [bundle])
.await
.map_err(FlashbotsMiddlewareError::RelayError)?;
Ok(PendingBundle::new(
response.bundle_hash,
bundle.block().unwrap(),
bundle.transaction_hashes(),
self.provider(),
))
}
/// Get stats for a particular bundle.
pub async fn get_bundle_stats(
&self,
bundle_hash: BundleHash,
block_number: U64,
) -> Result<BundleStats, FlashbotsMiddlewareError<M, S>> {
self.relay
.request(
"flashbots_getBundleStatsV2",
[GetBundleStatsParams {
bundle_hash,
block_number,
}],
)
.await
.map_err(FlashbotsMiddlewareError::RelayError)
}
/// Get stats for your searcher identity.
///
/// Your searcher identity is determined by the signer you
/// constructed the middleware with.
pub async fn get_user_stats(&self) -> Result<UserStats, FlashbotsMiddlewareError<M, S>> {
let latest_block = self
.inner
.get_block_number()
.await
.map_err(FlashbotsMiddlewareError::MiddlewareError)?;
self.relay
.request(
"flashbots_getUserStatsV2",
[GetUserStatsParams {
block_number: latest_block,
}],
)
.await
.map_err(FlashbotsMiddlewareError::RelayError)
}