Sitelet https://github.com/SharkHand3/examples/commit/563757390a7575f52ef0ca2e6b2640d3d930a945
Skip to content

Commit 5637573

Browse files
yash-atreyamattsse
andauthored
feat: DynProvider (alloy-rs#201)
* feat: DynProvider * nit * bump reth and exclude from ci * nit * nit Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de> --------- Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
1 parent 2a2a6fb commit 5637573

4 files changed

Lines changed: 74 additions & 6 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ This repository contains the following examples:
7171
- [x] [HTTP](./examples/providers/examples/http.rs)
7272
- [x] [IPC](./examples/providers/examples/ipc.rs)
7373
- [x] [Wrapping a Provider](./examples/providers/examples/wrapped_provider.rs)
74+
- [x] [DynProvider](./examples/providers/examples/dyn_provider.rs)
7475
- [x] [WS](./examples/providers/examples/ws.rs)
7576
- [x] [Multicall Builder](./examples/providers/examples/multicall.rs)
7677
- [x] [WS with authentication](./examples/providers/examples/ws_with_auth.rs)

‎examples/advanced/Cargo.toml‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ foundry-fork-db.workspace = true
1818

1919
# reth
2020
revm-primitives = { version = "16.0.0-alpha.3", default-features = false }
21-
revm = { version = "20.0.0-alpha.5", default-features = false }
22-
reth-db = { git = "https://github.com/paradigmxyz/reth", package = "reth-db", rev = "v1.3.1" }
23-
reth-provider = { git = "https://github.com/paradigmxyz/reth", package = "reth-provider", rev = "v1.3.1" }
24-
reth-node-types = { git = "https://github.com/paradigmxyz/reth", package = "reth-node-types", rev = "v1.3.1" }
25-
reth-chainspec = { git = "https://github.com/paradigmxyz/reth", package = "reth-chainspec", rev = "v1.3.1" }
26-
reth-node-ethereum = { git = "https://github.com/paradigmxyz/reth", package = "reth-node-ethereum", rev = "v1.3.1" }
21+
revm = { version = "20.0.0", default-features = false }
22+
reth-db = { git = "https://github.com/paradigmxyz/reth", package = "reth-db", rev = "v1.3.4" }
23+
reth-provider = { git = "https://github.com/paradigmxyz/reth", package = "reth-provider", rev = "v1.3.4" }
24+
reth-node-types = { git = "https://github.com/paradigmxyz/reth", package = "reth-node-types", rev = "v1.3.4" }
25+
reth-chainspec = { git = "https://github.com/paradigmxyz/reth", package = "reth-chainspec", rev = "v1.3.4" }
26+
reth-node-ethereum = { git = "https://github.com/paradigmxyz/reth", package = "reth-node-ethereum", rev = "v1.3.4" }
2727

2828
eyre.workspace = true
2929
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//! Demonstrates how to obtain a `DynProvider` from a Provider.
2+
3+
use alloy::{
4+
node_bindings::Anvil,
5+
providers::{Provider, ProviderBuilder},
6+
signers::local::PrivateKeySigner,
7+
sol,
8+
};
9+
10+
// Codegen from embedded Solidity code and precompiled bytecode.
11+
sol! {
12+
#[allow(missing_docs)]
13+
// solc v0.8.26; solc Counter.sol --via-ir --optimize --bin
14+
#[sol(rpc, bytecode="6080806040523460135760df908160198239f35b600080fdfe6080806040526004361015601257600080fd5b60003560e01c9081633fb5c1cb1460925781638381f58a146079575063d09de08a14603c57600080fd5b3460745760003660031901126074576000546000198114605e57600101600055005b634e487b7160e01b600052601160045260246000fd5b600080fd5b3460745760003660031901126074576020906000548152f35b34607457602036600319011260745760043560005500fea2646970667358221220e978270883b7baed10810c4079c941512e93a7ba1cd1108c781d4bc738d9090564736f6c634300081a0033")]
15+
contract Counter {
16+
uint256 public number;
17+
18+
function setNumber(uint256 newNumber) public {
19+
number = newNumber;
20+
}
21+
22+
function increment() public {
23+
number++;
24+
}
25+
}
26+
}
27+
28+
#[tokio::main]
29+
async fn main() -> eyre::Result<()> {
30+
// Spin up a local Anvil node.
31+
// Ensure `anvil` is available in $PATH.
32+
let anvil = Anvil::new().spawn();
33+
34+
let signer_pk: PrivateKeySigner =
35+
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80".parse()?;
36+
37+
let from = signer_pk.address();
38+
39+
// Provider with verbose types.
40+
let regular_provider =
41+
ProviderBuilder::new().wallet(signer_pk).connect(anvil.endpoint().as_str()).await?;
42+
43+
// One can use the erased method to obtain a DynProvider from a Provider.
44+
let dyn_provider = regular_provider.erased();
45+
46+
// Note that the fillers set while building provider are still available, only the types have
47+
// been erased OR boxed under the hood.
48+
// This enables us to use the DynProvider as one would use a regular Provider with verbose
49+
// types.
50+
let counter = Counter::deploy(&dyn_provider).await?;
51+
52+
println!("Counter deployed at {}", counter.address());
53+
54+
// Sends a transaction with required properties such as gas, nonce, from filled.
55+
let incr = counter.increment().send().await?;
56+
let receipt = incr.get_receipt().await?;
57+
assert_eq!(receipt.from, from);
58+
59+
let number = counter.number().call().await?;
60+
61+
println!("New number: {}", number.number);
62+
63+
Ok(())
64+
}

‎scripts/run.sh‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ function main () {
3434
-e 'ws_auth' \
3535
-e 'ws' \
3636
-e 'yubi_signer' \
37+
-e 'foundry_fork_db' \
38+
-e 'reth_db_layer' \
39+
-e 'reth_db_provider' \
3740
| xargs -n1 echo
3841
)"
3942

0 commit comments

Comments
 (0)