Skip to content

Commit

Permalink
Fix self deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
broody committed Oct 14, 2024
1 parent 617fd0a commit 71f4cae
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/account-wasm/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl CartridgeAccount {
let controller = Controller::new(
app_id,
username.clone(),
CONTROLLERS[&Version::V1_0_4].hash,
CONTROLLERS[&Version::LATEST].hash,
rpc_url,
signer.try_into()?,
address.0,
Expand Down
9 changes: 9 additions & 0 deletions packages/account_sdk/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,15 @@ impl Controller {
self.nonce = new_nonce;
retry_count += 1;
continue;
} else if data.contains(&format!("{:x} is not deployed.", self.address))
{
let balance = self.eth_balance().await?;
let mut fee_estimate = self.deploy().estimate_fee().await?;
fee_estimate.overall_fee += WEBAUTHN_GAS * fee_estimate.gas_price;
return Err(ControllerError::NotDeployed {
fee_estimate: Box::new(fee_estimate),
balance,
});
}
}
_ => {}
Expand Down
46 changes: 41 additions & 5 deletions packages/account_sdk/src/controller_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ use crate::{
abigen::erc_20::Erc20,
artifacts::{Version, CONTROLLERS},
controller::Controller,
errors::ControllerError,
signers::Signer,
tests::{
account::FEE_TOKEN_ADDRESS, runners::katana::KatanaRunner,
transaction_waiter::TransactionWaiter,
},
};
use cainome::cairo_serde::{ContractAddress, U256};
use starknet::{accounts::Account, macros::felt, providers::Provider, signers::SigningKey};
use starknet::{macros::felt, providers::Provider, signers::SigningKey};
use starknet_crypto::Felt;

#[tokio::test]
async fn test_deploy_controller() {
let runner = KatanaRunner::load();
dbg!(runner.declare_controller(Version::V1_0_4).await);
dbg!(runner.declare_controller(Version::LATEST).await);

// Create signers
let owner = Signer::Starknet(SigningKey::from_secret_scalar(felt!(
Expand All @@ -35,7 +36,7 @@ async fn test_deploy_controller() {
let address = Controller::new(
"app_id".to_string(),
username.clone(),
CONTROLLERS[&Version::V1_0_4].hash,
CONTROLLERS[&Version::LATEST].hash,
runner.rpc_url.clone(),
owner.clone(),
Felt::ZERO,
Expand All @@ -47,7 +48,7 @@ async fn test_deploy_controller() {
let controller = Controller::new(
"app_id".to_string(),
username.clone(),
CONTROLLERS[&Version::V1_0_4].hash,
CONTROLLERS[&Version::LATEST].hash,
runner.rpc_url.clone(),
owner.clone(),
address,
Expand All @@ -70,13 +71,48 @@ async fn test_deploy_controller() {
.unwrap();

// Verify the deployment
let deployed_address = controller.address();
let deployed_address = controller.address;
assert_eq!(
deployed_address, address,
"Deployed address doesn't match expected address"
);
}

#[tokio::test]
async fn test_controller_not_deployed() {
let runner = KatanaRunner::load();
let signer = Signer::new_starknet_random();
let _ = runner
.deploy_controller("deployed".to_string(), signer.clone(), Version::LATEST)
.await;
let chain_id = runner.client().chain_id().await.unwrap();

// Create a controller that is not deployed
let undeployed_controller = Controller::new(
"app_id".to_string(),
"testuser".to_string(),
CONTROLLERS[&Version::LATEST].hash,
runner.rpc_url.clone(),
signer.clone(),
felt!("0xdeadbeef"),
chain_id,
);

let recipient = ContractAddress(felt!("0x18301129"));
let amount = U256 { low: 0, high: 0 };
let erc20 = Erc20::new(*FEE_TOKEN_ADDRESS, &undeployed_controller);
let tx1 = erc20.transfer_getcall(&recipient, &amount);
let result = undeployed_controller
.estimate_invoke_fee(vec![tx1.clone()])
.await;

// Assert that the result is a NotDeployed error
match result {
Err(ControllerError::NotDeployed { .. }) => {}
_ => panic!("Expected NotDeployed error, got: {:?}", result),
}
}

#[tokio::test]
async fn test_controller_nonce_mismatch_recovery() {
let username = "testuser".to_string();
Expand Down
4 changes: 3 additions & 1 deletion packages/keychain/src/components/DeployController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ export function DeployController({
/>
</Content>

<Footer>
<Footer
hideTxSummary
>
{error ? (
<ErrorAlert
title="Something went wrong"
Expand Down

0 comments on commit 71f4cae

Please sign in to comment.