add json rpc bindings for eth_getCode (#15)

This commit is contained in:
Rohit Narurkar 2020-06-16 01:40:27 +05:30 committed by GitHub
parent 636762d464
commit 5d92e72882
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View File

@ -409,7 +409,27 @@ impl<P: JsonRpcClient> Provider<P> {
.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<NameOrAddress>,
block: Option<BlockNumber>,
) -> Result<Bytes, ProviderError> {
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

View File

@ -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(())
/// # }
/// ```