Skip to content

feat: introduced effectiveGasPrice #724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Sources/Web3Core/Structure/Transaction/TransactionReceipt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -45,6 +46,7 @@ extension TransactionReceipt: Decodable {
case logs
case logsBloom
case status
case effectiveGasPrice
}

public init(from decoder: Decoder) throws {
Expand All @@ -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
Expand All @@ -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)
}
}
}

Expand Down
44 changes: 44 additions & 0 deletions Tests/web3swiftTests/localTests/TransactionReceiptTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}

}
3 changes: 1 addition & 2 deletions Tests/web3swiftTests/localTests/TransactionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}