From 5d92e728823773158c7e532eba8da18a6a779735 Mon Sep 17 00:00:00 2001 From: Rohit Narurkar Date: Tue, 16 Jun 2020 01:40:27 +0530 Subject: [PATCH] add json rpc bindings for eth_getCode (#15) --- ethers-providers/src/provider.rs | 22 +++++++++++++++++++++- ethers/src/lib.rs | 3 +++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/ethers-providers/src/provider.rs b/ethers-providers/src/provider.rs index 8c6fe0f2..9b471bb8 100644 --- a/ethers-providers/src/provider.rs +++ b/ethers-providers/src/provider.rs @@ -409,7 +409,27 @@ impl Provider

{ .map_err(Into::into)?) } - // TODO: get_code, get_storage_at + // TODO: get_storage_at + + /// Returns the deployed code at a given address + pub async fn get_code( + &self, + at: impl Into, + block: Option, + ) -> Result { + let at = match at.into() { + NameOrAddress::Name(ens_name) => self.resolve_name(&ens_name).await?, + NameOrAddress::Address(addr) => addr, + }; + + let at = utils::serialize(&at); + let block = utils::serialize(&block.unwrap_or(BlockNumber::Latest)); + Ok(self + .0 + .request("eth_getCode", [at, block]) + .await + .map_err(Into::into)?) + } ////// Ethereum Naming Service // The Ethereum Naming Service (ENS) allows easy to remember and use names to diff --git a/ethers/src/lib.rs b/ethers/src/lib.rs index 096a656e..f6a0b8cc 100644 --- a/ethers/src/lib.rs +++ b/ethers/src/lib.rs @@ -132,6 +132,9 @@ pub mod contract { /// /// let block = provider.get_block(100u64).await?; /// println!("Got block: {}", serde_json::to_string(&block)?); +/// +/// let code = provider.get_code("0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359", None).await?; +/// println!("Got code: {}", serde_json::to_string(&code)?); /// # Ok(()) /// # } /// ```