Skip to content

Commit

Permalink
feat: auto check if class has been declared
Browse files Browse the repository at this point in the history
`starkli` no longer attempts a class declaration when it's already
declared. It exits early with exit code `0` as it's not treated as an
error. Note that if an attempt is made and still fails due to a race
condition, `starkli` would consider it a failure and still exits with
error like it did before.
  • Loading branch information
xJonathanLEI committed Jul 8, 2023
1 parent 56b7838 commit f426ace
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/subcommands/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use starknet::{
accounts::{Account, SingleOwnerAccount},
core::types::{
contract::{legacy::LegacyContractClass, CompiledClass, SierraClass},
BlockId, BlockTag, FieldElement,
BlockId, BlockTag, FieldElement, StarknetError,
},
providers::Provider,
providers::{Provider, ProviderError},
};

use crate::{
Expand Down Expand Up @@ -68,8 +68,6 @@ impl Declare {
SingleOwnerAccount::new(provider.clone(), signer, account_address, chain_id);
account.set_block_id(BlockId::Tag(BlockTag::Pending));

// TODO: check if class has already been declared

// Working around a deserialization bug in `starknet-rs`:
// https://github.com/xJonathanLEI/starknet-rs/issues/392

Expand All @@ -80,6 +78,11 @@ impl Declare {
// Declaring Cairo 1 class
let class_hash = class.class_hash()?;

// TODO: add option to skip checking
if Self::check_already_declared(&provider, class_hash).await? {
return Ok(());
}

let casm_source = self.casm.into_casm_hash_source(&provider).await?;

if !self.estimate_only {
Expand Down Expand Up @@ -144,6 +147,11 @@ impl Declare {
// Declaring Cairo 0 class
let class_hash = class.class_hash()?;

// TODO: add option to skip checking
if Self::check_already_declared(&provider, class_hash).await? {
return Ok(());
}

if !self.estimate_only {
eprintln!(
"Declaring Cairo 0 (deprecated) class: {}",
Expand Down Expand Up @@ -195,4 +203,24 @@ impl Declare {

Ok(())
}

async fn check_already_declared<P>(provider: P, class_hash: FieldElement) -> Result<bool>
where
P: Provider,
P::Error: 'static,
{
match provider
.get_class(BlockId::Tag(BlockTag::Pending), class_hash)
.await
{
Ok(_) => {
eprintln!("Not declaring class as it's already declared. Class hash:");
println!("{}", format!("{:#064x}", class_hash).bright_yellow());

Ok(true)
}
Err(ProviderError::StarknetError(StarknetError::ClassHashNotFound)) => Ok(false),
Err(err) => Err(err.into()),
}
}
}

0 comments on commit f426ace

Please sign in to comment.