Skip to content

Commit

Permalink
chore: define creator for executable transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Sep 4, 2024
1 parent ecc87c2 commit 12ec5d4
Showing 1 changed file with 78 additions and 2 deletions.
80 changes: 78 additions & 2 deletions crates/starknet_api/src/executable_transaction.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
use serde::{Deserialize, Serialize};

use crate::contract_class::ClassInfo;
use crate::core::{ClassHash, ContractAddress, Nonce};
use crate::core::{
calculate_contract_address,
ChainId,
ClassHash,
CompiledClassHash,
ContractAddress,
Nonce,
};
use crate::data_availability::DataAvailabilityMode;
use crate::rpc_transaction::RpcTransaction;
use crate::rpc_transaction::{RpcDeployAccountTransaction, RpcInvokeTransaction, RpcTransaction};
use crate::transaction::{
AccountDeploymentData,
Calldata,
Expand All @@ -12,9 +19,11 @@ use crate::transaction::{
PaymasterData,
Tip,
TransactionHash,
TransactionHasher,
TransactionSignature,
TransactionVersion,
};
use crate::StarknetApiError;

macro_rules! implement_inner_tx_getter_calls {
($(($field:ident, $field_type:ty)),*) => {
Expand Down Expand Up @@ -136,6 +145,34 @@ pub struct DeclareTransaction {
pub class_info: ClassInfo,
}

impl DeclareTransaction {
pub fn create(
declare_tx: crate::transaction::DeclareTransaction,
class_info: ClassInfo,
chain_id: &ChainId,
) -> Result<Self, StarknetApiError> {
let tx_hash = declare_tx.calculate_transaction_hash(chain_id, &declare_tx.version())?;
Ok(Self { tx: declare_tx, tx_hash, class_info })
}

/// Validates that the compiled class hash of the compiled contract class matches the supplied
/// compiled class hash.
/// Relevant only for version 3 transactions.
pub fn validate_compiled_class_hash(&self) -> bool {
let supplied_compiled_class_hash = match &self.tx {
crate::transaction::DeclareTransaction::V3(tx) => tx.compiled_class_hash,
crate::transaction::DeclareTransaction::V2(tx) => tx.compiled_class_hash,
crate::transaction::DeclareTransaction::V1(_)
| crate::transaction::DeclareTransaction::V0(_) => return true,
};

let casm_contract_class = &self.class_info.casm_contract_class;
let compiled_class_hash = CompiledClassHash(casm_contract_class.compiled_class_hash());

compiled_class_hash == supplied_compiled_class_hash
}
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct DeployAccountTransaction {
pub tx: crate::transaction::DeployAccountTransaction,
Expand All @@ -157,6 +194,29 @@ impl DeployAccountTransaction {
pub fn tx(&self) -> &crate::transaction::DeployAccountTransaction {
&self.tx
}

pub fn create(
deploy_account_tx: crate::transaction::DeployAccountTransaction,
chain_id: &ChainId,
) -> Result<Self, StarknetApiError> {
let contract_address = calculate_contract_address(
deploy_account_tx.contract_address_salt(),
deploy_account_tx.class_hash(),
&deploy_account_tx.constructor_calldata(),
ContractAddress::default(),
)?;
let tx_hash =
deploy_account_tx.calculate_transaction_hash(chain_id, &deploy_account_tx.version())?;
Ok(Self { tx: deploy_account_tx, tx_hash, contract_address })
}

pub fn from_rpc_tx(
rpc_tx: RpcDeployAccountTransaction,
chain_id: &ChainId,
) -> Result<Self, StarknetApiError> {
let deploy_account_tx: crate::transaction::DeployAccountTransaction = rpc_tx.into();
Self::create(deploy_account_tx, chain_id)
}
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
Expand All @@ -178,4 +238,20 @@ impl InvokeTransaction {
pub fn tx(&self) -> &crate::transaction::InvokeTransaction {
&self.tx
}

pub fn create(
invoke_tx: crate::transaction::InvokeTransaction,
chain_id: &ChainId,
) -> Result<Self, StarknetApiError> {
let tx_hash = invoke_tx.calculate_transaction_hash(chain_id, &invoke_tx.version())?;
Ok(Self { tx: invoke_tx, tx_hash })
}

pub fn from_rpc_tx(
rpc_tx: RpcInvokeTransaction,
chain_id: &ChainId,
) -> Result<Self, StarknetApiError> {
let invoke_tx: crate::transaction::InvokeTransaction = rpc_tx.into();
Self::create(invoke_tx, chain_id)
}
}

0 comments on commit 12ec5d4

Please sign in to comment.