Skip to content

Commit c8eae13

Browse files
Fixin FIXME memos
- Mostly of them are about making appropriate error thrown. - Change data method to return `HTTPURLResponse` instead of `URLResponse`. - Adding cache for GasOracle struct (were disabled, because it used method that available only from iOS 13). - Some comment formatting. - Delete `Package@5.0.swift` package config file.
1 parent b96155c commit c8eae13

File tree

11 files changed

+46
-79
lines changed

11 files changed

+46
-79
lines changed

Package@5.0.swift

Lines changed: 0 additions & 35 deletions
This file was deleted.

Sources/web3swift/API/APIMethod.swift

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public typealias Receipt = Hash
1313
public typealias Address = Hash // 20 bytes (40 chars length without 0x)
1414
public typealias TransactionHash = Hash // 64 chars length without 0x
1515

16-
// FIXME: Add documentation to each method.
1716
/// Ethereum JSON RPC API Calls
1817
///
1918
/// ## How to
@@ -74,7 +73,7 @@ public typealias TransactionHash = Hash // 64 chars length without 0x
7473
/// - `protocol APIRequestParameterType: Encodable` — this type is part of the ``RequestParameter`` enum mechanism which purpose is to restrict types that can be passed as associated types within `RequestParameter` cases.
7574
/// - `protocol APIRequestParameterElementType: Encodable` — this type purpose is the same as ``APIRequestParameterType` one except this one is made for `Element`s of an `Array` s when the latter is an associated type of a given `RequestParameter` case.
7675
public enum APIRequest {
77-
// MARK: - Official API
76+
// MARK: - Official Ethereum API
7877

7978
/// Gas price request
8079
case gasPrice
@@ -189,7 +188,7 @@ public enum APIRequest {
189188
/// by effective tip per gas and the coresponding effective tip for the percentile will be determined, accounting for gas consumed."
190189
case feeHistory(BigUInt, BlockNumber, [Double])
191190

192-
// MARK: - Additional API
191+
// MARK: - Additional Ethereum API
193192

194193
/// Creates new account.
195194
///
@@ -339,23 +338,16 @@ extension APIRequest {
339338
static func send<Result>(uRLRequest: URLRequest, with session: URLSession) async throws -> APIResponse<Result> {
340339
let (data, response) = try await session.data(for: uRLRequest)
341340

342-
// FIXME: Add appropriate error thrown
343-
guard let httpResponse = response as? HTTPURLResponse,
344-
200 ..< 400 ~= httpResponse.statusCode else { throw Web3Error.connectionError }
341+
guard 200 ..< 400 ~= response.statusCode else { throw Web3Error.serverError(code: response.statusCode) }
345342

346-
// FIXME: Add throwing an error from is server fails.
347343
/// This bit of code is purposed to work with literal types that comes in Response in hexString type.
348344
/// Currently it's just `Data` and any kind of Integers `(U)Int`, `Big(U)Int`.
349345
if Result.self == Data.self || Result.self == UInt.self || Result.self == Int.self || Result.self == BigInt.self || Result.self == BigUInt.self {
350-
// FIXME: Make appropriate error
351-
guard let Literal = Result.self as? LiteralInitiableFromString.Type else { throw Web3Error.unknownError }
352-
// FIXME: Add appropriate error thrown.
353-
guard let responseAsString = try? JSONDecoder().decode(APIResponse<String>.self, from: data) else { throw Web3Error.unknownError }
354-
// FIXME: Add appropriate error thrown.
355-
guard let literalValue = Literal.init(from: responseAsString.result) else { throw Web3Error.unknownError }
346+
guard let Literal = Result.self as? LiteralInitiableFromString.Type else { throw Web3Error.typeError }
347+
guard let responseAsString = try? JSONDecoder().decode(APIResponse<String>.self, from: data) else { throw Web3Error.dataError }
348+
guard let literalValue = Literal.init(from: responseAsString.result) else { throw Web3Error.dataError }
356349
/// `Literal` conforming `LiteralInitiableFromString`, that conforming to an `APIResponseType` type, so it's never fails.
357-
// FIXME: Make appropriate error
358-
guard let result = literalValue as? Result else { throw Web3Error.unknownError }
350+
guard let result = literalValue as? Result else { throw Web3Error.typeError }
359351
return APIResponse(id: responseAsString.id, jsonrpc: responseAsString.jsonrpc, result: result)
360352
}
361353
return try JSONDecoder().decode(APIResponse<Result>.self, from: data)

Sources/web3swift/API/Async+BackwardCapability.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,16 @@ import Foundation
1010
@available(iOS, obsoleted: 15.0, message: "Use the built-in API instead")
1111
@available(macOS, obsoleted: 12.0, message: "Use the built-in API instead")
1212
extension URLSession {
13-
func data(for request: URLRequest) async throws -> (Data, URLResponse) {
14-
var dataTask: URLSessionDataTask?
15-
16-
return try await withCheckedThrowingContinuation { continuation in
17-
dataTask = self.dataTask(with: request) { data, response, error in
18-
guard let data = data, let response = response else {
13+
func data(for request: URLRequest) async throws -> (Data, HTTPURLResponse) {
14+
try await withCheckedThrowingContinuation { continuation in
15+
let dataTask = self.dataTask(with: request) { data, response, error in
16+
guard let data = data, let response = response as? HTTPURLResponse else {
1917
let error = error ?? URLError(.badServerResponse)
2018
return continuation.resume(throwing: error)
2119
}
2220
continuation.resume(returning: (data, response))
2321
}
24-
dataTask?.resume()
22+
dataTask.resume()
2523
}
2624
}
2725
}

Sources/web3swift/EthereumAPICalls/Ethereum/Eth+Call.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ import Foundation
99

1010
extension web3.Eth {
1111

12-
// FIXME: Possible this not working at all.
1312
public func callTransaction(_ transaction: EthereumTransaction, transactionOptions: TransactionOptions?) async throws -> Data {
14-
// FIXME: Add appropriate error
15-
guard let transactionParameters = transaction.encodeAsDictionary(from: transactionOptions?.from) else { throw Web3Error.unknownError}
13+
guard let transactionParameters = transaction.encodeAsDictionary(from: transactionOptions?.from) else { throw Web3Error.dataError}
1614
let request: APIRequest = .call(transactionParameters, transactionOptions?.callOnBlock ?? .latest)
1715
let response: APIResponse<Data> = try await APIRequest.sendRequest(with: self.provider, for: request)
1816
return response.result

Sources/web3swift/EthereumAPICalls/Ethereum/Eth+EstimateGas.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import BigInt
1111
extension web3.Eth {
1212

1313
public func estimateGas(for transaction: EthereumTransaction, transactionOptions: TransactionOptions?) async throws -> BigUInt {
14-
// FIXME: Add appropriate error
15-
guard let transactionParameters = transaction.encodeAsDictionary(from: transactionOptions?.from) else { throw Web3Error.unknownError }
14+
guard let transactionParameters = transaction.encodeAsDictionary(from: transactionOptions?.from) else { throw Web3Error.dataError }
1615

1716
let request: APIRequest = .estimateGas(transactionParameters, transactionOptions?.callOnBlock ?? .latest)
1817
let response: APIResponse<BigUInt> = try await APIRequest.sendRequest(with: provider, for: request)

Sources/web3swift/EthereumAPICalls/Ethereum/Eth+SendRawTransaction.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ extension web3.Eth {
1616
}
1717

1818
public func send(raw transaction: EthereumTransaction) async throws -> TransactionSendingResult {
19-
// FIXME: Add appropriate error
20-
guard let transactionHexData = transaction.encode()?.toHexString().addHexPrefix() else { throw Web3Error.unknownError }
19+
guard let transactionHexData = transaction.encode()?.toHexString().addHexPrefix() else { throw Web3Error.dataError }
2120
let request: APIRequest = .sendRawTransaction(transactionHexData)
2221
let response: APIResponse<Hash> = try await APIRequest.sendRequest(with: self.provider, for: request)
2322

Sources/web3swift/EthereumAPICalls/Ethereum/Eth+SendTransaction.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ extension web3.Eth {
4646
return try await self.web3.eth.send(raw: assembledTransaction)
4747
}
4848

49-
// FIXME: Add appropriate error
50-
guard let transactionParameters = transaction.encodeAsDictionary(from: transactionOptions?.from) else { throw Web3Error.unknownError }
49+
guard let transactionParameters = transaction.encodeAsDictionary(from: transactionOptions?.from) else { throw Web3Error.dataError }
5150

5251
let request: APIRequest = .sendTransaction(transactionParameters)
5352
let response: APIResponse<Hash> = try await APIRequest.sendRequest(with: self.provider, for: request)

Sources/web3swift/EthereumAPICalls/Personal/Personal+Sign.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import BigInt
1010

1111
extension web3.Personal {
1212

13-
// FIXME: Possible this not working at all.
1413
public func signPersonal(message: Data, from: EthereumAddress, password: String = "web3swift") async throws -> Data {
1514

1615
guard let attachedKeystoreManager = self.web3.provider.attachedKeystoreManager else {

Sources/web3swift/Web3/Web3+EventParser.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ extension web3.web3contract {
200200
}
201201
}
202202

203-
// FIXME: This could not worked at all.
204203
let request: APIRequest = .getLogs(preEncoding)
205204
let response: APIResponse<[EventLog]> = try await APIRequest.sendRequest(with: self.web3.provider, for: request)
206205

Sources/web3swift/Web3/Web3+GasOracle.swift

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,22 @@ extension Web3 {
3939
/// Another example: If you set it [100.0] you'll get the very highest value of a dataset e.g. max Tip amount.
4040
var percentiles: [Double]
4141

42-
// TODO: Disabled until 3.0 version, coz will be enabled from 3.0.0.
43-
// var forceDropCache = false
42+
var forceDropCache = false
43+
44+
var cacheTimeout: Double
4445

4546
/// Oracle initializer
4647
/// - Parameters:
4748
/// - provider: Web3 Ethereum provider
4849
/// - block: Number of block from which counts starts backward
4950
/// - blockCount: Count of block to calculate statistics
5051
/// - percentiles: Percentiles of fees to which result of predictions will be split in
51-
public init(_ provider: web3, block: BlockNumber = .latest, blockCount: BigUInt = 20, percentiles: [Double] = [25, 50, 75]) {
52+
public init(_ provider: web3, block: BlockNumber = .latest, blockCount: BigUInt = 20, percentiles: [Double] = [25, 50, 75], cacheTimeout: Double = 10) {
5253
self.web3Provider = provider
5354
self.block = block
5455
self.blockCount = blockCount
5556
self.percentiles = percentiles
57+
self.cacheTimeout = cacheTimeout
5658
}
5759

5860

@@ -84,12 +86,18 @@ extension Web3 {
8486
}
8587

8688
private func suggestGasValues() async throws -> FeeHistory {
87-
// This is some kind of cache.
88-
// It stores about 9 seconds, than it rewrites it with newer data.
89-
// TODO: Disabled until 3.0 version, coz `distance` available from iOS 13.
90-
// guard feeHistory == nil, forceDropCache, feeHistory!.timestamp.distance(to: Date()) > cacheTimeout else { return feeHistory! }
89+
/// This is some kind of cache.
90+
/// It stores about 10 seconds, than it rewrites it with newer data.
91+
92+
/// We're explicitly checking that feeHistory is not nil before force unwrapping it.
93+
// swiftlint: disable force_unwrapping
94+
guard feeHistory == nil, forceDropCache, feeHistory!.timestamp.distance(to: Date()) > cacheTimeout else { return feeHistory! }
95+
96+
feeHistory = try await eth.feeHistory(blockCount: blockCount, block: block, percentiles: percentiles)
9197

92-
return try await eth.feeHistory(blockCount: blockCount, block: block, percentiles: percentiles)
98+
/// We're assigning this value the line very above, so it's free to force unwrapping here
99+
return feeHistory!
100+
// swiftlint: enable force_unwrapping
93101
}
94102

95103
/// Suggesting tip values
@@ -126,9 +134,8 @@ extension Web3 {
126134
switch block {
127135
case .latest: latestBlockNumber = try await eth.blockNumber()
128136
case let .exact(number): latestBlockNumber = number
129-
// FIXME: Make real error here
130-
// Error throws since pending and erliest are unable to be used in this method.
131-
default: throw Web3Error.unknownError
137+
// Error throws since pending and erliest are unable to be used in this method.
138+
default: throw Web3Error.valueError
132139
}
133140

134141
/// checking if latest block number is greather than number of blocks to take in account

0 commit comments

Comments
 (0)