Skip to content

Commit b865c4b

Browse files
committed
Handle redeclaration in estimate fee, closes #3253
commit-id:f3897b34
1 parent 77bb49d commit b865c4b

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

crates/sncast/src/starknet_commands/declare.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use sncast::response::errors::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,16 @@ 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") && skip_on_already_declared => {
168+
Ok(DeclareResponse::AlreadyDeclared(AlreadyDeclaredResponse {
169+
class_hash: class_hash.into_(),
170+
}))
171+
}
160172
Err(Provider(error)) => Err(StarknetCommandError::ProviderError(error.into())),
161173
Err(error) => Err(anyhow!(format!("Unexpected error occurred: {error}")).into()),
162174
}

crates/sncast/tests/e2e/declare.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ async fn test_contract_already_declared() {
293293
accounts_json_path.as_str(),
294294
"--account",
295295
"user1",
296+
"--wait",
296297
"declare",
297298
"--url",
298299
URL,
@@ -315,6 +316,46 @@ async fn test_contract_already_declared() {
315316
);
316317
}
317318

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

0 commit comments

Comments
 (0)