From cb73179d3806b13250cab443ccf5a6ec186ed7ec Mon Sep 17 00:00:00 2001 From: Evolve Date: Tue, 1 Aug 2023 20:16:12 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20errors=20primitives=20crate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/client/rpc/src/errors.rs | 2 +- crates/client/rpc/src/events/mod.rs | 2 +- crates/client/rpc/src/lib.rs | 2 +- .../client/rpc/src/madara_backend_client.rs | 2 +- crates/pallets/starknet/src/runtime_api.rs | 2 +- crates/primitives/starknet/src/errors/mod.rs | 44 +++++++++++++++++++ crates/primitives/starknet/src/lib.rs | 3 ++ .../starknet/src/starknet_serde/mod.rs | 43 ------------------ crates/runtime/src/lib.rs | 12 ++++- 9 files changed, 63 insertions(+), 49 deletions(-) create mode 100644 crates/primitives/starknet/src/errors/mod.rs diff --git a/crates/client/rpc/src/errors.rs b/crates/client/rpc/src/errors.rs index 290f7c256e..90521d66df 100644 --- a/crates/client/rpc/src/errors.rs +++ b/crates/client/rpc/src/errors.rs @@ -1,5 +1,5 @@ use jsonrpsee::types::error::{CallError, ErrorObject}; -use mp_starknet::starknet_serde::StarknetRpcApiError; +use mp_starknet::errors::StarknetRpcApiError; pub struct TxError(pub StarknetRpcApiError); diff --git a/crates/client/rpc/src/events/mod.rs b/crates/client/rpc/src/events/mod.rs index 1ebc5b0a8e..2f611a2f19 100644 --- a/crates/client/rpc/src/events/mod.rs +++ b/crates/client/rpc/src/events/mod.rs @@ -10,8 +10,8 @@ use log::error; use mc_rpc_core::utils::get_block_by_block_hash; use mc_transaction_pool::ChainApi; use mp_starknet::block::Block; +use mp_starknet::errors::StarknetRpcApiError; use mp_starknet::execution::types::Felt252Wrapper; -use mp_starknet::starknet_serde::StarknetRpcApiError; use mp_starknet::traits::hash::HasherT; use mp_starknet::traits::ThreadSafeCopy; use mp_starknet::transaction::types::{EventWrapper, TransactionReceiptWrapper}; diff --git a/crates/client/rpc/src/lib.rs b/crates/client/rpc/src/lib.rs index 0a5c33f981..bd362bf828 100644 --- a/crates/client/rpc/src/lib.rs +++ b/crates/client/rpc/src/lib.rs @@ -20,8 +20,8 @@ pub use mc_rpc_core::StarknetRpcApiServer; use mc_storage::OverrideHandle; use mc_transaction_pool::{ChainApi, Pool}; use mp_starknet::crypto::merkle_patricia_tree::merkle_tree::ProofNode; +use mp_starknet::errors::StarknetRpcApiError; use mp_starknet::execution::types::Felt252Wrapper; -use mp_starknet::starknet_serde::StarknetRpcApiError; use mp_starknet::traits::hash::HasherT; use mp_starknet::traits::ThreadSafeCopy; use mp_starknet::transaction::types::{ diff --git a/crates/client/rpc/src/madara_backend_client.rs b/crates/client/rpc/src/madara_backend_client.rs index 22333c0aa2..e6e812c81e 100644 --- a/crates/client/rpc/src/madara_backend_client.rs +++ b/crates/client/rpc/src/madara_backend_client.rs @@ -1,6 +1,6 @@ use mc_rpc_core::utils::get_block_by_block_hash; use mp_starknet::block::Block; -use mp_starknet::starknet_serde::StarknetRpcApiError; +use mp_starknet::errors::StarknetRpcApiError; use sc_client_api::backend::{Backend, StorageProvider}; use sp_api::BlockId; use sp_blockchain::HeaderBackend; diff --git a/crates/pallets/starknet/src/runtime_api.rs b/crates/pallets/starknet/src/runtime_api.rs index ae87b88038..e550d831d7 100644 --- a/crates/pallets/starknet/src/runtime_api.rs +++ b/crates/pallets/starknet/src/runtime_api.rs @@ -7,8 +7,8 @@ use blockifier::execution::contract_class::ContractClass; use mp_starknet::crypto::hash::Hasher; +use mp_starknet::errors::StarknetRpcApiError; use mp_starknet::execution::types::{ClassHashWrapper, ContractAddressWrapper, Felt252Wrapper, StorageKeyWrapper}; -use mp_starknet::starknet_serde::StarknetRpcApiError; use mp_starknet::transaction::types::{EventWrapper, Transaction, TxType}; use sp_api::BlockT; pub extern crate alloc; diff --git a/crates/primitives/starknet/src/errors/mod.rs b/crates/primitives/starknet/src/errors/mod.rs new file mode 100644 index 0000000000..3429c8e6ba --- /dev/null +++ b/crates/primitives/starknet/src/errors/mod.rs @@ -0,0 +1,44 @@ +use thiserror_no_std::Error; + +#[allow(missing_docs)] +#[derive(Error, Clone, Copy, Debug, scale_codec::Encode, scale_codec::Decode, scale_info::TypeInfo)] +pub enum StarknetRpcApiError { + #[error("Failed to write transaction")] + FailedToReceiveTxn = 1, + #[error("Contract not found")] + ContractNotFound = 20, + #[error("Invalid message selector")] + InvalidMessageSelector = 21, + #[error("Invalid call data")] + InvalidCallData = 22, + #[error("Block not found")] + BlockNotFound = 24, + #[error("Transaction hash not found")] + TxnHashNotFound = 25, + #[error("Invalid transaction index in a block")] + InvalidTxnIndex = 27, + #[error("Class hash not found")] + ClassHashNotFound = 28, + #[error("Class already declared")] + ClassAlreadyDeclared = 51, + #[error("Requested page size is too big")] + PageSizeTooBig = 31, + #[error("There are no blocks")] + NoBlocks = 32, + #[error("The supplied continuation token is invalid or unknown")] + InvalidContinuationToken = 33, + #[error("Contract error")] + ContractError = 40, + #[error("Invalid contract class")] + InvalidContractClass = 50, + #[error("Too many storage keys requested")] + ProofLimitExceeded = 10000_isize, + #[error("Too many keys provided in a filter")] + TooManyKeysInFilter = 34, + #[error("Internal server error")] + InternalServerError = 500_isize, + #[error("Failed to fetch pending transactions")] + FailedToFetchPendingTransactions = 38, + #[error("Unimplemented method")] + UnimplementedMethod = 501_isize, +} diff --git a/crates/primitives/starknet/src/lib.rs b/crates/primitives/starknet/src/lib.rs index 34a588aa39..6890934049 100644 --- a/crates/primitives/starknet/src/lib.rs +++ b/crates/primitives/starknet/src/lib.rs @@ -48,6 +48,9 @@ pub mod fees; /// Sequencer address inherent data. pub mod sequencer_address; +/// Starknet Errors types +pub mod errors; + /// Tests. #[cfg(test)] pub mod tests; diff --git a/crates/primitives/starknet/src/starknet_serde/mod.rs b/crates/primitives/starknet/src/starknet_serde/mod.rs index 746bdff3f3..7218368a73 100644 --- a/crates/primitives/starknet/src/starknet_serde/mod.rs +++ b/crates/primitives/starknet/src/starknet_serde/mod.rs @@ -28,49 +28,6 @@ fn string_to_felt(hex_str: &str) -> Result { } } -#[allow(missing_docs)] -#[derive(Error, Clone, Copy, Debug, scale_codec::Encode, scale_codec::Decode, scale_info::TypeInfo)] -pub enum StarknetRpcApiError { - #[error("Failed to write transaction")] - FailedToReceiveTxn = 1, - #[error("Contract not found")] - ContractNotFound = 20, - #[error("Invalid message selector")] - InvalidMessageSelector = 21, - #[error("Invalid call data")] - InvalidCallData = 22, - #[error("Block not found")] - BlockNotFound = 24, - #[error("Transaction hash not found")] - TxnHashNotFound = 25, - #[error("Invalid transaction index in a block")] - InvalidTxnIndex = 27, - #[error("Class hash not found")] - ClassHashNotFound = 28, - #[error("Class already declared")] - ClassAlreadyDeclared = 51, - #[error("Requested page size is too big")] - PageSizeTooBig = 31, - #[error("There are no blocks")] - NoBlocks = 32, - #[error("The supplied continuation token is invalid or unknown")] - InvalidContinuationToken = 33, - #[error("Contract error")] - ContractError = 40, - #[error("Invalid contract class")] - InvalidContractClass = 50, - #[error("Too many storage keys requested")] - ProofLimitExceeded = 10000_isize, - #[error("Too many keys provided in a filter")] - TooManyKeysInFilter = 34, - #[error("Internal server error")] - InternalServerError = 500_isize, - #[error("Failed to fetch pending transactions")] - FailedToFetchPendingTransactions = 38, - #[error("Unimplemented method")] - UnimplementedMethod = 501_isize, -} - // Deserialization and Conversion for JSON Transactions, Events, and CallEntryPoints /// Struct for deserializing CallEntryPoint from JSON #[derive(Debug, Serialize, Deserialize)] diff --git a/crates/runtime/src/lib.rs b/crates/runtime/src/lib.rs index 0ae5ecc8cd..0953da08d5 100644 --- a/crates/runtime/src/lib.rs +++ b/crates/runtime/src/lib.rs @@ -28,8 +28,8 @@ pub use frame_support::{construct_runtime, parameter_types, StorageValue}; pub use frame_system::Call as SystemCall; use frame_system::EventRecord; use mp_starknet::crypto::hash::Hasher; +use mp_starknet::errors::StarknetRpcApiError; use mp_starknet::execution::types::{ClassHashWrapper, ContractAddressWrapper, Felt252Wrapper, StorageKeyWrapper}; -use mp_starknet::starknet_serde::StarknetRpcApiError; use mp_starknet::transaction::types::{ DeclareTransaction, DeployAccountTransaction, EventWrapper, InvokeTransaction, Transaction, TxType, }; @@ -329,6 +329,16 @@ impl_runtime_apis! { if error == PalletError::::ContractNotFound.into() { return StarknetRpcApiError::ContractNotFound; } + if error == PalletError::::ClassHashAlreadyDeclared.into() { + return StarknetRpcApiError::ClassAlreadyDeclared; + } + if error == PalletError::::ContractClassHashUnknown.into() { + return StarknetRpcApiError::ClassHashNotFound; + } + if error == PalletError::::InvalidContractClass.into() { + return StarknetRpcApiError::InvalidContractClass; + } + StarknetRpcApiError::ContractError } }