Skip to content
This repository was archived by the owner on Oct 19, 2024. It is now read-only.

Commit 10310ce

Browse files
0xMelkorDaniPopes
andauthored
refactor: examples (#1940)
* ToC * Big numbers section * Middleware examples: builder * Middleware examples: gas_escalator * Middleware examples: gas_oracle * Middleware examples: signer * Middleware examples: missing stubs * review: applied DaniPopes suggestions to big numbers * typo * Middleware examples: nonce_manager * cargo +nightly fmt * update roadmap * Middleware examples: policy * Middleware examples: added docs * Contracts examples: created folder; included abigen example * Contracts examples: refactor abigen docs. Fixed cargo example reference * Contracts examples: contract_events; minor docs changes * Moved each example under its own crate. Cargo builds locally TODO: Fix broken examples CI * Big numbers examples: used regular operators for math * Single examples run correctly (missing overall CI execution) Example crates dependencies Removed duplicates * review: Applied gakonst note to remove commented items in workspace manifest * review: Applied gakonst note to restore visibility on contract constructor * ci: - Run/Build examples in a single step to avoid duplicated scripts - Removed ci.yaml step "Build all examples" * cargo +nightly fmt * ci: fix WASM step error * Removed deprecated EthGasStation example * WASM example uses local copy of `contract_abi.json`. In this way we keep the WASM example auto-consistent, at the cost of a small duplication * Cargo.lock aligned to master branch * Removed useless comments in examples * review: Applied gakonst note to add panic!() on the policy middleware example * review: Applied gakonst suggestion to add a custom middleware example * typos in docs * Update examples/big-numbers/examples/bn_math_operations.rs review: Accepted commit suggested by DaniPopes Co-authored-by: DaniPopes <[email protected]> * review: Applied DaniPopes suggestion on assert_eq! * Update examples/big-numbers/README.md review: Accepted DaniPopes suggestion on big-numbers docs Co-authored-by: DaniPopes <[email protected]> * review: All imports now reference the "ethers" crate * ci: added features ["ws", "rustls"] where needed cargo +nigthly fmt * Examples with special features (e.g. ipc, trezor etc.) are built alongside them. This is expressed as a "default" requirement in their respective Cargo.toml * cargo +nightly fmt * Examples: Gas oracle API keys from env Added missing features in middleware Cargo.toml * typo: use expect() instead of unwrap() * Updated ToC Moved 2 examples under more relevant folders * Gas oracle examples raise panic on middleware errors * review: removed useless [[example]] in Cargo.toml * review: removed #[allow(unused_must_use)] from gas_escalator example * review: Removed prefixes from file names * review: removed useless [[example]] in Cargo.toml * docs: Updated description to run examples in the workspace README.md Co-authored-by: Andrea Simeoni <> Co-authored-by: DaniPopes <[email protected]>
1 parent e26ede2 commit 10310ce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+1623
-207
lines changed

Diff for: .github/workflows/ci.yml

+1-26
Original file line numberDiff line numberDiff line change
@@ -232,32 +232,7 @@ jobs:
232232
- uses: Swatinem/rust-cache@v1
233233
with:
234234
cache-on-failure: true
235-
- name: Build all examples
236-
run: |
237-
export PATH=$HOME/bin:$PATH
238-
examples=$(cargo metadata --format-version 1 | \
239-
jq -c '.packages[]
240-
| select(.name == "ethers")
241-
| .targets[]
242-
| select(.kind[] | contains("example"))
243-
| with_entries(select([.key]
244-
| inside(["name", "required-features"])))'
245-
)
246-
for example in $examples; do
247-
name="$(echo "$example" | jq -r '.name')"
248-
args=(
249-
-p ethers
250-
--example "$name"
251-
)
252-
features="$(echo "$example" | jq -r 'try(."required-features" | join(","))')"
253-
if [[ ! -z "$features" ]]; then
254-
args+=(--features "$features")
255-
fi
256-
257-
echo "building $name"
258-
cargo build "${args[@]}"
259-
done
260-
- name: Run all examples
235+
- name: Build and run all examples
261236
run: |
262237
export PATH=$HOME/bin:$PATH
263238
chmod +x ./scripts/examples.sh

Diff for: Cargo.lock

+105
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+2-60
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ members = [
2121
"ethers-middleware",
2222
"ethers-etherscan",
2323
"ethers-solc",
24-
"examples/ethers-wasm",
24+
# Example crates
25+
"examples/*",
2526
]
2627

2728
default-members = [
@@ -128,62 +129,3 @@ bytes = "1.3.0"
128129
# Tell `rustc` to optimize for small code size.
129130
opt-level = "s"
130131

131-
[[example]]
132-
name = "abigen"
133-
path = "examples/abigen.rs"
134-
required-features = ["ethers-solc"]
135-
136-
[[example]]
137-
name = "contract_human_readable"
138-
path = "examples/contract_human_readable.rs"
139-
required-features = ["ethers-solc"]
140-
141-
[[example]]
142-
name = "contract_with_abi"
143-
path = "examples/contract_with_abi.rs"
144-
required-features = ["ethers-solc"]
145-
146-
[[example]]
147-
name = "contract_with_abi_and_bytecode"
148-
path = "examples/contract_with_abi_and_bytecode.rs"
149-
required-features = ["ethers-solc"]
150-
151-
[[example]]
152-
name = "ipc"
153-
path = "examples/ipc.rs"
154-
required-features = ["ipc"]
155-
156-
[[example]]
157-
name = "ledger"
158-
path = "examples/ledger.rs"
159-
required-features = ["ledger"]
160-
161-
[[example]]
162-
name = "moonbeam_with_abi"
163-
path = "examples/moonbeam_with_abi.rs"
164-
required-features = ["legacy", "ethers-solc"]
165-
166-
[[example]]
167-
name = "trezor"
168-
path = "examples/trezor.rs"
169-
required-features = ["trezor"]
170-
171-
[[example]]
172-
name = "yubi"
173-
path = "examples/yubi.rs"
174-
required-features = ["yubi"]
175-
176-
[[example]]
177-
name = "paginated_logs"
178-
path = "examples/paginated_logs.rs"
179-
required-features = ["rustls"]
180-
181-
[[example]]
182-
name = "subscribe_contract_events"
183-
path = "examples/subscribe_contract_events.rs"
184-
required-features = ["rustls", "ws"]
185-
186-
[[example]]
187-
name = "subscribe_contract_events_with_meta"
188-
path = "examples/subscribe_contract_events_with_meta.rs"
189-
required-features = ["rustls", "ws"]

Diff for: README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ Extensive documentation and examples are available [here](https://docs.rs/ethers
1515

1616
Alternatively, you may clone the repository and run `cd ethers/ && cargo doc --open`
1717

18-
You can also run any of the examples by executing: `cargo run -p ethers --example <name>`
18+
Examples are organized into individual crates under the `/examples` folder.
19+
You can run any of the examples by executing:
20+
```bash
21+
# cargo run -p <example-crate-name> --example <name>
22+
cargo run -p examples-big-numbers --example math_operations
23+
```
1924

2025
## Add ethers-rs to your repository
2126

Diff for: examples/README.md

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Examples
2+
- [ ] Address book
3+
- [ ] Anvil
4+
- [ ] Boot anvil
5+
- [ ] Deploy contracts
6+
- [x] Fork
7+
- [ ] Testing
8+
- [x] Big numbers
9+
- [x] Comparison and equivalence
10+
- [x] Conversion
11+
- [x] Creating Instances
12+
- [x] Math operations
13+
- [x] Utilities
14+
- [ ] Contracts
15+
- [x] Abigen
16+
- [x] Compile
17+
- [ ] Creating Instances
18+
- [x] Deploy Anvil
19+
- [x] Deploy from ABI and bytecode
20+
- [x] Deploy Moonbeam
21+
- [x] Events
22+
- [x] Events with meta
23+
- [ ] Methods
24+
- [ ] Events
25+
- [ ] Logs and filtering
26+
- [ ] Solidity topics
27+
- [ ] Middleware
28+
- [x] Builder
29+
- [x] Create custom middleware
30+
- [x] Gas escalator
31+
- [x] Gas oracle
32+
- [x] Nonce manager
33+
- [x] Policy
34+
- [x] Signer
35+
- [ ] Time lag
36+
- [ ] Transformer
37+
- [ ] Providers
38+
- [ ] Http
39+
- [x] IPC
40+
- [ ] Mock
41+
- [x] Quorum
42+
- [ ] Retry
43+
- [x] RW
44+
- [ ] WS
45+
- [ ] Queries
46+
- [ ] Blocks
47+
- [x] Contracts
48+
- [ ] Events
49+
- [x] Paginated logs
50+
- [x] UniswapV2 pair
51+
- [ ] Transactions
52+
- [x] Subscriptions
53+
- [x] Watch blocks
54+
- [x] Subscribe events by type
55+
- [x] Subscribe logs
56+
- [ ] Transactions
57+
- [x] Call override
58+
- [ ] Create raw transaction
59+
- [ ] Create typed transaction
60+
- [x] Decode input
61+
- [ ] EIP-1559
62+
- [x] ENS
63+
- [ ] Estimate gas
64+
- [ ] Get gas price
65+
- [x] Get gas price USD
66+
- [x] Remove liquidity
67+
- [ ] Set gas for a transaction
68+
- [ ] Send raw transaction
69+
- [ ] Send typed transaction
70+
- [x] Trace
71+
- [ ] Transaction receipt
72+
- [ ] Transaction status
73+
- [x] Transfer ETH
74+
- [x] Wallets
75+
- [x] Mnemonic
76+
- [x] Ledger
77+
- [x] Local
78+
- [x] Permit hash
79+
- [x] Sign message
80+
- [x] Trezor
81+
- [x] Yubi

Diff for: examples/anvil/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "examples-anvil"
3+
version = "1.0.2"
4+
authors = ["Andrea Simeoni <[email protected]>"]
5+
edition = "2021"
6+
7+
[dev-dependencies]
8+
ethers = { path = "../..", version = "1.0.0" }
9+
eyre = "0.6"
10+
tokio = { version = "1.18", features = ["full"] }

Diff for: examples/anvil/README.md

Whitespace-only changes.

Diff for: examples/anvil_fork.rs renamed to examples/anvil/examples/anvil_fork.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
//! Spawn an [anvil](https://github.com/foundry-rs/foundry/tree/master/anvil) instance in forking mode
22
33
use ethers::utils::Anvil;
4+
use eyre::Result;
45

56
#[tokio::main]
6-
async fn main() -> eyre::Result<()> {
7+
async fn main() -> Result<()> {
78
// ensure `anvil` is available in $PATH
89
let anvil =
910
Anvil::new().fork("https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27").spawn();

Diff for: examples/big-numbers/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "examples-big-numbers"
3+
version = "1.0.2"
4+
authors = ["Andrea Simeoni <[email protected]>"]
5+
edition = "2021"
6+
7+
[dev-dependencies]
8+
#ethers-core = { version = "^1.0.0", path = "../../ethers-core" }
9+
ethers = { path = "../..", version = "1.0.0" }
10+

Diff for: examples/big-numbers/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Big numbers
2+
Ethereum uses big numbers (also known as "bignums" or "arbitrary-precision integers") to represent certain values in its codebase and in blockchain transactions. This is necessary because [the EVM](https://ethereum.org/en/developers/docs/evm) operates on a 256-bit word size, which is different from the usual 32-bit or 64-bit of modern machines. This was chosen for the ease of use with 256-bit cryptography (such as Keccak-256 hashes or secp256k1 signatures).
3+
4+
It is worth noting that Ethereum is not the only blockchain or cryptocurrency that uses big numbers. Many other blockchains and cryptocurrencies also use big numbers to represent values in their respective systems.
5+
6+
## Utilities
7+
In order to create an application, it is often necessary to convert between the representation of values that is easily understood by humans (such as ether) and the machine-readable form that is used by contracts and math functions (such as wei). This is particularly important when working with Ethereum, as certain values, such as balances and gas prices, must be expressed in wei when sending transactions, even if they are displayed to the user in a different format, such as ether or gwei. To help with this conversion, ethers-rs provides two functions, `parse_units` and `format_units`, which allow you to easily convert between human-readable and machine-readable forms of values. `parse_units` can be used to convert a string representing a value in ether, such as "1.1", into a big number in wei, which can be used in contracts and math functions. `format_units` can be used to convert a big number value into a human-readable string, which is useful for displaying values to users.
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use ethers::types::U256;
2+
3+
fn main() {
4+
// a == b
5+
let a = U256::from(100_u32);
6+
let b = U256::from(100_u32);
7+
assert!(a == b);
8+
9+
// a < b
10+
let a = U256::from(1_u32);
11+
let b = U256::from(100_u32);
12+
assert!(a < b);
13+
14+
// a <= b
15+
let a = U256::from(100_u32);
16+
let b = U256::from(100_u32);
17+
assert!(a <= b);
18+
19+
// a > b
20+
let a = U256::from(100_u32);
21+
let b = U256::from(1_u32);
22+
assert!(a > b);
23+
24+
// a >= b
25+
let a = U256::from(100_u32);
26+
let b = U256::from(100_u32);
27+
assert!(a >= b);
28+
29+
// a == 0
30+
let a = U256::zero();
31+
assert!(a.is_zero());
32+
}

0 commit comments

Comments
 (0)