Skip to content

Commit 3552307

Browse files
committed
wip
1 parent 714d764 commit 3552307

File tree

6 files changed

+89
-18
lines changed

6 files changed

+89
-18
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cartridge/src/vrf/bootstrap.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ use std::time::{Duration, Instant};
1212

1313
use anyhow::{anyhow, Context, Result};
1414
use ark_ff::PrimeField;
15+
use katana_contracts::vrf::CartridgeVrfAccount;
1516
use katana_genesis::constant::DEFAULT_STRK_FEE_TOKEN_ADDRESS;
1617
use katana_primitives::chain::ChainId;
18+
use katana_primitives::class::ClassHash;
1719
use katana_primitives::utils::get_contract_address;
1820
use katana_primitives::{ContractAddress, Felt};
21+
use katana_rpc_types::RpcSierraContractClass;
1922
use stark_vrf::{generate_public_key, ScalarField};
2023
use starknet::accounts::{Account, ExecutionEncoding, SingleOwnerAccount};
2124
use starknet::contract::ContractFactory;
22-
use starknet::core::types::{BlockId, BlockTag, Call, StarknetError};
25+
use starknet::core::types::{BlockId, BlockTag, Call, FlattenedSierraClass, StarknetError};
2326
use starknet::macros::selector;
2427
use starknet::providers::jsonrpc::HttpTransport;
2528
use starknet::providers::{JsonRpcClient, Provider, ProviderError};
@@ -99,7 +102,8 @@ pub fn derive_vrf_accounts(
99102

100103
let account_public_key =
101104
SigningKey::from_secret_scalar(bootstrapper_private_key).verifying_key().scalar();
102-
let vrf_account_class_hash = katana_contracts::vrf::CartridgeVrfAccount::HASH;
105+
106+
let vrf_account_class_hash = CartridgeVrfAccount::HASH;
103107
// When using UDC with unique=0 (non-unique deployment), the deployer_address
104108
// used in address computation is 0, not the actual deployer or UDC address.
105109
let vrf_account_address = get_contract_address(
@@ -136,7 +140,8 @@ pub async fn bootstrap_vrf(
136140
let vrf_account_address = derived.vrf_account_address;
137141
let account_public_key =
138142
SigningKey::from_secret_scalar(bootstrapper_account_private_key).verifying_key().scalar();
139-
let vrf_account_class_hash = katana_contracts::vrf::CartridgeVrfAccount::HASH;
143+
144+
let vrf_account_class_hash = CartridgeVrfAccount::HASH;
140145

141146
// Create the source account for transactions
142147
let signer =
@@ -149,6 +154,22 @@ pub async fn bootstrap_vrf(
149154
ExecutionEncoding::New,
150155
);
151156

157+
if !is_declared(&provider, vrf_account_class_hash).await? {
158+
let class = CartridgeVrfAccount::CLASS.clone();
159+
let compiled_hash = CartridgeVrfAccount::CASM_HASH;
160+
161+
let rpc_class = RpcSierraContractClass::from(class.to_sierra().unwrap());
162+
let rpc_class = FlattenedSierraClass::try_from(rpc_class).unwrap();
163+
164+
let result = account
165+
.declare_v3(rpc_class.into(), compiled_hash)
166+
.send()
167+
.await
168+
.expect("fail to declare class");
169+
170+
assert_eq!(result.class_hash, vrf_account_class_hash, "Class hash mismatch");
171+
}
172+
152173
// Deploy VRF account if not already deployed
153174
if !is_deployed(&provider, vrf_account_address).await? {
154175
#[allow(deprecated)]
@@ -253,6 +274,17 @@ async fn is_deployed(
253274
}
254275
}
255276

277+
async fn is_declared(
278+
provider: &JsonRpcClient<HttpTransport>,
279+
class_hash: ClassHash,
280+
) -> Result<bool> {
281+
match provider.get_class(BlockId::Tag(BlockTag::PreConfirmed), class_hash).await {
282+
Ok(_) => Ok(true),
283+
Err(ProviderError::StarknetError(StarknetError::ClassHashNotFound)) => Ok(false),
284+
Err(e) => Err(anyhow!("failed to check class declaration: {e}")),
285+
}
286+
}
287+
256288
async fn wait_for_contract(
257289
provider: &JsonRpcClient<HttpTransport>,
258290
address: ContractAddress,

crates/paymaster/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ version.workspace = true
77

88
[dependencies]
99
katana-primitives.workspace = true
10+
katana-rpc-types.workspace = true
1011
katana-contracts.workspace = true
1112

1213
http.workspace = true

crates/paymaster/src/lib.rs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ use std::sync::Arc;
1818
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
1919
use std::{env, fs, io};
2020

21+
use katana_contracts::avnu::AvnuForwarder;
2122
use katana_primitives::chain::{ChainId, NamedChainId};
2223
use katana_primitives::class::ComputeClassHashError;
2324
use katana_primitives::utils::get_contract_address;
2425
use katana_primitives::{ContractAddress, Felt};
26+
use katana_rpc_types::RpcSierraContractClass;
2527
use serde::Serialize;
2628
use starknet::accounts::{Account, ExecutionEncoding, SingleOwnerAccount};
2729
use starknet::contract::ContractFactory;
28-
use starknet::core::types::{BlockId, BlockTag, Call, StarknetError};
30+
use starknet::core::types::{BlockId, BlockTag, Call, FlattenedSierraClass, StarknetError};
2931
use starknet::macros::selector;
3032
use starknet::providers::jsonrpc::HttpTransport;
3133
use starknet::providers::{JsonRpcClient, Provider, ProviderError};
@@ -496,17 +498,7 @@ impl PaymasterService {
496498
chain_id_felt
497499
};
498500

499-
let forwarder_class_hash = katana_contracts::avnu::AvnuForwarder::HASH;
500-
501-
// When using UDC with unique=0 (non-unique deployment), the deployer_address
502-
// used in address computation is 0, not the actual deployer or UDC address.
503-
let forwarder_address = get_contract_address(
504-
Felt::from(FORWARDER_SALT),
505-
forwarder_class_hash,
506-
&[self.config.relayer_address.into(), self.config.gas_tank_address.into()],
507-
Felt::ZERO,
508-
)
509-
.into();
501+
let forwarder_class_hash = AvnuForwarder::HASH;
510502

511503
// Create the relayer account for transactions
512504
let secret_key = SigningKey::from_secret_scalar(self.config.relayer_private_key);
@@ -518,6 +510,39 @@ impl PaymasterService {
518510
ExecutionEncoding::New,
519511
);
520512

513+
// declare class if not yet declred
514+
match provider.get_class(BlockId::Tag(BlockTag::PreConfirmed), forwarder_class_hash).await {
515+
Ok(..) => {}
516+
517+
Err(ProviderError::StarknetError(StarknetError::ClassHashNotFound)) => {
518+
let class = AvnuForwarder::CLASS.clone();
519+
let compiled_hash = AvnuForwarder::CASM_HASH;
520+
521+
let rpc_class = RpcSierraContractClass::from(class.to_sierra().unwrap());
522+
let rpc_class = FlattenedSierraClass::try_from(rpc_class).unwrap();
523+
524+
let result = account
525+
.declare_v3(rpc_class.into(), compiled_hash)
526+
.send()
527+
.await
528+
.expect("fail to declare class");
529+
530+
assert_eq!(result.class_hash, forwarder_class_hash, "Class hash mismatch");
531+
}
532+
533+
Err(..) => panic!("fail to fetch class"),
534+
};
535+
536+
// When using UDC with unique=0 (non-unique deployment), the deployer_address
537+
// used in address computation is 0, not the actual deployer or UDC address.
538+
let forwarder_address = get_contract_address(
539+
Felt::from(FORWARDER_SALT),
540+
forwarder_class_hash,
541+
&[self.config.relayer_address.into(), self.config.gas_tank_address.into()],
542+
Felt::ZERO,
543+
)
544+
.into();
545+
521546
// Deploy forwarder if not already deployed
522547
if !is_deployed(&provider, forwarder_address).await? {
523548
#[allow(deprecated)]

crates/primitives/src/class.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,20 @@ impl ContractClass {
229229
Self::Legacy(..) => 0,
230230
}
231231
}
232+
233+
pub fn to_sierra(self) -> Option<SierraContractClass> {
234+
match self {
235+
Self::Class(class) => Some(class),
236+
_ => None,
237+
}
238+
}
239+
240+
pub fn to_legacy(self) -> Option<LegacyContractClass> {
241+
match self {
242+
Self::Legacy(class) => Some(class),
243+
_ => None,
244+
}
245+
}
232246
}
233247

234248
#[derive(Debug, thiserror::Error)]

crates/rpc/rpc-types/src/class.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ use katana_primitives::class::{
1010
compute_legacy_class_hash, compute_sierra_class_hash, ClassHash, ComputeClassHashError,
1111
ContractClass, LegacyContractClass, SierraContractClass,
1212
};
13-
use katana_primitives::{
14-
Felt, {self},
15-
};
13+
use katana_primitives::{self, Felt};
1614
use serde::{Deserialize, Serialize};
1715
use serde_utils::base64;
1816
use starknet::core::types::{CompressedLegacyContractClass, FlattenedSierraClass};

0 commit comments

Comments
 (0)