From aea3726c88b4e881bcd0f4a60ff32a730f200938 Mon Sep 17 00:00:00 2001 From: perekopskiy <53865202+perekopskiy@users.noreply.github.com> Date: Mon, 26 Aug 2024 18:05:04 +0300 Subject: [PATCH] fix(api): `tx.gas_price` field (#2734) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## What ❔ Fixes `tx.gas_price` field for legacy and EIP-2930 transactions. ## Why ❔ Follow the [spec](https://ethereum.github.io/execution-apis/api-documentation/) ## Checklist - [ ] PR title corresponds to the body of PR (we generate changelog entries from PRs). - [ ] Tests for the changes have been added / updated. - [ ] Documentation comments have been added / updated. - [ ] Code has been formatted via `zk fmt` and `zk lint`. --- .../lib/dal/src/models/storage_transaction.rs | 19 ++++++++++++++----- .../ts-integration/tests/api/web3.test.ts | 6 ++++-- .../src/commands/database/reset.rs | 2 +- .../zk_supervisor/src/commands/test/prover.rs | 2 +- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/core/lib/dal/src/models/storage_transaction.rs b/core/lib/dal/src/models/storage_transaction.rs index aca93ee8c5a..9f67e9025e0 100644 --- a/core/lib/dal/src/models/storage_transaction.rs +++ b/core/lib/dal/src/models/storage_transaction.rs @@ -508,6 +508,19 @@ impl StorageApiTransaction { .signature .and_then(|signature| PackedEthSignature::deserialize_packed(&signature).ok()); + // For legacy and EIP-2930 transactions it is gas price willing to be paid by the sender in wei. + // For other transactions it should be the effective gas price if transaction is included in block, + // otherwise this value should be set equal to the max fee per gas. + let gas_price = match self.tx_format { + None | Some(0) | Some(1) => self + .max_fee_per_gas + .clone() + .unwrap_or_else(BigDecimal::zero), + _ => self + .effective_gas_price + .or_else(|| self.max_fee_per_gas.clone()) + .unwrap_or_else(BigDecimal::zero), + }; let mut tx = api::Transaction { hash: H256::from_slice(&self.tx_hash), nonce: U256::from(self.nonce.unwrap_or(0) as u64), @@ -517,11 +530,7 @@ impl StorageApiTransaction { from: Some(Address::from_slice(&self.initiator_address)), to: Some(serde_json::from_value(self.execute_contract_address).unwrap()), value: bigdecimal_to_u256(self.value), - gas_price: Some(bigdecimal_to_u256( - self.effective_gas_price - .or_else(|| self.max_fee_per_gas.clone()) - .unwrap_or_else(BigDecimal::zero), - )), + gas_price: Some(bigdecimal_to_u256(gas_price)), gas: bigdecimal_to_u256(self.gas_limit.unwrap_or_else(BigDecimal::zero)), input: serde_json::from_value(self.calldata).expect("incorrect calldata in Postgres"), v: signature.as_ref().map(|s| U64::from(s.v())), diff --git a/core/tests/ts-integration/tests/api/web3.test.ts b/core/tests/ts-integration/tests/api/web3.test.ts index c6d0ae40a43..b20e9d1e37d 100644 --- a/core/tests/ts-integration/tests/api/web3.test.ts +++ b/core/tests/ts-integration/tests/api/web3.test.ts @@ -249,14 +249,16 @@ describe('web3 API compatibility tests', () => { test('Should check transactions from API / Legacy tx', async () => { const LEGACY_TX_TYPE = 0; + const gasPrice = (await alice._providerL2().getGasPrice()) * 2n; const legacyTx = await alice.sendTransaction({ type: LEGACY_TX_TYPE, - to: alice.address + to: alice.address, + gasPrice }); await legacyTx.wait(); const legacyApiReceipt = await alice.provider.getTransaction(legacyTx.hash); - expect(legacyApiReceipt.gasPrice).toBeLessThanOrEqual(legacyTx.gasPrice!); + expect(legacyApiReceipt.gasPrice).toEqual(gasPrice); }); test('Should check transactions from API / EIP1559 tx', async () => { diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/database/reset.rs b/zk_toolbox/crates/zk_supervisor/src/commands/database/reset.rs index d25f2a8cd54..88f2069bf3a 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/database/reset.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/database/reset.rs @@ -26,7 +26,7 @@ pub async fn run(shell: &Shell, args: DatabaseCommonArgs) -> anyhow::Result<()> let dals = get_dals(shell, &args.selected_dals)?; for dal in dals { - logger::info(&msg_database_loading(MSG_DATABASE_RESET_GERUND, &dal.path)); + logger::info(msg_database_loading(MSG_DATABASE_RESET_GERUND, &dal.path)); reset_database(shell, ecoseystem_config.link_to_code.clone(), dal).await?; } diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/test/prover.rs b/zk_toolbox/crates/zk_supervisor/src/commands/test/prover.rs index 3d8131a180c..4e9c4fc2528 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/test/prover.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/test/prover.rs @@ -6,7 +6,7 @@ use crate::messages::MSG_PROVER_TEST_SUCCESS; pub fn run(shell: &Shell) -> anyhow::Result<()> { let ecosystem = EcosystemConfig::from_file(shell)?; - let _dir_guard = shell.push_dir(&ecosystem.link_to_code.join("prover")); + let _dir_guard = shell.push_dir(ecosystem.link_to_code.join("prover")); Cmd::new(cmd!(shell, "cargo test --release --workspace --locked")) .with_force_run()