Skip to content

Commit

Permalink
feat: implement thin transaction converter
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadNassar1 committed May 19, 2024
1 parent 78975d5 commit 8c0da42
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
31 changes: 30 additions & 1 deletion crates/gateway/src/starknet_api_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use starknet_api::external_transaction::{
};
use starknet_api::internal_transaction::{InternalInvokeTransaction, InternalTransaction};
use starknet_api::transaction::{
Calldata, InvokeTransaction, InvokeTransactionV3, ResourceBounds, ResourceBoundsMapping,
Calldata, InvokeTransaction, InvokeTransactionV3, ResourceBounds, ResourceBoundsMapping, Tip,
TransactionSignature,
};

Expand All @@ -21,6 +21,35 @@ pub enum TransactionType {
Invoke,
}

// TODO(Mohammad): Use the StarkNet API's get_tip function.
pub fn get_tip(tx: &ExternalTransaction) -> Tip {
match tx {
ExternalTransaction::Declare(ExternalDeclareTransaction::V3(tx)) => tx.tip,
ExternalTransaction::DeployAccount(ExternalDeployAccountTransaction::V3(tx)) => tx.tip,
ExternalTransaction::Invoke(ExternalInvokeTransaction::V3(tx)) => tx.tip,
}
}

// TODO(Mohammad): Use the StarkNet API's get_nonce function.
pub fn get_nonce(tx: &ExternalTransaction) -> Nonce {
match tx {
ExternalTransaction::Declare(ExternalDeclareTransaction::V3(tx)) => tx.nonce,
ExternalTransaction::DeployAccount(ExternalDeployAccountTransaction::V3(tx)) => tx.nonce,
ExternalTransaction::Invoke(ExternalInvokeTransaction::V3(tx)) => tx.nonce,
}
}

pub fn get_sender_address(tx: &ExternalTransaction) -> ContractAddress {
match tx {
ExternalTransaction::Declare(ExternalDeclareTransaction::V3(tx)) => tx.sender_address,
// TODO(Mohammad): Add support for deploy account.
ExternalTransaction::DeployAccount(ExternalDeployAccountTransaction::V3(_)) => {
ContractAddress::default()
}
ExternalTransaction::Invoke(ExternalInvokeTransaction::V3(tx)) => tx.sender_address,
}
}

pub fn create_internal_tx_for_testing() -> InternalTransaction {
let tx = InvokeTransactionV3 {
resource_bounds: ResourceBoundsMapping::try_from(vec![
Expand Down
16 changes: 14 additions & 2 deletions crates/gateway/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ use starknet_api::external_transaction::{
};
use starknet_api::transaction::{
DeclareTransaction, DeclareTransactionV3, DeployAccountTransaction, DeployAccountTransactionV3,
InvokeTransaction, InvokeTransactionV3, ResourceBoundsMapping, TransactionHasher,
TransactionSignature,
InvokeTransaction, InvokeTransactionV3, ResourceBoundsMapping, TransactionHash,
TransactionHasher, TransactionSignature,
};
use starknet_mempool_types::mempool_types::ThinTransaction;

use crate::errors::StatefulTransactionValidatorResult;
use crate::starknet_api_test_utils::{get_nonce, get_sender_address, get_tip};

macro_rules! implement_ref_getters {
($(($member_name:ident, $member_type:ty));* $(;)?) => {
Expand All @@ -43,6 +45,16 @@ impl ExternalTransactionExt for ExternalTransaction {
);
}

pub fn external_tx_to_thin_tx(external_tx: &ExternalTransaction) -> ThinTransaction {
ThinTransaction {
tip: get_tip(external_tx),
nonce: get_nonce(external_tx),
contract_address: get_sender_address(external_tx),
// TODO(Yael): Add transaction hash calculation.
tx_hash: TransactionHash::default(),
}
}

// TODO(Arni, 1/5/2025): Remove this trait once it is implemented in StarkNet API.
pub trait ExternalTransactionExt {
fn resource_bounds(&self) -> &ResourceBoundsMapping;
Expand Down

0 comments on commit 8c0da42

Please sign in to comment.