Skip to content

Commit a118e3b

Browse files
committed
Handle redeclaration in estimate fee, closes #3253
commit-id:f3897b34
1 parent a649be8 commit a118e3b

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

crates/sncast/src/starknet_commands/declare.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ use sncast::helpers::rpc::RpcArgs;
99
use sncast::response::declare::{
1010
AlreadyDeclaredResponse, DeclareResponse, DeclareTransactionResponse,
1111
};
12-
use sncast::response::errors::StarknetCommandError;
12+
use sncast::response::errors::{SNCastProviderError, SNCastStarknetError, StarknetCommandError};
1313
use sncast::{ErrorData, WaitForTx, apply_optional_fields, handle_wait_for_tx};
1414
use starknet::accounts::AccountError::Provider;
1515
use starknet::accounts::{ConnectedAccount, DeclarationV3};
16-
use starknet::core::types::{DeclareTransactionResult, StarknetError};
16+
use starknet::core::types::{
17+
ContractExecutionError, DeclareTransactionResult, StarknetError, TransactionExecutionErrorData,
18+
};
1719
use starknet::providers::ProviderError;
1820
use starknet::{
1921
accounts::{Account, SingleOwnerAccount},
@@ -157,6 +159,22 @@ pub async fn declare_with_artifacts(
157159
class_hash: class_hash.into_(),
158160
}))
159161
}
162+
Err(Provider(ProviderError::StarknetError(StarknetError::TransactionExecutionError(
163+
TransactionExecutionErrorData {
164+
execution_error: ContractExecutionError::Message(message),
165+
..
166+
},
167+
)))) if message.contains("is already declared") => {
168+
if skip_on_already_declared {
169+
Ok(DeclareResponse::AlreadyDeclared(AlreadyDeclaredResponse {
170+
class_hash: class_hash.into_(),
171+
}))
172+
} else {
173+
Err(StarknetCommandError::ProviderError(
174+
SNCastProviderError::StarknetError(SNCastStarknetError::ClassAlreadyDeclared),
175+
))
176+
}
177+
}
160178
Err(Provider(error)) => Err(StarknetCommandError::ProviderError(error.into())),
161179
Err(error) => Err(anyhow!(format!("Unexpected error occurred: {error}")).into()),
162180
}

crates/sncast/tests/e2e/declare.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,45 @@ async fn test_contract_already_declared() {
315315
);
316316
}
317317

318+
#[tokio::test]
319+
async fn test_contract_already_declared_estimate_fee() {
320+
let contract_path = duplicate_contract_directory_with_salt(
321+
CONTRACTS_DIR.to_string() + "/map",
322+
"put",
323+
"74362345",
324+
);
325+
let tempdir = create_and_deploy_oz_account().await;
326+
join_tempdirs(&contract_path, &tempdir);
327+
328+
let args = vec![
329+
"--accounts-file",
330+
"accounts.json",
331+
"--account",
332+
"my_account",
333+
"declare",
334+
"--url",
335+
URL,
336+
"--contract-name",
337+
"Map",
338+
];
339+
// Commented out because we explicitly do not want to set any resource bounds.
340+
// Transaction has to go through estimate fee endpoint first because it throws different errors
341+
// than the normal declare endpoint and we want to test them.
342+
343+
runner(&args).current_dir(tempdir.path()).assert().success();
344+
345+
let snapbox = runner(&args).current_dir(tempdir.path());
346+
let output = snapbox.assert().success();
347+
348+
assert_stderr_contains(
349+
output,
350+
indoc! {r"
351+
Command: declare
352+
Error: Contract with the same class hash is already declared
353+
"},
354+
);
355+
}
356+
318357
#[tokio::test]
319358
async fn test_invalid_nonce() {
320359
let contract_path =

0 commit comments

Comments
 (0)