Skip to content

Commit

Permalink
Added tx-hash-mining examples && providers/examples/trace_block && pr…
Browse files Browse the repository at this point in the history
…oviders/examples/mempool_tracecall
  • Loading branch information
lcfr-eth committed Jan 10, 2025
1 parent 9db483f commit 9ee1191
Show file tree
Hide file tree
Showing 71 changed files with 27,539 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ tokio = "1.42"
eyre = "0.6"
serde = "1.0"
serde_json = "1.0"
rand = "0.8"

[patch.crates-io]
# alloy = { git = "https://github.com/alloy-rs/alloy", rev = "65dfbe" }
Expand Down
35 changes: 35 additions & 0 deletions examples/providers/examples/mempool_tracecall.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//! Example of using trace_call on pending transactions.
use alloy::{
providers::{ext::TraceApi, ProviderBuilder, Provider, WsConnect},
rpc::types::{trace::parity::TraceType},
};

use eyre::Result;
use futures_util::StreamExt;

#[tokio::main]
async fn main() -> Result<()> {
let rpc_url = "wss://eth-mainnet.g.alchemy.com/v2/your-api-key";

let ws = WsConnect::new(rpc_url);
let provider = ProviderBuilder::new().on_ws(ws).await?;

let sub = provider.subscribe_full_pending_transactions().await?;

let mut stream = sub.into_stream().take(1);

println!("Awaiting pending transactions...");

let handle = tokio::spawn(async move {
while let Some(tx) = stream.next().await {
let trace_type = [TraceType::Trace];
let result = provider.trace_call(&tx.into(), &trace_type).await;
println!("{:?}", result.unwrap().trace);
}
});

handle.await?;
Ok(())
}

50 changes: 50 additions & 0 deletions examples/providers/examples/trace_block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//! Example of using trace_block to examine transactions of the latest block.
use alloy::{
providers::{ext::TraceApi, Provider, ProviderBuilder, WsConnect},
rpc::types::trace::parity::{Action, TraceOutput},
rpc::types::{BlockId, BlockNumberOrTag},
};

use eyre::Result;
use futures_util::StreamExt;

#[tokio::main]
async fn main() -> Result<()> {
let ws_url = "wss://eth-mainnet.g.alchemy.com/v2/your-api-key";

let ws = WsConnect::new(ws_url);
let provider = ProviderBuilder::new().on_ws(ws).await?;


let subscription = provider.subscribe_blocks().await?;
let mut stream = subscription.into_stream();

while let Some(block) = stream.next().await {
println!(
"Received block number: {}",
block.inner.number
);

let traces = provider.trace_block(
BlockId::Number(
BlockNumberOrTag::Number(
block.inner.number
)
)
).await?;

for trace in traces {
match trace.trace.action {
Action::Call(tx) => {
if tx.input.0.len() < 4 {
continue;
}
println!("{:?}", tx);
},
_ => {}
}
}
}
Ok(())
}
21 changes: 21 additions & 0 deletions examples/tx-hash-mining/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "examples-tx-mining"
publish.workspace = true
version.workspace = true
edition.workspace = true
rust-version.workspace = true
authors.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true

[lints]
workspace = true

[dev-dependencies]
alloy.workspace = true

rand.workspace = true
eyre.workspace = true
futures-util.workspace = true
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: test

on: workflow_dispatch

env:
FOUNDRY_PROFILE: ci

jobs:
check:
strategy:
fail-fast: true

name: Foundry project
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- name: Run Forge build
run: |
forge --version
forge build --sizes
id: build

- name: Run Forge tests
run: |
forge test -vvv
id: test
14 changes: 14 additions & 0 deletions examples/tx-hash-mining/examples/dynamic-example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Compiler files
cache/
out/

# Ignores development broadcast logs
!/broadcast
/broadcast/*/31337/
/broadcast/**/dry-run/

# Docs
docs/

# Dotenv file
.env
66 changes: 66 additions & 0 deletions examples/tx-hash-mining/examples/dynamic-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
## Foundry

**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.**

Foundry consists of:

- **Forge**: Ethereum testing framework (like Truffle, Hardhat and DappTools).
- **Cast**: Swiss army knife for interacting with EVM smart contracts, sending transactions and getting chain data.
- **Anvil**: Local Ethereum node, akin to Ganache, Hardhat Network.
- **Chisel**: Fast, utilitarian, and verbose solidity REPL.

## Documentation

https://book.getfoundry.sh/

## Usage

### Build

```shell
$ forge build
```

### Test

```shell
$ forge test
```

### Format

```shell
$ forge fmt
```

### Gas Snapshots

```shell
$ forge snapshot
```

### Anvil

```shell
$ anvil
```

### Deploy

```shell
$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key>
```

### Cast

```shell
$ cast <subcommand>
```

### Help

```shell
$ forge --help
$ anvil --help
$ cast --help
```
6 changes: 6 additions & 0 deletions examples/tx-hash-mining/examples/dynamic-example/foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[profile.default]
src = "src"
out = "out"
libs = ["lib"]

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/Vm.sol linguist-generated
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
name: CI

on:
workflow_dispatch:
pull_request:
push:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- name: Print forge version
run: forge --version

# Backwards compatibility checks:
# - the oldest and newest version of each supported minor version
# - versions with specific issues
- name: Check compatibility with latest
if: always()
run: |
output=$(forge build --skip test)
if echo "$output" | grep -q "Warning"; then
echo "$output"
exit 1
fi
- name: Check compatibility with 0.8.0
if: always()
run: |
output=$(forge build --skip test --use solc:0.8.0)
if echo "$output" | grep -q "Warning"; then
echo "$output"
exit 1
fi
- name: Check compatibility with 0.7.6
if: always()
run: |
output=$(forge build --skip test --use solc:0.7.6)
if echo "$output" | grep -q "Warning"; then
echo "$output"
exit 1
fi
- name: Check compatibility with 0.7.0
if: always()
run: |
output=$(forge build --skip test --use solc:0.7.0)
if echo "$output" | grep -q "Warning"; then
echo "$output"
exit 1
fi
- name: Check compatibility with 0.6.12
if: always()
run: |
output=$(forge build --skip test --use solc:0.6.12)
if echo "$output" | grep -q "Warning"; then
echo "$output"
exit 1
fi
- name: Check compatibility with 0.6.2
if: always()
run: |
output=$(forge build --skip test --use solc:0.6.2)
if echo "$output" | grep -q "Warning"; then
echo "$output"
exit 1
fi
# via-ir compilation time checks.
- name: Measure compilation time of Test with 0.8.17 --via-ir
if: always()
run: forge build --skip test --contracts test/compilation/CompilationTest.sol --use solc:0.8.17 --via-ir

- name: Measure compilation time of TestBase with 0.8.17 --via-ir
if: always()
run: forge build --skip test --contracts test/compilation/CompilationTestBase.sol --use solc:0.8.17 --via-ir

- name: Measure compilation time of Script with 0.8.17 --via-ir
if: always()
run: forge build --skip test --contracts test/compilation/CompilationScript.sol --use solc:0.8.17 --via-ir

- name: Measure compilation time of ScriptBase with 0.8.17 --via-ir
if: always()
run: forge build --skip test --contracts test/compilation/CompilationScriptBase.sol --use solc:0.8.17 --via-ir

test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- name: Print forge version
run: forge --version

- name: Run tests
run: forge test -vvv

fmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- name: Print forge version
run: forge --version

- name: Check formatting
run: forge fmt --check
Loading

0 comments on commit 9ee1191

Please sign in to comment.