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

Commit

Permalink
dev: clippy::nursery non breaking (#653)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhavyagosai authored Nov 20, 2023
1 parent fd5fe43 commit 698737e
Show file tree
Hide file tree
Showing 22 changed files with 78 additions and 70 deletions.
24 changes: 14 additions & 10 deletions crates/core/src/client/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@ pub enum Network {
impl Network {
pub fn gateway_url(&self) -> Result<Url, ConfigError> {
match self {
Network::MainnetGateway => Ok(Url::parse("https://alpha-mainnet.starknet.io/feeder_gateway/")?),
Network::Goerli1Gateway => Ok(Url::parse("https://alpha4.starknet.io/feeder_gateway/")?),
Network::Goerli2Gateway => Ok(Url::parse("https://alpha4-2.starknet.io/feeder_gateway/")?),
Self::MainnetGateway => Ok(Url::parse("https://alpha-mainnet.starknet.io/feeder_gateway/")?),
Self::Goerli1Gateway => Ok(Url::parse("https://alpha4.starknet.io/feeder_gateway/")?),
Self::Goerli2Gateway => Ok(Url::parse("https://alpha4-2.starknet.io/feeder_gateway/")?),
_ => Err(ConfigError::InvalidNetwork(format!("Network {:?} is not supported for gateway url", self))),
}
}

pub fn provider_url(&self) -> Result<Url, ConfigError> {
match self {
Network::Katana => Ok(Url::parse(KATANA_RPC_URL)?),
Network::Madara => Ok(Url::parse(MADARA_RPC_URL)?),
Network::Sharingan => Ok(Url::parse(
Self::Katana => Ok(Url::parse(KATANA_RPC_URL)?),
Self::Madara => Ok(Url::parse(MADARA_RPC_URL)?),
Self::Sharingan => Ok(Url::parse(
std::env::var("SHARINGAN_RPC_URL")
.map_err(|_| ConfigError::EnvironmentVariableMissing("SHARINGAN_RPC_URL".to_string()))?
.as_str(),
)?),
Network::JsonRpcProvider(url) => Ok(url.clone()),
Self::JsonRpcProvider(url) => Ok(url.clone()),
_ => Err(ConfigError::InvalidNetwork(format!("Network {:?} is not supported for provider url", self))),
}
}
Expand All @@ -71,14 +71,14 @@ pub struct KakarotRpcConfig {
}

impl KakarotRpcConfig {
pub fn new(
pub const fn new(
network: Network,
kakarot_address: FieldElement,
proxy_account_class_hash: FieldElement,
externally_owned_account_class_hash: FieldElement,
contract_account_class_hash: FieldElement,
) -> Self {
KakarotRpcConfig {
Self {
network,
kakarot_address,
proxy_account_class_hash,
Expand Down Expand Up @@ -109,7 +109,7 @@ impl KakarotRpcConfig {
let externally_owned_account_class_hash = field_element_from_env("EXTERNALLY_OWNED_ACCOUNT_CLASS_HASH")?;
let contract_account_class_hash = field_element_from_env("CONTRACT_ACCOUNT_CLASS_HASH")?;

Ok(KakarotRpcConfig::new(
Ok(Self::new(
network,
kakarot_address,
proxy_account_class_hash,
Expand All @@ -128,6 +128,8 @@ impl<T: JsonRpcTransport> JsonRpcClientBuilder<T> {
Self(JsonRpcClient::new(transport))
}

// This clippy lint is false positive, trying to make this function `const` but it doesn't work.
#[allow(clippy::missing_const_for_fn)]
/// Build the `JsonRpcClient`.
pub fn build(self) -> JsonRpcClient<T> {
self.0
Expand Down Expand Up @@ -178,6 +180,8 @@ impl SequencerGatewayProviderBuilder {
}
}

// This clippy lint is false positive, trying to make this function `const` but it doesn't work.
#[allow(clippy::missing_const_for_fn)]
/// Build the `SequencerGatewayProvider`.
pub fn build(self) -> SequencerGatewayProvider {
self.0
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/client/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl<E: std::error::Error> From<EthApiError<E>> for ErrorObject<'static> {

impl<E: std::error::Error> From<EthApiError<E>> for jsonrpsee::core::Error {
fn from(err: EthApiError<E>) -> Self {
jsonrpsee::core::Error::Call(err.into())
Self::Call(err.into())
}
}

Expand Down
12 changes: 6 additions & 6 deletions crates/core/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ impl<P: Provider + Send + Sync + 'static> KakarotClient<P> {
}

/// Returns the max_priority_fee_per_gas of Kakarot
pub fn max_priority_fee_per_gas(&self) -> U128 {
pub const fn max_priority_fee_per_gas(&self) -> U128 {
MAX_PRIORITY_FEE_PER_GAS
}

Expand Down Expand Up @@ -310,7 +310,7 @@ impl<P: Provider + Send + Sync + 'static> KakarotClient<P> {
}
};

let chain_id = request.chain_id.unwrap_or(CHAIN_ID.into());
let chain_id = request.chain_id.unwrap_or_else(|| CHAIN_ID.into());

let from = request.from.ok_or_else(|| EthApiError::MissingParameterError("from for estimate_gas".into()))?;
let nonce = self.nonce(from, block_id).await?.try_into().map_err(ConversionError::<u64>::from)?;
Expand Down Expand Up @@ -400,22 +400,22 @@ impl<P: Provider + Send + Sync + 'static> KakarotClient<P> {
Ok(U256::from(fee_estimate.gas_price))
}
/// Returns the Kakarot contract address.
pub fn kakarot_address(&self) -> FieldElement {
pub const fn kakarot_address(&self) -> FieldElement {
self.kakarot_contract.address
}

/// Returns the Kakarot externally owned account class hash.
pub fn externally_owned_account_class_hash(&self) -> FieldElement {
pub const fn externally_owned_account_class_hash(&self) -> FieldElement {
self.kakarot_contract.externally_owned_account_class_hash
}

/// Returns the Kakarot contract account class hash.
pub fn contract_account_class_hash(&self) -> FieldElement {
pub const fn contract_account_class_hash(&self) -> FieldElement {
self.kakarot_contract.contract_account_class_hash
}

/// Returns the Kakarot proxy account class hash.
pub fn proxy_account_class_hash(&self) -> FieldElement {
pub const fn proxy_account_class_hash(&self) -> FieldElement {
self.kakarot_contract.proxy_account_class_hash
}

Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/client/waiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ impl<P: Provider> TransactionWaiter<P> {
})));
}

let receipt = self.provider.get_transaction_receipt(self.transaction_hash).await;
match receipt {
let receipt = self.provider.get_transaction_receipt(self.transaction_hash);
match receipt.await {
Ok(receipt) => match receipt {
MaybePendingTransactionReceipt::Receipt(receipt) => match receipt.execution_result() {
ExecutionResult::Succeeded => {
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/contracts/erc20/ethereum_erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct EthereumErc20<'a, P> {
}

impl<'a, P: Provider + Send + Sync + 'static> EthereumErc20<'a, P> {
pub fn new(address: FieldElement, kakarot_contract: &'a KakarotContract<P>) -> Self {
pub const fn new(address: FieldElement, kakarot_contract: &'a KakarotContract<P>) -> Self {
Self { address, kakarot_contract }
}

Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/contracts/erc20/starknet_erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct StarknetErc20<'a, P> {
}

impl<'a, P: Provider + Send + Sync> StarknetErc20<'a, P> {
pub fn new(provider: &'a P, address: FieldElement) -> Self {
pub const fn new(provider: &'a P, address: FieldElement) -> Self {
Self { provider, address }
}

Expand Down
18 changes: 9 additions & 9 deletions crates/core/src/mock/mock_starknet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ impl From<AvailableFixtures> for JsonRpcMethod {
AvailableFixtures::ComputeStarknetAddress
| AvailableFixtures::GetImplementation
| AvailableFixtures::GetEvmAddress
| AvailableFixtures::GetNonce => JsonRpcMethod::Call,
AvailableFixtures::GetClassHashAt(_, _) => JsonRpcMethod::GetClassHashAt,
| AvailableFixtures::GetNonce => Self::Call,
AvailableFixtures::GetClassHashAt(_, _) => Self::GetClassHashAt,
}
}
}
Expand All @@ -56,12 +56,12 @@ impl Serialize for AvailableFixtures {
S: Serializer,
{
match self {
AvailableFixtures::ComputeStarknetAddress => serializer.serialize_str("kakarot_computeStarknetAddress"),
AvailableFixtures::GetEvmAddress => serializer.serialize_str("account_getEvmAddress"),
AvailableFixtures::GetImplementation => serializer.serialize_str("account_getImplementation"),
AvailableFixtures::GetNonce => serializer.serialize_str("account_getNonce"),
AvailableFixtures::GetClassHashAt(_, _) => serializer.serialize_str("starknet_getClassHashAt"),
AvailableFixtures::Other(method) => method.serialize(serializer),
Self::ComputeStarknetAddress => serializer.serialize_str("kakarot_computeStarknetAddress"),
Self::GetEvmAddress => serializer.serialize_str("account_getEvmAddress"),
Self::GetImplementation => serializer.serialize_str("account_getImplementation"),
Self::GetNonce => serializer.serialize_str("account_getNonce"),
Self::GetClassHashAt(_, _) => serializer.serialize_str("starknet_getClassHashAt"),
Self::Other(method) => method.serialize(serializer),
}
}
}
Expand All @@ -87,7 +87,7 @@ pub struct StarknetRpcFixtureBuilder {

impl StarknetRpcFixtureBuilder {
/// Returns a new `StarknetRpcFixtureBuilder`.
pub fn new(method: AvailableFixtures) -> Self {
pub const fn new(method: AvailableFixtures) -> Self {
Self {
method,
fixture: StarknetRpcFixture {
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/models/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct FutureTokenBalance<P: Provider, F: Future<Output = BalanceOfResult<P>
}

impl<P: Provider, F: Future<Output = BalanceOfResult<P>>> FutureTokenBalance<P, F> {
pub fn new(balance: F, token_address: Address) -> Self {
pub const fn new(balance: F, token_address: Address) -> Self {
Self { balance, token_address, _phantom: PhantomData }
}
}
Expand Down
16 changes: 8 additions & 8 deletions crates/core/src/models/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::client::KakarotClient;
pub struct EthBlockId(EthereumBlockId);

impl EthBlockId {
pub fn new(block_id: EthereumBlockId) -> Self {
pub const fn new(block_id: EthereumBlockId) -> Self {
Self(block_id)
}
}
Expand All @@ -29,7 +29,7 @@ impl TryFrom<EthBlockId> for StarknetBlockId {
match eth_block_id.0 {
EthereumBlockId::Hash(hash) => {
let hash: Felt252Wrapper = hash.block_hash.try_into()?;
Ok(StarknetBlockId::Hash(hash.into()))
Ok(Self::Hash(hash.into()))
}
EthereumBlockId::Number(block_number_or_tag) => {
let block_number_or_tag: EthBlockNumberOrTag = block_number_or_tag.into();
Expand Down Expand Up @@ -64,11 +64,11 @@ impl From<EthBlockNumberOrTag> for StarknetBlockId {
let block_number_or_tag = block_number_or_tag.into();
match block_number_or_tag {
BlockNumberOrTag::Safe | BlockNumberOrTag::Latest | BlockNumberOrTag::Finalized => {
StarknetBlockId::Tag(BlockTag::Latest)
Self::Tag(BlockTag::Latest)
}
BlockNumberOrTag::Earliest => StarknetBlockId::Number(EARLIEST_BLOCK_NUMBER),
BlockNumberOrTag::Pending => StarknetBlockId::Tag(BlockTag::Pending),
BlockNumberOrTag::Number(number) => StarknetBlockId::Number(number),
BlockNumberOrTag::Earliest => Self::Number(EARLIEST_BLOCK_NUMBER),
BlockNumberOrTag::Pending => Self::Tag(BlockTag::Pending),
BlockNumberOrTag::Number(number) => Self::Number(number),
}
}
}
Expand Down Expand Up @@ -110,7 +110,7 @@ macro_rules! implement_starknet_block_getters_not_pending {
pub struct BlockWithTxHashes(MaybePendingBlockWithTxHashes);

impl BlockWithTxHashes {
pub fn new(block: MaybePendingBlockWithTxHashes) -> Self {
pub const fn new(block: MaybePendingBlockWithTxHashes) -> Self {
Self(block)
}

Expand All @@ -130,7 +130,7 @@ impl BlockWithTxHashes {
pub struct BlockWithTxs(MaybePendingBlockWithTxs);

impl BlockWithTxs {
pub fn new(block: MaybePendingBlockWithTxs) -> Self {
pub const fn new(block: MaybePendingBlockWithTxs) -> Self {
Self(block)
}

Expand Down
5 changes: 2 additions & 3 deletions crates/core/src/models/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl TryFrom<Vec<FieldElement>> for Calls {
offset += calldata_len;
calls.push(call);
}
Ok(Calls(calls))
Ok(Self(calls))
}
}

Expand All @@ -98,8 +98,7 @@ impl TryFrom<&Calls> for TransactionSigned {
.iter()
.filter_map(|x| u8::try_from(*x).ok())
.collect::<Vec<u8>>();
TransactionSigned::decode(&mut call.as_slice())
.map_err(|e| DataDecodingError::SignatureDecodingError(e.to_string()))
Self::decode(&mut call.as_slice()).map_err(|e| DataDecodingError::SignatureDecodingError(e.to_string()))
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/models/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::models::convertible::ConvertibleStarknetEvent;
pub struct StarknetEvent(Event);

impl StarknetEvent {
pub fn new(sn_event: Event) -> Self {
pub const fn new(sn_event: Event) -> Self {
Self(sn_event)
}
}
Expand Down
14 changes: 7 additions & 7 deletions crates/core/src/models/felt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::ConversionError;
pub struct Felt252Wrapper(FieldElement);

impl Felt252Wrapper {
pub const ZERO: Felt252Wrapper = Self(FieldElement::ZERO);
pub const ZERO: Self = Self(FieldElement::ZERO);
/// Troncate the first 12 bytes of the `FieldElement` and return the last 20 bytes as an
/// Ethereum address. This is used to convert Starknet addresses to Ethereum addresses in
/// cases where the Starknet address does not represent a Kakarot address, i.e. it does not have
Expand Down Expand Up @@ -51,15 +51,15 @@ impl TryFrom<Felt252Wrapper> for u64 {
type Error = ConversionError<()>;

fn try_from(value: Felt252Wrapper) -> Result<Self, Self::Error> {
u64::try_from(value.0).map_err(|e| ConversionError::ValueOutOfRange(e.to_string()))
Self::try_from(value.0).map_err(|e| ConversionError::ValueOutOfRange(e.to_string()))
}
}

impl TryFrom<Felt252Wrapper> for u128 {
type Error = ConversionError<()>;

fn try_from(value: Felt252Wrapper) -> Result<Self, Self::Error> {
u128::try_from(value.0).map_err(|e| ConversionError::ValueOutOfRange(e.to_string()))
Self::try_from(value.0).map_err(|e| ConversionError::ValueOutOfRange(e.to_string()))
}
}

Expand All @@ -82,7 +82,7 @@ impl TryFrom<Felt252Wrapper> for Address {
return Err(ConversionError::ToEthereumAddressError);
}

Ok(Address::from_slice(&bytes[12..]))
Ok(Self::from_slice(&bytes[12..]))
}
}

Expand All @@ -98,7 +98,7 @@ impl TryFrom<H256> for Felt252Wrapper {
impl From<Felt252Wrapper> for H256 {
fn from(felt: Felt252Wrapper) -> Self {
let felt: FieldElement = felt.into();
H256::from_slice(&felt.to_bytes_be())
Self::from_slice(&felt.to_bytes_be())
}
}

Expand All @@ -114,14 +114,14 @@ impl TryFrom<U256> for Felt252Wrapper {
impl From<Felt252Wrapper> for U256 {
fn from(felt: Felt252Wrapper) -> Self {
let felt: FieldElement = felt.into();
U256::from_be_bytes(felt.to_bytes_be())
Self::from_be_bytes(felt.to_bytes_be())
}
}

impl From<Felt252Wrapper> for Bytes {
fn from(felt: Felt252Wrapper) -> Self {
let bytes = felt.0.to_bytes_be();
Bytes::from(bytes)
Self::from(bytes)
}
}

Expand Down
13 changes: 8 additions & 5 deletions crates/core/src/models/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use thiserror::Error;

use super::felt::Felt252Wrapper;

#[derive(Debug, Error, PartialEq)]
#[derive(Debug, Error, PartialEq, Eq)]
pub enum StarknetSignatureError {
#[error("missing Starknet signature param {0}")]
MissingSignatureParamsError(String),
Expand All @@ -23,12 +23,15 @@ impl TryFrom<StarknetSignature> for EthSignature {

fn try_from(value: StarknetSignature) -> Result<Self, Self::Error> {
let r: Felt252Wrapper =
(*value.0.get(0).ok_or(StarknetSignatureError::MissingSignatureParamsError("r".to_string()))?).into();
(*value.0.get(0).ok_or_else(|| StarknetSignatureError::MissingSignatureParamsError("r".to_string()))?)
.into();
let s: Felt252Wrapper =
(*value.0.get(1).ok_or(StarknetSignatureError::MissingSignatureParamsError("s".to_string()))?).into();
(*value.0.get(1).ok_or_else(|| StarknetSignatureError::MissingSignatureParamsError("s".to_string()))?)
.into();
let v: Felt252Wrapper =
(*value.0.get(2).ok_or(StarknetSignatureError::MissingSignatureParamsError("v".to_string()))?).into();
Ok(EthSignature { r: r.into(), s: s.into(), v: v.into(), y_parity: None })
(*value.0.get(2).ok_or_else(|| StarknetSignatureError::MissingSignatureParamsError("v".to_string()))?)
.into();
Ok(Self { r: r.into(), s: s.into(), v: v.into(), y_parity: None })
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/core/tests/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async fn test_wait_for_confirmation_on_l2(#[future] katana: Katana) {

match transaction_receipt {
MaybePendingTransactionReceipt::Receipt(TransactionReceipt::Invoke(receipt)) => {
assert!(matches!(receipt.execution_result, ExecutionResult::Succeeded))
assert!(matches!(receipt.execution_result, ExecutionResult::Succeeded));
}
_ => panic!(
"Expected MaybePendingTransactionReceipt::Receipt(TransactionReceipt::Invoke), got {:?}",
Expand Down
Loading

0 comments on commit 698737e

Please sign in to comment.