diff --git a/README.md b/README.md index f479e33..ca7ff41 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ Commands: call Make a call to a contract chain-id Get the network's chain id. deploy Deploy a contract + code Returns code at a given address hash Get either the keccak for a given input, the zero hash, the empty string, or a random hash [aliases: h, h] l2 L2 specific commands. nonce Get the account's nonce. [aliases: n] diff --git a/cli/src/cli.rs b/cli/src/cli.rs index ad163d7..48befe8 100644 --- a/cli/src/cli.rs +++ b/cli/src/cli.rs @@ -90,6 +90,20 @@ pub(crate) enum Command { #[arg(default_value = "http://localhost:8545", env = "RPC_URL")] rpc_url: String, }, + #[clap(about = "Returns code at a given address")] + Code { + address: Address, + #[arg( + short = 'B', + long = "block", + required = false, + default_value_t = String::from("latest"), + help = "defaultBlock parameter: can be integer block number, 'earliest', 'finalized', 'safe', 'latest' or 'pending'" + )] + block: String, + #[arg(default_value = "http://localhost:8545", env = "RPC_URL")] + rpc_url: String, + }, #[clap(about = "Deploy a contract")] Deploy { #[clap(flatten)] @@ -407,6 +421,17 @@ impl Command { println!("{chain_id}"); } } + Command::Code { + address, + block, + rpc_url, + } => { + let eth_client = EthClient::new(&rpc_url); + + let code = eth_client.get_code(address, block).await?; + + println!("{}", code); + } }; Ok(()) } diff --git a/sdk/src/client/eth/mod.rs b/sdk/src/client/eth/mod.rs index 8f7b50d..6a01123 100644 --- a/sdk/src/client/eth/mod.rs +++ b/sdk/src/client/eth/mod.rs @@ -1076,6 +1076,29 @@ impl EthClient { "Transaction receipt is None".to_owned(), )) } + + pub async fn get_code( + &self, + address: Address, + block: String, + ) -> Result<String, EthClientError> { + let request = RpcRequest { + id: RpcRequestId::Number(1), + jsonrpc: "2.0".to_string(), + method: "eth_getCode".to_string(), + params: Some(vec![json!(address), json!(block)]), + }; + + match self.send_request(request).await { + Ok(RpcResponse::Success(result)) => serde_json::from_value(result.result) + .map_err(SendRawTransactionError::SerdeJSONError) + .map_err(EthClientError::from), + Ok(RpcResponse::Error(error_response)) => { + Err(SendRawTransactionError::RPCError(error_response.error.message).into()) + } + Err(error) => Err(error), + } + } } pub fn from_hex_string_to_u256(hex_str: &str) -> Result<U256, EthClientError> {