|
| 1 | +use anyhow::Result; |
| 2 | +use clap::Args; |
| 3 | +use sncast::get_block_id; |
| 4 | +use sncast::helpers::command::process_command_result; |
| 5 | +use sncast::helpers::configuration::CastConfig; |
| 6 | +use sncast::helpers::rpc::RpcArgs; |
| 7 | +use sncast::response::errors::{SNCastProviderError, StarknetCommandError}; |
| 8 | +use sncast::response::nonce::NonceResponse; |
| 9 | +use sncast::response::ui::UI; |
| 10 | +use starknet_rust::providers::jsonrpc::HttpTransport; |
| 11 | +use starknet_rust::providers::{JsonRpcClient, Provider}; |
| 12 | +use starknet_types_core::felt::Felt; |
| 13 | + |
| 14 | +#[derive(Debug, Args)] |
| 15 | +#[command(about = "Get the nonce of a contract")] |
| 16 | +pub struct Nonce { |
| 17 | + /// Address of the contract |
| 18 | + pub contract_address: Felt, |
| 19 | + |
| 20 | + /// Block identifier on which nonce should be fetched. |
| 21 | + /// Possible values: `pre_confirmed`, `latest`, block hash (0x prefixed string) |
| 22 | + /// and block number (u64) |
| 23 | + #[arg(short, long, default_value = "pre_confirmed")] |
| 24 | + pub block_id: String, |
| 25 | + |
| 26 | + #[command(flatten)] |
| 27 | + pub rpc: RpcArgs, |
| 28 | +} |
| 29 | + |
| 30 | +pub async fn nonce(nonce: Nonce, config: CastConfig, ui: &UI) -> Result<()> { |
| 31 | + let provider = nonce.rpc.get_provider(&config, ui).await?; |
| 32 | + |
| 33 | + let result = get_nonce(&provider, nonce.contract_address, &nonce.block_id).await; |
| 34 | + |
| 35 | + process_command_result("get nonce", result, ui, None); |
| 36 | + Ok(()) |
| 37 | +} |
| 38 | + |
| 39 | +pub async fn get_nonce( |
| 40 | + provider: &JsonRpcClient<HttpTransport>, |
| 41 | + contract_address: Felt, |
| 42 | + block_id: &str, |
| 43 | +) -> Result<NonceResponse> { |
| 44 | + let block_id = get_block_id(block_id)?; |
| 45 | + let nonce = provider |
| 46 | + .get_nonce(block_id, contract_address) |
| 47 | + .await |
| 48 | + .map_err(|err| StarknetCommandError::ProviderError(SNCastProviderError::from(err)))?; |
| 49 | + Ok(NonceResponse { nonce }) |
| 50 | +} |
0 commit comments