Skip to content

Commit 659ed21

Browse files
feat: added effectiveGasPrice to TransactionReceipt
1 parent 25be280 commit 659ed21

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

Sources/Web3Core/Structure/Transaction/TransactionReceipt.swift

Lines changed: 5 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
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
let logs: [EventLog] = []
25+
let status = TransactionReceipt.TXStatus.ok
26+
let logsBloom = EthereumBloomFilter(0)!
27+
28+
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\"}"
29+
let transactionReceipt = try JSONDecoder().decode(TransactionReceipt.self, from: transactionJson.data(using: .utf8)!)
30+
31+
XCTAssertEqual(blockHash, transactionReceipt.blockHash)
32+
XCTAssertEqual(blockNumber, transactionReceipt.blockNumber)
33+
XCTAssertEqual(transactionIndex, transactionReceipt.transactionIndex)
34+
XCTAssertEqual(contractAddress, transactionReceipt.contractAddress)
35+
XCTAssertEqual(cumulativeGasUsed, transactionReceipt.cumulativeGasUsed)
36+
XCTAssertEqual(gasUsed, transactionReceipt.gasUsed)
37+
XCTAssertEqual(effectiveGasPrice, transactionReceipt.effectiveGasPrice)
38+
XCTAssertEqual(logs.count, transactionReceipt.logs.count)
39+
XCTAssertEqual(status, transactionReceipt.status)
40+
XCTAssertEqual(logsBloom.bytes, transactionReceipt.logsBloom?.bytes)
41+
}
42+
43+
}

0 commit comments

Comments
 (0)