Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
fix(rpc/eth_getBalance): class hash not found (#1265)
Browse files Browse the repository at this point in the history
fix the class hash not found error
  • Loading branch information
greged93 authored Jul 4, 2024
1 parent f997943 commit 0e759fd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/eth_provider/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use super::starknet::kakarot_core::{
starknet_address, to_starknet_transaction, KAKAROT_ADDRESS,
};
use super::starknet::{ERC20Reader, STARKNET_NATIVE_TOKEN};
use super::utils::{contract_not_found, entrypoint_not_found, split_u256};
use super::utils::{class_hash_not_declared, contract_not_found, entrypoint_not_found, split_u256};
use crate::models::block::{EthBlockId, EthBlockNumberOrTag};
use crate::models::felt::Felt252Wrapper;
use crate::models::transaction::validate_transaction;
Expand Down Expand Up @@ -295,10 +295,11 @@ where
// Call the `balanceOf` method on the contract for the given address and block ID, awaiting the result
let res = eth_contract.balanceOf(&starknet_address(address)).block_id(starknet_block_id).call().await;

// Check if the contract was not found, returning a default balance of 0 if true
// Check if the contract was not found or the class hash not declared,
// returning a default balance of 0 if true.
// The native token contract should be deployed on Kakarot, so this should not happen
// We want to avoid errors in this case and return a default balance of 0
if contract_not_found(&res) {
if contract_not_found(&res) || class_hash_not_declared(&res) {
return Ok(Default::default());
}
// Otherwise, extract the balance from the result, converting any errors to ExecutionError
Expand Down
27 changes: 27 additions & 0 deletions src/eth_provider/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ pub(crate) fn contract_not_found<T>(err: &Result<T, Error>) -> bool {
}
}

#[inline]
pub(crate) fn class_hash_not_declared<T>(err: &Result<T, Error>) -> bool {
match err {
Ok(_) => false,
Err(err) => {
matches!(
err,
Error::Provider(ProviderError::StarknetError(StarknetError::ContractError(ContractErrorData {
revert_error
}))) if revert_error.contains("is not declared.")
)
}
}
}

/// Checks if the error is an entrypoint not found error.
#[inline]
pub(crate) fn entrypoint_not_found<T>(err: &Result<T, Error>) -> bool {
Expand Down Expand Up @@ -68,4 +83,16 @@ mod tests {
assert_eq!(U256::from_str(&combined_hex).unwrap(), value);
});
}

#[test]
fn test_class_hash_not_declared() {
let err = Error::Provider(ProviderError::StarknetError(StarknetError::ContractError(ContractErrorData {
revert_error: "\"Error in the called contract (0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7):
\\nError at pc=0:104:\\nGot an exception while executing a hint: Class with ClassHash(\\n StarkFelt(\\n
\\\"0x0000000000000000000000000000000000000000000000000000000000000000\\\",\\n ),\\n) is not declared.\\nCairo traceback (most recent call last):\\n
Unknown location (pc=0:1678)\\nUnknown location (pc=0:1664)\\n\"".to_string(),
})));

assert!(class_hash_not_declared::<()>(&Err(err)));
}
}

0 comments on commit 0e759fd

Please sign in to comment.