Skip to content

Commit 04ec15d

Browse files
chore: avoiding force unwraps; shortened a few expressions
1 parent 3e5da9f commit 04ec15d

File tree

3 files changed

+26
-40
lines changed

3 files changed

+26
-40
lines changed

Sources/web3swift/EthereumABI/ABIParsing.swift

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ extension ABI {
3838

3939
extension ABI.Record {
4040
public func parse() throws -> ABI.Element {
41-
let typeString = self.type != nil ? self.type! : "function"
41+
let typeString = self.type ?? "function"
4242
guard let type = ABI.ElementType(rawValue: typeString) else {
4343
throw ABI.ParsingError.elementTypeInvalid
4444
}
@@ -75,15 +75,14 @@ fileprivate func parseFunction(abiRecord:ABI.Record) throws -> ABI.Element.Funct
7575
let nativeInput = try input.parse()
7676
return nativeInput
7777
})
78-
let abiInputs = inputs != nil ? inputs! : [ABI.Element.InOut]()
78+
let abiInputs = inputs ?? [ABI.Element.InOut]()
7979
let outputs = try abiRecord.outputs?.map({ (output:ABI.Output) throws -> ABI.Element.InOut in
8080
let nativeOutput = try output.parse()
8181
return nativeOutput
8282
})
83-
let abiOutputs = outputs != nil ? outputs! : [ABI.Element.InOut]()
84-
let name = abiRecord.name != nil ? abiRecord.name! : ""
85-
let payable = abiRecord.stateMutability != nil ?
86-
(abiRecord.stateMutability == "payable" || abiRecord.payable ?? false) : false
83+
let abiOutputs = outputs ?? [ABI.Element.InOut]()
84+
let name = abiRecord.name ?? ""
85+
let payable = abiRecord.stateMutability == "payable" || abiRecord.payable ?? false
8786
let constant = (abiRecord.constant == true || abiRecord.stateMutability == "view" || abiRecord.stateMutability == "pure")
8887
let functionElement = ABI.Element.Function(name: name, inputs: abiInputs, outputs: abiOutputs, constant: constant, payable: payable)
8988
return functionElement
@@ -104,16 +103,9 @@ fileprivate func parseConstructor(abiRecord:ABI.Record) throws -> ABI.Element.Co
104103
let nativeInput = try input.parse()
105104
return nativeInput
106105
})
107-
let abiInputs = inputs != nil ? inputs! : [ABI.Element.InOut]()
108-
var payable = false
109-
if (abiRecord.payable != nil) {
110-
payable = abiRecord.payable!
111-
}
112-
if (abiRecord.stateMutability == "payable") {
113-
payable = true
114-
}
115-
let constant = false
116-
let functionElement = ABI.Element.Constructor(inputs: abiInputs, constant: constant, payable: payable)
106+
let abiInputs = inputs ?? [ABI.Element.InOut]()
107+
var payable = abiRecord.stateMutability == "payable" || abiRecord.payable ?? false
108+
let functionElement = ABI.Element.Constructor(inputs: abiInputs, constant: false, payable: payable)
117109
return functionElement
118110
}
119111

@@ -122,9 +114,9 @@ fileprivate func parseEvent(abiRecord:ABI.Record) throws -> ABI.Element.Event {
122114
let nativeInput = try input.parseForEvent()
123115
return nativeInput
124116
})
125-
let abiInputs = inputs != nil ? inputs! : [ABI.Element.Event.Input]()
126-
let name = abiRecord.name != nil ? abiRecord.name! : ""
127-
let anonymous = abiRecord.anonymous != nil ? abiRecord.anonymous! : false
117+
let abiInputs = inputs ?? [ABI.Element.Event.Input]()
118+
let name = abiRecord.name ?? ""
119+
let anonymous = abiRecord.anonymous ?? false
128120
let functionElement = ABI.Element.Event(name: name, inputs: abiInputs, anonymous: anonymous)
129121
return functionElement
130122
}
@@ -134,14 +126,8 @@ fileprivate func parseReceive(abiRecord:ABI.Record) throws -> ABI.Element.Receiv
134126
let nativeInput = try input.parse()
135127
return nativeInput
136128
})
137-
let abiInputs = inputs != nil ? inputs! : [ABI.Element.InOut]()
138-
var payable = false
139-
if (abiRecord.payable != nil) {
140-
payable = abiRecord.payable!
141-
}
142-
if (abiRecord.stateMutability == "payable") {
143-
payable = true
144-
}
129+
let abiInputs = inputs ?? [ABI.Element.InOut]()
130+
var payable = abiRecord.stateMutability == "payable" || abiRecord.payable ?? false
145131
let functionElement = ABI.Element.Receive(inputs: abiInputs, payable: payable)
146132
return functionElement
147133
}
@@ -152,13 +138,13 @@ fileprivate func parseError(abiRecord:ABI.Record) throws -> ABI.Element.EthError
152138
return nativeInput
153139
})
154140
let abiInputs = inputs ?? []
155-
let name = abiRecord.name != nil ? abiRecord.name! : ""
141+
let name = abiRecord.name ?? ""
156142
return ABI.Element.EthError(name: name, inputs: abiInputs)
157143
}
158144

159145
extension ABI.Input {
160146
func parse() throws -> ABI.Element.InOut {
161-
let name = self.name != nil ? self.name! : ""
147+
let name = self.name ?? ""
162148
let parameterType = try ABITypeParser.parseTypeString(self.type)
163149
if case .tuple(types: _) = parameterType {
164150
let components = try self.components?.compactMap({ (inp: ABI.Input) throws -> ABI.Element.ParameterType in
@@ -187,14 +173,14 @@ extension ABI.Input {
187173
}
188174

189175
func parseForEvent() throws -> ABI.Element.Event.Input {
190-
let name = self.name != nil ? self.name! : ""
176+
let name = self.name ?? ""
191177
let parameterType = try ABITypeParser.parseTypeString(self.type)
192178
let indexed = self.indexed == true
193179
return ABI.Element.Event.Input(name:name, type: parameterType, indexed: indexed)
194180
}
195181

196182
func parseForError() throws -> ABI.Element.EthError.Input {
197-
let name = self.name != nil ? self.name! : ""
183+
let name = self.name ?? ""
198184
let parameterType = try ABITypeParser.parseTypeString(self.type)
199185
return ABI.Element.EthError.Input(name:name, type: parameterType)
200186
}

Sources/web3swift/HookedFunctions/Web3+BrowserFunctions.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ extension web3.BrowserFunctions {
7979
var transactionOptions = TransactionOptions()
8080
transactionOptions.from = options.from
8181
transactionOptions.to = options.to
82-
transactionOptions.value = options.value != nil ? options.value! : BigUInt(0)
83-
transactionOptions.gasLimit = options.gasLimit != nil ? options.gasLimit! : .automatic
84-
transactionOptions.gasPrice = options.gasPrice != nil ? options.gasPrice! : .automatic
82+
transactionOptions.value = options.value ?? BigUInt(0)
83+
transactionOptions.gasLimit = options.gasLimit ?? .automatic
84+
transactionOptions.gasPrice = options.gasPrice ?? .automatic
8585
return self.sendTransaction(transaction, transactionOptions: transactionOptions, password: password)
8686
}
8787

@@ -100,9 +100,9 @@ extension web3.BrowserFunctions {
100100
var transactionOptions = TransactionOptions()
101101
transactionOptions.from = options.from
102102
transactionOptions.to = options.to
103-
transactionOptions.value = options.value != nil ? options.value! : BigUInt(0)
103+
transactionOptions.value = options.value ?? BigUInt(0)
104104
transactionOptions.gasLimit = .automatic
105-
transactionOptions.gasPrice = options.gasPrice != nil ? options.gasPrice! : .automatic
105+
transactionOptions.gasPrice = options.gasPrice ?? .automatic
106106
return self.estimateGas(transaction, transactionOptions: transactionOptions)
107107
}
108108

@@ -149,9 +149,9 @@ extension web3.BrowserFunctions {
149149
var transactionOptions = TransactionOptions()
150150
transactionOptions.from = options.from
151151
transactionOptions.to = options.to
152-
transactionOptions.value = options.value != nil ? options.value! : BigUInt(0)
153-
transactionOptions.gasLimit = options.gasLimit != nil ? options.gasLimit! : .automatic
154-
transactionOptions.gasPrice = options.gasPrice != nil ? options.gasPrice! : .automatic
152+
transactionOptions.value = options.value ?? BigUInt(0)
153+
transactionOptions.gasLimit = options.gasLimit ?? .automatic
154+
transactionOptions.gasPrice = options.gasPrice ?? .automatic
155155
if let nonceString = transactionJSON["nonce"] as? String, let nonce = BigUInt(nonceString.stripHexPrefix(), radix: 16) {
156156
transactionOptions.nonce = .manual(nonce)
157157
} else {

Sources/web3swift/Web3/Web3+InfuraProviders.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public enum BlockNumber {
3131
public final class InfuraProvider: Web3HttpProvider {
3232
public init?(_ net:Networks, accessToken token: String? = nil, keystoreManager manager: KeystoreManager? = nil) {
3333
var requestURLstring = "https://" + net.name + Constants.infuraHttpScheme
34-
requestURLstring += token != nil ? token! : Constants.infuraToken
34+
requestURLstring += token ?? Constants.infuraToken
3535
let providerURL = URL(string: requestURLstring)
3636
super.init(providerURL!, network: net, keystoreManager: manager)
3737
}

0 commit comments

Comments
 (0)