Skip to content

Commit

Permalink
chore: add impl from rpc tx to snapi tx (#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware authored Sep 3, 2024
1 parent 8b1fae1 commit ecfcd2a
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions crates/starknet_api/src/rpc_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ use crate::transaction::{
AllResourceBounds,
Calldata,
ContractAddressSalt,
DeclareTransaction,
DeclareTransactionV3,
DeployAccountTransaction,
DeployAccountTransactionV3,
InvokeTransaction,
InvokeTransactionV3,
PaymasterData,
Resource,
Tip,
Transaction,
TransactionSignature,
};
use crate::StarknetApiError;
Expand Down Expand Up @@ -84,6 +91,16 @@ impl RpcTransaction {
}
}

impl From<RpcTransaction> for Transaction {
fn from(rpc_transaction: RpcTransaction) -> Self {
match rpc_transaction {
RpcTransaction::Declare(tx) => Transaction::Declare(tx.into()),
RpcTransaction::DeployAccount(tx) => Transaction::DeployAccount(tx.into()),
RpcTransaction::Invoke(tx) => Transaction::Invoke(tx.into()),
}
}
}

/// A RPC declare transaction.
///
/// This transaction is equivalent to the component DECLARE_TXN in the
Expand All @@ -98,6 +115,14 @@ pub enum RpcDeclareTransaction {
V3(RpcDeclareTransactionV3),
}

impl From<RpcDeclareTransaction> for DeclareTransaction {
fn from(rpc_declare_transaction: RpcDeclareTransaction) -> Self {
match rpc_declare_transaction {
RpcDeclareTransaction::V3(tx) => DeclareTransaction::V3(tx.into()),
}
}
}

/// A RPC deploy account transaction.
///
/// This transaction is equivalent to the component DEPLOY_ACCOUNT_TXN in the
Expand All @@ -111,6 +136,14 @@ pub enum RpcDeployAccountTransaction {
V3(RpcDeployAccountTransactionV3),
}

impl From<RpcDeployAccountTransaction> for DeployAccountTransaction {
fn from(rpc_deploy_account_transaction: RpcDeployAccountTransaction) -> Self {
match rpc_deploy_account_transaction {
RpcDeployAccountTransaction::V3(tx) => DeployAccountTransaction::V3(tx.into()),
}
}
}

/// A RPC invoke transaction.
///
/// This transaction is equivalent to the component INVOKE_TXN in the
Expand All @@ -124,6 +157,14 @@ pub enum RpcInvokeTransaction {
V3(RpcInvokeTransactionV3),
}

impl From<RpcInvokeTransaction> for InvokeTransaction {
fn from(rpc_invoke_tx: RpcInvokeTransaction) -> Self {
match rpc_invoke_tx {
RpcInvokeTransaction::V3(tx) => InvokeTransaction::V3(tx.into()),
}
}
}

/// A declare transaction of a Cairo-v1 contract class that can be added to Starknet through the
/// RPC.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
Expand All @@ -143,6 +184,25 @@ pub struct RpcDeclareTransactionV3 {
pub fee_data_availability_mode: DataAvailabilityMode,
}

impl From<RpcDeclareTransactionV3> for DeclareTransactionV3 {
fn from(tx: RpcDeclareTransactionV3) -> Self {
Self {
class_hash: ClassHash::default(), /* FIXME(yael 15/4/24): call the starknet-api
* function once ready */
resource_bounds: tx.resource_bounds.into(),
tip: tx.tip,
signature: tx.signature,
nonce: tx.nonce,
compiled_class_hash: tx.compiled_class_hash,
sender_address: tx.sender_address,
nonce_data_availability_mode: tx.nonce_data_availability_mode,
fee_data_availability_mode: tx.fee_data_availability_mode,
paymaster_data: tx.paymaster_data,
account_deployment_data: tx.account_deployment_data,
}
}
}

/// A deploy account transaction that can be added to Starknet through the RPC.
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct RpcDeployAccountTransactionV3 {
Expand All @@ -158,6 +218,23 @@ pub struct RpcDeployAccountTransactionV3 {
pub fee_data_availability_mode: DataAvailabilityMode,
}

impl From<RpcDeployAccountTransactionV3> for DeployAccountTransactionV3 {
fn from(tx: RpcDeployAccountTransactionV3) -> Self {
Self {
resource_bounds: tx.resource_bounds.into(),
tip: tx.tip,
signature: tx.signature,
nonce: tx.nonce,
class_hash: tx.class_hash,
contract_address_salt: tx.contract_address_salt,
constructor_calldata: tx.constructor_calldata,
nonce_data_availability_mode: tx.nonce_data_availability_mode,
fee_data_availability_mode: tx.fee_data_availability_mode,
paymaster_data: tx.paymaster_data,
}
}
}

/// An invoke account transaction that can be added to Starknet through the RPC.
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct RpcInvokeTransactionV3 {
Expand All @@ -173,6 +250,23 @@ pub struct RpcInvokeTransactionV3 {
pub fee_data_availability_mode: DataAvailabilityMode,
}

impl From<RpcInvokeTransactionV3> for InvokeTransactionV3 {
fn from(tx: RpcInvokeTransactionV3) -> Self {
Self {
resource_bounds: tx.resource_bounds.into(),
tip: tx.tip,
signature: tx.signature,
nonce: tx.nonce,
sender_address: tx.sender_address,
calldata: tx.calldata,
nonce_data_availability_mode: tx.nonce_data_availability_mode,
fee_data_availability_mode: tx.fee_data_availability_mode,
paymaster_data: tx.paymaster_data,
account_deployment_data: tx.account_deployment_data,
}
}
}

// The contract class in SN_API state doesn't have `contract_class_version`, not following the spec.
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct ContractClass {
Expand Down

0 comments on commit ecfcd2a

Please sign in to comment.