Skip to content

Commit fd73726

Browse files
mechanix97ilitteri
andauthored
Feat/code command (#99)
* Replace local `auth` module with `ethrex_rpc::clients::auth` module & fix compilation * Add command mock * Add get code code * Add block option * Update readme --------- Co-authored-by: ilitteri <ilitteri@fi.uba.ar> Co-authored-by: Ivan Litteri <67517699+ilitteri@users.noreply.github.com>
1 parent 33ef44c commit fd73726

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Commands:
4343
call Make a call to a contract
4444
chain-id Get the network's chain id.
4545
deploy Deploy a contract
46+
code Returns code at a given address
4647
hash Get either the keccak for a given input, the zero hash, the empty string, or a random hash [aliases: h, h]
4748
l2 L2 specific commands.
4849
nonce Get the account's nonce. [aliases: n]

cli/src/cli.rs

+25
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,20 @@ pub(crate) enum Command {
9090
#[arg(default_value = "http://localhost:8545", env = "RPC_URL")]
9191
rpc_url: String,
9292
},
93+
#[clap(about = "Returns code at a given address")]
94+
Code {
95+
address: Address,
96+
#[arg(
97+
short = 'B',
98+
long = "block",
99+
required = false,
100+
default_value_t = String::from("latest"),
101+
help = "defaultBlock parameter: can be integer block number, 'earliest', 'finalized', 'safe', 'latest' or 'pending'"
102+
)]
103+
block: String,
104+
#[arg(default_value = "http://localhost:8545", env = "RPC_URL")]
105+
rpc_url: String,
106+
},
93107
#[clap(about = "Deploy a contract")]
94108
Deploy {
95109
#[clap(flatten)]
@@ -407,6 +421,17 @@ impl Command {
407421
println!("{chain_id}");
408422
}
409423
}
424+
Command::Code {
425+
address,
426+
block,
427+
rpc_url,
428+
} => {
429+
let eth_client = EthClient::new(&rpc_url);
430+
431+
let code = eth_client.get_code(address, block).await?;
432+
433+
println!("{}", code);
434+
}
410435
};
411436
Ok(())
412437
}

sdk/src/client/eth/mod.rs

+23
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,29 @@ impl EthClient {
10761076
"Transaction receipt is None".to_owned(),
10771077
))
10781078
}
1079+
1080+
pub async fn get_code(
1081+
&self,
1082+
address: Address,
1083+
block: String,
1084+
) -> Result<String, EthClientError> {
1085+
let request = RpcRequest {
1086+
id: RpcRequestId::Number(1),
1087+
jsonrpc: "2.0".to_string(),
1088+
method: "eth_getCode".to_string(),
1089+
params: Some(vec![json!(address), json!(block)]),
1090+
};
1091+
1092+
match self.send_request(request).await {
1093+
Ok(RpcResponse::Success(result)) => serde_json::from_value(result.result)
1094+
.map_err(SendRawTransactionError::SerdeJSONError)
1095+
.map_err(EthClientError::from),
1096+
Ok(RpcResponse::Error(error_response)) => {
1097+
Err(SendRawTransactionError::RPCError(error_response.error.message).into())
1098+
}
1099+
Err(error) => Err(error),
1100+
}
1101+
}
10791102
}
10801103

10811104
pub fn from_hex_string_to_u256(hex_str: &str) -> Result<U256, EthClientError> {

0 commit comments

Comments
 (0)