Skip to content

Commit ead45a1

Browse files
authored
chore: general cleanup (#679)
1 parent a3fecba commit ead45a1

File tree

10 files changed

+31
-25
lines changed

10 files changed

+31
-25
lines changed

Sources/Core/EthereumNetwork/Request/APIRequest+ComputedProperties.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ import Foundation
99

1010
extension APIRequest {
1111
var method: REST {
12-
switch self {
13-
default: return .POST
14-
}
12+
.POST
1513
}
1614

1715
public var encodedBody: Data {
18-
let request = RequestBody(method: self.call, params: self.parameters)
16+
let request = RequestBody(method: call, params: parameters)
1917
// this is safe to force try this here
2018
// Because request must failed to compile if it not conformable with `Encodable` protocol
2119
return try! JSONEncoder().encode(request)

Sources/Core/KeystoreManager/KeystoreManager.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ public class KeystoreManager: AbstractKeystore {
4242
}
4343

4444
public func UNSAFE_getPrivateKeyData(password: String, account: EthereumAddress) throws -> Data {
45-
guard let keystore = self.walletForAddress(account) else {throw AbstractKeystoreError.invalidAccountError}
46-
return try keystore.UNSAFE_getPrivateKeyData(password: password, account: account)
47-
}
45+
guard let keystore = walletForAddress(account) else {
46+
throw AbstractKeystoreError.invalidAccountError
47+
}
48+
return try keystore.UNSAFE_getPrivateKeyData(password: password, account: account)
49+
}
4850

4951
public static var allManagers = [KeystoreManager]()
5052
public static var defaultManager: KeystoreManager? {

Sources/Core/Transaction/Envelope/EIP1559Envelope.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,10 @@ extension EIP1559Envelope {
249249
let list = accessList.map { $0.encodeAsList() as AnyObject }
250250

251251
switch type {
252-
case .transaction: fields = [chainID, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to.addressData, value, data, list, v, r, s] as [AnyObject]
253-
case .signature: fields = [chainID, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to.addressData, value, data, list] as [AnyObject]
252+
case .transaction:
253+
fields = [chainID, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to.addressData, value, data, list, v, r, s] as [AnyObject]
254+
case .signature:
255+
fields = [chainID, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to.addressData, value, data, list] as [AnyObject]
254256
}
255257
guard var result = RLP.encode(fields) else { return nil }
256258
result.insert(UInt8(self.type.rawValue), at: 0)

Sources/Core/Transaction/Envelope/EIP2930Envelope.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,10 @@ extension EIP2930Envelope {
210210
let list = accessList.map { $0.encodeAsList() as AnyObject }
211211

212212
switch type {
213-
case .transaction: fields = [chainID, nonce, gasPrice, gasLimit, to.addressData, value, data, list, v, r, s] as [AnyObject]
214-
case .signature: fields = [chainID, nonce, gasPrice, gasLimit, to.addressData, value, data, list] as [AnyObject]
213+
case .transaction:
214+
fields = [chainID, nonce, gasPrice, gasLimit, to.addressData, value, data, list, v, r, s] as [AnyObject]
215+
case .signature:
216+
fields = [chainID, nonce, gasPrice, gasLimit, to.addressData, value, data, list] as [AnyObject]
215217
}
216218
guard var result = RLP.encode(fields) else { return nil }
217219
result.insert(UInt8(self.type.rawValue), at: 0)

Sources/Core/Transaction/Envelope/LegacyEnvelope.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,13 @@ extension LegacyEnvelope {
194194
public func encode(for type: EncodeType = .transaction) -> Data? {
195195
let fields: [AnyObject]
196196
switch type {
197-
case .transaction: fields = [self.nonce, self.gasPrice, self.gasLimit, self.to.addressData, self.value, self.data, v, r, s] as [AnyObject]
197+
case .transaction:
198+
fields = [nonce, gasPrice, gasLimit, to.addressData, value, data, v, r, s] as [AnyObject]
198199
case .signature:
199-
if let chainID = self.chainID, chainID != 0 {
200-
fields = [self.nonce, self.gasPrice, self.gasLimit, self.to.addressData, self.value, self.data, chainID, BigUInt(0), BigUInt(0)] as [AnyObject]
200+
if let chainID = chainID, chainID != 0 {
201+
fields = [nonce, gasPrice, gasLimit, to.addressData, value, data, chainID, BigUInt(0), BigUInt(0)] as [AnyObject]
201202
} else {
202-
fields = [self.nonce, self.gasPrice, self.gasLimit, self.to.addressData, self.value, self.data] as [AnyObject]
203+
fields = [nonce, gasPrice, gasLimit, to.addressData, value, data] as [AnyObject]
203204
}
204205
}
205206
return RLP.encode(fields)

Sources/Core/Utility/Utilities.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// Utilities.swift
3-
//
3+
//
44
//
55
// Created by Yaroslav Yashin on 11.07.2022.
66
//
@@ -144,12 +144,12 @@ public struct Utilities {
144144
}
145145
let divisor = BigUInt(10).power(unitDecimals)
146146
let (quotient, remainder) = bigNumber.quotientAndRemainder(dividingBy: divisor)
147-
var fullRemainder = String(remainder)
147+
var fullRemainder = "\(remainder)"
148148
let fullPaddedRemainder = fullRemainder.leftPadding(toLength: unitDecimals, withPad: "0")
149149
let remainderPadded = fullPaddedRemainder[0..<toDecimals]
150150
if remainderPadded == String(repeating: "0", count: toDecimals) {
151151
if quotient != 0 {
152-
return String(quotient)
152+
return "\(quotient)"
153153
} else if fallbackToScientific {
154154
var firstDigit = 0
155155
for char in fullPaddedRemainder {
@@ -180,9 +180,9 @@ public struct Utilities {
180180
}
181181
}
182182
if toDecimals == 0 {
183-
return String(quotient)
183+
return "\(quotient)"
184184
}
185-
return String(quotient) + decimalSeparator + remainderPadded
185+
return "\(quotient)" + decimalSeparator + remainderPadded
186186
}
187187

188188
/// Recover the Ethereum address from recoverable secp256k1 signature. Message is first hashed using the "personal hash" protocol.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Core
99

1010
extension Web3.Eth {
1111
public func block(by hash: Data, fullTransactions: Bool = false) async throws -> Block {
12-
let request = APIRequest.getBlockByHash(hash.toHexString().addHexPrefix(), fullTransactions)
12+
let request: APIRequest = .getBlockByHash(hash.toHexString().addHexPrefix(), fullTransactions)
1313
return try await APIRequest.sendRequest(with: provider, for: request).result
1414
}
1515
}

Sources/web3swift/Operations/ReadOperation.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class ReadOperation {
3636

3737
// TODO: Remove type erasing here, some broad wide protocol should be added instead
3838
public func callContractMethod() async throws -> [String: Any] {
39+
3940
// MARK: Read data from ABI flow
4041
// FIXME: This should be dropped, and after `execute()` call, just to decode raw data.
4142
let data: Data = try await self.web3.eth.callTransaction(transaction)

Sources/web3swift/Web3/Web3.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ extension Web3 {
1212

1313
/// Initialized provider-bound Web3 instance using a provider's URL. Under the hood it performs a synchronous call to get
1414
/// the Network ID for EIP155 purposes
15-
public static func new(_ providerURL: URL) async throws -> Web3 {
15+
public static func new(_ providerURL: URL, network: Networks = .Mainnet) async throws -> Web3 {
1616
// FIXME: Change this hardcoded value to dynamicly fethed from a Node
17-
guard let provider = await Web3HttpProvider(providerURL, network: .Mainnet) else {
17+
guard let provider = await Web3HttpProvider(providerURL, network: network) else {
1818
throw Web3Error.inputError(desc: "Wrong provider - should be Web3HttpProvider with endpoint scheme http or https")
1919
}
2020
return Web3(provider: provider)

Tests/web3swiftTests/localTests/LocalTestCase.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class LocalTestCase: XCTestCase {
1515
override func setUp() async throws {
1616
let web3 = try! await Web3.new(LocalTestCase.url)
1717

18-
let block = try! await web3.eth.blockNumber()
19-
if block >= 25 { return }
18+
let block = try await web3.eth.blockNumber()
19+
guard block < 25 else { return }
2020

2121
print("\n ****** Preloading Ganache (\(25 - block) blocks) *****\n")
2222

0 commit comments

Comments
 (0)