Skip to content

Feat/code command #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
25 changes: 25 additions & 0 deletions cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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(())
}
Expand Down
23 changes: 23 additions & 0 deletions sdk/src/client/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
Loading