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

refactor: examples #1940

Merged
merged 50 commits into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from 45 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
247265f
ToC
Dec 15, 2022
535f865
Big numbers section
Dec 16, 2022
a0edb1e
Middleware examples: builder
Dec 16, 2022
47f8d86
Middleware examples: gas_escalator
Dec 16, 2022
dcb1eea
Middleware examples: gas_oracle
Dec 16, 2022
9641955
Middleware examples: signer
Dec 16, 2022
692e277
Middleware examples: missing stubs
Dec 16, 2022
e163986
review: applied DaniPopes suggestions to big numbers
Dec 17, 2022
afdcb1d
typo
Dec 17, 2022
9d6935f
Middleware examples: nonce_manager
Dec 17, 2022
7139097
cargo +nightly fmt
Dec 17, 2022
b8bbc0b
update roadmap
Dec 17, 2022
e64c80c
Middleware examples: policy
Dec 17, 2022
e56386e
Middleware examples: added docs
Dec 17, 2022
d1be75c
Contracts examples: created folder; included abigen example
Dec 18, 2022
a9f63c0
Contracts examples: refactor abigen docs. Fixed cargo example reference
Dec 18, 2022
0fd100f
Contracts examples: contract_events; minor docs changes
Dec 18, 2022
4092ca5
Moved each example under its own crate.
Dec 21, 2022
19125ba
Big numbers examples: used regular operators for math
Dec 21, 2022
7fdd23a
Single examples run correctly (missing overall CI execution)
Dec 21, 2022
a4ce6e4
review: Applied gakonst note to remove commented items in workspace m…
Dec 21, 2022
6e0bfe6
review: Applied gakonst note to restore visibility on contract constr…
Dec 21, 2022
2a2ba3e
ci:
Dec 21, 2022
f4ebd91
cargo +nightly fmt
Dec 21, 2022
aa0cc4b
Merge branch 'master' into docs-examples-refactoring
Dec 21, 2022
4a11cce
ci: fix WASM step error
Dec 21, 2022
629b5c0
Removed deprecated EthGasStation example
Dec 21, 2022
9380c9e
WASM example uses local copy of `contract_abi.json`. In this way we k…
Dec 21, 2022
7dfac21
Cargo.lock aligned to master branch
Dec 21, 2022
3c3bae1
Removed useless comments in examples
Dec 21, 2022
fdf2dcc
review: Applied gakonst note to add panic!() on the policy middleware…
Dec 21, 2022
73b521d
review: Applied gakonst suggestion to add a custom middleware example
Dec 26, 2022
7ecbe08
typos in docs
Dec 26, 2022
8b583b5
Update examples/big-numbers/examples/bn_math_operations.rs
0xMelkor Dec 26, 2022
f89dbbd
review: Applied DaniPopes suggestion on assert_eq!
Dec 26, 2022
36e4c4b
Update examples/big-numbers/README.md
0xMelkor Dec 26, 2022
a9a8cbe
review: All imports now reference the "ethers" crate
Dec 26, 2022
a162e98
Merge branch 'docs-examples-refactoring' of https://github.com/0xMelk…
Dec 26, 2022
df51c10
ci: added features ["ws", "rustls"] where needed
Dec 27, 2022
020b150
Examples with special features (e.g. ipc, trezor etc.) are built alon…
Dec 27, 2022
79e4643
cargo +nightly fmt
Dec 27, 2022
d1f9db8
Examples: Gas oracle API keys from env
Dec 27, 2022
deb7b96
typo: use expect() instead of unwrap()
Dec 27, 2022
d2c4e77
Updated ToC
Dec 27, 2022
20d304f
Gas oracle examples raise panic on middleware errors
Dec 27, 2022
f449f8f
review: removed useless [[example]] in Cargo.toml
Dec 27, 2022
c698efc
review: removed #[allow(unused_must_use)] from gas_escalator example
Dec 27, 2022
2975c97
review: Removed prefixes from file names
Dec 27, 2022
3df7010
review: removed useless [[example]] in Cargo.toml
Dec 27, 2022
25fb72d
docs: Updated description to run examples in the workspace README.md
Dec 27, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 1 addition & 26 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -232,32 +232,7 @@ jobs:
- uses: Swatinem/rust-cache@v1
with:
cache-on-failure: true
- name: Build all examples
run: |
export PATH=$HOME/bin:$PATH
examples=$(cargo metadata --format-version 1 | \
jq -c '.packages[]
| select(.name == "ethers")
| .targets[]
| select(.kind[] | contains("example"))
| with_entries(select([.key]
| inside(["name", "required-features"])))'
)
for example in $examples; do
name="$(echo "$example" | jq -r '.name')"
args=(
-p ethers
--example "$name"
)
features="$(echo "$example" | jq -r 'try(."required-features" | join(","))')"
if [[ ! -z "$features" ]]; then
args+=(--features "$features")
fi

echo "building $name"
cargo build "${args[@]}"
done
- name: Run all examples
- name: Build and run all examples
run: |
export PATH=$HOME/bin:$PATH
chmod +x ./scripts/examples.sh
Expand Down
105 changes: 105 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 2 additions & 60 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ members = [
"ethers-middleware",
"ethers-etherscan",
"ethers-solc",
"examples/ethers-wasm",
# Example crates
"examples/*",
]

default-members = [
Expand Down Expand Up @@ -127,62 +128,3 @@ bytes = "1.3.0"
# Tell `rustc` to optimize for small code size.
opt-level = "s"

[[example]]
name = "abigen"
path = "examples/abigen.rs"
required-features = ["ethers-solc"]

[[example]]
name = "contract_human_readable"
path = "examples/contract_human_readable.rs"
required-features = ["ethers-solc"]

[[example]]
name = "contract_with_abi"
path = "examples/contract_with_abi.rs"
required-features = ["ethers-solc"]

[[example]]
name = "contract_with_abi_and_bytecode"
path = "examples/contract_with_abi_and_bytecode.rs"
required-features = ["ethers-solc"]

[[example]]
name = "ipc"
path = "examples/ipc.rs"
required-features = ["ipc"]

[[example]]
name = "ledger"
path = "examples/ledger.rs"
required-features = ["ledger"]

[[example]]
name = "moonbeam_with_abi"
path = "examples/moonbeam_with_abi.rs"
required-features = ["legacy", "ethers-solc"]

[[example]]
name = "trezor"
path = "examples/trezor.rs"
required-features = ["trezor"]

[[example]]
name = "yubi"
path = "examples/yubi.rs"
required-features = ["yubi"]

[[example]]
name = "paginated_logs"
path = "examples/paginated_logs.rs"
required-features = ["rustls"]

[[example]]
name = "subscribe_contract_events"
path = "examples/subscribe_contract_events.rs"
required-features = ["rustls", "ws"]

[[example]]
name = "subscribe_contract_events_with_meta"
path = "examples/subscribe_contract_events_with_meta.rs"
required-features = ["rustls", "ws"]
81 changes: 81 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Examples
- [ ] Address book
- [ ] Anvil
- [ ] Boot anvil
- [ ] Deploy contracts
- [x] Fork
- [ ] Testing
- [x] Big numbers
- [x] Comparison and equivalence
- [x] Conversion
- [x] Creating Instances
- [x] Math operations
- [x] Utilities
- [ ] Contracts
- [x] Abigen
- [x] Compile
- [ ] Creating Instances
- [x] Deploy Anvil
- [x] Deploy from ABI and bytecode
- [x] Deploy Moonbeam
- [x] Events
- [x] Events with meta
- [ ] Methods
- [ ] Events
- [ ] Logs and filtering
- [ ] Solidity topics
- [ ] Middleware
- [x] Builder
- [x] Create custom middleware
- [x] Gas escalator
- [x] Gas oracle
- [x] Nonce manager
- [x] Policy
- [x] Signer
- [ ] Time lag
- [ ] Transformer
- [ ] Providers
- [ ] Http
- [x] IPC
- [ ] Mock
- [x] Quorum
- [ ] Retry
- [x] RW
- [ ] WS
- [ ] Queries
- [ ] Blocks
- [x] Contracts
- [ ] Events
- [x] Paginated logs
- [x] UniswapV2 pair
- [ ] Transactions
- [x] Subscriptions
- [x] Watch blocks
- [x] Subscribe events by type
- [x] Subscribe logs
- [ ] Transactions
- [x] Call override
- [ ] Create raw transaction
- [ ] Create typed transaction
- [x] Decode input
- [ ] EIP-1559
- [x] ENS
- [ ] Estimate gas
- [ ] Get gas price
- [x] Get gas price USD
- [x] Remove liquidity
- [ ] Set gas for a transaction
- [ ] Send raw transaction
- [ ] Send typed transaction
- [x] Trace
- [ ] Transaction receipt
- [ ] Transaction status
- [x] Transfer ETH
- [x] Wallets
- [x] Mnemonic
- [x] Ledger
- [x] Local
- [x] Permit hash
- [x] Sign message
- [x] Trezor
- [x] Yubi
10 changes: 10 additions & 0 deletions examples/anvil/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "examples-anvil"
version = "1.0.2"
authors = ["Andrea Simeoni <[email protected]>"]
edition = "2021"

[dev-dependencies]
ethers = { path = "../..", version = "1.0.0" }
eyre = "0.6"
tokio = { version = "1.18", features = ["full"] }
Empty file added examples/anvil/README.md
Empty file.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! Spawn an [anvil](https://github.com/foundry-rs/foundry/tree/master/anvil) instance in forking mode

use ethers::utils::Anvil;
use eyre::Result;

#[tokio::main]
async fn main() -> eyre::Result<()> {
async fn main() -> Result<()> {
// ensure `anvil` is available in $PATH
let anvil =
Anvil::new().fork("https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27").spawn();
Expand Down
10 changes: 10 additions & 0 deletions examples/big-numbers/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "examples-big-numbers"
version = "1.0.2"
authors = ["Andrea Simeoni <[email protected]>"]
edition = "2021"

[dev-dependencies]
#ethers-core = { version = "^1.0.0", path = "../../ethers-core" }
ethers = { path = "../..", version = "1.0.0" }

7 changes: 7 additions & 0 deletions examples/big-numbers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Big numbers
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).

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.

## Utilities
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 changes: 32 additions & 0 deletions examples/big-numbers/examples/bn_comparison_equivalence.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use ethers::types::U256;

fn main() {
// a == b
let a = U256::from(100_u32);
let b = U256::from(100_u32);
assert!(a == b);

// a < b
let a = U256::from(1_u32);
let b = U256::from(100_u32);
assert!(a < b);

// a <= b
let a = U256::from(100_u32);
let b = U256::from(100_u32);
assert!(a <= b);

// a > b
let a = U256::from(100_u32);
let b = U256::from(1_u32);
assert!(a > b);

// a >= b
let a = U256::from(100_u32);
let b = U256::from(100_u32);
assert!(a >= b);

// a == 0
let a = U256::zero();
assert!(a.is_zero());
}
23 changes: 23 additions & 0 deletions examples/big-numbers/examples/bn_conversion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use ethers::{types::U256, utils::format_units};

fn main() {
let num = U256::from(42_u8);

let a: u128 = num.as_u128();
assert_eq!(a, 42);

let b: u64 = num.as_u64();
assert_eq!(b, 42);

let c: u32 = num.as_u32();
assert_eq!(c, 42);

let d: usize = num.as_usize();
assert_eq!(d, 42);

let e: String = num.to_string();
assert_eq!(e, "42");

let f: String = format_units(num, 4).unwrap();
assert_eq!(f, "0.0042");
}
Loading