Skip to content

Commit 5063ca5

Browse files
Merge pull request #724 from JeneaVranceanu/feat/effective-gas-field
feat: introduced effectiveGasPrice
2 parents 496a9fd + 25359e7 commit 5063ca5

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

Sources/Web3Core/Structure/Transaction/TransactionReceipt.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ public struct TransactionReceipt {
1616
public var contractAddress: EthereumAddress?
1717
public var cumulativeGasUsed: BigUInt
1818
public var gasUsed: BigUInt
19+
public var effectiveGasPrice: BigUInt
1920
public var logs: [EventLog]
2021
public var status: TXStatus
2122
public var logsBloom: EthereumBloomFilter?
2223

2324
static func notProcessed(transactionHash: Data) -> TransactionReceipt {
24-
TransactionReceipt(transactionHash: transactionHash, blockHash: Data(), blockNumber: BigUInt(0), transactionIndex: BigUInt(0), contractAddress: nil, cumulativeGasUsed: BigUInt(0), gasUsed: BigUInt(0), logs: [EventLog](), status: .notYetProcessed, logsBloom: nil)
25+
TransactionReceipt(transactionHash: transactionHash, blockHash: Data(), blockNumber: 0, transactionIndex: 0, contractAddress: nil, cumulativeGasUsed: 0, gasUsed: 0, effectiveGasPrice: 0, logs: [], status: .notYetProcessed, logsBloom: nil)
2526
}
2627
}
2728

@@ -45,6 +46,7 @@ extension TransactionReceipt: Decodable {
4546
case logs
4647
case logsBloom
4748
case status
49+
case effectiveGasPrice
4850
}
4951

5052
public init(from decoder: Decoder) throws {
@@ -64,6 +66,8 @@ extension TransactionReceipt: Decodable {
6466

6567
self.gasUsed = try container.decodeHex(BigUInt.self, forKey: .gasUsed)
6668

69+
self.effectiveGasPrice = (try? container.decodeHex(BigUInt.self, forKey: .effectiveGasPrice)) ?? 0
70+
6771
let status = try? container.decodeHex(BigUInt.self, forKey: .status)
6872
switch status {
6973
case nil: self.status = .notYetProcessed
@@ -72,6 +76,10 @@ extension TransactionReceipt: Decodable {
7276
}
7377

7478
self.logs = try container.decode([EventLog].self, forKey: .logs)
79+
80+
if let hexBytes = try? container.decodeHex(Data.self, forKey: .logsBloom) {
81+
self.logsBloom = EthereumBloomFilter(hexBytes)
82+
}
7583
}
7684
}
7785

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// TransactionReceiptTests.swift
3+
//
4+
// Created by JeneaVranceanu on 10.01.2023.
5+
//
6+
7+
import Foundation
8+
import XCTest
9+
import BigInt
10+
11+
@testable import Web3Core
12+
13+
class TransactionReceiptTests: XCTestCase {
14+
15+
func testDecodeTransactionReceiptJson() throws {
16+
let transactionHash: Data = Data.fromHex("0xbe981126f05b4110d5bf4a22d474b6a7ef861ae79fc6939260bb2c3003367eed")!
17+
let blockHash: Data = Data.fromHex("0x0103a5759c39720ecd23d48281e32526ae50eaa3e651a5e8c86e47838e060cb8")!
18+
let blockNumber: BigUInt = 12
19+
let transactionIndex: BigUInt = 10
20+
let contractAddress = EthereumAddress("0xdf85ee41abbf15cdf1dbf89fb7af9a9557c5dd7e")!
21+
let cumulativeGasUsed: BigUInt = 789456132
22+
let gasUsed: BigUInt = 8857745
23+
let effectiveGasPrice: BigUInt = 123456
24+
/// This is not an EventLog decoding test so the array is empty
25+
let logs: [EventLog] = []
26+
let status = TransactionReceipt.TXStatus.ok
27+
let logsBloom = EthereumBloomFilter(12348880)!
28+
29+
let transactionJson = "{\"transactionHash\":\"\(transactionHash.toHexString().addHexPrefix())\",\"transactionIndex\":\"\(transactionIndex.hexString)\",\"blockNumber\":\"\(blockNumber.hexString)\",\"blockHash\":\"\(blockHash.toHexString().addHexPrefix())\",\"from\":\"0xdf85ee41abbf15cdf1dbf89fb7af9a9557c5dd7e\",\"to\":\"0xe22b8979739d724343bd002f9f432f5990879901\",\"cumulativeGasUsed\":\"\(cumulativeGasUsed.hexString)\",\"gasUsed\":\"\(gasUsed.hexString)\",\"contractAddress\":\"\(contractAddress.address)\",\"logs\":[],\"logsBloom\":\"\(logsBloom.bytes.toHexString().addHexPrefix())\",\"status\":\"0x1\",\"effectiveGasPrice\":\"\(effectiveGasPrice.hexString)\",\"type\":\"0x2\"}"
30+
let transactionReceipt = try JSONDecoder().decode(TransactionReceipt.self, from: transactionJson.data(using: .utf8)!)
31+
32+
XCTAssertEqual(blockHash, transactionReceipt.blockHash)
33+
XCTAssertEqual(blockNumber, transactionReceipt.blockNumber)
34+
XCTAssertEqual(transactionIndex, transactionReceipt.transactionIndex)
35+
XCTAssertEqual(contractAddress, transactionReceipt.contractAddress)
36+
XCTAssertEqual(cumulativeGasUsed, transactionReceipt.cumulativeGasUsed)
37+
XCTAssertEqual(gasUsed, transactionReceipt.gasUsed)
38+
XCTAssertEqual(effectiveGasPrice, transactionReceipt.effectiveGasPrice)
39+
XCTAssertEqual(logs.count, transactionReceipt.logs.count)
40+
XCTAssertEqual(status, transactionReceipt.status)
41+
XCTAssertEqual(logsBloom.bytes, transactionReceipt.logsBloom?.bytes)
42+
}
43+
44+
}

Tests/web3swiftTests/localTests/TransactionsTests.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,6 @@ class TransactionsTests: XCTestCase {
670670
func testGenerateDummyKeystore() throws {
671671
let keystore = try! EthereumKeystoreV3.init(password: "web3swift")
672672
let dump = try! keystore!.serialize()
673-
let jsonString = String.init(data: dump!, encoding: .ascii)
674-
673+
XCTAssertNotNil(String(data: dump!, encoding: .ascii))
675674
}
676675
}

0 commit comments

Comments
 (0)