|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use server::test_utils::assert_contains; |
| 4 | +use starknet_rs_accounts::{Account, ExecutionEncoding, SingleOwnerAccount}; |
| 5 | +use starknet_rs_contract::ContractFactory; |
| 6 | +use starknet_rs_core::types::Felt; |
| 7 | + |
| 8 | +use crate::common::background_devnet::BackgroundDevnet; |
| 9 | +use crate::common::constants; |
| 10 | +use crate::common::utils::get_simple_contract_in_sierra_and_compiled_class_hash; |
| 11 | + |
| 12 | +#[tokio::test] |
| 13 | +async fn double_deployment_not_allowed() { |
| 14 | + let devnet = BackgroundDevnet::spawn().await.unwrap(); |
| 15 | + |
| 16 | + let (signer, account_address) = devnet.get_first_predeployed_account().await; |
| 17 | + let account = SingleOwnerAccount::new( |
| 18 | + &devnet.json_rpc_client, |
| 19 | + signer, |
| 20 | + account_address, |
| 21 | + constants::CHAIN_ID, |
| 22 | + ExecutionEncoding::New, |
| 23 | + ); |
| 24 | + |
| 25 | + // declare |
| 26 | + let (contract_class, casm_hash) = get_simple_contract_in_sierra_and_compiled_class_hash(); |
| 27 | + let declaration_result = account |
| 28 | + .declare_v3(Arc::new(contract_class), casm_hash) |
| 29 | + .gas(1e7 as u64) |
| 30 | + .send() |
| 31 | + .await |
| 32 | + .unwrap(); |
| 33 | + |
| 34 | + // prepare deployment |
| 35 | + let contract_factory = ContractFactory::new(declaration_result.class_hash, account.clone()); |
| 36 | + let ctor_args = vec![Felt::ZERO]; // initial value |
| 37 | + let salt = Felt::from(10); |
| 38 | + let unique = false; |
| 39 | + |
| 40 | + // first deployment should be successful |
| 41 | + contract_factory.deploy_v3(ctor_args.clone(), salt, unique).send().await.unwrap(); |
| 42 | + |
| 43 | + // second deployment should be unsuccessful |
| 44 | + match contract_factory.deploy_v3(ctor_args, salt, unique).send().await { |
| 45 | + Err(e) => assert_contains(&format!("{e:?}"), "unavailable for deployment"), |
| 46 | + other => panic!("Unexpected result: {other:?}"), |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +#[tokio::test] |
| 51 | +async fn cannot_deploy_undeclared_class() { |
| 52 | + let devnet = BackgroundDevnet::spawn().await.unwrap(); |
| 53 | + |
| 54 | + let (signer, account_address) = devnet.get_first_predeployed_account().await; |
| 55 | + let account = SingleOwnerAccount::new( |
| 56 | + &devnet.json_rpc_client, |
| 57 | + signer, |
| 58 | + account_address, |
| 59 | + constants::CHAIN_ID, |
| 60 | + ExecutionEncoding::New, |
| 61 | + ); |
| 62 | + |
| 63 | + // skip declaration |
| 64 | + let (contract_class, _) = get_simple_contract_in_sierra_and_compiled_class_hash(); |
| 65 | + |
| 66 | + // prepare deployment |
| 67 | + let contract_factory = ContractFactory::new(contract_class.class_hash(), account.clone()); |
| 68 | + let ctor_args = vec![Felt::ZERO]; // initial value |
| 69 | + let salt = Felt::from(10); |
| 70 | + let unique = false; |
| 71 | + |
| 72 | + // deployment should fail |
| 73 | + match contract_factory.deploy_v3(ctor_args, salt, unique).send().await { |
| 74 | + Err(e) => assert_contains(&format!("{e:?}"), "not declared"), |
| 75 | + other => panic!("Unexpected result: {other:?}"), |
| 76 | + }; |
| 77 | +} |
0 commit comments