diff --git a/Sources/Web3Core/Structure/Transaction/TransactionReceipt.swift b/Sources/Web3Core/Structure/Transaction/TransactionReceipt.swift index 1c931719f..9390134a9 100644 --- a/Sources/Web3Core/Structure/Transaction/TransactionReceipt.swift +++ b/Sources/Web3Core/Structure/Transaction/TransactionReceipt.swift @@ -16,12 +16,13 @@ public struct TransactionReceipt { public var contractAddress: EthereumAddress? public var cumulativeGasUsed: BigUInt public var gasUsed: BigUInt + public var effectiveGasPrice: BigUInt public var logs: [EventLog] public var status: TXStatus public var logsBloom: EthereumBloomFilter? static func notProcessed(transactionHash: Data) -> TransactionReceipt { - 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) + TransactionReceipt(transactionHash: transactionHash, blockHash: Data(), blockNumber: 0, transactionIndex: 0, contractAddress: nil, cumulativeGasUsed: 0, gasUsed: 0, effectiveGasPrice: 0, logs: [], status: .notYetProcessed, logsBloom: nil) } } @@ -45,6 +46,7 @@ extension TransactionReceipt: Decodable { case logs case logsBloom case status + case effectiveGasPrice } public init(from decoder: Decoder) throws { @@ -64,6 +66,8 @@ extension TransactionReceipt: Decodable { self.gasUsed = try container.decodeHex(BigUInt.self, forKey: .gasUsed) + self.effectiveGasPrice = (try? container.decodeHex(BigUInt.self, forKey: .effectiveGasPrice)) ?? 0 + let status = try? container.decodeHex(BigUInt.self, forKey: .status) switch status { case nil: self.status = .notYetProcessed @@ -72,6 +76,10 @@ extension TransactionReceipt: Decodable { } self.logs = try container.decode([EventLog].self, forKey: .logs) + + if let hexBytes = try? container.decodeHex(Data.self, forKey: .logsBloom) { + self.logsBloom = EthereumBloomFilter(hexBytes) + } } } diff --git a/Tests/web3swiftTests/localTests/TransactionReceiptTests.swift b/Tests/web3swiftTests/localTests/TransactionReceiptTests.swift new file mode 100644 index 000000000..7521d613a --- /dev/null +++ b/Tests/web3swiftTests/localTests/TransactionReceiptTests.swift @@ -0,0 +1,44 @@ +// +// TransactionReceiptTests.swift +// +// Created by JeneaVranceanu on 10.01.2023. +// + +import Foundation +import XCTest +import BigInt + +@testable import Web3Core + +class TransactionReceiptTests: XCTestCase { + + func testDecodeTransactionReceiptJson() throws { + let transactionHash: Data = Data.fromHex("0xbe981126f05b4110d5bf4a22d474b6a7ef861ae79fc6939260bb2c3003367eed")! + let blockHash: Data = Data.fromHex("0x0103a5759c39720ecd23d48281e32526ae50eaa3e651a5e8c86e47838e060cb8")! + let blockNumber: BigUInt = 12 + let transactionIndex: BigUInt = 10 + let contractAddress = EthereumAddress("0xdf85ee41abbf15cdf1dbf89fb7af9a9557c5dd7e")! + let cumulativeGasUsed: BigUInt = 789456132 + let gasUsed: BigUInt = 8857745 + let effectiveGasPrice: BigUInt = 123456 + /// This is not an EventLog decoding test so the array is empty + let logs: [EventLog] = [] + let status = TransactionReceipt.TXStatus.ok + let logsBloom = EthereumBloomFilter(12348880)! + + 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\"}" + let transactionReceipt = try JSONDecoder().decode(TransactionReceipt.self, from: transactionJson.data(using: .utf8)!) + + XCTAssertEqual(blockHash, transactionReceipt.blockHash) + XCTAssertEqual(blockNumber, transactionReceipt.blockNumber) + XCTAssertEqual(transactionIndex, transactionReceipt.transactionIndex) + XCTAssertEqual(contractAddress, transactionReceipt.contractAddress) + XCTAssertEqual(cumulativeGasUsed, transactionReceipt.cumulativeGasUsed) + XCTAssertEqual(gasUsed, transactionReceipt.gasUsed) + XCTAssertEqual(effectiveGasPrice, transactionReceipt.effectiveGasPrice) + XCTAssertEqual(logs.count, transactionReceipt.logs.count) + XCTAssertEqual(status, transactionReceipt.status) + XCTAssertEqual(logsBloom.bytes, transactionReceipt.logsBloom?.bytes) + } + +} diff --git a/Tests/web3swiftTests/localTests/TransactionsTests.swift b/Tests/web3swiftTests/localTests/TransactionsTests.swift index 99d102032..02a560b2f 100755 --- a/Tests/web3swiftTests/localTests/TransactionsTests.swift +++ b/Tests/web3swiftTests/localTests/TransactionsTests.swift @@ -670,7 +670,6 @@ class TransactionsTests: XCTestCase { func testGenerateDummyKeystore() throws { let keystore = try! EthereumKeystoreV3.init(password: "web3swift") let dump = try! keystore!.serialize() - let jsonString = String.init(data: dump!, encoding: .ascii) - + XCTAssertNotNil(String(data: dump!, encoding: .ascii)) } }