This repository has been archived by the owner on Oct 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 795
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support eth_sendRawTransactionConditional
- Loading branch information
Showing
7 changed files
with
124 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use crate::types::{Address, BlockNumber, H256}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::collections::HashMap; | ||
|
||
/// Extra options parameter for `eth_sendRawTransactionConditional` | ||
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] | ||
pub struct ConditionalOptions { | ||
/// A map of accounts with expected storage | ||
#[serde(rename = "knownAccounts")] | ||
pub known_accounts: HashMap<Address, AccountStorage>, | ||
|
||
/// Minimal block number for inclusion | ||
#[serde(rename = "blockNumberMin", skip_serializing_if = "Option::is_none")] | ||
pub block_number_min: Option<BlockNumber>, | ||
|
||
/// Maximum block number for inclusion | ||
#[serde(rename = "blockNumberMax", skip_serializing_if = "Option::is_none")] | ||
pub block_number_max: Option<BlockNumber>, | ||
|
||
/// Minimal block timestamp for inclusion | ||
#[serde(rename = "timestampMin", skip_serializing_if = "Option::is_none")] | ||
pub timestamp_min: Option<u64>, | ||
|
||
/// Maximum block timestamp for inclusion | ||
#[serde(rename = "timestampMax", skip_serializing_if = "Option::is_none")] | ||
pub timestamp_max: Option<u64>, | ||
} | ||
|
||
/// Account storage | ||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | ||
#[serde(untagged)] | ||
pub enum AccountStorage { | ||
RootHash(H256), | ||
SlotValues(HashMap<H256, H256>), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use ethers::{ | ||
middleware::SignerMiddleware, | ||
providers::{Http, Middleware, Provider}, | ||
signers::{LocalWallet, Signer}, | ||
types::{transaction::conditional::ConditionalOptions, BlockNumber, TransactionRequest}, | ||
}; | ||
use eyre::Result; | ||
|
||
/// Use 'eth_sendRawTransactionConditional' to send a transaction with a conditional options | ||
/// requires, a valid endpoint in `RPC_URL` env var that supports | ||
/// `eth_sendRawTransactionConditional` | ||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
if let Ok(url) = std::env::var("RPC_URL") { | ||
let provider = Provider::<Http>::try_from(url)?; | ||
let chain_id = provider.get_chainid().await?; | ||
let wallet: LocalWallet = | ||
"380eb0f3d505f087e438eca80bc4df9a7faa24f868e69fc0440261a0fc0567dc".parse()?; | ||
let from = wallet.address(); | ||
|
||
let client = SignerMiddleware::new(provider, wallet.with_chain_id(chain_id.as_u64())); | ||
|
||
let mut tx = TransactionRequest::default() | ||
.from(from) | ||
.to("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045") | ||
.value(100) | ||
.into(); | ||
|
||
client.fill_transaction(&mut tx, None).await.unwrap(); | ||
|
||
let signed_tx = client.sign_transaction(tx).await.unwrap(); | ||
let pending_tx = client | ||
.send_raw_transaction_conditional( | ||
signed_tx, | ||
ConditionalOptions { | ||
block_number_min: Some(BlockNumber::from(33285900)), | ||
..Default::default() | ||
}, | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
let receipt = pending_tx.await?.ok_or_else(|| eyre::eyre!("tx not included"))?; | ||
let tx = client.get_transaction(receipt.transaction_hash).await?; | ||
|
||
println!("Sent transaction: {}\n", serde_json::to_string(&tx)?); | ||
println!("Receipt: {}\n", serde_json::to_string(&receipt)?); | ||
} | ||
|
||
Ok(()) | ||
} |