Skip to content

Commit

Permalink
feat(jsonrpc): switch to spec 0.6.0 (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
xJonathanLEI authored Dec 11, 2023
1 parent b8642ce commit 63f091d
Show file tree
Hide file tree
Showing 17 changed files with 1,737 additions and 436 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
**Complete Starknet library in Rust[](https://www.reddit.com/r/rust/comments/12e7tdb/rust_trademark_policy_feedback_form/)**

![starknet-version-v0.13.0](https://img.shields.io/badge/Starknet_Version-v0.13.0-2ea44f?logo=ethereum)
[![jsonrpc-spec-v0.5.1](https://img.shields.io/badge/JSON--RPC-v0.5.1-2ea44f?logo=ethereum)](https://github.com/starkware-libs/starknet-specs/tree/v0.5.1)
[![jsonrpc-spec-v0.6.0](https://img.shields.io/badge/JSON--RPC-v0.6.0-2ea44f?logo=ethereum)](https://github.com/starkware-libs/starknet-specs/tree/v0.6.0)
[![linting-badge](https://github.com/xJonathanLEI/starknet-rs/actions/workflows/lint.yaml/badge.svg?branch=master)](https://github.com/xJonathanLEI/starknet-rs/actions/workflows/lint.yaml)
[![crates-badge](https://img.shields.io/crates/v/starknet.svg)](https://crates.io/crates/starknet)

Expand Down
12 changes: 10 additions & 2 deletions starknet-accounts/src/account/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ where
Some(value) => value,
None => {
let fee_estimate = self.estimate_fee_with_nonce(nonce).await?;
((fee_estimate.overall_fee as f64 * self.fee_estimate_multiplier) as u64).into()
((((TryInto::<u64>::try_into(fee_estimate.overall_fee)
.map_err(|_| AccountError::FeeOutOfRange)?) as f64)
* self.fee_estimate_multiplier) as u64)
.into()
}
};

Expand Down Expand Up @@ -186,6 +189,7 @@ where
.provider()
.estimate_fee_single(
BroadcastedTransaction::Declare(BroadcastedDeclareTransaction::V2(declare)),
[],
self.account.block_id(),
)
.await
Expand Down Expand Up @@ -339,7 +343,10 @@ where
Some(value) => value,
None => {
let fee_estimate = self.estimate_fee_with_nonce(nonce).await?;
((fee_estimate.overall_fee as f64 * self.fee_estimate_multiplier) as u64).into()
((((TryInto::<u64>::try_into(fee_estimate.overall_fee)
.map_err(|_| AccountError::FeeOutOfRange)?) as f64)
* self.fee_estimate_multiplier) as u64)
.into()
}
};

Expand Down Expand Up @@ -371,6 +378,7 @@ where
.provider()
.estimate_fee_single(
BroadcastedTransaction::Declare(BroadcastedDeclareTransaction::V1(declare)),
[],
self.account.block_id(),
)
.await
Expand Down
28 changes: 17 additions & 11 deletions starknet-accounts/src/account/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::{Call, ExecutionEncoder};
use starknet_core::{
crypto::compute_hash_on_elements,
types::{
BroadcastedInvokeTransaction, BroadcastedTransaction, FeeEstimate, FieldElement,
InvokeTransactionResult, SimulatedTransaction, SimulationFlag,
BroadcastedInvokeTransaction, BroadcastedInvokeTransactionV1, BroadcastedTransaction,
FeeEstimate, FieldElement, InvokeTransactionResult, SimulatedTransaction, SimulationFlag,
},
};
use starknet_providers::Provider;
Expand Down Expand Up @@ -135,7 +135,10 @@ where
Some(value) => value,
None => {
let fee_estimate = self.estimate_fee_with_nonce(nonce).await?;
((fee_estimate.overall_fee as f64 * self.fee_estimate_multiplier) as u64).into()
((((TryInto::<u64>::try_into(fee_estimate.overall_fee)
.map_err(|_| AccountError::FeeOutOfRange)?) as f64)
* self.fee_estimate_multiplier) as u64)
.into()
}
};

Expand Down Expand Up @@ -170,6 +173,7 @@ where
.provider()
.estimate_fee_single(
BroadcastedTransaction::Invoke(invoke),
[],
self.account.block_id(),
)
.await
Expand Down Expand Up @@ -285,13 +289,15 @@ where
) -> Result<BroadcastedInvokeTransaction, A::SignError> {
let signature = self.account.sign_execution(&self.inner, query_only).await?;

Ok(BroadcastedInvokeTransaction {
max_fee: self.inner.max_fee,
signature,
nonce: self.inner.nonce,
sender_address: self.account.address(),
calldata: self.account.encode_calls(&self.inner.calls),
is_query: query_only,
})
Ok(BroadcastedInvokeTransaction::V1(
BroadcastedInvokeTransactionV1 {
max_fee: self.inner.max_fee,
signature,
nonce: self.inner.nonce,
sender_address: self.account.address(),
calldata: self.account.encode_calls(&self.inner.calls),
is_query: query_only,
},
))
}
}
2 changes: 2 additions & 0 deletions starknet-accounts/src/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ pub enum AccountError<S> {
ClassHashCalculation(ComputeClassHashError),
#[error(transparent)]
ClassCompression(CompressProgramError),
#[error("fee calculation overflow")]
FeeOutOfRange,
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
Expand Down
33 changes: 21 additions & 12 deletions starknet-accounts/src/factory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use async_trait::async_trait;
use starknet_core::{
crypto::compute_hash_on_elements,
types::{
BlockId, BlockTag, BroadcastedDeployAccountTransaction, BroadcastedTransaction,
BlockId, BlockTag, BroadcastedDeployAccountTransaction,
BroadcastedDeployAccountTransactionV1, BroadcastedTransaction,
DeployAccountTransactionResult, FeeEstimate, FieldElement, SimulatedTransaction,
SimulationFlag, StarknetError,
},
Expand Down Expand Up @@ -103,6 +104,8 @@ pub enum AccountFactoryError<S> {
Signing(S),
#[error(transparent)]
Provider(ProviderError),
#[error("fee calculation overflow")]
FeeOutOfRange,
}

impl<'f, F> AccountDeployment<'f, F> {
Expand Down Expand Up @@ -237,7 +240,10 @@ where
Some(value) => value,
None => {
let fee_estimate = self.estimate_fee_with_nonce(nonce).await?;
((fee_estimate.overall_fee as f64 * self.fee_estimate_multiplier) as u64).into()
((((TryInto::<u64>::try_into(fee_estimate.overall_fee)
.map_err(|_| AccountFactoryError::FeeOutOfRange)?) as f64)
* self.fee_estimate_multiplier) as u64)
.into()
}
};

Expand Down Expand Up @@ -272,6 +278,7 @@ where
.provider()
.estimate_fee_single(
BroadcastedTransaction::DeployAccount(deploy),
[],
self.factory.block_id(),
)
.await
Expand Down Expand Up @@ -375,16 +382,18 @@ where
) -> Result<BroadcastedDeployAccountTransaction, F::SignError> {
let signature = self.factory.sign_deployment(&self.inner).await?;

Ok(BroadcastedDeployAccountTransaction {
max_fee: self.inner.max_fee,
signature,
nonce: self.inner.nonce,
contract_address_salt: self.inner.salt,
constructor_calldata: self.factory.calldata(),
class_hash: self.factory.class_hash(),
// TODO: make use of query version tx for estimating fees
is_query: false,
})
Ok(BroadcastedDeployAccountTransaction::V1(
BroadcastedDeployAccountTransactionV1 {
max_fee: self.inner.max_fee,
signature,
nonce: self.inner.nonce,
contract_address_salt: self.inner.salt,
constructor_calldata: self.factory.calldata(),
class_hash: self.factory.class_hash(),
// TODO: make use of query version tx for estimating fees
is_query: false,
},
))
}
}

Expand Down
10 changes: 5 additions & 5 deletions starknet-accounts/tests/single_owner_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ fn create_sequencer_client() -> SequencerGatewayProvider {
}

fn create_jsonrpc_client() -> JsonRpcClient<HttpTransport> {
let rpc_url =
std::env::var("STARKNET_RPC").unwrap_or("https://rpc-goerli-1.starknet.rs/rpc/v0.5".into());
let rpc_url = std::env::var("STARKNET_RPC")
.unwrap_or("https://juno.rpc.goerli.starknet.rs/rpc/v0_6".into());
JsonRpcClient::new(HttpTransport::new(url::Url::parse(&rpc_url).unwrap()))
}

Expand Down Expand Up @@ -205,7 +205,7 @@ async fn can_estimate_fee_inner<P: Provider + Send + Sync>(provider: P, address:
.await
.unwrap();

assert!(fee_estimate.overall_fee > 0);
assert!(fee_estimate.overall_fee > FieldElement::ZERO);
}

async fn can_parse_fee_estimation_error_inner<P: Provider + Send + Sync>(
Expand Down Expand Up @@ -248,9 +248,9 @@ async fn can_parse_fee_estimation_error_inner<P: Provider + Send + Sync>(
{
Ok(_) => panic!("unexpected successful fee estimation"),
Err(AccountError::Provider(ProviderError::StarknetError(
StarknetError::ContractError(err_data),
StarknetError::TransactionExecutionError(err_data),
))) => {
assert!(!err_data.revert_error.is_empty());
assert!(!err_data.execution_error.is_empty());
}
_ => panic!("unexpected error type"),
}
Expand Down
4 changes: 2 additions & 2 deletions starknet-contract/tests/contract_deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use url::Url;

#[tokio::test]
async fn can_deploy_contract_to_alpha_goerli() {
let rpc_url =
std::env::var("STARKNET_RPC").unwrap_or("https://rpc-goerli-1.starknet.rs/rpc/v0.5".into());
let rpc_url = std::env::var("STARKNET_RPC")
.unwrap_or("https://juno.rpc.goerli.starknet.rs/rpc/v0_6".into());
let provider = JsonRpcClient::new(HttpTransport::new(Url::parse(&rpc_url).unwrap()));
let signer = LocalWallet::from(SigningKey::from_secret_scalar(
FieldElement::from_hex_be(
Expand Down
Loading

0 comments on commit 63f091d

Please sign in to comment.