Skip to content

Commit a9d4bd2

Browse files
authored
feat: add nonce to cast (#266)
* fix: do not track .DS_STORE * feat: add nonce to cast * chore: lint * docs: add code doctest * chore: Makefile commands for local development
1 parent 65dac0f commit a9d4bd2

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.DS_STORE
12
/target
23
out/
34
out.json

Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.PHONY: format lint test ready
2+
13
# Repositories for integration tests that will be cloned inside `integration-tests/testdata/REPO` folders
24
INTEGRATION_TESTS_REPOS = \
35
mds1/drai \
@@ -17,3 +19,16 @@ $(INTEGRATION_TESTS_REPOS):
1719
else cd $$FOLDER; git pull --recurse-submodules; fi
1820

1921
testdata: integration-tests-testdata
22+
23+
format:
24+
cargo +nightly fmt
25+
26+
lint:
27+
cargo +nightly clippy --all-features -- -D warnings
28+
29+
test:
30+
cargo check
31+
cargo test
32+
cargo doc --open
33+
34+
ready: format lint test

cast/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,29 @@ where
290290
Ok(self.provider.get_gas_price().await?)
291291
}
292292

293+
/// ```no_run
294+
/// use cast::Cast;
295+
/// use ethers_providers::{Provider, Http};
296+
/// use ethers_core::types::Address;
297+
/// use std::{str::FromStr, convert::TryFrom};
298+
///
299+
/// # async fn foo() -> eyre::Result<()> {
300+
/// let provider = Provider::<Http>::try_from("http://localhost:8545")?;
301+
/// let cast = Cast::new(provider);
302+
/// let addr = Address::from_str("0x7eD52863829AB99354F3a0503A622e82AcD5F7d3")?;
303+
/// let nonce = cast.nonce(addr, None).await?;
304+
/// println!("{}", nonce);
305+
/// # Ok(())
306+
/// # }
307+
/// ```
308+
pub async fn nonce<T: Into<NameOrAddress> + Send + Sync>(
309+
&self,
310+
who: T,
311+
block: Option<BlockId>,
312+
) -> Result<U256> {
313+
Ok(self.provider.get_transaction_count(who, block).await?)
314+
}
315+
293316
/// ```no_run
294317
/// use cast::Cast;
295318
/// use ethers_providers::{Provider, Http};

cli/src/cast.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ async fn main() -> eyre::Result<()> {
217217
let value = provider.get_storage_at(address, slot, block).await?;
218218
println!("{:?}", value);
219219
}
220+
Subcommands::Nonce { block, who, rpc_url } => {
221+
let provider = Provider::try_from(rpc_url)?;
222+
println!("{}", Cast::new(provider).nonce(who, block).await?);
223+
}
220224
};
221225

222226
Ok(())

cli/src/opts/cast.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,16 @@ pub enum Subcommands {
223223
)]
224224
block: Option<BlockId>,
225225
},
226+
#[structopt(name = "nonce")]
227+
#[structopt(about = "Prints the number of transactions sent from <address>")]
228+
Nonce {
229+
#[structopt(long, short = "-B", help = "the block you want to query, can also be earliest/latest/pending", parse(try_from_str = parse_block_id))]
230+
block: Option<BlockId>,
231+
#[structopt(help = "the address you want to query", parse(try_from_str = parse_name_or_address))]
232+
who: NameOrAddress,
233+
#[structopt(short, long, env = "ETH_RPC_URL")]
234+
rpc_url: String,
235+
},
226236
}
227237

228238
fn parse_name_or_address(s: &str) -> eyre::Result<NameOrAddress> {

0 commit comments

Comments
 (0)