Skip to content

Commit 34c7f5b

Browse files
authored
feat: use query txs for account deployment simulation (#611)
1 parent c6c2714 commit 34c7f5b

File tree

3 files changed

+60
-24
lines changed

3 files changed

+60
-24
lines changed

starknet-accounts/src/factory/argent.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ where
8080
async fn sign_deployment_v1(
8181
&self,
8282
deployment: &RawAccountDeploymentV1,
83+
query_only: bool,
8384
) -> Result<Vec<Felt>, Self::SignError> {
84-
let tx_hash =
85-
PreparedAccountDeploymentV1::from_raw(deployment.clone(), self).transaction_hash();
85+
let tx_hash = PreparedAccountDeploymentV1::from_raw(deployment.clone(), self)
86+
.transaction_hash(query_only);
8687
let signature = self.signer.sign_hash(&tx_hash).await?;
8788

8889
Ok(vec![signature.r, signature.s])
@@ -91,9 +92,10 @@ where
9192
async fn sign_deployment_v3(
9293
&self,
9394
deployment: &RawAccountDeploymentV3,
95+
query_only: bool,
9496
) -> Result<Vec<Felt>, Self::SignError> {
95-
let tx_hash =
96-
PreparedAccountDeploymentV3::from_raw(deployment.clone(), self).transaction_hash();
97+
let tx_hash = PreparedAccountDeploymentV3::from_raw(deployment.clone(), self)
98+
.transaction_hash(query_only);
9799
let signature = self.signer.sign_hash(&tx_hash).await?;
98100

99101
Ok(vec![signature.r, signature.s])

starknet-accounts/src/factory/mod.rs

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ const PREFIX_DEPLOY_ACCOUNT: Felt = Felt::from_raw([
2626
3350261884043292318,
2727
]);
2828

29+
/// 2 ^ 128 + 1
30+
const QUERY_VERSION_ONE: Felt = Felt::from_raw([
31+
576460752142433776,
32+
18446744073709551584,
33+
17407,
34+
18446744073700081633,
35+
]);
36+
37+
/// 2 ^ 128 + 3
38+
const QUERY_VERSION_THREE: Felt = Felt::from_raw([
39+
576460752142432688,
40+
18446744073709551584,
41+
17407,
42+
18446744073700081569,
43+
]);
44+
2945
/// Cairo string for "STARKNET_CONTRACT_ADDRESS"
3046
const PREFIX_CONTRACT_ADDRESS: Felt = Felt::from_raw([
3147
533439743893157637,
@@ -65,11 +81,13 @@ pub trait AccountFactory: Sized {
6581
async fn sign_deployment_v1(
6682
&self,
6783
deployment: &RawAccountDeploymentV1,
84+
query_only: bool,
6885
) -> Result<Vec<Felt>, Self::SignError>;
6986

7087
async fn sign_deployment_v3(
7188
&self,
7289
deployment: &RawAccountDeploymentV3,
90+
query_only: bool,
7391
) -> Result<Vec<Felt>, Self::SignError>;
7492

7593
fn deploy_v1(&self, salt: Felt) -> AccountDeploymentV1<Self> {
@@ -404,7 +422,7 @@ where
404422
},
405423
};
406424
let deploy = prepared
407-
.get_deploy_request()
425+
.get_deploy_request(true)
408426
.await
409427
.map_err(AccountFactoryError::Signing)?;
410428

@@ -436,7 +454,7 @@ where
436454
},
437455
};
438456
let deploy = prepared
439-
.get_deploy_request()
457+
.get_deploy_request(true)
440458
.await
441459
.map_err(AccountFactoryError::Signing)?;
442460

@@ -637,7 +655,7 @@ where
637655
},
638656
};
639657
let deploy = prepared
640-
.get_deploy_request()
658+
.get_deploy_request(true)
641659
.await
642660
.map_err(AccountFactoryError::Signing)?;
643661

@@ -670,7 +688,7 @@ where
670688
},
671689
};
672690
let deploy = prepared
673-
.get_deploy_request()
691+
.get_deploy_request(true)
674692
.await
675693
.map_err(AccountFactoryError::Signing)?;
676694

@@ -760,13 +778,17 @@ where
760778
)
761779
}
762780

763-
pub fn transaction_hash(&self) -> Felt {
781+
pub fn transaction_hash(&self, query_only: bool) -> Felt {
764782
let mut calldata_to_hash = vec![self.factory.class_hash(), self.inner.salt];
765783
calldata_to_hash.append(&mut self.factory.calldata());
766784

767785
compute_hash_on_elements(&[
768786
PREFIX_DEPLOY_ACCOUNT,
769-
Felt::ONE, // version
787+
if query_only {
788+
QUERY_VERSION_ONE
789+
} else {
790+
Felt::ONE
791+
}, // version
770792
self.address(),
771793
Felt::ZERO, // entry_point_selector
772794
compute_hash_on_elements(&calldata_to_hash),
@@ -780,7 +802,7 @@ where
780802
&self,
781803
) -> Result<DeployAccountTransactionResult, AccountFactoryError<F::SignError>> {
782804
let tx_request = self
783-
.get_deploy_request()
805+
.get_deploy_request(false)
784806
.await
785807
.map_err(AccountFactoryError::Signing)?;
786808
self.factory
@@ -792,8 +814,12 @@ where
792814

793815
async fn get_deploy_request(
794816
&self,
817+
query_only: bool,
795818
) -> Result<BroadcastedDeployAccountTransactionV1, F::SignError> {
796-
let signature = self.factory.sign_deployment_v1(&self.inner).await?;
819+
let signature = self
820+
.factory
821+
.sign_deployment_v1(&self.inner, query_only)
822+
.await?;
797823

798824
Ok(BroadcastedDeployAccountTransactionV1 {
799825
max_fee: self.inner.max_fee,
@@ -802,8 +828,7 @@ where
802828
contract_address_salt: self.inner.salt,
803829
constructor_calldata: self.factory.calldata(),
804830
class_hash: self.factory.class_hash(),
805-
// TODO: make use of query version tx for estimating fees
806-
is_query: false,
831+
is_query: query_only,
807832
})
808833
}
809834
}
@@ -821,11 +846,15 @@ where
821846
)
822847
}
823848

824-
pub fn transaction_hash(&self) -> Felt {
849+
pub fn transaction_hash(&self, query_only: bool) -> Felt {
825850
let mut hasher = PoseidonHasher::new();
826851

827852
hasher.update(PREFIX_DEPLOY_ACCOUNT);
828-
hasher.update(Felt::THREE);
853+
hasher.update(if query_only {
854+
QUERY_VERSION_THREE
855+
} else {
856+
Felt::THREE
857+
});
829858
hasher.update(self.address());
830859

831860
hasher.update({
@@ -882,7 +911,7 @@ where
882911
&self,
883912
) -> Result<DeployAccountTransactionResult, AccountFactoryError<F::SignError>> {
884913
let tx_request = self
885-
.get_deploy_request()
914+
.get_deploy_request(false)
886915
.await
887916
.map_err(AccountFactoryError::Signing)?;
888917
self.factory
@@ -894,8 +923,12 @@ where
894923

895924
async fn get_deploy_request(
896925
&self,
926+
query_only: bool,
897927
) -> Result<BroadcastedDeployAccountTransactionV3, F::SignError> {
898-
let signature = self.factory.sign_deployment_v3(&self.inner).await?;
928+
let signature = self
929+
.factory
930+
.sign_deployment_v3(&self.inner, query_only)
931+
.await?;
899932

900933
Ok(BroadcastedDeployAccountTransactionV3 {
901934
signature,
@@ -921,8 +954,7 @@ where
921954
// Hard-coded L1 DA mode for nonce and fee
922955
nonce_data_availability_mode: DataAvailabilityMode::L1,
923956
fee_data_availability_mode: DataAvailabilityMode::L1,
924-
// TODO: make use of query version tx for estimating fees
925-
is_query: false,
957+
is_query: query_only,
926958
})
927959
}
928960
}

starknet-accounts/src/factory/open_zeppelin.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ where
7777
async fn sign_deployment_v1(
7878
&self,
7979
deployment: &RawAccountDeploymentV1,
80+
query_only: bool,
8081
) -> Result<Vec<Felt>, Self::SignError> {
81-
let tx_hash =
82-
PreparedAccountDeploymentV1::from_raw(deployment.clone(), self).transaction_hash();
82+
let tx_hash = PreparedAccountDeploymentV1::from_raw(deployment.clone(), self)
83+
.transaction_hash(query_only);
8384
let signature = self.signer.sign_hash(&tx_hash).await?;
8485

8586
Ok(vec![signature.r, signature.s])
@@ -88,9 +89,10 @@ where
8889
async fn sign_deployment_v3(
8990
&self,
9091
deployment: &RawAccountDeploymentV3,
92+
query_only: bool,
9193
) -> Result<Vec<Felt>, Self::SignError> {
92-
let tx_hash =
93-
PreparedAccountDeploymentV3::from_raw(deployment.clone(), self).transaction_hash();
94+
let tx_hash = PreparedAccountDeploymentV3::from_raw(deployment.clone(), self)
95+
.transaction_hash(query_only);
9496
let signature = self.signer.sign_hash(&tx_hash).await?;
9597

9698
Ok(vec![signature.r, signature.s])

0 commit comments

Comments
 (0)