Skip to content

Commit f7391a0

Browse files
committed
wip
1 parent e5f8d73 commit f7391a0

File tree

2 files changed

+80
-47
lines changed

2 files changed

+80
-47
lines changed

crates/rpc/rpc-server/src/cartridge/mod.rs

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -187,34 +187,35 @@ where
187187
let pm_address = this.controller_deployer_address;
188188
let pm_private_key = this.controller_deployer_private_key;
189189

190-
// ====================== CONTROLLER DEPLOYMENT ======================
191-
let state = this.state()?;
192-
let is_controller_deployed = state.class_hash_of_contract(contract_address)?.is_some();
193-
194-
if !is_controller_deployed {
195-
debug!(controller = %contract_address, "Controller not yet deployed");
196-
197-
let deploy_tx = craft_deploy_cartridge_controller_tx(
198-
&this.api_client,
199-
contract_address,
200-
pm_address,
201-
pm_private_key,
202-
this.backend.chain_spec.id(),
203-
this.nonce(pm_address)?.unwrap_or_default(),
204-
)
205-
.await?;
206-
207-
if let Some(tx) = deploy_tx {
208-
debug!(controller = %contract_address, tx = format!("{:#x}", tx.hash), "Inserting Controller deployment transaction");
209-
this.pool.add_transaction(tx).await?;
210-
this.block_producer.force_mine();
211-
}
212-
}
213-
// ===================================================================
190+
// // ====================== CONTROLLER DEPLOYMENT ======================
191+
// let state = this.state()?;
192+
// let is_controller_deployed =
193+
// state.class_hash_of_contract(contract_address)?.is_some();
194+
195+
// if !is_controller_deployed {
196+
// debug!(controller = %contract_address, "Controller not yet deployed");
197+
198+
// let deploy_tx = craft_deploy_cartridge_controller_tx(
199+
// &this.api_client,
200+
// contract_address,
201+
// pm_address,
202+
// pm_private_key,
203+
// this.backend.chain_spec.id(),
204+
// this.nonce(pm_address)?.unwrap_or_default(),
205+
// )
206+
// .await?;
207+
208+
// if let Some(tx) = deploy_tx {
209+
// debug!(controller = %contract_address, tx = format!("{:#x}", tx.hash),
210+
// "Inserting Controller deployment transaction"); this.pool.
211+
// add_transaction(tx).await?; this.block_producer.force_mine();
212+
// }
213+
// }
214+
// // ===================================================================
214215

215216
let entry_point_selector = outside_execution.selector();
216217
let mut calldata = outside_execution.as_felts();
217-
calldata.extend(signature);
218+
calldata.extend(signature.clone());
218219

219220
let mut call: Call = Call { contract_address, entry_point_selector, calldata };
220221
let mut user_address: Felt = contract_address.into();
@@ -257,7 +258,6 @@ where
257258
_ => FeeMode::Sponsored { tip: Default::default() },
258259
};
259260

260-
261261
let invoke = RawInvokeParameters {
262262
user_address,
263263
gas_token: None,
@@ -274,12 +274,9 @@ where
274274
parameters: ExecutionParameters::V1 { fee_mode, time_bounds: None },
275275
};
276276

277-
let response = this
278-
.paymaster_client
279-
.execute_raw_transaction(request)
280-
.await
281-
.map_err(|e| CartridgeApiError::PaymasterExecutionFailed {
282-
reason: e.to_string(),
277+
let response =
278+
this.paymaster_client.execute_raw_transaction(request).await.map_err(|e| {
279+
CartridgeApiError::PaymasterExecutionFailed { reason: e.to_string() }
283280
})?;
284281

285282
Ok(AddInvokeTransactionResponse { transaction_hash: response.transaction_hash })

crates/rpc/rpc-server/src/middleware/cartridge.rs

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use katana_primitives::execution::Call;
1818
use katana_primitives::fee::{AllResourceBoundsMapping, ResourceBoundsMapping};
1919
use katana_primitives::ContractAddress;
2020
use katana_provider::{ProviderFactory, ProviderRO};
21-
use katana_rpc_client::starknet::StarknetApiError;
21+
use katana_rpc_api::error::starknet::StarknetApiError;
2222
use katana_rpc_types::broadcasted::{BroadcastedTx, BroadcastedTxWithChainId};
2323
use katana_rpc_types::{BroadcastedInvokeTx, FeeEstimate};
2424
use serde::Deserialize;
@@ -99,9 +99,10 @@ where
9999
vrf_service: Option<VrfService>,
100100
}
101101

102-
impl<Pool, PP, PF> ControllerDeployment<Pool, PP, PF>
102+
impl<Pool, PoolTx, PP, PF> ControllerDeployment<Pool, PP, PF>
103103
where
104-
Pool: TransactionPool,
104+
Pool: TransactionPool<Transaction = PoolTx> + Send + Sync + 'static,
105+
PoolTx: From<BroadcastedTxWithChainId>,
105106
PP: PendingBlockProvider,
106107
PF: ProviderFactory,
107108
<PF as ProviderFactory>::Provider: ProviderRO,
@@ -110,9 +111,18 @@ where
110111
starknet: StarknetApi<Pool, PP, PF>,
111112
cartridge_api: CartridgeApiClient,
112113
paymaster_client: HttpClient,
114+
deployer_address: ContractAddress,
115+
deployer_private_key: SigningKey,
113116
vrf_service: Option<VrfService>,
114117
) -> Self {
115-
Self { starknet, cartridge_api, paymaster_client, vrf_service }
118+
Self {
119+
starknet,
120+
cartridge_api,
121+
paymaster_client,
122+
deployer_address,
123+
deployer_private_key,
124+
vrf_service,
125+
}
116126
}
117127

118128
pub async fn handle_estimate_fee_inner(
@@ -170,6 +180,31 @@ where
170180
}
171181
}
172182

183+
pub async fn handle_execute_outside_inner(
184+
&self,
185+
address: ContractAddress,
186+
) -> Result<(), Error> {
187+
// check if the address has already been deployed.
188+
match self.starknet.class_hash_at_address(block_id, address).await {
189+
// attempt to deploy if the address belongs to a Controller account
190+
Err(StarknetApiError::ContractNotFound) => {
191+
let mut deployer_nonce =
192+
self.starknet.nonce_at(block_id, self.deployer_address).await.unwrap();
193+
194+
let result = self.get_controller_deployment_tx(address, deployer_nonce).await?;
195+
196+
// none means the address is not a Controller
197+
if let Some(tx) = result {
198+
let _ = self.starknet.add_invoke_tx(tx).await.unwrap();
199+
}
200+
}
201+
Err(e) => panic!("{}", e.to_string()),
202+
Ok(..) => {}
203+
}
204+
205+
Ok(())
206+
}
207+
173208
async fn get_controller_deployment_tx(
174209
&self,
175210
address: ContractAddress,
@@ -241,18 +276,19 @@ where
241276
return self.service.call(request).await;
242277
};
243278

244-
let updated_txs = self
245-
.inner
246-
.handle_estimate_fee_inner(params.block_id, params.txs)
247-
.await
248-
.unwrap_or_default();
249-
250279
// if `handle_estimate_fees` has added some new transactions at the
251280
// beginning of updated_txs, we have to remove
252281
// extras results from estimate_fees to be
253282
// sure to return the same number of result than the number
254283
// of transactions in the request.
255284
let nb_of_txs = params.txs.len();
285+
286+
let updated_txs = self
287+
.inner
288+
.handle_estimate_fee_inner(params.block_id, params.txs)
289+
.await
290+
.unwrap_or_default();
291+
256292
let nb_of_extra_txs = updated_txs.len() - nb_of_txs;
257293

258294
let new_request = build_new_estimate_fee_request(&request, &params, updated_txs);
@@ -274,7 +310,7 @@ where
274310
);
275311

276312
// TODO: restore the real response
277-
return Self::build_no_fee_response(&request, nb_of_txs);
313+
return build_no_fee_response(&request, nb_of_txs);
278314
}
279315
}
280316

@@ -305,10 +341,10 @@ where
305341
request: Request<'a>,
306342
) -> impl Future<Output = Self::MethodResponse> + Send + 'a {
307343
async move {
308-
if request.method_name() == "starknet_estimateFee" {
309-
self.handle_estimate_fee(request).await
310-
} else {
311-
self.service.call(request).await
344+
match request.method_name() {
345+
"starknet_estimateFee" => self.handle_estimate_fee(request).await,
346+
"addExecuteOutsideTransaction" | "addExecuteFromOutside" => todo!(),
347+
_ => self.service.call(request).await,
312348
}
313349
}
314350
}

0 commit comments

Comments
 (0)