diff --git a/crates/papyrus_protobuf/src/converters/receipt.rs b/crates/papyrus_protobuf/src/converters/receipt.rs index d4ff3771f7..c71c9614f8 100644 --- a/crates/papyrus_protobuf/src/converters/receipt.rs +++ b/crates/papyrus_protobuf/src/converters/receipt.rs @@ -50,6 +50,28 @@ impl TryFrom for TransactionOutput { } } +impl From for protobuf::Receipt { + fn from(value: TransactionOutput) -> Self { + match value { + TransactionOutput::Invoke(invoke) => { + protobuf::Receipt { r#type: Some(protobuf::receipt::Type::Invoke(invoke.into())) } + } + TransactionOutput::L1Handler(l1_handler) => protobuf::Receipt { + r#type: Some(protobuf::receipt::Type::L1Handler(l1_handler.into())), + }, + TransactionOutput::Declare(declare) => { + protobuf::Receipt { r#type: Some(protobuf::receipt::Type::Declare(declare.into())) } + } + TransactionOutput::Deploy(deploy) => protobuf::Receipt { + r#type: Some(protobuf::receipt::Type::DeprecatedDeploy(deploy.into())), + }, + TransactionOutput::DeployAccount(deploy_account) => protobuf::Receipt { + r#type: Some(protobuf::receipt::Type::DeployAccount(deploy_account.into())), + }, + } + } +} + // The output will have an empty events vec impl TryFrom for DeployAccountTransactionOutput { type Error = ProtobufConversionError; @@ -82,6 +104,24 @@ impl TryFrom for DeployAccountTransactionOutpu } } +impl From for protobuf::receipt::DeployAccount { + /// The returned price_unit isn't correct. + /// It can be fixed by calling set_price_unit_based_on_transaction + fn from(value: DeployAccountTransactionOutput) -> Self { + let common = create_proto_receipt_common_from_txn_output_fields( + value.actual_fee, + value.messages_sent, + value.execution_resources, + value.execution_status, + ); + + protobuf::receipt::DeployAccount { + common: Some(common), + contract_address: Some(StarkFelt::from(value.contract_address).into()), + } + } +} + // The output will have an empty events vec impl TryFrom for DeployTransactionOutput { type Error = ProtobufConversionError; @@ -114,6 +154,24 @@ impl TryFrom for DeployTransactionOutput { } } +impl From for protobuf::receipt::Deploy { + /// The returned price_unit isn't correct. + /// It can be fixed by calling set_price_unit_based_on_transaction + fn from(value: DeployTransactionOutput) -> Self { + let common = create_proto_receipt_common_from_txn_output_fields( + value.actual_fee, + value.messages_sent, + value.execution_resources, + value.execution_status, + ); + + protobuf::receipt::Deploy { + common: Some(common), + contract_address: Some(StarkFelt::from(value.contract_address).into()), + } + } +} + // The output will have an empty events vec impl TryFrom for DeclareTransactionOutput { type Error = ProtobufConversionError; @@ -127,6 +185,21 @@ impl TryFrom for DeclareTransactionOutput { } } +impl From for protobuf::receipt::Declare { + /// The returned price_unit isn't correct. + /// It can be fixed by calling set_price_unit_based_on_transaction + fn from(value: DeclareTransactionOutput) -> Self { + let common = create_proto_receipt_common_from_txn_output_fields( + value.actual_fee, + value.messages_sent, + value.execution_resources, + value.execution_status, + ); + + protobuf::receipt::Declare { common: Some(common) } + } +} + // The output will have an empty events vec impl TryFrom for InvokeTransactionOutput { type Error = ProtobufConversionError; @@ -140,6 +213,21 @@ impl TryFrom for InvokeTransactionOutput { } } +impl From for protobuf::receipt::Invoke { + /// The returned price_unit isn't correct. + /// It can be fixed by calling set_price_unit_based_on_transaction + fn from(value: InvokeTransactionOutput) -> Self { + let common = create_proto_receipt_common_from_txn_output_fields( + value.actual_fee, + value.messages_sent, + value.execution_resources, + value.execution_status, + ); + + protobuf::receipt::Invoke { common: Some(common) } + } +} + // The output will have an empty events vec impl TryFrom for L1HandlerTransactionOutput { type Error = ProtobufConversionError; @@ -153,6 +241,21 @@ impl TryFrom for L1HandlerTransactionOutput { } } +impl From for protobuf::receipt::L1Handler { + /// The returned price_unit isn't correct. + /// It can be fixed by calling set_price_unit_based_on_transaction + fn from(value: L1HandlerTransactionOutput) -> Self { + let common = create_proto_receipt_common_from_txn_output_fields( + value.actual_fee, + value.messages_sent, + value.execution_resources, + value.execution_status, + ); + + protobuf::receipt::L1Handler { common: Some(common), msg_hash: None } + } +} + type ProtobufBuiltinCounter = protobuf::receipt::execution_resources::BuiltinCounter; impl TryFrom for HashMap { @@ -350,3 +453,27 @@ fn parse_common_receipt_fields( )?)?; Ok((actual_fee, messages_sent, execution_status, execution_resources)) } + +fn create_proto_receipt_common_from_txn_output_fields( + actual_fee: Fee, + messages_sent: Vec, + execution_resources: ExecutionResources, + execution_status: TransactionExecutionStatus, +) -> protobuf::receipt::Common { + let actual_fee = StarkFelt::from(actual_fee).into(); + let messages_sent = messages_sent.into_iter().map(protobuf::MessageToL1::from).collect(); + let execution_resources = execution_resources.into(); + let revert_reason = + if let TransactionExecutionStatus::Reverted(reverted_status) = execution_status { + Some(reverted_status.revert_reason) + } else { + None + }; + protobuf::receipt::Common { + actual_fee: Some(actual_fee), + price_unit: 0, + messages_sent, + execution_resources: Some(execution_resources), + revert_reason, + } +} diff --git a/crates/papyrus_protobuf/src/converters/transaction.rs b/crates/papyrus_protobuf/src/converters/transaction.rs index eb75fccca5..0e7f5fc477 100644 --- a/crates/papyrus_protobuf/src/converters/transaction.rs +++ b/crates/papyrus_protobuf/src/converters/transaction.rs @@ -1101,3 +1101,37 @@ impl From for protobuf::transaction::Txn { } } } + +#[allow(dead_code)] +pub fn set_price_unit_based_on_transaction( + receipt: &mut protobuf::Receipt, + transaction: &protobuf::Transaction, +) { + let price_unit = match &transaction.txn { + Some(protobuf::transaction::Txn::DeclareV1(_)) => protobuf::PriceUnit::Wei, + Some(protobuf::transaction::Txn::DeclareV2(_)) => protobuf::PriceUnit::Wei, + Some(protobuf::transaction::Txn::DeclareV3(_)) => protobuf::PriceUnit::Fri, + Some(protobuf::transaction::Txn::Deploy(_)) => protobuf::PriceUnit::Wei, + Some(protobuf::transaction::Txn::DeployAccountV1(_)) => protobuf::PriceUnit::Wei, + Some(protobuf::transaction::Txn::DeployAccountV3(_)) => protobuf::PriceUnit::Fri, + Some(protobuf::transaction::Txn::InvokeV1(_)) => protobuf::PriceUnit::Wei, + Some(protobuf::transaction::Txn::InvokeV3(_)) => protobuf::PriceUnit::Fri, + Some(protobuf::transaction::Txn::L1Handler(_)) => protobuf::PriceUnit::Wei, + _ => return, + }; + let Some(ref mut receipt_type) = receipt.r#type else { + return; + }; + + let common = match receipt_type { + protobuf::receipt::Type::Invoke(invoke) => invoke.common.as_mut(), + protobuf::receipt::Type::L1Handler(l1_handler) => l1_handler.common.as_mut(), + protobuf::receipt::Type::Declare(declare) => declare.common.as_mut(), + protobuf::receipt::Type::DeprecatedDeploy(deploy) => deploy.common.as_mut(), + protobuf::receipt::Type::DeployAccount(deploy_account) => deploy_account.common.as_mut(), + }; + + if let Some(common) = common { + common.price_unit = price_unit.into(); + } +}