Skip to content

Commit

Permalink
Merge branch 'master' into messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Oct 26, 2023
2 parents 632b095 + 33e200d commit e0ea55d
Show file tree
Hide file tree
Showing 29 changed files with 1,527 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ all-features = true
[dependencies]
starknet-ff = { version = "0.3.4", path = "./starknet-ff", default-features = false }
starknet-crypto = { version = "0.6.0", path = "./starknet-crypto" }
starknet-core = { version = "0.6.0", path = "./starknet-core", default-features = false }
starknet-core = { version = "0.6.1", path = "./starknet-core", default-features = false }
starknet-providers = { version = "0.6.0", path = "./starknet-providers" }
starknet-contract = { version = "0.5.0", path = "./starknet-contract" }
starknet-signers = { version = "0.4.0", path = "./starknet-signers" }
Expand Down
2 changes: 1 addition & 1 deletion starknet-accounts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = ["ethereum", "starknet", "web3"]
exclude = ["test-data/**"]

[dependencies]
starknet-core = { version = "0.6.0", path = "../starknet-core" }
starknet-core = { version = "0.6.1", path = "../starknet-core" }
starknet-providers = { version = "0.6.0", path = "../starknet-providers" }
starknet-signers = { version = "0.4.0", path = "../starknet-signers" }
async-trait = "0.1.68"
Expand Down
117 changes: 116 additions & 1 deletion starknet-accounts/src/account/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use starknet_core::{
contract::{legacy::LegacyContractClass, ComputeClassHashError},
BroadcastedDeclareTransaction, BroadcastedDeclareTransactionV1,
BroadcastedDeclareTransactionV2, BroadcastedTransaction, DeclareTransactionResult,
FeeEstimate, FieldElement, FlattenedSierraClass,
FeeEstimate, FieldElement, FlattenedSierraClass, SimulatedTransaction, SimulationFlag,
},
};
use starknet_providers::Provider;
Expand Down Expand Up @@ -115,6 +115,26 @@ where
self.estimate_fee_with_nonce(nonce).await
}

pub async fn simulate(
&self,
skip_validate: bool,
skip_fee_charge: bool,
) -> Result<SimulatedTransaction, AccountError<A::SignError, <A::Provider as Provider>::Error>>
{
// Resolves nonce
let nonce = match self.nonce {
Some(value) => value,
None => self
.account
.get_nonce()
.await
.map_err(AccountError::Provider)?,
};

self.simulate_with_nonce(nonce, skip_validate, skip_fee_charge)
.await
}

pub async fn send(
&self,
) -> Result<
Expand Down Expand Up @@ -184,6 +204,44 @@ where
.await
.map_err(AccountError::Provider)
}

async fn simulate_with_nonce(
&self,
nonce: FieldElement,
skip_validate: bool,
skip_fee_charge: bool,
) -> Result<SimulatedTransaction, AccountError<A::SignError, <A::Provider as Provider>::Error>>
{
let prepared = PreparedDeclaration {
account: self.account,
inner: RawDeclaration {
contract_class: self.contract_class.clone(),
compiled_class_hash: self.compiled_class_hash,
nonce,
max_fee: self.max_fee.unwrap_or_default(),
},
};
let declare = prepared.get_declare_request(true).await?;

let mut flags = vec![];

if skip_validate {
flags.push(SimulationFlag::SkipValidate);
}
if skip_fee_charge {
flags.push(SimulationFlag::SkipFeeCharge);
}

self.account
.provider()
.simulate_transaction(
self.account.block_id(),
BroadcastedTransaction::Declare(BroadcastedDeclareTransaction::V2(declare)),
&flags,
)
.await
.map_err(AccountError::Provider)
}
}

impl<'a, A> LegacyDeclaration<'a, A> {
Expand Down Expand Up @@ -256,6 +314,26 @@ where
self.estimate_fee_with_nonce(nonce).await
}

pub async fn simulate(
&self,
skip_validate: bool,
skip_fee_charge: bool,
) -> Result<SimulatedTransaction, AccountError<A::SignError, <A::Provider as Provider>::Error>>
{
// Resolves nonce
let nonce = match self.nonce {
Some(value) => value,
None => self
.account
.get_nonce()
.await
.map_err(AccountError::Provider)?,
};

self.simulate_with_nonce(nonce, skip_validate, skip_fee_charge)
.await
}

pub async fn send(
&self,
) -> Result<
Expand Down Expand Up @@ -323,6 +401,43 @@ where
.await
.map_err(AccountError::Provider)
}

async fn simulate_with_nonce(
&self,
nonce: FieldElement,
skip_validate: bool,
skip_fee_charge: bool,
) -> Result<SimulatedTransaction, AccountError<A::SignError, <A::Provider as Provider>::Error>>
{
let prepared = PreparedLegacyDeclaration {
account: self.account,
inner: RawLegacyDeclaration {
contract_class: self.contract_class.clone(),
nonce,
max_fee: self.max_fee.unwrap_or_default(),
},
};
let declare = prepared.get_declare_request(true).await?;

let mut flags = vec![];

if skip_validate {
flags.push(SimulationFlag::SkipValidate);
}
if skip_fee_charge {
flags.push(SimulationFlag::SkipFeeCharge);
}

self.account
.provider()
.simulate_transaction(
self.account.block_id(),
BroadcastedTransaction::Declare(BroadcastedDeclareTransaction::V1(declare)),
&flags,
)
.await
.map_err(AccountError::Provider)
}
}

impl RawDeclaration {
Expand Down
63 changes: 60 additions & 3 deletions starknet-accounts/src/account/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use starknet_core::{
crypto::compute_hash_on_elements,
types::{
BroadcastedInvokeTransaction, BroadcastedTransaction, FeeEstimate, FieldElement,
InvokeTransactionResult,
InvokeTransactionResult, SimulatedTransaction, SimulationFlag,
},
};
use starknet_providers::Provider;
Expand Down Expand Up @@ -98,8 +98,25 @@ where
self.estimate_fee_with_nonce(nonce).await
}

// The `simulate` function is temporarily removed until it's supported in [Provider]
// TODO: add `simulate` back once transaction simulation in supported
pub async fn simulate(
&self,
skip_validate: bool,
skip_fee_charge: bool,
) -> Result<SimulatedTransaction, AccountError<A::SignError, <A::Provider as Provider>::Error>>
{
// Resolves nonce
let nonce = match self.nonce {
Some(value) => value,
None => self
.account
.get_nonce()
.await
.map_err(AccountError::Provider)?,
};

self.simulate_with_nonce(nonce, skip_validate, skip_fee_charge)
.await
}

pub async fn send(
&self,
Expand Down Expand Up @@ -169,6 +186,46 @@ where
.await
.map_err(AccountError::Provider)
}

async fn simulate_with_nonce(
&self,
nonce: FieldElement,
skip_validate: bool,
skip_fee_charge: bool,
) -> Result<SimulatedTransaction, AccountError<A::SignError, <A::Provider as Provider>::Error>>
{
let prepared = PreparedExecution {
account: self.account,
inner: RawExecution {
calls: self.calls.clone(),
nonce,
max_fee: self.max_fee.unwrap_or_default(),
},
};
let invoke = prepared
.get_invoke_request(true)
.await
.map_err(AccountError::Signing)?;

let mut flags = vec![];

if skip_validate {
flags.push(SimulationFlag::SkipValidate);
}
if skip_fee_charge {
flags.push(SimulationFlag::SkipFeeCharge);
}

self.account
.provider()
.simulate_transaction(
self.account.block_id(),
BroadcastedTransaction::Invoke(invoke),
&flags,
)
.await
.map_err(AccountError::Provider)
}
}

impl RawExecution {
Expand Down
13 changes: 12 additions & 1 deletion starknet-accounts/src/factory/argent.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{AccountFactory, PreparedAccountDeployment, RawAccountDeployment};

use async_trait::async_trait;
use starknet_core::types::FieldElement;
use starknet_core::types::{BlockId, BlockTag, FieldElement};
use starknet_providers::Provider;
use starknet_signers::Signer;

Expand All @@ -12,6 +12,7 @@ pub struct ArgentAccountFactory<S, P> {
guardian_public_key: FieldElement,
signer: S,
provider: P,
block_id: BlockId,
}

impl<S, P> ArgentAccountFactory<S, P>
Expand All @@ -33,8 +34,14 @@ where
guardian_public_key,
signer,
provider,
block_id: BlockId::Tag(BlockTag::Latest),
})
}

pub fn set_block_id(&mut self, block_id: BlockId) -> &Self {
self.block_id = block_id;
self
}
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
Expand Down Expand Up @@ -63,6 +70,10 @@ where
&self.provider
}

fn block_id(&self) -> BlockId {
self.block_id
}

async fn sign_deployment(
&self,
deployment: &RawAccountDeployment,
Expand Down
66 changes: 65 additions & 1 deletion starknet-accounts/src/factory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use starknet_core::{
crypto::compute_hash_on_elements,
types::{
BlockId, BlockTag, BroadcastedDeployAccountTransaction, BroadcastedTransaction,
DeployAccountTransactionResult, FeeEstimate, FieldElement, StarknetError,
DeployAccountTransactionResult, FeeEstimate, FieldElement, SimulatedTransaction,
SimulationFlag, StarknetError,
},
};
use starknet_providers::{
Expand Down Expand Up @@ -203,6 +204,27 @@ where
self.estimate_fee_with_nonce(nonce).await
}

pub async fn simulate(
&self,
skip_validate: bool,
skip_fee_charge: bool,
) -> Result<
SimulatedTransaction,
AccountFactoryError<F::SignError, <F::Provider as Provider>::Error>,
> {
// Resolves nonce
let nonce = match self.nonce {
Some(value) => value,
None => self
.fetch_nonce()
.await
.map_err(AccountFactoryError::Provider)?,
};

self.simulate_with_nonce(nonce, skip_validate, skip_fee_charge)
.await
}

pub async fn send(
&self,
) -> Result<
Expand Down Expand Up @@ -273,6 +295,48 @@ where
.await
.map_err(AccountFactoryError::Provider)
}

async fn simulate_with_nonce(
&self,
nonce: FieldElement,
skip_validate: bool,
skip_fee_charge: bool,
) -> Result<
SimulatedTransaction,
AccountFactoryError<F::SignError, <F::Provider as Provider>::Error>,
> {
let prepared = PreparedAccountDeployment {
factory: self.factory,
inner: RawAccountDeployment {
salt: self.salt,
nonce,
max_fee: self.max_fee.unwrap_or_default(),
},
};
let deploy = prepared
.get_deploy_request()
.await
.map_err(AccountFactoryError::Signing)?;

let mut flags = vec![];

if skip_validate {
flags.push(SimulationFlag::SkipValidate);
}
if skip_fee_charge {
flags.push(SimulationFlag::SkipFeeCharge);
}

self.factory
.provider()
.simulate_transaction(
self.factory.block_id(),
BroadcastedTransaction::DeployAccount(deploy),
&flags,
)
.await
.map_err(AccountFactoryError::Provider)
}
}

impl<'f, F> PreparedAccountDeployment<'f, F> {
Expand Down
Loading

0 comments on commit e0ea55d

Please sign in to comment.