Skip to content

Commit

Permalink
feat(mempool_test_utils): util to generate deploy account txs
Browse files Browse the repository at this point in the history
  • Loading branch information
yair-starkware committed Dec 19, 2024
1 parent 3a078d8 commit c81c661
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions crates/mempool_test_utils/src/starknet_api_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ type SharedNonceManager = Rc<RefCell<NonceManager>>;
/// let undeployed_account = tx_generator.account_with_id(2).account;
/// // Generate a transfer to fund the undeployed account.
/// let transfer_tx = tx_generator.account_with_id_mut(0).generate_transfer(&undeployed_account);
/// // Generate a deploy account transaction for the undeployed account.
/// let deploy_account_tx = tx_generator.account_with_id_mut(2).generate_deploy_account();
/// // Mark the undeployed account as deployed.
/// tx_generator.account_with_id_mut(2).mark_deployed();
/// ```
// Note: when moving this to starknet api crate, see if blockifier's
// [blockifier::transaction::test_utils::FaultyAccountTxCreatorArgs] can be made to use this.
Expand Down Expand Up @@ -278,6 +282,7 @@ pub struct AccountTransactionGenerator {
pub account: Contract,
pub is_deployed: bool,
nonce_manager: SharedNonceManager,
contract_address_salt: ContractAddressSalt,
}

impl AccountTransactionGenerator {
Expand Down Expand Up @@ -366,6 +371,27 @@ impl AccountTransactionGenerator {
rpc_invoke_tx(invoke_args)
}

pub fn generate_deploy_account(&mut self) -> RpcTransaction {
assert!(
!self.is_deployed,
"Cannot deploy an already deployed account: the first transaction of every account \
must be a deploy account transaction."
);
let nonce = self.next_nonce();
assert_ne!(nonce, nonce!(0), "The deploy account tx should have nonce 0.");
let deploy_account_args = deploy_account_tx_args!(
class_hash: self.account.class_hash(),
resource_bounds: test_valid_resource_bounds(),
contract_address_salt: ContractAddressSalt(self.contract_address_salt.0)
);
rpc_deploy_account_tx(deploy_account_args)
}

pub fn mark_deployed(&mut self) {
// TODO: Assert that the account is actually deployed once we have a StateReader.
self.is_deployed = true;
}

pub fn sender_address(&self) -> ContractAddress {
self.account.sender_address
}
Expand Down Expand Up @@ -395,6 +421,7 @@ impl AccountTransactionGenerator {
account: Contract::new_for_account(account, &default_deploy_account_tx),
nonce_manager,
is_deployed,
contract_address_salt,
};
// Bump the account nonce after transaction creation.
account_tx_generator.next_nonce();
Expand Down

0 comments on commit c81c661

Please sign in to comment.